Disallow joins whose lateral references need an unformable outer join

If a LATERAL subquery references a PlaceHolderVar that must be
evaluated at an outer join, the subquery's lateral_relids include that
outer join's relid, since the lateral parameter is the outer join's
output.  join_is_legal() verified that the rels named by a proposed
join's minimum parameterization could still be joined to from outside,
but it did not consider such outer-join relids.  As a result, when
identity 3 permitted a commuted join order, we could approve a join
that includes part of that outer join's required input, even though
the outer join could then be completed only above the proposed join,
leaving the lateral parameter forever unsatisfiable.

In assert-enabled builds this tripped the backstop Assert in
try_nestloop_path().  In production builds the bogus join generated
only paths whose parameterization can never be satisfied, so they
could never appear in a complete plan.  This not only wasted planning
effort, but also could mislead the clauseless-join heuristics into
thinking that legality of this join means that some other join rel
need not be formed, and that could lead to failure to find any plan at
all.

Such joins used to be rejected by the have_dangerous_phv() check in
join_is_legal(), which refused any join where a PlaceHolderVar's eval
set overlapped the other side of the join without being contained in
it.  Commit a16ef313f removed that restriction, exposing this
oversight.

To fix, teach join_is_legal() to reject a proposed join if its minimum
parameterization includes an outer-join relid whose outer join cannot
be formed strictly outside the join, that is, when any rel needed to
form that outer join must become part of the proposed join's own join
tree.

Back-patch to v18, where commit a16ef313f removed the
have_dangerous_phv() restriction.

Author: Richard Guo <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Reviewed-by: Tender Wang <[email protected]>
Discussion: 
https://postgr.es/m/CAMbWs49ZveAYx9T5b0=gxn0pnvqq=mbn-z4klblae+kh06m...@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_18_STABLE

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

Modified Files
--------------
src/backend/optimizer/path/joinrels.c | 26 +++++++++++++++++++++++
src/test/regress/expected/join.out    | 40 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         | 20 ++++++++++++++++++
3 files changed, 86 insertions(+)

Reply via email to