Skip to content

Commit b44a170

Browse files
committed
Fix the way SJE removes references from PHVs
Add missing replacement of relids in phv->phexpr. Also, remove extra replace_relid() over phv->phrels. Reported-by: Zuming Jiang Bug: #18187 Discussion: https://postgr.es/m/flat/18187-831da249cbd2ff8e%40postgresql.org Author: Richard Guo Reviewed-by: Andrei Lepikhov
1 parent 3850d4d commit b44a170

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/backend/optimizer/plan/analyzejoins.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ remove_rel_from_query(PlannerInfo *root, RelOptInfo *rel,
474474
/* ph_needed might or might not become empty */
475475
phv->phrels = replace_relid(phv->phrels, relid, subst);
476476
phv->phrels = replace_relid(phv->phrels, ojrelid, subst);
477-
phinfo->ph_lateral = replace_relid(phinfo->ph_lateral, relid, subst);
478-
phinfo->ph_var->phrels = replace_relid(phinfo->ph_var->phrels, relid, subst);
479477
Assert(!bms_is_empty(phv->phrels));
478+
replace_varno((Node *) phv->phexpr, relid, subst);
479+
phinfo->ph_lateral = replace_relid(phinfo->ph_lateral, relid, subst);
480480
Assert(phv->phnullingrels == NULL); /* no need to adjust */
481481
}
482482
}

src/test/regress/expected/join.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6776,6 +6776,25 @@ SELECT c3.code FROM emp1 c3;
67766776
-> Seq Scan on emp1 c3
67776777
(7 rows)
67786778

6779+
-- Check that SJE removes references from PHVs correctly
6780+
explain (costs off)
6781+
select * from emp1 t1 left join
6782+
(select coalesce(t3.code, 1) from emp1 t2
6783+
left join (emp1 t3 join emp1 t4 on t3.id = t4.id)
6784+
on true)
6785+
on true;
6786+
QUERY PLAN
6787+
----------------------------------------------------
6788+
Nested Loop Left Join
6789+
-> Seq Scan on emp1 t1
6790+
-> Materialize
6791+
-> Nested Loop Left Join
6792+
-> Seq Scan on emp1 t2
6793+
-> Materialize
6794+
-> Seq Scan on emp1 t4
6795+
Filter: (id IS NOT NULL)
6796+
(8 rows)
6797+
67796798
-- We can remove the join even if we find the join can't duplicate rows and
67806799
-- the base quals of each side are different. In the following case we end up
67816800
-- moving quals over to s1 to make it so it can't match any rows.

src/test/regress/sql/join.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,14 @@ WHERE c2.id IS NOT NULL
25802580
EXCEPT ALL
25812581
SELECT c3.code FROM emp1 c3;
25822582

2583+
-- Check that SJE removes references from PHVs correctly
2584+
explain (costs off)
2585+
select * from emp1 t1 left join
2586+
(select coalesce(t3.code, 1) from emp1 t2
2587+
left join (emp1 t3 join emp1 t4 on t3.id = t4.id)
2588+
on true)
2589+
on true;
2590+
25832591
-- We can remove the join even if we find the join can't duplicate rows and
25842592
-- the base quals of each side are different. In the following case we end up
25852593
-- moving quals over to s1 to make it so it can't match any rows.

0 commit comments

Comments
 (0)