Video in progress

Which metrics and alerts do you consider mandatory for PostgreSQL?

Whether the primary is alive, replica lag, connections, the oldest transaction and XID age, lock waits, disk/WAL growth, replication slots, and latency from pg_stat_statements. Alerts point at symptoms, and the dashboard points at causes.

📖 Answer

I split them into two groups.

Alerts that wake someone up (real danger):

  • The primary isn't responding, or a replica has disconnected.
  • Disk at 85%+, or the WAL folder growing fast.
  • A replication slot is holding WAL (> N GB) or is inactive.
  • age(datfrozenxid) > 800 million.
  • Connections above 80% of max_connections.
  • Replica lag > the SLA (say, 30 s).

Warnings handled during working hours:

  • The oldest open transaction > 5 minutes (now() - min(xact_start)).
  • The number of sessions with wait_event_type = 'Lock'.
  • Growth in deadlocks (pg_stat_database.deadlocks).
  • Lots of checkpoints caused by max_wal_size (checkpoints_req).
  • n_dead_tup and the last autovacuum time on known tables.
  • Temp files (temp_bytes): queries that work_mem isn't enough for.

On the dashboard too: TPS, the time of the top queries from pg_stat_statements, cache hit ratio (but I don't alert on it, since it doesn't say much by itself), IO latency.

The application side is needed too: HikariCP metrics (hikaricp_connections_pending, acquire time) show a problem before the database does. If waiting in the pool grows while load on the database is low, the problem is in the application (long transactions), not the database.

The tools: postgres_exporter + Prometheus + Grafana, or the cloud provider's Performance Insights. A runbook link on every alert: the person woken up at night must know what to do.