From 324577f39bc8738ed0ec24c36c5cb2c2f81ec660 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 14 Dec 2013 17:33:56 -0500 Subject: [PATCH] Fix inherited UPDATE/DELETE with UNION ALL subqueries. Fix an oversight in commit b3aaf9081a1a95c245fd605dcf02c91b3a5c3a29: 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 a7b965382cf0cb30aeacb112572718045e6d4be7: 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. --- src/backend/optimizer/plan/planner.c | 9 ++++++++- src/backend/optimizer/plan/setrefs.c | 3 ++- src/test/regress/expected/inherit.out | 21 +++++++++++++++++++-- src/test/regress/sql/inherit.sql | 11 +++++++++-- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 432ea3106b..4a390c493d 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -825,6 +825,13 @@ inheritance_planner(PlannerInfo *root) */ subroot.rowMarks = (List *) copyObject(root->rowMarks); + /* + * The append_rel_list likewise might contain references to subquery + * RTEs (if any subqueries were flattenable UNION ALLs). So prepare + * to apply ChangeVarNodes to that, too. + */ + subroot.append_rel_list = (List *) copyObject(root->append_rel_list); + /* * Add placeholders to the child Query's rangetable list to fill the * RT indexes already reserved for subqueries in previous children. @@ -865,6 +872,7 @@ inheritance_planner(PlannerInfo *root) newrti = list_length(subroot.parse->rtable) + 1; ChangeVarNodes((Node *) subroot.parse, rti, newrti, 0); ChangeVarNodes((Node *) subroot.rowMarks, rti, newrti, 0); + ChangeVarNodes((Node *) subroot.append_rel_list, rti, newrti, 0); rte = copyObject(rte); subroot.parse->rtable = lappend(subroot.parse->rtable, rte); @@ -873,7 +881,6 @@ inheritance_planner(PlannerInfo *root) } } - /* We needn't modify the child's append_rel_list */ /* There shouldn't be any OJ or LATERAL info to translate, as yet */ Assert(subroot.join_info_list == NIL); Assert(subroot.lateral_info_list == NIL); diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index b78d72701b..36f1d84adb 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -280,7 +280,8 @@ add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing) * RTEs without matching RelOptInfos, as they likewise have been * pulled up. */ - if (rte->rtekind == RTE_SUBQUERY && !rte->inh) + if (rte->rtekind == RTE_SUBQUERY && !rte->inh && + rti < root->simple_rel_array_size) { RelOptInfo *rel = root->simple_rel_array[rti]; diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index bfe67337b7..c84c435a8a 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -557,8 +557,7 @@ insert into bar2 values(2,2,2); insert into bar2 values(3,3,3); insert into bar2 values(4,4,4); update bar set f2 = f2 + 100 where f1 in (select f1 from foo); -SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid -order by 1,2; +select tableoid::regclass::text as relname, bar.* from bar order by 1,2; relname | f1 | f2 ---------+----+----- bar | 1 | 101 @@ -571,6 +570,24 @@ order by 1,2; bar2 | 4 | 4 (8 rows) +-- Check UPDATE with inherited target and an appendrel subquery +update bar set f2 = f2 + 100 +from + ( select f1 from foo union all select f1+3 from foo ) ss +where bar.f1 = ss.f1; +select tableoid::regclass::text as relname, bar.* from bar order by 1,2; + relname | f1 | f2 +---------+----+----- + bar | 1 | 201 + bar | 2 | 202 + bar | 3 | 203 + bar | 4 | 104 + bar2 | 1 | 201 + bar2 | 2 | 202 + bar2 | 3 | 203 + bar2 | 4 | 104 +(8 rows) + /* Test multiple inheritance of column defaults */ CREATE TABLE firstparent (tomorrow date default now()::date + 1); CREATE TABLE secondparent (tomorrow date default now() :: date + 1); diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql index 747aa88666..09bb7507ad 100644 --- a/src/test/regress/sql/inherit.sql +++ b/src/test/regress/sql/inherit.sql @@ -118,8 +118,15 @@ insert into bar2 values(4,4,4); update bar set f2 = f2 + 100 where f1 in (select f1 from foo); -SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid -order by 1,2; +select tableoid::regclass::text as relname, bar.* from bar order by 1,2; + +-- Check UPDATE with inherited target and an appendrel subquery +update bar set f2 = f2 + 100 +from + ( select f1 from foo union all select f1+3 from foo ) ss +where bar.f1 = ss.f1; + +select tableoid::regclass::text as relname, bar.* from bar order by 1,2; /* Test multiple inheritance of column defaults */ -- 2.40.0