Skip to content

Commit 77a94c3

Browse files
committed
Fix missed step in removal of useless RESULT RTEs in the planner.
Commit 4be058f forgot that the append_rel_list would already be populated at the time we remove useless result RTEs, and it might contain PlaceHolderVars that need to be adjusted like the ones in the main parse tree. This could lead to "no relation entry for relid N" failures later on, when the planner tries to do something with an unadjusted PHV. Per report from Tom Ellis. Back-patch to v12 where the bug came in. Discussion: https://postgr.es/m/20201205173056.GF30712@cloudinit-builder
1 parent ad3fb04 commit 77a94c3

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/backend/optimizer/prep/prepjointree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,7 @@ remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
30903090
subrelids = get_relids_in_jointree(newjtloc, false);
30913091
Assert(!bms_is_empty(subrelids));
30923092
substitute_phv_relids((Node *) root->parse, varno, subrelids);
3093+
fix_append_rel_relids(root->append_rel_list, varno, subrelids);
30933094
}
30943095

30953096
/*

src/test/regress/expected/join.out

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,6 +3330,42 @@ select * from
33303330
1 | 2 | 2
33313331
(1 row)
33323332

3333+
-- Test proper handling of appendrel PHVs during useless-RTE removal
3334+
explain (costs off)
3335+
select * from
3336+
(select 0 as z) as t1
3337+
left join
3338+
(select true as a) as t2
3339+
on true,
3340+
lateral (select true as b
3341+
union all
3342+
select a as b) as t3
3343+
where b;
3344+
QUERY PLAN
3345+
---------------------------------------
3346+
Nested Loop
3347+
-> Result
3348+
-> Append
3349+
-> Result
3350+
-> Result
3351+
One-Time Filter: (true)
3352+
(6 rows)
3353+
3354+
select * from
3355+
(select 0 as z) as t1
3356+
left join
3357+
(select true as a) as t2
3358+
on true,
3359+
lateral (select true as b
3360+
union all
3361+
select a as b) as t3
3362+
where b;
3363+
z | a | b
3364+
---+---+---
3365+
0 | t | t
3366+
0 | t | t
3367+
(2 rows)
3368+
33333369
--
33343370
-- test extraction of restriction OR clauses from join OR clause
33353371
-- (we used to only do this for indexable clauses)

src/test/regress/sql/join.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,28 @@ select * from
10521052
(select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
10531053
lateral (select ss2.y as z limit 1) ss3;
10541054

1055+
-- Test proper handling of appendrel PHVs during useless-RTE removal
1056+
explain (costs off)
1057+
select * from
1058+
(select 0 as z) as t1
1059+
left join
1060+
(select true as a) as t2
1061+
on true,
1062+
lateral (select true as b
1063+
union all
1064+
select a as b) as t3
1065+
where b;
1066+
1067+
select * from
1068+
(select 0 as z) as t1
1069+
left join
1070+
(select true as a) as t2
1071+
on true,
1072+
lateral (select true as b
1073+
union all
1074+
select a as b) as t3
1075+
where b;
1076+
10551077
--
10561078
-- test extraction of restriction OR clauses from join OR clause
10571079
-- (we used to only do this for indexable clauses)

0 commit comments

Comments
 (0)