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 1gtlhE-0008Td-Fs for pgsql-bugs@arkaria.postgresql.org; Wed, 13 Feb 2019 03:58:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gtlhC-0000Wg-QX for pgsql-bugs@arkaria.postgresql.org; Wed, 13 Feb 2019 03:58:50 +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 1gtlhC-0000Vw-Ip for pgsql-bugs@lists.postgresql.org; Wed, 13 Feb 2019 03:58:50 +0000 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gtlh9-0006d6-Q8 for pgsql-bugs@postgresql.org; Wed, 13 Feb 2019 03:58:50 +0000 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id x1D3wifT003539; Wed, 13 Feb 2019 12:58:44 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id C4B4A63884A; Wed, 13 Feb 2019 12:58:44 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id AFA3B638500; Wed, 13 Feb 2019 12:58:44 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id AB6034006B5; Wed, 13 Feb 2019 12:58:44 +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 From: Amit Langote References: <5da6f0f0-1364-1876-6978-907678f89a3e@phystech.edu> <907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp> Message-ID: <42050d24-e037-9e6b-79d5-eaf40a28dd3b@lab.ntt.co.jp> Date: Wed, 13 Feb 2019 12:58:35 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: <907bb879-872e-8395-80af-1b98efc10b06@lab.ntt.co.jp> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-CC-Mail-RelayStamp: 1 To: 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/06 16:35, Amit Langote wrote: > Hi, > > On 2019/02/01 23:32, Petr Fedorov wrote: >> Hello, >> >> The following code snippet demonstrates the problem: the first select >> passes and the second [select * from testf(FALSE)] fails. I would expect >> that select * from testf(...); works without errors in both cases. >> >> begin; >> >> create table test (id integer, data char(1)) partition by list (id) >> tablespace pg_default; >> create table test_1 partition of test for values in (1) partition by >> list (data); >> create table test_1_a partition of test_1 for values in ('a'); >> create function testf(p boolean) returns setof test language 'plpgsql' >> as $body$ begin return query update test set id=id where p returning *; >> end; $body$; >> insert into test (id, data) values (1, 'a'); >> select * from testf(TRUE); >> select * from testf(FALSE); >> >> rollback;    >> >> The result: >> >> ERROR: structure of query does not match function result type >> >> SQL state: 42804 >> >> Detail: Number of returned columns (0) does not match expected column >> count (2). >> >> Context: PL/pgSQL function testf(boolean) line 1 at RETURN QUERY > > Thanks for the report. There indeed appears to be a bug here. > > The problem seems to be with how planner handles an empty plan (due to > constant-FALSE qual) when the target table is an inheritance tree. OP's > example contains a partitioned table, but I could reproduce it with > regular inheritance: > > create table parent (id int); > create table child () inherits (parent); > create or replace function testf(p boolean) returns setof parent > language 'plpgsql' as $body$ > begin > return query update parent set id = id where p returning *; > end; > $body$; > > select * from testf(true); > id > ──── > (0 rows) > > select * from testf(false); > ERROR: structure of query does not match function result type > DETAIL: Number of returned columns (0) does not match expected column > count (1). > CONTEXT: PL/pgSQL function testf(boolean) line 1 at RETURN QUERY > > No problem when there is no inheritance: > > drop function testf; > create table foo (like parent); > create or replace function testf(p boolean) returns setof foo > language 'plpgsql' as $body$ > begin > return query update foo set id = id where p returning *; > end; > $body$; > select * from testf(false); > id > ──── > (0 rows) > > > Mismatch between the query result type and the function result type occurs > in the inheritance case, because the targetlist of the plan for the UPDATE > query in testf's body is empty, whereas the function execution code > (pl_exec.c) expects it match the function's result type (set of parent). > It's empty because inheritance_planner sets an empty Result path when it > finds that all the children are excluded, but hasn't generated enough > state in the path's RelOptInfo and PlannerInfo such that the correct > targetlist could be set in the empty Result plan that's eventually created. > > Attached patch seems to fix it. It also adds a test in inherit.sql. > > Thoughts? Will add this to next CF. Thanks, Amit