From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Petr Fedorov <petr.fedorov@phystech.edu>
Cc: pgsql-bugs@postgresql.org
Subject: Re: 'update returning *' returns 0 columns instead of empty row with 2 columns when (i) no rows updated and (ii) when applied to a partitioned table with sub-partition
Date: Fri, 22 Feb 2019 14:33:55 +0900
Message-ID: <c3b4762b-ccb4-8e6d-7012-e2c7ea76bc01@lab.ntt.co.jp> (raw)
In-Reply-To: <3381.1550810593@sss.pgh.pa.us>
References: <5da6f0f0-1364-1876-6978-907678f89a3e@phystech.edu>
<907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp>
<13543.1550787490@sss.pgh.pa.us>
<6a08a294-6a16-e384-db25-8f4d3cc026d1@lab.ntt.co.jp>
<3381.1550810593@sss.pgh.pa.us>
On 2019/02/22 13:43, Tom Lane wrote:
> Amit Langote <Langote_Amit_f8@lab.ntt.co.jp> writes:
>> Also, I noticed regression test failure having to do with statement
>> triggers not firing, which makes sense, as there's no ModifyTable to
>> invoke them.
>
> Oh! You are right, that's a separate bug. So really we can't have
> this fast-path exit at all, we should produce a ModifyTable node in
> every case.
>
> I'm too tired to work on that anymore today, do you want to run
> with it?
Sure, see attached a patch.
To fix the trigger bug, we'll be putting a minimally valid-looking
ModifyTable node on top of a dummy plan. So, the bug reported on this
thread is taken care of automatically, because ModifyTable plan's
targetlist is already set correctly.
Thanks,
Amit
Attachments:fix-empty-plan-with-RETURNING-3.patch (text/plain, 6.7K) downloaddiff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.cindex 5579dfa65e..4a9b9c16f0 100644--- a/src/backend/optimizer/plan/planner.c+++ b/src/backend/optimizer/plan/planner.c@@ -1584,13 +1584,36 @@ inheritance_planner(PlannerInfo *root)
*/
/*
- * If we managed to exclude every child rel, return a dummy plan; it- * doesn't even need a ModifyTable node.+ * We managed to exclude every child rel, so generate a dummy path+ * representing 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.
*/
if (subpaths == NIL)
{
- set_dummy_rel_pathlist(final_rel);- return;+ Path *dummy_path;+ List *tlist;++ tlist = root->processed_tlist = preprocess_targetlist(root);+ final_rel->reltarget = create_pathtarget(root, tlist);++ /*+ * Following information will be needed to make a minimally valid+ * ModifyTable.+ */+ dummy_path = (Path *) create_append_path(NULL, final_rel, NIL, NIL,+ NULL, 0, false, NIL, -1);+ resultRelations = list_make1_int(parse->resultRelation);+ subpaths = list_make1(dummy_path);+ subroots = list_make1(root);+ if (parse->returningList)+ returningLists = list_make1(parse->returningList);++ /*+ * At least simple_rte_array must be valid for plan creation to work+ * correctly.+ */+ final_rtable = parse->rtable;
}
/*
diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.outindex f259d07535..565d947b6d 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.outindex 2b908ceec4..40aa8d18ae 100644--- a/src/test/regress/expected/triggers.out+++ b/src/test/regress/expected/triggers.out@@ -2790,3 +2790,35 @@ drop table self_ref;
drop function dump_insert();
drop function dump_update();
drop function dump_delete();
+-- Check that statement triggers work correctly even with an empty update+create table stmt_trig_on_empty_upd (a int);+create table stmt_trig_on_empty_upd1 () inherits (stmt_trig_on_empty_upd);+create or replace 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 function update_stmt_notice();+create trigger before_stmt_trigger+ before update on stmt_trig_on_empty_upd1+ execute function update_stmt_notice();+-- inherited 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 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;diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sqlindex 425052c1f4..5480fe7db4 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.sqlindex 60d1dc6f46..73128a6542 100644--- a/src/test/regress/sql/triggers.sql+++ b/src/test/regress/sql/triggers.sql@@ -2124,3 +2124,25 @@ drop table self_ref;
drop function dump_insert();
drop function dump_update();
drop function dump_delete();
++-- Check that statement triggers work correctly even with an empty update+create table stmt_trig_on_empty_upd (a int);+create table stmt_trig_on_empty_upd1 () inherits (stmt_trig_on_empty_upd);+create or replace 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 function update_stmt_notice();+create trigger before_stmt_trigger+ before update on stmt_trig_on_empty_upd1+ execute function update_stmt_notice();+-- inherited update+update stmt_trig_on_empty_upd set a = a where false returning a+1 as aa;+-- simple 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;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: Langote_Amit_f8@lab.ntt.co.jp, tgl@sss.pgh.pa.us, petr.fedorov@phystech.edu
Subject: Re: 'update returning *' returns 0 columns instead of empty row with 2 columns when (i) no rows updated and (ii) when applied to a partitioned table with sub-partition
In-Reply-To: <c3b4762b-ccb4-8e6d-7012-e2c7ea76bc01@lab.ntt.co.jp>
References: <5da6f0f0-1364-1876-6978-907678f89a3e@phystech.edu> <907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp> <13543.1550787490@sss.pgh.pa.us> <6a08a294-6a16-e384-db25-8f4d3cc026d1@lab.ntt.co.jp> <3381.1550810593@sss.pgh.pa.us> <c3b4762b-ccb4-8e6d-7012-e2c7ea76bc01@lab.ntt.co.jp>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox