aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/backend/optimizer/prep/prepjointree.c1
-rw-r--r--src/test/regress/expected/join.out36
-rw-r--r--src/test/regress/sql/join.sql22
3 files changed, 59 insertions, 0 deletions
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index 2b19af7cb70..722fd16eeb4 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -3090,6 +3090,7 @@ remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
subrelids = get_relids_in_jointree(newjtloc, false);
Assert(!bms_is_empty(subrelids));
substitute_phv_relids((Node *) root->parse, varno, subrelids);
+ fix_append_rel_relids(root->append_rel_list, varno, subrelids);
}
/*
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index 55f17ffe1dd..3d889314a24 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -3330,6 +3330,42 @@ select * from
1 | 2 | 2
(1 row)
+-- Test proper handling of appendrel PHVs during useless-RTE removal
+explain (costs off)
+select * from
+ (select 0 as z) as t1
+ left join
+ (select true as a) as t2
+ on true,
+ lateral (select true as b
+ union all
+ select a as b) as t3
+where b;
+ QUERY PLAN
+---------------------------------------
+ Nested Loop
+ -> Result
+ -> Append
+ -> Result
+ -> Result
+ One-Time Filter: (true)
+(6 rows)
+
+select * from
+ (select 0 as z) as t1
+ left join
+ (select true as a) as t2
+ on true,
+ lateral (select true as b
+ union all
+ select a as b) as t3
+where b;
+ z | a | b
+---+---+---
+ 0 | t | t
+ 0 | t | t
+(2 rows)
+
--
-- test extraction of restriction OR clauses from join OR clause
-- (we used to only do this for indexable clauses)
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index 7a68d306d1d..9e8dfd156ab 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -1052,6 +1052,28 @@ select * from
(select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
lateral (select ss2.y as z limit 1) ss3;
+-- Test proper handling of appendrel PHVs during useless-RTE removal
+explain (costs off)
+select * from
+ (select 0 as z) as t1
+ left join
+ (select true as a) as t2
+ on true,
+ lateral (select true as b
+ union all
+ select a as b) as t3
+where b;
+
+select * from
+ (select 0 as z) as t1
+ left join
+ (select true as a) as t2
+ on true,
+ lateral (select true as b
+ union all
+ select a as b) as t3
+where b;
+
--
-- test extraction of restriction OR clauses from join OR clause
-- (we used to only do this for indexable clauses)