From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: pgsql-bugs@lists.postgresql.org
To: px812@mailbox.org
Subject: Re: BUG #15906: Insert ops within a branch of "RETURN QUERY WITH" are not seen by a subsequent SELECT
Date: Sat, 13 Jul 2019 12:07:44 +0100
Message-ID: <87y312nqrf.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <15906-d64845e69b79f9dd@postgresql.org>
References: <15906-d64845e69b79f9dd@postgresql.org>
>>>>> "PG" == PG Bug reporting form <noreply@postgresql.org> 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)
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: andrew@tao11.riddles.org.uk, 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: <87y312nqrf.fsf@news-spur.riddles.org.uk>
References: <15906-d64845e69b79f9dd@postgresql.org> <87y312nqrf.fsf@news-spur.riddles.org.uk>
* 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