The two most dangerous statements
UPDATE and DELETE will affect every row in the table if you forget the WHERE clause. This has cost real teams real money. The discipline is simple and worth memorising.
The four-step safe pattern
- Open a transaction:
BEGIN; - Write a SELECT with the same WHERE clause; verify the row count and sample.
- Run the UPDATE/DELETE.
- If everything looks right,
COMMIT;— otherwiseROLLBACK;.
RETURNING shows you exactly what changed
Add RETURNING * (or specific columns) to UPDATE/DELETE to see the affected rows in the same round trip. This is invaluable for audit logs and "did the right thing happen?" sanity checks.