diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/optimizer/plan/planner.c | 52 | ||||
-rw-r--r-- | src/test/regress/expected/inherit.out | 41 | ||||
-rw-r--r-- | src/test/regress/expected/triggers.out | 34 | ||||
-rw-r--r-- | src/test/regress/sql/inherit.sql | 15 | ||||
-rw-r--r-- | src/test/regress/sql/triggers.sql | 27 |
5 files changed, 152 insertions, 17 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 615f1cbc01d..879220c63e4 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1191,29 +1191,47 @@ inheritance_planner(PlannerInfo *root) /* Mark result as unordered (probably unnecessary) */ root->query_pathkeys = NIL; - /* - * If we managed to exclude every child rel, return a dummy plan; it - * doesn't even need a ModifyTable node. - */ if (subplans == NIL) { - /* although dummy, it must have a valid tlist for executor */ + /* + * We managed to exclude every child rel, so generate a dummy path + * representing the empty set. Although it's clear that no data will + * be updated or deleted, we will still need to have a ModifyTable + * node so that any statement triggers are executed. (This could be + * cleaner if we fixed nodeModifyTable.c to support zero child nodes, + * but that probably wouldn't be a net win.) + */ List *tlist; + Plan *subplan; + /* tlist processing never got done, either */ tlist = preprocess_targetlist(root); - return (Plan *) make_result(root, - tlist, - (Node *) list_make1(makeBoolConst(false, - false)), - NULL); - } - /* - * Put back the final adjusted rtable into the master copy of the Query. - */ - parse->rtable = final_rtable; - root->simple_rel_array_size = save_rel_array_size; - root->simple_rel_array = save_rel_array; + subplan = (Plan *) make_result(root, + tlist, + (Node *) list_make1(makeBoolConst(false, + false)), + NULL); + + /* These lists must be nonempty to make a valid ModifyTable node */ + subplans = list_make1(subplan); + resultRelations = list_make1_int(parse->resultRelation); + if (parse->withCheckOptions) + withCheckOptionLists = list_make1(parse->withCheckOptions); + if (parse->returningList) + returningLists = list_make1(parse->returningList); + } + else + { + /* + * Put back the final adjusted rtable into the master copy of the + * Query. (We mustn't do this if we found no non-excluded children, + * since we never saved an adjusted rtable at all.) + */ + parse->rtable = final_rtable; + root->simple_rel_array_size = save_rel_array_size; + root->simple_rel_array = save_rel_array; + } /* * If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node will diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index 50d3403011a..550842fbae2 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -539,6 +539,47 @@ CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a); INSERT INTO z VALUES (NULL, 'text'); -- should fail ERROR: null value in column "aa" violates not-null constraint DETAIL: Failing row contains (null, text). +-- Check inherited UPDATE with all children excluded +create table some_tab (a int, b int); +create table some_tab_child () inherits (some_tab); +insert into some_tab_child values(1,2); +explain (verbose, costs off) +update some_tab set a = a + 1 where false; + QUERY PLAN +---------------------------------- + Update on public.some_tab + Update on public.some_tab + -> Result + Output: (a + 1), b, ctid + One-Time Filter: false +(5 rows) + +update some_tab set a = a + 1 where false; +explain (verbose, costs off) +update some_tab set a = a + 1 where false returning b, a; + QUERY PLAN +---------------------------------- + Update on public.some_tab + Output: b, a + Update on public.some_tab + -> Result + Output: (a + 1), b, ctid + One-Time Filter: false +(6 rows) + +update some_tab set a = a + 1 where false returning b, a; + b | a +---+--- +(0 rows) + +table some_tab; + a | b +---+--- + 1 | 2 +(1 row) + +drop table some_tab cascade; +NOTICE: drop cascades to table some_tab_child -- Check UPDATE with inherited target and an inherited source table create temp table foo(f1 int, f2 int); create temp table foo2(f3 int) inherits (foo); diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out index 5ccbea00a91..57bce17abb2 100644 --- a/src/test/regress/expected/triggers.out +++ b/src/test/regress/expected/triggers.out @@ -1768,6 +1768,40 @@ drop table self_ref_trigger; drop function self_ref_trigger_ins_func(); drop function self_ref_trigger_del_func(); -- +-- Check that statement triggers work correctly even with all children excluded +-- +create table stmt_trig_on_empty_upd (a int); +create table stmt_trig_on_empty_upd1 () inherits (stmt_trig_on_empty_upd); +create function update_stmt_notice() returns trigger as $$ +begin + raise notice 'updating %', TG_TABLE_NAME; + return null; +end; +$$ language plpgsql; +create trigger before_stmt_trigger + before update on stmt_trig_on_empty_upd + execute procedure update_stmt_notice(); +create trigger before_stmt_trigger + before update on stmt_trig_on_empty_upd1 + execute procedure update_stmt_notice(); +-- inherited no-op update +update stmt_trig_on_empty_upd set a = a where false returning a+1 as aa; +NOTICE: updating stmt_trig_on_empty_upd + aa +---- +(0 rows) + +-- simple no-op update +update stmt_trig_on_empty_upd1 set a = a where false returning a+1 as aa; +NOTICE: updating stmt_trig_on_empty_upd1 + aa +---- +(0 rows) + +drop table stmt_trig_on_empty_upd cascade; +NOTICE: drop cascades to table stmt_trig_on_empty_upd1 +drop function update_stmt_notice(); +-- -- Verify behavior of before and after triggers with INSERT...ON CONFLICT -- DO UPDATE -- diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql index 385359b7611..6bfd333f916 100644 --- a/src/test/regress/sql/inherit.sql +++ b/src/test/regress/sql/inherit.sql @@ -97,6 +97,21 @@ SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid; CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a); INSERT INTO z VALUES (NULL, 'text'); -- should fail +-- Check inherited UPDATE with all children excluded +create table some_tab (a int, b int); +create table some_tab_child () inherits (some_tab); +insert into some_tab_child values(1,2); + +explain (verbose, costs off) +update some_tab set a = a + 1 where false; +update some_tab set a = a + 1 where false; +explain (verbose, costs off) +update some_tab set a = a + 1 where false returning b, a; +update some_tab set a = a + 1 where false returning b, a; +table some_tab; + +drop table some_tab cascade; + -- Check UPDATE with inherited target and an inherited source table create temp table foo(f1 int, f2 int); create temp table foo2(f3 int) inherits (foo); diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sql index 17620aee130..1c05b665d12 100644 --- a/src/test/regress/sql/triggers.sql +++ b/src/test/regress/sql/triggers.sql @@ -1200,6 +1200,33 @@ drop function self_ref_trigger_ins_func(); drop function self_ref_trigger_del_func(); -- +-- Check that statement triggers work correctly even with all children excluded +-- + +create table stmt_trig_on_empty_upd (a int); +create table stmt_trig_on_empty_upd1 () inherits (stmt_trig_on_empty_upd); +create function update_stmt_notice() returns trigger as $$ +begin + raise notice 'updating %', TG_TABLE_NAME; + return null; +end; +$$ language plpgsql; +create trigger before_stmt_trigger + before update on stmt_trig_on_empty_upd + execute procedure update_stmt_notice(); +create trigger before_stmt_trigger + before update on stmt_trig_on_empty_upd1 + execute procedure update_stmt_notice(); + +-- inherited no-op update +update stmt_trig_on_empty_upd set a = a where false returning a+1 as aa; +-- simple no-op update +update stmt_trig_on_empty_upd1 set a = a where false returning a+1 as aa; + +drop table stmt_trig_on_empty_upd cascade; +drop function update_stmt_notice(); + +-- -- Verify behavior of before and after triggers with INSERT...ON CONFLICT -- DO UPDATE -- |