mirror of
https://github.com/postgres/postgres.git
synced 2025-06-30 21:42:05 +03:00
Still more fixes for planner's handling of LATERAL references.
More fuzz testing by Andreas Seltenreich exposed that the planner did not
cope well with chains of lateral references. If relation X references Y
laterally, and Y references Z laterally, then we will have to scan X on the
inside of a nestloop with Z, so for all intents and purposes X is laterally
dependent on Z too. The planner did not understand this and would generate
intermediate joins that could not be used. While that was usually harmless
except for wasting some planning cycles, under the right circumstances it
would lead to "failed to build any N-way joins" or "could not devise a
query plan" planner failures.
To fix that, convert the existing per-relation lateral_relids and
lateral_referencers relid sets into their transitive closures; that is,
they now show all relations on which a rel is directly or indirectly
laterally dependent. This not only fixes the chained-reference problem
but allows some of the relevant tests to be made substantially simpler
and faster, since they can be reduced to simple bitmap manipulations
instead of searches of the LateralJoinInfo list.
Also, when a PlaceHolderVar that is due to be evaluated at a join contains
lateral references, we should treat those references as indirect lateral
dependencies of each of the join's base relations. This prevents us from
trying to join any individual base relations to the lateral reference
source before the join is formed, which again cannot work.
Andreas' testing also exposed another oversight in the "dangerous
PlaceHolderVar" test added in commit 85e5e222b1
. Simply rejecting
unsafe join paths in joinpath.c is insufficient, because in some cases
we will end up rejecting *all* possible paths for a particular join, again
leading to "could not devise a query plan" failures. The restriction has
to be known also to join_is_legal and its cohort functions, so that they
will not select a join for which that will happen. I chose to move the
supporting logic into joinrels.c where the latter functions are.
Back-patch to 9.3 where LATERAL support was introduced.
This commit is contained in:
@ -3620,6 +3620,159 @@ where t1.f1 = ss.f1;
|
||||
doh! | 4567890123456789 | 123 | 4567890123456789 | doh!
|
||||
(1 row)
|
||||
|
||||
explain (verbose, costs off)
|
||||
select * from
|
||||
text_tbl t1
|
||||
left join int8_tbl i8
|
||||
on i8.q2 = 123,
|
||||
lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
|
||||
lateral (select ss1.* from text_tbl t3 limit 1) as ss2
|
||||
where t1.f1 = ss2.f1;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------
|
||||
Nested Loop
|
||||
Output: t1.f1, i8.q1, i8.q2, (i8.q1), t2.f1, ((i8.q1)), (t2.f1)
|
||||
Join Filter: (t1.f1 = (t2.f1))
|
||||
-> Nested Loop
|
||||
Output: t1.f1, i8.q1, i8.q2, (i8.q1), t2.f1
|
||||
-> Nested Loop Left Join
|
||||
Output: t1.f1, i8.q1, i8.q2
|
||||
-> Seq Scan on public.text_tbl t1
|
||||
Output: t1.f1
|
||||
-> Materialize
|
||||
Output: i8.q1, i8.q2
|
||||
-> Seq Scan on public.int8_tbl i8
|
||||
Output: i8.q1, i8.q2
|
||||
Filter: (i8.q2 = 123)
|
||||
-> Limit
|
||||
Output: (i8.q1), t2.f1
|
||||
-> Seq Scan on public.text_tbl t2
|
||||
Output: i8.q1, t2.f1
|
||||
-> Limit
|
||||
Output: ((i8.q1)), (t2.f1)
|
||||
-> Seq Scan on public.text_tbl t3
|
||||
Output: (i8.q1), t2.f1
|
||||
(22 rows)
|
||||
|
||||
select * from
|
||||
text_tbl t1
|
||||
left join int8_tbl i8
|
||||
on i8.q2 = 123,
|
||||
lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
|
||||
lateral (select ss1.* from text_tbl t3 limit 1) as ss2
|
||||
where t1.f1 = ss2.f1;
|
||||
f1 | q1 | q2 | q1 | f1 | q1 | f1
|
||||
------+------------------+-----+------------------+------+------------------+------
|
||||
doh! | 4567890123456789 | 123 | 4567890123456789 | doh! | 4567890123456789 | doh!
|
||||
(1 row)
|
||||
|
||||
explain (verbose, costs off)
|
||||
select 1 from
|
||||
text_tbl as tt1
|
||||
inner join text_tbl as tt2 on (tt1.f1 = 'foo')
|
||||
left join text_tbl as tt3 on (tt3.f1 = 'foo')
|
||||
left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
|
||||
lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
|
||||
where tt1.f1 = ss1.c0;
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------
|
||||
Nested Loop
|
||||
Output: 1
|
||||
-> Nested Loop Left Join
|
||||
Output: tt1.f1, tt4.f1
|
||||
-> Nested Loop
|
||||
Output: tt1.f1
|
||||
-> Seq Scan on public.text_tbl tt1
|
||||
Output: tt1.f1
|
||||
Filter: (tt1.f1 = 'foo'::text)
|
||||
-> Seq Scan on public.text_tbl tt2
|
||||
Output: tt2.f1
|
||||
-> Materialize
|
||||
Output: tt4.f1
|
||||
-> Nested Loop Left Join
|
||||
Output: tt4.f1
|
||||
Join Filter: (tt3.f1 = tt4.f1)
|
||||
-> Seq Scan on public.text_tbl tt3
|
||||
Output: tt3.f1
|
||||
Filter: (tt3.f1 = 'foo'::text)
|
||||
-> Seq Scan on public.text_tbl tt4
|
||||
Output: tt4.f1
|
||||
Filter: (tt4.f1 = 'foo'::text)
|
||||
-> Subquery Scan on ss1
|
||||
Output: ss1.c0
|
||||
Filter: (ss1.c0 = 'foo'::text)
|
||||
-> Limit
|
||||
Output: (tt4.f1)
|
||||
-> Seq Scan on public.text_tbl tt5
|
||||
Output: tt4.f1
|
||||
(29 rows)
|
||||
|
||||
select 1 from
|
||||
text_tbl as tt1
|
||||
inner join text_tbl as tt2 on (tt1.f1 = 'foo')
|
||||
left join text_tbl as tt3 on (tt3.f1 = 'foo')
|
||||
left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
|
||||
lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
|
||||
where tt1.f1 = ss1.c0;
|
||||
?column?
|
||||
----------
|
||||
(0 rows)
|
||||
|
||||
--
|
||||
-- check a case in which a PlaceHolderVar forces join order
|
||||
--
|
||||
explain (verbose, costs off)
|
||||
select ss2.* from
|
||||
int4_tbl i41
|
||||
left join int8_tbl i8
|
||||
join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
|
||||
from int4_tbl i42, int4_tbl i43) ss1
|
||||
on i8.q1 = ss1.c2
|
||||
on i41.f1 = ss1.c1,
|
||||
lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
|
||||
where ss1.c2 = 0;
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------
|
||||
Nested Loop
|
||||
Output: (i41.f1), (i8.q1), (i8.q2), (i42.f1), (i43.f1), ((42))
|
||||
-> Hash Join
|
||||
Output: i41.f1, i42.f1, i8.q1, i8.q2, i43.f1, 42
|
||||
Hash Cond: (i41.f1 = i42.f1)
|
||||
-> Nested Loop
|
||||
Output: i8.q1, i8.q2, i43.f1, i41.f1
|
||||
-> Nested Loop
|
||||
Output: i8.q1, i8.q2, i43.f1
|
||||
-> Seq Scan on public.int8_tbl i8
|
||||
Output: i8.q1, i8.q2
|
||||
Filter: (i8.q1 = 0)
|
||||
-> Seq Scan on public.int4_tbl i43
|
||||
Output: i43.f1
|
||||
Filter: (i43.f1 = 0)
|
||||
-> Seq Scan on public.int4_tbl i41
|
||||
Output: i41.f1
|
||||
-> Hash
|
||||
Output: i42.f1
|
||||
-> Seq Scan on public.int4_tbl i42
|
||||
Output: i42.f1
|
||||
-> Limit
|
||||
Output: (i41.f1), (i8.q1), (i8.q2), (i42.f1), (i43.f1), ((42))
|
||||
-> Seq Scan on public.text_tbl
|
||||
Output: i41.f1, i8.q1, i8.q2, i42.f1, i43.f1, (42)
|
||||
(25 rows)
|
||||
|
||||
select ss2.* from
|
||||
int4_tbl i41
|
||||
left join int8_tbl i8
|
||||
join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
|
||||
from int4_tbl i42, int4_tbl i43) ss1
|
||||
on i8.q1 = ss1.c2
|
||||
on i41.f1 = ss1.c1,
|
||||
lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
|
||||
where ss1.c2 = 0;
|
||||
f1 | q1 | q2 | c1 | c2 | c3
|
||||
----+----+----+----+----+----
|
||||
(0 rows)
|
||||
|
||||
--
|
||||
-- test ability to push constants through outer join clauses
|
||||
--
|
||||
@ -4741,14 +4894,12 @@ select * from
|
||||
Output: a.q1, a.q2
|
||||
-> Nested Loop
|
||||
Output: b.q1, c.q1, LEAST(a.q1, b.q1, c.q1)
|
||||
Join Filter: (a.q2 = b.q1)
|
||||
-> Seq Scan on public.int8_tbl b
|
||||
Output: b.q1, b.q2
|
||||
-> Materialize
|
||||
Output: c.q1
|
||||
-> Seq Scan on public.int8_tbl c
|
||||
Output: c.q1
|
||||
(13 rows)
|
||||
Filter: (a.q2 = b.q1)
|
||||
-> Seq Scan on public.int8_tbl c
|
||||
Output: c.q1, c.q2
|
||||
(11 rows)
|
||||
|
||||
select * from
|
||||
int8_tbl a left join lateral
|
||||
|
@ -1134,6 +1134,65 @@ select * from
|
||||
lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
|
||||
where t1.f1 = ss.f1;
|
||||
|
||||
explain (verbose, costs off)
|
||||
select * from
|
||||
text_tbl t1
|
||||
left join int8_tbl i8
|
||||
on i8.q2 = 123,
|
||||
lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
|
||||
lateral (select ss1.* from text_tbl t3 limit 1) as ss2
|
||||
where t1.f1 = ss2.f1;
|
||||
|
||||
select * from
|
||||
text_tbl t1
|
||||
left join int8_tbl i8
|
||||
on i8.q2 = 123,
|
||||
lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
|
||||
lateral (select ss1.* from text_tbl t3 limit 1) as ss2
|
||||
where t1.f1 = ss2.f1;
|
||||
|
||||
explain (verbose, costs off)
|
||||
select 1 from
|
||||
text_tbl as tt1
|
||||
inner join text_tbl as tt2 on (tt1.f1 = 'foo')
|
||||
left join text_tbl as tt3 on (tt3.f1 = 'foo')
|
||||
left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
|
||||
lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
|
||||
where tt1.f1 = ss1.c0;
|
||||
|
||||
select 1 from
|
||||
text_tbl as tt1
|
||||
inner join text_tbl as tt2 on (tt1.f1 = 'foo')
|
||||
left join text_tbl as tt3 on (tt3.f1 = 'foo')
|
||||
left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
|
||||
lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
|
||||
where tt1.f1 = ss1.c0;
|
||||
|
||||
--
|
||||
-- check a case in which a PlaceHolderVar forces join order
|
||||
--
|
||||
|
||||
explain (verbose, costs off)
|
||||
select ss2.* from
|
||||
int4_tbl i41
|
||||
left join int8_tbl i8
|
||||
join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
|
||||
from int4_tbl i42, int4_tbl i43) ss1
|
||||
on i8.q1 = ss1.c2
|
||||
on i41.f1 = ss1.c1,
|
||||
lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
|
||||
where ss1.c2 = 0;
|
||||
|
||||
select ss2.* from
|
||||
int4_tbl i41
|
||||
left join int8_tbl i8
|
||||
join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
|
||||
from int4_tbl i42, int4_tbl i43) ss1
|
||||
on i8.q1 = ss1.c2
|
||||
on i41.f1 = ss1.c1,
|
||||
lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
|
||||
where ss1.c2 = 0;
|
||||
|
||||
--
|
||||
-- test ability to push constants through outer join clauses
|
||||
--
|
||||
|
Reference in New Issue
Block a user