PostgreSQL in Practice

PostgreSQL Locks, Monitoring, and Production Diagnostics

PostgreSQL incident diagnosis correlates sessions, waits, locks, query fingerprints, WAL/replication, checkpoint and vacuum signals with configuration provenance. The runbook preserves evidence, contains harm with the least destructive action, and verifies recovery rather than stopping at symptom disappearance.
  • A wait event names the current wait, not the root cause.
  • pg_locks shows held and awaited lockable objects.
  • Statistics views are cumulative and version-sensitive.
  • pg_stat_statements is an optional, normalized workload view.
  • Settings need provenance.
  • Escalation should preserve correctness and evidence.
Snapshot activity and blocking edges
SELECT clock_timestamp() AS captured_at, pid, usename, application_name,
       state, wait_event_type, wait_event, xact_start, query_start,
       backend_xmin, pg_blocking_pids(pid) AS blockers, query_id, left(query, 500) AS query
FROM pg_stat_activity WHERE pid <> pg_backend_pid();

SELECT pid, locktype, relation::regclass, transactionid, mode, granted, waitstart
FROM pg_locks WHERE NOT granted ORDER BY waitstart;

SELECT name, setting, unit, source, sourcefile, pending_restart
FROM pg_settings WHERE name IN ('max_connections','shared_buffers','work_mem','synchronous_commit');
Evidence-preserving production runbook
Signal bundle
QuestionPostgreSQL evidenceInterpret with
Who is blocked?pg_stat_activity, pg_locks, pg_blocking_pidstransaction age, application owner, DDL/job timeline
Which workload changed?pg_stat_statements if installed; logs/query_idstats reset, deploy, binds unavailable
Is storage/checkpoint saturated?pg_stat_io, pg_stat_bgwriter, pg_stat_checkpointer, pg_stat_walOS/device latency and sample deltas
Is maintenance pinned?backend_xmin, progress views, pg_stat_user_tablesslots, prepared xacts, standby feedback
Is recovery path healthy?pg_stat_replication, pg_replication_slots, pg_stat_archiverarchive continuity and business freshness
Sources
  • PostgreSQL 14 Internalsbook · 2023Ch. 12 — Relation-Level Locks; Ch. 13 — Row-Level Locks; Ch. 14 — Miscellaneous Locks; Ch. 17 — Statistics
  • PostgreSQL 18 Documentationdocumentation · 202513.3 — Explicit Locking; Ch. 27 — Monitoring Database Activity; pg_stat_statements
  • Database Reliability Engineeringbook · 2017Ch. 4 — Operational Visibility; Ch. 5 — Infrastructure Engineering