Remove useless self-joins
authorAlexander Korotkov <[email protected]>
Wed, 25 Oct 2023 09:46:22 +0000 (12:46 +0300)
committerAlexander Korotkov <[email protected]>
Wed, 25 Oct 2023 09:59:16 +0000 (12:59 +0300)
commitd3d55ce571369dad6e1d582f1655e5a3fbd8594a
tree81ab68bf8addc06e7a61bb6d645dec4902946539
parent8f0fd47fa33720dd09ad0ae74a8a583b9780e328
Remove useless self-joins

The Self Join Elimination (SJE) feature removes an inner join of a plain table
to itself in the query tree if is proved that the join can be replaced with
a scan without impacting the query result.  Self join and inner relation are
replaced with the outer in query, equivalence classes, and planner info
structures. Also, inner restrictlist moves to the outer one with removing
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 === selectivity estimations, and potentially
can improve total planner prediction for the query.

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 row as the outer one.

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 inner and outer clauses must have
    an exact match.

The relation replacement procedure is not trivial and it is partly combined
with the one, used to remove useless left joins.  Tests, covering this feature,
were added to join.sql.  Some 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, Alexander Kuzmenkov
Reviewed-by: Tom Lane, Robert Haas, Andres Freund, Simon Riggs, Jonathan S. Katz
Reviewed-by: David Rowley, Thomas Munro, Konstantin Knizhnik, Heikki Linnakangas
Reviewed-by: Hywel Carver, Laurenz Albe, Ronan Dunklau, vignesh C, Zhihong Yu
Reviewed-by: Greg Stark, Jaime Casanova, Michał Kłeczek, Alena Rybakina
Reviewed-by: Alexander Korotkov
14 files changed:
doc/src/sgml/config.sgml
src/backend/optimizer/path/indxpath.c
src/backend/optimizer/plan/analyzejoins.c
src/backend/optimizer/plan/planmain.c
src/backend/utils/misc/guc_tables.c
src/include/optimizer/paths.h
src/include/optimizer/planmain.h
src/test/regress/expected/equivclass.out
src/test/regress/expected/join.out
src/test/regress/expected/sysviews.out
src/test/regress/expected/updatable_views.out
src/test/regress/sql/equivclass.sql
src/test/regress/sql/join.sql
src/tools/pgindent/typedefs.list