Learn Labs
Monitoring & Performance

Monitoring the Cluster

Thirteen scrape targets, nineteen alerts with tests, and the dashboard, with each threshold traced to a lab

A single Postgres needs one exporter. A cluster needs to answer questions no single node can: is there exactly one primary, can clients reach it, is anything holding WAL, are backups happening. The Prometheus stack in ../prometheus scrapes five kinds of target:

JobTargetsAnswers
postgrespostgres_exporter on each node (:9187-9189)connections, transactions, lag, slots, archiver, long transactions
patroniPatroni's REST API (:8008-8010)who is primary, timeline, failsafe mode
pgbouncerpgbouncer_exporter (:9127)pool use, clients waiting
postgres-haproxyHAProxy's built-in exporter (:8404)healthy backends behind :5000 and :5001
postgres-etcdeach etcd member (:2381-2383)members up, leader present

Plus one pushed metric: the backup loop on the primary pushes the newest backup's time to the Pushgateway after every check.

The alerts, and where each threshold comes from

All 19 live in the postgres group of rules/alerts.yml, with unit tests in tests/alerts.test.yml:

docker exec prometheus promtool test rules /etc/prometheus/tests/alerts.test.yml
SUCCESS
AlertFires whenWhy that threshold
PostgresNoPrimaryno node reports primary for 30 sthe etcd drill had 36 s with no primary; a kill -9 takes about 23 s to fail over on its own
PostgresMultiplePrimariesmore than one primary, at oncesplit brain should be impossible; if it happens, it's the worst thing that can happen
PostgresHAProxyNoPrimaryHAProxy has 0 healthy primary backends for 30 swhat applications actually experience, independent of Patroni's view
PostgresFailoverHappenedthe timeline changed in the last 15 minevery failover deserves a look, even a successful one
PostgresReplicaMissingfewer than 2 replicas for 2 minone more failure would leave no failover target
PostgresReplicationLagHigha replica 30 s behind for 2 minthe lab's worst bulk write was 315 ms; 30 s is far outside normal
PostgresReplicationSlotRetainingWALa slot on the primary holds over 1 GiB for 5 minthe slot drill retained 1.5 GB; max_slot_wal_keep_size invalidates at 2 GB
PostgresWALArchivingFailingany archive failure in 10 minwithout the archive there is no point-in-time recovery, and pg_wal grows
PostgresWALArchiveStalethe primary hasn't archived for 10 minarchive_timeout = 60 means at least once a minute
PostgresBackupTooOldnewest backup older than 26 hbackups run daily; 2 h of slack
PostgresBackupMetricsStaleno backup status pushed for 30 minthe loop pushes every 5 min; silence means it died
PostgresConnectionsNearLimitover 80% of max_connections for 5 minat 200 the load test failed outright
PostgresLongRunningTransactiona transaction open over 10 minthe MVCC lab: one open snapshot blocks VACUUM completely
PostgresDeadlocksmore than 5 deadlocks in 10 minan occasional one is normal; a stream is a code bug
PatroniFailsafeActivefailsafe mode in effectthe primary is running without etcd; fix etcd
PostgresEtcdQuorumAtRiskfewer than 3 healthy etcd members for 1 minat 1 member left the primary demotes (failsafe off)
PgBouncerDownPgBouncer unreachable for 1 minit's the only way in for applications
PgBouncerClientsWaitingclients waiting over 1 s for 2 minthe pool is too small for the load
PostgresDownan exporter can't connect to its node for 1 mina node is down or refusing connections

Some expressions are less obvious than the table suggests. Patroni keeps a copy of every replication slot on the replicas too, so they can take over after a failover; on a replica those copies look inactive and far behind. The slot alert therefore only counts slots on the primary:

pg_replication_slots_pg_wal_lsn_diff{job="postgres"} > 1073741824
and on(instance) pg_replication_is_replica == 0

The unit tests check exactly that case: a 2 GB slot on a replica must not fire, a 1.9 GB slot on the primary must.

Verified against live data

A query that returns nothing never fires, so after writing the rules each expression was run against the live cluster. All 13 targets were up and every alert query and every dashboard query returned data. When the rules were loaded, one alert was already firing, correctly: PostgresFailoverHappened, because the upgrade drill had switched over less than 15 minutes earlier.

The dashboard

Grafana, folder Databases, PostgreSQL HA Cluster (21 panels). The top row is the one to look at during an incident: which node is primary, how many primaries there are (exactly 1), streaming replicas (2), HAProxy's healthy primary backends (1), healthy etcd members (3), PgBouncer up, and the age of the last backup. Below that: role per node over time (each step is a failover), commits per node, replication lag, WAL held by slots, timeline, connections against max_connections, PgBouncer waiting clients, archive age, oldest transaction, cache hit ratio, sessions through HAProxy, deadlocks and database size.

On this page