Implement Self-Join Elimination
authorAlexander Korotkov
Wed, 12 Feb 2025 22:56:03 +0000 (00:56 +0200)
committerAlexander Korotkov
Mon, 17 Feb 2025 10:44:12 +0000 (12:44 +0200)
commitfc069a3a6319b5bf40d2f0f1efceae1c9b7a68a8
tree3a82ac49a60ad4d82ec3070cf412fa73b62c3695
parent3fb58625d18fd226cb929c9700d0db72ac92c075
Implement Self-Join Elimination

The Self-Join Elimination (SJE) feature removes an inner join of a plain
table to itself in the query tree if it is proven that the join can be
replaced with a scan without impacting the query result.  Self-join and
inner relation get replaced with the outer in query, equivalence classes,
and planner info structures.  Also, the inner restrictlist moves to the
outer one with the removal of duplicated clauses.  Thus, this optimization
reduces the length of the range table list (this especially makes sense for
partitioned relations), reduces the number of restriction clauses and,
in turn, selectivity estimations, and potentially improves total planner
prediction for the query.

This feature is dedicated to avoiding redundancy, which can appear after
pull-up transformations or the creation of an EquivalenceClass-derived clause
like the below.

  SELECT * FROM t1 WHERE x IN (SELECT t3.x FROM t1 t3);
  SELECT * FROM t1 WHERE EXISTS (SELECT t3.x FROM t1 t3 WHERE t3.x = t1.x);
  SELECT * FROM t1,t2, t1 t3 WHERE t1.x = t2.x AND t2.x = t3.x;

In the future, we could also reduce redundancy caused by subquery pull-up
after unnecessary outer join removal in cases like the one below.

  SELECT * FROM t1 WHERE x IN
    (SELECT t3.x FROM t1 t3 LEFT JOIN t2 ON t2.x = t1.x);

Also, it can drastically help to join partitioned tables, removing entries
even before their expansion.

The SJE proof is based on innerrel_is_unique() machinery.

We can remove a self-join when for each outer row:

 1. At most, one inner row matches the join clause;
 2. Each matched inner row must be (physically) the same as the outer one;
 3. Inner and outer rows have the same row mark.

In this patch, we use the next approach to identify a self-join:

 1. Collect all merge-joinable join quals which look like a.x = b.x;
 2. Add to the list above the baseretrictinfo of the inner table;
 3. Check innerrel_is_unique() for the qual list.  If it returns false, skip
    this pair of joining tables;
 4. Check uniqueness, proved by the baserestrictinfo clauses. To prove the
    possibility of self-join elimination, the inner and outer clauses must
    match exactly.

The relation replacement procedure is not trivial and is partly combined
with the one used to remove useless left joins.  Tests covering this feature
were added to join.sql.  Some of the existing regression tests changed due
to self-join removal logic.

Discussion: https://postgr.es/m/flat/64486b0b-0404-e39e-322d-0801154901f3%40postgrespro.ru
Author: Andrey Lepikhov 
Author: Alexander Kuzmenkov 
Co-authored-by: Alexander Korotkov
Co-authored-by: Alena Rybakina
Reviewed-by: Tom Lane
Reviewed-by: Robert Haas
Reviewed-by: Andres Freund
Reviewed-by: Simon Riggs
Reviewed-by: Jonathan S. Katz
Reviewed-by: David Rowley
Reviewed-by: Thomas Munro
Reviewed-by: Konstantin Knizhnik
Reviewed-by: Heikki Linnakangas
Reviewed-by: Hywel Carver
Reviewed-by: Laurenz Albe
Reviewed-by: Ronan Dunklau
Reviewed-by: vignesh C
Reviewed-by: Zhihong Yu
Reviewed-by: Greg Stark
Reviewed-by: Jaime Casanova
Reviewed-by: Michał Kłeczek
Reviewed-by: Alena Rybakina
Reviewed-by: Alexander Korotkov
19 files changed:
doc/src/sgml/config.sgml
src/backend/optimizer/path/equivclass.c
src/backend/optimizer/path/indxpath.c
src/backend/optimizer/plan/analyzejoins.c
src/backend/optimizer/plan/planmain.c
src/backend/optimizer/prep/prepunion.c
src/backend/rewrite/rewriteManip.c
src/backend/utils/misc/guc_tables.c
src/include/nodes/pathnodes.h
src/include/optimizer/optimizer.h
src/include/optimizer/paths.h
src/include/optimizer/planmain.h
src/include/rewrite/rewriteManip.h
src/test/regress/expected/equivclass.out
src/test/regress/expected/join.out
src/test/regress/expected/sysviews.out
src/test/regress/sql/equivclass.sql
src/test/regress/sql/join.sql
src/tools/pgindent/typedefs.list