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

Commit c03ad56

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 60eea37 commit c03ad56

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

src/backend/optimizer/plan/planner.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,13 @@ inheritance_planner(PlannerInfo *root)
848848
*/
849849
subroot.rowMarks = (List *) copyObject(root->rowMarks);
850850

851+
/*
852+
* The append_rel_list likewise might contain references to subquery
853+
* RTEs (if any subqueries were flattenable UNION ALLs). So prepare
854+
* to apply ChangeVarNodes to that, too.
855+
*/
856+
subroot.append_rel_list = (List *) copyObject(root->append_rel_list);
857+
851858
/*
852859
* Add placeholders to the child Query's rangetable list to fill the
853860
* RT indexes already reserved for subqueries in previous children.
@@ -888,6 +895,7 @@ inheritance_planner(PlannerInfo *root)
888895
newrti = list_length(subroot.parse->rtable) + 1;
889896
ChangeVarNodes((Node *) subroot.parse, rti, newrti, 0);
890897
ChangeVarNodes((Node *) subroot.rowMarks, rti, newrti, 0);
898+
ChangeVarNodes((Node *) subroot.append_rel_list, rti, newrti, 0);
891899
rte = copyObject(rte);
892900
subroot.parse->rtable = lappend(subroot.parse->rtable,
893901
rte);
@@ -896,7 +904,6 @@ inheritance_planner(PlannerInfo *root)
896904
}
897905
}
898906

899-
/* We needn't modify the child's append_rel_list */
900907
/* There shouldn't be any OJ or LATERAL info to translate, as yet */
901908
Assert(subroot.join_info_list == NIL);
902909
Assert(subroot.lateral_info_list == NIL);

src/backend/optimizer/plan/setrefs.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
280280
* RTEs without matching RelOptInfos, as they likewise have been
281281
* pulled up.
282282
*/
283-
if (rte->rtekind == RTE_SUBQUERY && !rte->inh)
283+
if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
284+
rti < root->simple_rel_array_size)
284285
{
285286
RelOptInfo *rel = root->simple_rel_array[rti];
286287

src/test/regress/expected/inherit.out

+19-2
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,7 @@ insert into bar2 values(2,2,2);
557557
insert into bar2 values(3,3,3);
558558
insert into bar2 values(4,4,4);
559559
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
560-
SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid
561-
order by 1,2;
560+
select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
562561
relname | f1 | f2
563562
---------+----+-----
564563
bar | 1 | 101
@@ -571,6 +570,24 @@ order by 1,2;
571570
bar2 | 4 | 4
572571
(8 rows)
573572

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

src/test/regress/sql/inherit.sql

+9-2
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)