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

Commit a7928a5

Browse files
committed
Replace the relid in some missing fields during SJE
Reported-by: Alexander Lakhin Discussion: https://postgr.es/m/a89f480f-8143-0965-f22d-0a892777f501%40gmail.com Author: Andrei Lepikhov
1 parent 9a17be1 commit a7928a5

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/backend/optimizer/plan/analyzejoins.c

+5
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,8 @@ remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark,
18661866
/* Replace varno in all the query structures */
18671867
query_tree_walker(root->parse, replace_varno_walker, &ctx,
18681868
QTW_EXAMINE_SORTGROUP);
1869+
if (root->parse->resultRelation == toRemove->relid)
1870+
root->parse->resultRelation = toKeep->relid;
18691871

18701872
/* Replace links in the planner info */
18711873
remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL);
@@ -1875,6 +1877,9 @@ remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark,
18751877
toRemove->relid, toKeep->relid);
18761878
replace_varno((Node *) root->processed_groupClause,
18771879
toRemove->relid, toKeep->relid);
1880+
replace_relid(root->all_result_relids, toRemove->relid, toKeep->relid);
1881+
replace_relid(root->leaf_result_relids, toRemove->relid, toKeep->relid);
1882+
18781883

18791884
/*
18801885
* There may be references to the rel in root->fkey_list, but if so,

src/test/regress/expected/join.out

+13-1
Original file line numberDiff line numberDiff line change
@@ -6868,6 +6868,18 @@ select * from emp1 t1
68686868
-> Seq Scan on emp1 t3
68696869
(6 rows)
68706870

6871+
-- Check that SJE replaces target relation correctly
6872+
explain (costs off)
6873+
WITH t1 AS (SELECT * FROM emp1)
6874+
UPDATE emp1 SET code = t1.code + 1 FROM t1
6875+
WHERE t1.id = emp1.id RETURNING emp1.id, emp1.code;
6876+
QUERY PLAN
6877+
----------------------------------
6878+
Update on emp1
6879+
-> Seq Scan on emp1
6880+
Filter: (id IS NOT NULL)
6881+
(3 rows)
6882+
68716883
-- We can remove the join even if we find the join can't duplicate rows and
68726884
-- the base quals of each side are different. In the following case we end up
68736885
-- moving quals over to s1 to make it so it can't match any rows.
@@ -7030,7 +7042,7 @@ EXPLAIN (COSTS OFF) -- A RowMark exists for the table being kept
70307042
UPDATE sj sq SET b = 1 FROM sj as sz WHERE sq.a = sz.a;
70317043
QUERY PLAN
70327044
---------------------------------
7033-
Update on sj sq
7045+
Update on sj sz
70347046
-> Seq Scan on sj sz
70357047
Filter: (a IS NOT NULL)
70367048
(3 rows)

src/test/regress/sql/join.sql

+6
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,12 @@ select * from emp1 t1
26162616
inner join emp1 t2 on t1.id = t2.id
26172617
left join emp1 t3 on t1.id > 1 and t1.id < 2;
26182618

2619+
-- Check that SJE replaces target relation correctly
2620+
explain (costs off)
2621+
WITH t1 AS (SELECT * FROM emp1)
2622+
UPDATE emp1 SET code = t1.code + 1 FROM t1
2623+
WHERE t1.id = emp1.id RETURNING emp1.id, emp1.code;
2624+
26192625
-- We can remove the join even if we find the join can't duplicate rows and
26202626
-- the base quals of each side are different. In the following case we end up
26212627
-- moving quals over to s1 to make it so it can't match any rows.

0 commit comments

Comments
 (0)