mirror of
https://github.com/postgres/postgres.git
synced 2026-10-23 13:29:09 +03:00
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. Commita16ef313fremoved 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 commita16ef313fremoved the have_dangerous_phv() restriction. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/CAMbWs49ZveAYx9T5b0=gxn0pnvqq=MbN-Z4KLbLAE+kH06mWVA@mail.gmail.com Backpatch-through: 18
This commit is contained in:
@@ -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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5880,6 +5880,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)
|
||||
--
|
||||
|
||||
@@ -2093,6 +2093,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)
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user