Sub topic 10
Seeing What Changed with `RETURNING`
This chapter assumes you know basic SELECT, WHERE, and JOIN syntax, single-table UPDATE and DELETE, table aliases, and PostgreSQL transactions. It also assumes the customers and orders tables from the earlier examples.
A bulk change can affect many rows. RETURNING makes PostgreSQL send the affected rows back to the client as a result set. You can inspect those rows, save them in an application, or pass them to another SQL statement.
Return updated rows
Place RETURNING after the WHERE clause of an UPDATE. The returned values show the row after the update.
UPDATE orders AS o
SET status = 'cancelled'
FROM customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
AND o.status = 'open'
RETURNING o.id, o.customer_id, o.status;The join selects orders belonging to closed customers. The SET clause changes only open orders. The result contains the identifiers and new status of every order that changed.
You can return expressions as well:
UPDATE orders
SET total = round(total * 1.10, 2)
WHERE status = 'open'
RETURNING id, total AS new_total;If no row matches, PostgreSQL returns an empty result set. It does not report an error.
Return deleted rows
DELETE can also return values. PostgreSQL evaluates the expressions against the rows being deleted, so RETURNING is useful for audit logs and archives.
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
RETURNING o.id, o.customer_id, o.total;The returned rows are gone from orders, but their data is available to the client or to a data-modifying common table expression.
| Statement | Values returned |
|---|---|
| `UPDATE ... RETURNING` | Values from rows after the update |
| `DELETE ... RETURNING` | Values from rows immediately before deletion |
Inspect changes inside a transaction
A transaction groups statements into one all-or-nothing operation. You can run a modification, inspect its returned rows, and roll it back while testing.
BEGIN;
UPDATE orders AS o
SET status = 'cancelled'
FROM customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
AND o.status = 'open'
RETURNING o.id, o.customer_id, o.status;
-- After checking the returned rows:
ROLLBACK;
-- Use COMMIT instead when the result is correct.ROLLBACK undoes the update. COMMIT makes it permanent. A transaction does not replace a correct join condition, so preview the matching rows with a SELECT first.
Pass returned rows to another statement
A data-modifying common table expression, or CTE, can expose returned rows to an outer query. This example counts deleted orders without requiring the client to process each row.
WITH removed AS (
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
RETURNING o.id
)
SELECT count(*) AS deleted_orders
FROM removed;The DELETE runs once. The outer SELECT reads the rows exposed by RETURNING. You can also insert the returned columns into an archive table.
Exercise
How would you return the ID and new status for every open order belonging to a closed customer after cancelling it?
Show answer
Add RETURNING o.id, o.status after the WHERE clause of the UPDATE ... FROM statement.