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 1hmFtg-0005mg-TE for pgsql-bugs@arkaria.postgresql.org; Sat, 13 Jul 2019 11:08:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hmFsg-0003vR-5a for pgsql-bugs@arkaria.postgresql.org; Sat, 13 Jul 2019 11:07:54 +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 1hmFsf-0003vK-UQ for pgsql-bugs@lists.postgresql.org; Sat, 13 Jul 2019 11:07:53 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hmFsb-00011n-Gy for pgsql-bugs@lists.postgresql.org; Sat, 13 Jul 2019 11:07:53 +0000 Received: from [192.168.127.1] (port=42086 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1hmFsY-0004CQ-6K; Sat, 13 Jul 2019 11:07:46 +0000 Received: from [127.0.0.1] (port=27947 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1hmFsX-0001mC-DV; Sat, 13 Jul 2019 11:07:45 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, px812@mailbox.org Subject: Re: BUG #15906: Insert ops within a branch of "RETURN QUERY WITH" are not seen by a subsequent SELECT In-Reply-To: <15906-d64845e69b79f9dd@postgresql.org> (PG Bug reporting form's message of "Sat, 13 Jul 2019 08:37:47 +0000") Message-ID: <87y312nqrf.fsf@news-spur.riddles.org.uk> References: <15906-d64845e69b79f9dd@postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sat, 13 Jul 2019 12:07:44 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form writes: PG> CREATE OR REPLACE FUNCTION itest1( aName Text) RETURNS SETOF xtmp AS $$ PG> BEGIN PG> RETURN QUERY PG> WITH ix AS ( PG> INSERT INTO xtmp(name) VALUES(aName) RETURNING * PG> ) SELECT * FROM xtmp WHERE name = (SELECT name from ix); PG> -- Same result as with PG> -- ) SELECT * FROM xtmp; PG> -- ) SELECT * FROM xtmp WHERE name = aName; PG> END PG> $$ LANGUAGE plpgsql; I think the point that you're missing here is that a SELECT (or indeed any other statement) sees only the effects of _previously started_ modification statements, while the INSERT in the WITH is part of the _same_ statement as the final SELECT and therefore its effects are not visible. This is explicitly documented at: https://www.postgresql.org/docs/11/queries-with.html#QUERIES-WITH-MODIFYING If you want to return the inserted value then you must do so by querying the CTE ("ix" in this example), using UNION ALL if necessary. -- Andrew (irc:RhodiumToad)