Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7d43b76

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 e41a2ef commit 7d43b76

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/backend/optimizer/prep/prepjointree.c

+1
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

+36
Original file line numberDiff line numberDiff line change
@@ -3332,6 +3332,42 @@ select * from
33323332
1 | 2 | 2
33333333
(1 row)
33343334

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

src/test/regress/sql/join.sql

+22
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,28 @@ select * from
10551055
(select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
10561056
lateral (select ss2.y as z limit 1) ss3;
10571057

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

0 commit comments

Comments
 (0)