Skip to content

Commit 5d545b7

Browse files
committed
Fix inherited UPDATE/DELETE with UNION ALL subqueries.
Fix an oversight in commit b3aaf90: we do indeed need to process the planner's append_rel_list when copying RTE subqueries, because if any of them were flattenable UNION ALL subqueries, the append_rel_list shows which subquery RTEs were pulled up out of which other ones. Without this, UNION ALL subqueries aren't correctly inserted into the update plans for inheritance child tables after the first one, typically resulting in no update happening for those child table(s). Per report from Victor Yegorov. Experimentation with this case also exposed a fault in commit a7b9653: if an inherited UPDATE/DELETE was proven totally dummy by constraint exclusion, we might arrive at add_rtes_to_flat_rtable with root->simple_rel_array being NULL. This should be interpreted as not having any RelOptInfos. I chose to code the guard as a check against simple_rel_array_size, so as to also provide some protection against indexing off the end of the array. Back-patch to 9.2 where the faulty code was added.
1 parent d20eec8 commit 5d545b7

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,13 @@ inheritance_planner(PlannerInfo *root)
781781
*/
782782
subroot.rowMarks = (List *) copyObject(root->rowMarks);
783783

784+
/*
785+
* The append_rel_list likewise might contain references to subquery
786+
* RTEs (if any subqueries were flattenable UNION ALLs). So prepare
787+
* to apply ChangeVarNodes to that, too.
788+
*/
789+
subroot.append_rel_list = (List *) copyObject(root->append_rel_list);
790+
784791
/*
785792
* Add placeholders to the child Query's rangetable list to fill the
786793
* RT indexes already reserved for subqueries in previous children.
@@ -821,6 +828,7 @@ inheritance_planner(PlannerInfo *root)
821828
newrti = list_length(subroot.parse->rtable) + 1;
822829
ChangeVarNodes((Node *) subroot.parse, rti, newrti, 0);
823830
ChangeVarNodes((Node *) subroot.rowMarks, rti, newrti, 0);
831+
ChangeVarNodes((Node *) subroot.append_rel_list, rti, newrti, 0);
824832
rte = copyObject(rte);
825833
subroot.parse->rtable = lappend(subroot.parse->rtable,
826834
rte);
@@ -829,7 +837,6 @@ inheritance_planner(PlannerInfo *root)
829837
}
830838
}
831839

832-
/* We needn't modify the child's append_rel_list */
833840
/* There shouldn't be any OJ info to translate, as yet */
834841
Assert(subroot.join_info_list == NIL);
835842
/* and we haven't created PlaceHolderInfos, either */

src/test/regress/expected/inherit.out

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ insert into bar2 values(2,2,2);
558558
insert into bar2 values(3,3,3);
559559
insert into bar2 values(4,4,4);
560560
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
561-
SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid
562-
order by 1,2;
561+
select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
563562
relname | f1 | f2
564563
---------+----+-----
565564
bar | 1 | 101
@@ -572,6 +571,24 @@ order by 1,2;
572571
bar2 | 4 | 4
573572
(8 rows)
574573

574+
-- Check UPDATE with inherited target and an appendrel subquery
575+
update bar set f2 = f2 + 100
576+
from
577+
( select f1 from foo union all select f1+3 from foo ) ss
578+
where bar.f1 = ss.f1;
579+
select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
580+
relname | f1 | f2
581+
---------+----+-----
582+
bar | 1 | 201
583+
bar | 2 | 202
584+
bar | 3 | 203
585+
bar | 4 | 104
586+
bar2 | 1 | 201
587+
bar2 | 2 | 202
588+
bar2 | 3 | 203
589+
bar2 | 4 | 104
590+
(8 rows)
591+
575592
/* Test multiple inheritance of column defaults */
576593
CREATE TABLE firstparent (tomorrow date default now()::date + 1);
577594
CREATE TABLE secondparent (tomorrow date default now() :: date + 1);

src/test/regress/sql/inherit.sql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,15 @@ insert into bar2 values(4,4,4);
118118

119119
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
120120

121-
SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid
122-
order by 1,2;
121+
select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
122+
123+
-- Check UPDATE with inherited target and an appendrel subquery
124+
update bar set f2 = f2 + 100
125+
from
126+
( select f1 from foo union all select f1+3 from foo ) ss
127+
where bar.f1 = ss.f1;
128+
129+
select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
123130

124131
/* Test multiple inheritance of column defaults */
125132

0 commit comments

Comments
 (0)