Обсуждение: Data visibility for returning statement
The following documentation comment has been logged on the website: Page: https://www.postgresql.org/docs/17/dml-returning.html Description: Today I found a pretty special use-case for the "RETURNING" functionality, which I cannot find documentation for? If you have a statement as follows: UPDATE persons SET name = 'Bob' WHERE id = 4 RETURNING (SELECT name FROM persons WHERE id = 4) The returning data will be whatever the value was before the row was modified. This differs from if I were to "RETURNING name". I found this to be interesting and could possibly warrant some kind of explanation in the documentation? Best regards Robin
On Sat, Apr 26, 2025 at 5:50 AM PG Doc comments form <noreply@postgresql.org> wrote:
The following documentation comment has been logged on the website:
Page: https://www.postgresql.org/docs/17/dml-returning.html
Description:
Today I found a pretty special use-case for the "RETURNING" functionality,
which I cannot find documentation for?
If you have a statement as follows:
UPDATE persons SET name = 'Bob' WHERE id = 4
RETURNING (SELECT name FROM persons WHERE id = 4)
The returning data will be whatever the value was before the row was
modified. This differs from if I were to "RETURNING name". I found this to
be interesting and could possibly warrant some kind of explanation in the
documentation?
I would not want to encourage that form of query. The novelty is more problematic than the brevity. Plus, repetition.
with new_p as ( update persons set name = 'Bob' returning name where id = 4)
select
old_p.id as id,
old_p.name as old_name,
new_p.name as new_name
from persons as old_p
And now that we actually allow references to "new" and "old" in v18 that form is also obsolete and you can do this directly.
David J.