Lesson 7 of 9 · 24m
Monitoring PostgreSQL: Signals That Predict Incidents
The PostgreSQL statistics that matter — connections, transactions, replication, vacuum, checkpoints, locks, and query latency — and how to turn them into alerts instead of dashboard wallpaper.
Most database dashboards fail the same way: forty graphs, no decision. Monitoring is useful only when each signal has a threshold, an owner, and a next action. PostgreSQL exposes excellent internal statistics, but many are cumulative counters or sampled activity views; know what is exact, estimated, and reset.
Start with saturation and correctness
The first alerts answer four questions: Is PostgreSQL accepting connections? Are commits durable and replication healthy? Are transactions backing up? Is vacuum keeping up?
SELECT count(*) FILTER (WHERE state = 'active') AS active, count(*) FILTER (WHERE state = 'idle in transaction') AS idle_in_xact, count(*) AS total
FROM pg_stat_activity;
SELECT wait_event_type, wait_event, count(*)
FROM pg_stat_activity WHERE state = 'active' GROUP BY 1, 2 ORDER BY 3 DESC;Total connections near max_connections is saturation. Growing idle in transaction is an application bug in progress; the second query shows whether active sessions are waiting on locks, I/O, or clients.
pg_stat_statements is the query ledger
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = allSELECT query, calls, total_exec_time, mean_exec_time, rows
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;A 20 ms query executed 500,000 times can matter more than a two-second report run twice a day. Total time, call count, row volume, and application context belong together. Remember that this data is in-memory and can reset.
Vacuum and transaction age are leading indicators
SELECT relname, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 20;
SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC;Do not alert on a universal dead-tuple percentage without size context. Ten percent of a thousand-row table is noise; ten percent of a billion-row table is a scan and storage problem.
Increasing transaction ID age means anti-wraparound work is approaching whether you planned for it or not.
Alert only on actionable thresholds
CRITICAL: archive failing 5m; inactive slot retaining >20GB WAL; connections >90% of max_connections
WARNING: oldest idle-in-transaction >10m; replay lag on reporting replica >5m; checkpoint write-time risingEach alert should link the query, dashboard, and first mitigation. Otherwise you built a pager that says "something is wrong somewhere."
Field notes
pg_stat_activityis a snapshot and misses short-lived connections.- Compare statistics timestamps before interpreting cumulative counters.
- CPU can be idle while the database is blocked on locks.
- Query text may contain sensitive literals; configure scrubbing deliberately.
- Keep incident triage and capacity planning dashboards separate.
Was this lesson helpful?