Overview

PostgreSQL exposes its internals through statistics views (pg_stat_*), catalog views (pg_locks, pg_settings) and extensions such as pg_stat_statements. A short list of queries answers most “what is the database doing?” questions during an incident.

Core concepts

QuestionSource
Who is connected, what are they running, for how long?pg_stat_activity (state, wait_event_type, wait_event, xact_start, query_start)
Who is blocking whom?pg_blocking_pids(pid), pg_locks
Which statements consume the most time?pg_stat_statements
Is autovacuum keeping up?pg_stat_user_tables (n_dead_tup, last_autovacuum)
Are indexes used?pg_stat_user_indexes (idx_scan)
Cache effectiveness and I/Opg_statio_*, pg_stat_io (PostgreSQL 16+), pg_stat_database
Checkpoints and WALpg_stat_checkpointer (17), pg_stat_wal
Replication healthpg_stat_replication, pg_replication_slots

pg_stat_statements must be listed in shared_preload_libraries (restart), then CREATE EXTENSION pg_stat_statements;. It aggregates by normalised query text.

Worked example

Find sessions that block others:

SELECT a.pid, a.usename, a.state, now() - a.xact_start AS xact_age,
       pg_blocking_pids(a.pid) AS blocked_by, left(a.query, 80) AS query
FROM pg_stat_activity a
WHERE cardinality(pg_blocking_pids(a.pid)) > 0;

Top statements by total time:

SELECT left(query, 60) AS query, calls,
       round(total_exec_time::numeric, 1) AS total_ms,
       round(mean_exec_time::numeric, 2)  AS mean_ms, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC LIMIT 10;

Oldest open transactions (the usual cause of bloat and blocked DDL):

SELECT pid, state, now() - xact_start AS age, left(query, 60)
FROM pg_stat_activity
WHERE xact_start IS NOT NULL ORDER BY xact_start LIMIT 5;

Act on a problem session: SELECT pg_cancel_backend(pid); cancels the current query; pg_terminate_backend(pid) closes the connection.

Pitfalls and production notes

  • Statistics are cumulative since the last reset or restart; compare two samples over time or export them to Prometheus (postgres_exporter) and graph rates.
  • Alert on: replication lag, connections near max_connections, oldest transaction age, age(datfrozenxid), disk space (data and WAL), dead tuples growth, archive failures, and checkpoint request rate.
  • Turn on log_min_duration_statement, log_lock_waits, log_checkpoints and log_autovacuum_min_duration for first-class evidence after the fact; auto_explain captures plans of slow queries (pg-explain-analyze).
  • pg_stat_activity shows full query text only to superusers or roles with pg_read_all_stats/pg_monitor.
  • Reset statistics deliberately (pg_stat_statements_reset()) after tuning to measure the effect.

Practice questions

  1. A query is slow only at 09:00 each day. What would you capture to explain it?
  2. How do you distinguish “waiting on a lock” from “waiting on I/O” in pg_stat_activity?
  3. Which three metrics would you page on first?

References

pg-vacuum-bloat · pg-replication-failover · pg-explain-analyze · pg-tuning · locking-and-mvcc