1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +03:00

Fix some new issues with planning of PlaceHolderVars.

In the wake of commit a16ef313f, we need to deal with more cases
involving PlaceHolderVars in NestLoopParams than we did before.

For one thing, a16ef313f was incorrect to suppose that we could
rely on the required-outer relids of the lefthand path to decide
placement of nestloop-parameter PHVs.  As Richard Guo argued at
the time, we must look at the required-outer relids of the join
path itself.

For another, we have to apply replace_nestloop_params() to such
a PHV's expression, in case it contains references to values that
will be supplied from NestLoopParams of higher-level nestloops.

For another, we need to be more careful about the phnullingrels
of the PHV than we were being.  identify_current_nestloop_params
only bothered to ensure that the phnullingrels didn't contain
"too many" relids, but now it has to be exact, because setrefs.c
will apply both NRM_SUBSET and NRM_SUPERSET checks in different
places.  We can compute the correct relids by determining the
set of outer joins that should be able to null the PHV and then
subtracting whatever's been applied at or below this join.
Do the same for plain Vars, too.  (This should make it possible
to use NRM_EQUAL to process nestloop params in setrefs.c, but
I won't risk making such a change in v18 now.)

Lastly, if a nestloop parameter PHV was pulled up out of a subquery
and it contains a subquery that was originally pushed down from this
query level, then that will still be represented as a SubLink, because
SS_process_sublinks won't recurse into outer PHVs, so it didn't get
transformed during expression preprocessing in the subquery.  We can
substitute the version of the PHV's expression appearing in its
PlaceHolderInfo to ensure that that preprocessing has happened.
(Seems like this processing sequence could stand to be redesigned,
but again, late in v18 development is not the time for that.)

It's not very clear to me why the old have_dangerous_phv join-order
restriction prevented us from seeing the last three of these problems.
But given the lack of field complaints, it must have done so.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/18953-1c9883a9d4afeb30@postgresql.org
This commit is contained in:
Tom Lane
2025-06-29 15:04:32 -04:00
parent 8319e5cb54
commit 66e9df9f6e
7 changed files with 330 additions and 37 deletions

View File

@@ -4119,6 +4119,164 @@ select * from int8_tbl t1
(16 rows)
rollback;
-- ... not that the initial replacement didn't have some bugs too
begin;
create temp table t(i int primary key);
explain (verbose, costs off)
select * from t t1
left join (select 1 as x, * from t t2(i2)) t2ss on t1.i = t2ss.i2
left join t t3(i3) on false
left join t t4(i4) on t4.i4 > t2ss.x;
QUERY PLAN
----------------------------------------------------------
Nested Loop Left Join
Output: t1.i, (1), t2.i2, i3, t4.i4
-> Nested Loop Left Join
Output: t1.i, t2.i2, (1), i3
Join Filter: false
-> Hash Left Join
Output: t1.i, t2.i2, (1)
Inner Unique: true
Hash Cond: (t1.i = t2.i2)
-> Seq Scan on pg_temp.t t1
Output: t1.i
-> Hash
Output: t2.i2, (1)
-> Seq Scan on pg_temp.t t2
Output: t2.i2, 1
-> Result
Output: i3
One-Time Filter: false
-> Memoize
Output: t4.i4
Cache Key: (1)
Cache Mode: binary
-> Index Only Scan using t_pkey on pg_temp.t t4
Output: t4.i4
Index Cond: (t4.i4 > (1))
(25 rows)
explain (verbose, costs off)
select * from
(select k from
(select i, coalesce(i, j) as k from
(select i from t union all select 0)
join (select 1 as j limit 1) on i = j)
right join (select 2 as x) on true
join (select 3 as y) on i is not null
),
lateral (select k as kl limit 1);
QUERY PLAN
-------------------------------------------------------------------
Nested Loop
Output: COALESCE(t.i, (1)), ((COALESCE(t.i, (1))))
-> Limit
Output: 1
-> Result
Output: 1
-> Nested Loop
Output: t.i, ((COALESCE(t.i, (1))))
-> Result
Output: t.i, COALESCE(t.i, (1))
-> Append
-> Index Only Scan using t_pkey on pg_temp.t
Output: t.i
Index Cond: (t.i = (1))
-> Result
Output: 0
One-Time Filter: ((1) = 0)
-> Limit
Output: ((COALESCE(t.i, (1))))
-> Result
Output: (COALESCE(t.i, (1)))
(21 rows)
rollback;
-- PHVs containing SubLinks are quite tricky to get right
explain (verbose, costs off)
select *
from int8_tbl i8
inner join
(select (select true) as x
from int4_tbl i4, lateral (select i4.f1 as y limit 1) ss1
where i4.f1 = 0) ss2 on true
right join (select false as z) ss3 on true,
lateral (select i8.q2 as q2l where x limit 1) ss4
where i8.q2 = 123;
QUERY PLAN
----------------------------------------------------------------
Nested Loop
Output: i8.q1, i8.q2, (InitPlan 1).col1, false, (i8.q2)
InitPlan 1
-> Result
Output: true
InitPlan 2
-> Result
Output: true
-> Seq Scan on public.int4_tbl i4
Output: i4.f1
Filter: (i4.f1 = 0)
-> Nested Loop
Output: i8.q1, i8.q2, (i8.q2)
-> Subquery Scan on ss1
Output: ss1.y, (InitPlan 1).col1
-> Limit
Output: NULL::integer
-> Result
Output: NULL::integer
-> Nested Loop
Output: i8.q1, i8.q2, (i8.q2)
-> Seq Scan on public.int8_tbl i8
Output: i8.q1, i8.q2
Filter: (i8.q2 = 123)
-> Limit
Output: (i8.q2)
-> Result
Output: i8.q2
One-Time Filter: ((InitPlan 1).col1)
(29 rows)
explain (verbose, costs off)
select *
from int8_tbl i8
inner join
(select (select true) as x
from int4_tbl i4, lateral (select 1 as y limit 1) ss1
where i4.f1 = 0) ss2 on true
right join (select false as z) ss3 on true,
lateral (select i8.q2 as q2l where x limit 1) ss4
where i8.q2 = 123;
QUERY PLAN
----------------------------------------------------------------
Nested Loop
Output: i8.q1, i8.q2, (InitPlan 1).col1, false, (i8.q2)
InitPlan 1
-> Result
Output: true
InitPlan 2
-> Result
Output: true
-> Limit
Output: NULL::integer
-> Result
Output: NULL::integer
-> Nested Loop
Output: i8.q1, i8.q2, (i8.q2)
-> Seq Scan on public.int4_tbl i4
Output: i4.f1, (InitPlan 1).col1
Filter: (i4.f1 = 0)
-> Nested Loop
Output: i8.q1, i8.q2, (i8.q2)
-> Seq Scan on public.int8_tbl i8
Output: i8.q1, i8.q2
Filter: (i8.q2 = 123)
-> Limit
Output: (i8.q2)
-> Result
Output: i8.q2
One-Time Filter: ((InitPlan 1).col1)
(27 rows)
-- Test proper handling of appendrel PHVs during useless-RTE removal
explain (costs off)
select * from

View File

@@ -1361,6 +1361,52 @@ select * from int8_tbl t1
on true;
rollback;
-- ... not that the initial replacement didn't have some bugs too
begin;
create temp table t(i int primary key);
explain (verbose, costs off)
select * from t t1
left join (select 1 as x, * from t t2(i2)) t2ss on t1.i = t2ss.i2
left join t t3(i3) on false
left join t t4(i4) on t4.i4 > t2ss.x;
explain (verbose, costs off)
select * from
(select k from
(select i, coalesce(i, j) as k from
(select i from t union all select 0)
join (select 1 as j limit 1) on i = j)
right join (select 2 as x) on true
join (select 3 as y) on i is not null
),
lateral (select k as kl limit 1);
rollback;
-- PHVs containing SubLinks are quite tricky to get right
explain (verbose, costs off)
select *
from int8_tbl i8
inner join
(select (select true) as x
from int4_tbl i4, lateral (select i4.f1 as y limit 1) ss1
where i4.f1 = 0) ss2 on true
right join (select false as z) ss3 on true,
lateral (select i8.q2 as q2l where x limit 1) ss4
where i8.q2 = 123;
explain (verbose, costs off)
select *
from int8_tbl i8
inner join
(select (select true) as x
from int4_tbl i4, lateral (select 1 as y limit 1) ss1
where i4.f1 = 0) ss2 on true
right join (select false as z) ss3 on true,
lateral (select i8.q2 as q2l where x limit 1) ss4
where i8.q2 = 123;
-- Test proper handling of appendrel PHVs during useless-RTE removal
explain (costs off)
select * from