Thread: https://www.postgresql.org/docs/current/sql-insert.html

https://www.postgresql.org/docs/current/sql-insert.html

From
PG Doc comments form
Date:
The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/17/sql-insert.html
Description:

Hi,
the example found in the doc above:

WITH upd AS (
  UPDATE employees SET sales_count = sales_count + 1 WHERE id =
    (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation')
    RETURNING *
)
INSERT INTO employees_log SELECT *, current_timestamp FROM upd;

might result into UPDATE 0 / INSERT 0 because the documentation regarding
the CTE states: "The sub-statements in WITH are executed concurrently with
each other and with the main query".
The subquery SELECT sales_person FROM accounts WHERE name = 'Acme
Corporation' might not be waited on by the UPDATE query resulting into an
UPDATE 0.
Is my suspicion valid?

Re: https://www.postgresql.org/docs/current/sql-insert.html

From
"David G. Johnston"
Date:
On Saturday, November 2, 2024, 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/sql-insert.html
Description:

Is my suspicion valid?

The presence of the returning clause and subsequent usage of the CTE name in the select/insert ensures that the update executes first.

David J.