RETURNING does not explain evaluation context for subqueries - Mailing list pgsql-docs

From PG Doc comments form
Subject RETURNING does not explain evaluation context for subqueries
Date
Msg-id 158092036515.1089.2611167368836790265@wrigleys.postgresql.org
Whole thread Raw
List pgsql-docs
The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/11/dml-returning.html
Description:

In the docs explaining RETURNING
https://www.postgresql.org/docs/11/dml-returning.html there is no mention of
the fact that a nested sub-select in the RETURNING statement executes on the
table as if the INSERT/UPDATE had not happened. 

I suppose maybe this might be obvious if you understand how SQL works but I
think it is nuanced enough that it is worth explaining here as it provides
some useful features for UPSERT queries. Example:

```sql
create table foo (x int primary key, y int);
--=> CREATE TABLE
insert into foo (x, y) values (1, 1);
--=> INSERT 0 1
update foo set y = 2 where x = 1 returning (select y from foo where x = 1)
as old_y;
/* =>
 *  old_y 
 * -------
 *      1
 * (1 row)
 *
 * UPDATE 1
 */
select * from foo;
/* =>
 *  x | y 
 * ---+---
 *  1 | 2
 * (1 row)
 */
```

pgsql-docs by date:

Previous
From: PG Doc comments form
Date:
Subject: Wrong insert before trigger examples
Next
From: Stephen Frost
Date:
Subject: Re: Users/Roles do not align.