Sub topic 9
Using EXPLAIN ANALYZE on INSERT, UPDATE and DELETE safely
Assume you can write INSERT, UPDATE, and DELETE statements and understand transactions. This chapter uses PostgreSQL. The key fact is that EXPLAIN ANALYZE executes the statement. It does not only inspect it.
Why data changes need extra care
Plain EXPLAIN estimates a plan without running the statement:
EXPLAIN
UPDATE orders
SET status = 'archived'
WHERE created_at < DATE '2020-01-01';EXPLAIN ANALYZE runs the update, reports actual timings and row counts, and leaves the changed rows changed if you run it normally:
EXPLAIN (ANALYZE, BUFFERS)
UPDATE orders
SET status = 'archived'
WHERE created_at < DATE '2020-01-01';For testing, put the statement inside a transaction and roll the transaction back afterward:
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
UPDATE orders
SET status = 'archived'
WHERE created_at < DATE '2020-01-01';
ROLLBACK;The update executes, so the plan contains real information. ROLLBACK then removes the update's row changes.
The safe transaction pattern
Use this pattern when investigating a data-modifying statement:
- Start a transaction with
BEGIN. - Run
EXPLAIN (ANALYZE, BUFFERS)and the exact statement. - Inspect the output.
- Run
ROLLBACK, even if the statement appears to affect no rows.
For an insert:
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
INSERT INTO audit_events (order_id, event_type)
SELECT id, 'reviewed'
FROM orders
WHERE status = 'pending';
ROLLBACK;For a delete:
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
DELETE FROM orders
WHERE created_at < DATE '2020-01-01';
ROLLBACK;The plan shows the work performed by the statement. Look for scan nodes that find candidate rows, filter nodes that remove rows, and the final modification node. Compare estimated rows with actual rows. A large difference can explain a slow update or delete.
What rollback does not undo
A rollback usually restores table data, but it does not make every effect disappear. Sequence values consumed by identity or serial columns are not rolled back, so a tested insert can leave a gap in an ID sequence. Triggers can call functions with effects outside the database, such as sending a notification or writing to a file. Those effects may also remain.
Locks held by the transaction are released when it rolls back. A long-running analyzed statement can still block other sessions while it runs, so avoid testing large writes during busy periods.
A practical checklist
- Confirm the connection points to a safe database.
- Run the statement with a selective condition first when possible.
- Start an explicit transaction.
- Capture the plan and buffer information.
- Check the actual row count.
- Roll back and verify the transaction ended.
- Consider triggers, sequences, notifications, and other side effects.
Exercise
What is the essential difference between EXPLAIN and EXPLAIN ANALYZE for an UPDATE?
Show answer
EXPLAIN estimates the plan without running the update. EXPLAIN ANALYZE executes the update and reports actual execution details, so it must be rolled back when the changes should not persist.