Skip to content

Commit e98c900

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 36a4ac2 commit e98c900

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
@@ -3256,6 +3256,7 @@ remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
32563256
subrelids = get_relids_in_jointree(newjtloc, false);
32573257
Assert(!bms_is_empty(subrelids));
32583258
substitute_phv_relids((Node *) root->parse, varno, subrelids);
3259+
fix_append_rel_relids(root->append_rel_list, varno, subrelids);
32593260
}
32603261

32613262
/*

src/test/regress/expected/join.out

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,6 +3333,42 @@ select * from
33333333
1 | 2 | 2
33343334
(1 row)
33353335

3336+
-- Test proper handling of appendrel PHVs during useless-RTE removal
3337+
explain (costs off)
3338+
select * from
3339+
(select 0 as z) as t1
3340+
left join
3341+
(select true as a) as t2
3342+
on true,
3343+
lateral (select true as b
3344+
union all
3345+
select a as b) as t3
3346+
where b;
3347+
QUERY PLAN
3348+
---------------------------------------
3349+
Nested Loop
3350+
-> Result
3351+
-> Append
3352+
-> Result
3353+
-> Result
3354+
One-Time Filter: (true)
3355+
(6 rows)
3356+
3357+
select * from
3358+
(select 0 as z) as t1
3359+
left join
3360+
(select true as a) as t2
3361+
on true,
3362+
lateral (select true as b
3363+
union all
3364+
select a as b) as t3
3365+
where b;
3366+
z | a | b
3367+
---+---+---
3368+
0 | t | t
3369+
0 | t | t
3370+
(2 rows)
3371+
33363372
--
33373373
-- test inlining of immutable functions
33383374
--

src/test/regress/sql/join.sql

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

1059+
-- Test proper handling of appendrel PHVs during useless-RTE removal
1060+
explain (costs off)
1061+
select * from
1062+
(select 0 as z) as t1
1063+
left join
1064+
(select true as a) as t2
1065+
on true,
1066+
lateral (select true as b
1067+
union all
1068+
select a as b) as t3
1069+
where b;
1070+
1071+
select * from
1072+
(select 0 as z) as t1
1073+
left join
1074+
(select true as a) as t2
1075+
on true,
1076+
lateral (select true as b
1077+
union all
1078+
select a as b) as t3
1079+
where b;
1080+
10591081
--
10601082
-- test inlining of immutable functions
10611083
--

0 commit comments

Comments
 (0)