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 1gx0Om-0005o3-Lf for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Feb 2019 02:17:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gx0Ok-00065F-IN for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Feb 2019 02:17:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gx0Ok-00061v-8T for pgsql-bugs@lists.postgresql.org; Fri, 22 Feb 2019 02:17:10 +0000 Received: from tama50.ecl.ntt.co.jp ([129.60.39.147]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gx0Ob-0005gU-DZ for pgsql-bugs@postgresql.org; Fri, 22 Feb 2019 02:17:07 +0000 Received: from vc1.ecl.ntt.co.jp (vc1.ecl.ntt.co.jp [129.60.86.153]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id x1M2GvVD012904; Fri, 22 Feb 2019 11:16:57 +0900 Received: from vc1.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id A9BC7EA77C6; Fri, 22 Feb 2019 11:16:57 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 9D214EA75E1; Fri, 22 Feb 2019 11:16:57 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id 9311D400343; Fri, 22 Feb 2019 11:16:57 +0900 (JST) 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 References: <5da6f0f0-1364-1876-6978-907678f89a3e@phystech.edu> <907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp> <13543.1550787490@sss.pgh.pa.us> From: Amit Langote Message-ID: <6a08a294-6a16-e384-db25-8f4d3cc026d1@lab.ntt.co.jp> Date: Fri, 22 Feb 2019 11:16:40 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: <13543.1550787490@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-CC-Mail-RelayStamp: 1 To: Tom Lane Cc: Petr Fedorov , pgsql-bugs@postgresql.org X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019/02/22 7:18, Tom Lane wrote: > 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. Ah, indeed. >> 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. Looks good. I know this code may not be with us forever, but I wonder why the plan shape looks different for an empty update on a regular table vs inheritance tree. For regular table, it's ModifyTable on top of a dummy Result node, whereas it's just dummy Result node for the latter. If the plan shape for the two cases had matched, we wouldn't have this bug at all or we'd have it for both cases (in slightly different form for the regular table case). To check I patched grouping_planner() to not add a ModifyTable on top of a dummy result path for a regular table and the resulting target list is not quite right (what you get with my patch upthread): create table foo (a int); update foo set a = a where false returning a+1 as b; a ─── (0 rows) explain verbose update foo set a = a where false returning a+1 as b; QUERY PLAN ─────────────────────────────────────────── Result (cost=0.00..0.00 rows=0 width=10) Output: a, ctid One-Time Filter: false (3 rows) 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. That means we have a bug (?) today that statement triggers of inheritance parent tables don't fire when it's an empty update/delete. create table parent (a int, b int); create table child () inherits (parent); create or replace function before_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 parent execute function before_stmt_notice(); -- trigger doesn't fire update parent set a = a where false returning a+1 as b; ── (0 rows) It does fire for an empty update on a regular table (with HEAD I mean) create trigger before_stmt_trigger before update on foo execute function before_stmt_notice(); update foo set a = a where false returning a+1 as b; NOTICE: updating foo b ─── (0 rows) Thanks, Amit