PostgreSQL On-Call Playbook for P1/P0 Incidents
A calm first-15-minutes playbook for PostgreSQL incidents: triage queries for connections, locks, replication, disk and wraparound, plus communication and post-mortems.
As primary on-call DBA for P1 and P0 database incidents, I wrote a lot of post-mortems (12 of them led to 8 systemic fixes). The pattern that helped most was a calm, repeatable first 15 minutes. This is that playbook.
1. Ground rules
- Capture evidence before you change anything. A snapshot of
pg_stat_activityis gone once you restart. - Do not restart first. Restarts clear caches, drop connections and hide the cause.
- One incident commander talks to stakeholders; you work the problem.
- Write a timeline as you go. Future you will need it.
2. Triage in five questions
Is the database up and accepting connections? Then: what are sessions doing?
SELECT state, wait_event_type, count(*)
FROM pg_stat_activity
WHERE datname IS NOT NULL
GROUP BY 1, 2
ORDER BY 3 DESC;
Lots of active with Lock waits means blocking. Many idle in transaction means an application leak. Connection count near max_connections means saturation.
3. Blocking and long queries
SELECT pid, pg_blocking_pids(pid) AS blocked_by,
now() - query_start AS running,
left(query, 80) AS query
FROM pg_stat_activity
WHERE cardinality(pg_blocking_pids(pid)) > 0;
Find the root blocker (the pid that is blocking but not itself blocked). Try pg_cancel_backend(pid) first; use pg_terminate_backend(pid) if cancel does not work. Agree with the commander before killing business-critical work.
4. Disk and WAL growth
A full disk takes PostgreSQL down. The usual culprit is WAL retained by an inactive or lagging replication slot:
SELECT slot_name, active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained
FROM pg_replication_slots;
If a slot is inactive and its consumer is gone for good, dropping it frees WAL. Confirm with the owner first, because dropping a slot breaks that subscriber.
5. Replication and failover
SELECT client_addr, state, sync_state, replay_lag
FROM pg_stat_replication;
If the primary is truly lost, follow your failover runbook (Patroni, RDS/Aurora failover, etc.) rather than improvising. Verify the new primary accepts writes and that applications reconnected.
6. The quiet killer: transaction ID age
SELECT datname, age(datfrozenxid) AS xid_age
FROM pg_database
ORDER BY 2 DESC;
Ages creeping toward the high hundreds of millions deserve attention long before the hard limit near 2 billion. Fixing it means getting autovacuum to complete freezing: remove long transactions and stale replication slots, and consider a manual VACUUM (FREEZE) on the worst tables.
7. Quick scenario cheat sheet
- Connections exhausted: look for
idle in transactionsessions and app leaks; check whether a pooler is in front; raise limits only as a last resort. - CPU pegged: rank statements by total time in
pg_stat_statements; look for a new query, a missing index or a plan change after a statistics shift. - Replica lag climbing: check for heavy writes on the primary, long queries on the replica that conflict with replay, and network or I/O limits.
- Out-of-memory kills: remember
work_memapplies per sort/hash node per connection, so large values multiplied by many connections add up. - Sudden slowdown after a deploy: compare the plans of the top queries before and after; roll back the change if it correlates.
8. Communicate on a schedule
- First update within minutes: impact, what you know, next update time.
- Then every 15-30 minutes, even if the update is "still investigating".
- Close with a clear "resolved" message and the post-mortem date.
9. Blameless post-mortem template
- Summary and impact (who, how long, how bad).
- Timeline with timestamps.
- Root cause and contributing factors.
- What went well / what did not.
- Action items with owners and dates, focused on systemic fixes: alerts, automation, guardrails.
The measure of a good post-mortem is whether the same incident can still happen next quarter.
Enjoyed this?
I write about PostgreSQL, AWS and database reliability. Have a question or a similar problem to solve?