Fix duplicate qual clauses in parameterized paths

When outer-join identity 3 permits a join to commute with lower outer
joins, we generate multiple clone versions of its join clause, of
which only one should be applied in any given plan.  When building a
join relation's restriction list, subbuild_joinrel_restrictlist
selects the appropriate clone by checking required_relids and
incompatible_relids, but no such selection was made for movable join
clauses pushed down into a parameterized path.  As a result, a
parameterized scan or join could enforce more than one clone of the
same condition, shown by EXPLAIN as a duplicate qual.  This wastes
effort evaluating the same condition repeatedly.  What is worse, it
applies the clause's selectivity multiple times, underestimating the
result's row count.

To fix, in get_baserel_parampathinfo and get_joinrel_parampathinfo,
skip a clone clause if any outer join listed in its
incompatible_relids has already been computed below the point of
evaluation.

Back-patch to v19, but no further.  While this bug goes back to v16,
applying the fix in released branches risks destabilizing plans that
users are currently happy with, and there have been no field
complaints.

Author: Richard Guo <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Reviewed-by: Tender Wang <[email protected]>
Discussion: 
https://postgr.es/m/CAMbWs4-g5TFF5k=jfe3jvwqtv_cctommqrup0_e8fapgqcq...@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f836b688f8dc627ce97760dec5569aa7c064ffe9

Modified Files
--------------
src/backend/optimizer/util/relnode.c | 14 ++++++++++
src/include/nodes/pathnodes.h        |  2 +-
src/test/regress/expected/join.out   | 51 +++++++++++++++++++++++++++++++++---
src/test/regress/sql/join.sql        | 22 ++++++++++++++++
4 files changed, 85 insertions(+), 4 deletions(-)

Reply via email to