Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwwfk-0001a0-Fz for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Feb 2019 22:18:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gwwfi-0004EM-W1 for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Feb 2019 22:18:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwwfi-0004EF-PS for pgsql-bugs@lists.postgresql.org; Thu, 21 Feb 2019 22:18:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gwwfb-0003Bt-JI for pgsql-bugs@postgresql.org; Thu, 21 Feb 2019 22:18:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x1LMIAvX013544; Thu, 21 Feb 2019 17:18:11 -0500 From: Tom Lane To: Amit Langote cc: Petr Fedorov , 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 In-reply-to: <907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp> References: <5da6f0f0-1364-1876-6978-907678f89a3e@phystech.edu> <907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp> Comments: In-reply-to Amit Langote message dated "Wed, 06 Feb 2019 16:35:09 +0900" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <13498.1550787409.0@sss.pgh.pa.us> Date: Thu, 21 Feb 2019 17:18:10 -0500 Message-ID: <13543.1550787490@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <13498.1550787409.1@sss.pgh.pa.us> Amit Langote writes: > On 2019/02/01 23:32, Petr Fedorov wrote: >> ERROR: structure of query does not match function result type > Thanks for the report. There indeed appears to be a bug here. Yup, for sure. You don't actually need a function at all to see that there's a problem: if you just execute UPDATE ... WHERE false RETURNING some-columns; you will notice that the emitted resultset has zero columns. > Attached patch seems to fix it. It also adds a test in inherit.sql. This isn't quite right, because what we actually need to return is the RETURNING column set. If you only check "RETURNING *" then you might not notice the difference, but with anything else it's obviously wrong. I propose the attached modification instead. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-empty-plan-with-RETURNING-2.patch"; charset="us-ascii" Content-ID: <13498.1550787409.2@sss.pgh.pa.us> Content-Description: fix-empty-plan-with-RETURNING-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/= plan/planner.c index 5579dfa..93d9448 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1585,10 +1585,19 @@ inheritance_planner(PlannerInfo *root) = /* * If we managed to exclude every child rel, return a dummy plan; it - * doesn't even need a ModifyTable node. + * doesn't even need a ModifyTable node. But, if the query has RETURNIN= G, + * we need to cons up a suitable pathtarget, else the finished plan will + * appear to return the wrong column set. */ if (subpaths =3D=3D NIL) { + if (parse->returningList) + { + final_rel->reltarget =3D create_pathtarget(root, + parse->returningList); + /* hack to keep createplan.c from breaking things: */ + root->processed_tlist =3D parse->returningList; + } set_dummy_rel_pathlist(final_rel); return; } diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expe= cted/inherit.out index f259d07..9d610b8 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -539,6 +539,41 @@ CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inhe= rits (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 =3D a + 1 where false; + QUERY PLAN = +-------------------------- + Result + One-Time Filter: false +(2 rows) + +update some_tab set a =3D a + 1 where false; +explain (verbose, costs off) +update some_tab set a =3D a + 1 where false returning b, a; + QUERY PLAN = +---------------------------------- + Result + Output: some_tab.b, some_tab.a + One-Time Filter: false +(3 rows) + +update some_tab set a =3D 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/sql/inherit.sql b/src/test/regress/sql/inher= it.sql index 425052c..5480fe7 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.table= oid =3D 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 =3D a + 1 where false; +update some_tab set a =3D a + 1 where false; +explain (verbose, costs off) +update some_tab set a =3D a + 1 where false returning b, a; +update some_tab set a =3D 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); ------- =_aaaaaaaaaa0--