From e485b2db871b459f2899a939c4dd2df27395eba4 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Mon, 31 Aug 2026 21:09:11 +0900 Subject: [PATCH v1] 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. 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. --- 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(+) diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c index 443e2dca7c0..10fb3e39d28 100644 --- a/src/backend/optimizer/path/joinrels.c +++ b/src/backend/optimizer/path/joinrels.c @@ -640,6 +640,32 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, } while (more); if (bms_overlap(join_plus_rhs, join_lateral_rels)) return false; /* will not be able to join to some RHS rel */ + + /* + * Furthermore, the minimum parameterization can include + * outer-join relids as well as baserel relids. In such a case + * the value laterally needed is an output of that outer join, so + * the outer join must be formed strictly outside this join for + * the value to be supplied to it. That's not possible if any rel + * needed to form the outer join is within join_plus_rhs, since + * all such rels must join into this join's own join tree. If we + * accepted this join anyway, every path for it would require a + * parameter that can never be supplied, so it could never appear + * in a complete plan. + */ + if (bms_overlap(join_lateral_rels, root->outer_join_rels)) + { + foreach(l, root->join_info_list) + { + SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l); + + if (!bms_is_member(sjinfo->ojrelid, join_lateral_rels)) + continue; + if (bms_overlap(join_plus_rhs, sjinfo->min_lefthand) || + bms_overlap(join_plus_rhs, sjinfo->min_righthand)) + return false; /* OJ can't be formed outside join */ + } + } } } diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index db4fcc5a5a0..38bf1c66abb 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -6591,6 +6591,46 @@ where ss1.c2 = 0; ----+----+----+----+----+---- (0 rows) +-- +-- check that a join is disallowed when a lateral reference to an outer-join +-- output would have to be passed down into that outer join's own input +-- +explain (costs off) +select count(*) from int4_tbl t1 left join + (select b.q1 as bx, 1 as one from int4_tbl a left join int8_tbl b on a.f1 = b.q2) t2 + on true + left join lateral + (select c.f1 as cnt from int4_tbl c where c.f1 = t2.one offset 0) t3 + on t2.bx = t3.cnt; + QUERY PLAN +------------------------------------------------------------------------- + Aggregate + -> Nested Loop Left Join + -> Seq Scan on int4_tbl t1 + -> Materialize + -> Nested Loop Left Join + -> Hash Left Join + Hash Cond: (a.f1 = b.q2) + -> Seq Scan on int4_tbl a + -> Hash + -> Seq Scan on int8_tbl b + -> Subquery Scan on t3 + Filter: ((b.q1 = t3.cnt) AND (b.q1 = t3.cnt)) + -> Seq Scan on int4_tbl c + Filter: (f1 = (1)) +(14 rows) + +select count(*) from int4_tbl t1 left join + (select b.q1 as bx, 1 as one from int4_tbl a left join int8_tbl b on a.f1 = b.q2) t2 + on true + left join lateral + (select c.f1 as cnt from int4_tbl c where c.f1 = t2.one offset 0) t3 + on t2.bx = t3.cnt; + count +------- + 25 +(1 row) + -- -- test successful handling of full join underneath left join (bug #14105) -- diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 9533af8656e..c222e2172db 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -2352,6 +2352,26 @@ select ss2.* from lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2 where ss1.c2 = 0; +-- +-- check that a join is disallowed when a lateral reference to an outer-join +-- output would have to be passed down into that outer join's own input +-- + +explain (costs off) +select count(*) from int4_tbl t1 left join + (select b.q1 as bx, 1 as one from int4_tbl a left join int8_tbl b on a.f1 = b.q2) t2 + on true + left join lateral + (select c.f1 as cnt from int4_tbl c where c.f1 = t2.one offset 0) t3 + on t2.bx = t3.cnt; + +select count(*) from int4_tbl t1 left join + (select b.q1 as bx, 1 as one from int4_tbl a left join int8_tbl b on a.f1 = b.q2) t2 + on true + left join lateral + (select c.f1 as cnt from int4_tbl c where c.f1 = t2.one offset 0) t3 + on t2.bx = t3.cnt; + -- -- test successful handling of full join underneath left join (bug #14105) -- -- 2.37.1 (Apple Git-137.1)