Sub topic 3
Seeing what changed with RETURNING
This chapter assumes you know single-table UPDATE and DELETE, joins, table aliases, and how to run statements in psql. The examples use the schema from the earlier chapters: products, orders, customers, and price_updates.
Seeing what changed with RETURNING
RETURNING is a clause that makes UPDATE and DELETE send back the rows they touched. Without it, you only learn how many rows changed.
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id
RETURNING p.id, p.price;If a product has several rows in price_updates, UPDATE ... FROM picks one of them arbitrarily. Use the DISTINCT ON subquery from the previous chapter to make the choice deterministic.
Previewing and running bulk changes safely
Wrap a bulk change in a transaction. Read the returned rows, then either keep the change or undo it.
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;
-- Check the rows returned, then run ROLLBACK; or COMMIT;Chaining changes with data-modifying CTEs
A common table expression (CTE) is a named subquery in a WITH clause. PostgreSQL lets a CTE contain a DELETE or UPDATE, and the outer query can read its RETURNING rows.
WITH removed AS (
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
RETURNING o.*
)
SELECT count(*) AS deleted_orders FROM removed;Self-joins: removing duplicate rows
A self-join joins a table to itself. This statement keeps the row with the lowest id for each email address.
DELETE FROM contacts AS a
USING contacts AS b
WHERE a.email = b.email
AND a.id > b.id;Indexes, locks, and batching
Columns used in the join condition, such as orders.customer_id, should have indexes. Without them, PostgreSQL may scan both tables for each match.
Matched rows are locked until the transaction ends. A large statement can block other writers for a long time, so delete in batches:
DELETE FROM orders
WHERE id IN (
SELECT o.id
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
WHERE c.status = 'closed'
LIMIT 1000
);Run the statement repeatedly until it reports DELETE 0.
Translating from MySQL and SQL Server syntax
PostgreSQL has no UPDATE ... JOIN or DELETE ... JOIN. Move the joined table into FROM or USING, and move the ON condition into WHERE.
| Source | Statement | PostgreSQL equivalent |
|---|---|---|
| MySQL | `UPDATE orders o JOIN customers c ON o.customer_id = c.id SET o.status = 'x' WHERE c.status = 'closed'` | `UPDATE orders AS o SET status = 'x' FROM customers AS c WHERE o.customer_id = c.id AND c.status = 'closed';` |
| SQL Server | `DELETE o FROM orders o JOIN customers c ON o.customer_id = c.id WHERE c.status = 'closed'` | `DELETE FROM orders AS o USING customers AS c WHERE o.customer_id = c.id AND c.status = 'closed';` |
Exercise
Write a statement that cancels open orders for closed customers and returns the id of each changed order. Wrap it in a transaction so you can roll it back.
Show answer
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; ROLLBACK;