PostgreSQL Production Troubleshooting
The database failures that take an app down: connection exhaustion, locks, sequence/constraint errors, and recovery.
- Who it’s for
- Engineers who own or operate a Postgres database in production.
- Prerequisites
- Basic SQL and psql familiarity.
Skills you’ll build
- ✓Diagnose connection/pool exhaustion
- ✓Resolve lock and constraint errors
- ✓Recover from common failure modes
-
Module 1
Work the common Postgres errors
Systematically resolve the highest-frequency Postgres errors.
Exercise
Inserts fail with "duplicate key value violates unique constraint" after a data import. Determine whether the sequence is behind the table’s max id, and how to reset it safely.
Open in Workspace → -
Module 2
Connections and locks
Diagnose "too many connections" and lock contention under load.
Diagnostic commands run in order — each one narrows the fault
- Check connection usage against the limit
psql -c "SELECT count(*), (SELECT setting::int FROM pg_settings WHERE name='max_connections') AS max FROM pg_stat_activity;"Close to max means new connections will be refused — usually a pooler problem, not a database one.
- Find blocking locks
psql -c "SELECT blocked.pid AS blocked_pid, blocking.pid AS blocking_pid, blocked.query AS blocked_query FROM pg_stat_activity blocked JOIN pg_stat_activity blocking ON blocking.pid = ANY(pg_blocking_pids(blocked.pid)) WHERE cardinality(pg_blocking_pids(blocked.pid)) > 0;"Gives you the blocking PID directly. Investigate the blocker before killing anything.
- Find long-running and idle-in-transaction sessions
psql -c "SELECT pid, state, now()-xact_start AS xact_age, left(query,60) FROM pg_stat_activity WHERE state <> 'idle' ORDER BY xact_age DESC NULLS LAST LIMIT 20;""idle in transaction" holds locks and blocks vacuum indefinitely — it is the single most common Postgres production problem.
-
-
Module 3
Run a database incident
Triage a Postgres outage in the workspace and export the summary.
Exercise
The app is throwing database errors under load. Triage connections, locks, and slow queries; record findings and root cause.
Open in Workspace →
Mission complete 🎉
You’ve worked every module of PostgreSQL Production Troubleshooting.
Next: OpenStack Production Troubleshooting →