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:
| Job | Targets | Answers |
|---|---|---|
postgres | postgres_exporter on each node (:9187-9189) | connections, transactions, lag, slots, archiver, long transactions |
patroni | Patroni's REST API (:8008-8010) | who is primary, timeline, failsafe mode |
pgbouncer | pgbouncer_exporter (:9127) | pool use, clients waiting |
postgres-haproxy | HAProxy's built-in exporter (:8404) | healthy backends behind :5000 and :5001 |
postgres-etcd | each 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| Alert | Fires when | Why that threshold |
|---|---|---|
PostgresNoPrimary | no node reports primary for 30 s | the etcd drill had 36 s with no primary; a kill -9 takes about 23 s to fail over on its own |
PostgresMultiplePrimaries | more than one primary, at once | split brain should be impossible; if it happens, it's the worst thing that can happen |
PostgresHAProxyNoPrimary | HAProxy has 0 healthy primary backends for 30 s | what applications actually experience, independent of Patroni's view |
PostgresFailoverHappened | the timeline changed in the last 15 min | every failover deserves a look, even a successful one |
PostgresReplicaMissing | fewer than 2 replicas for 2 min | one more failure would leave no failover target |
PostgresReplicationLagHigh | a replica 30 s behind for 2 min | the lab's worst bulk write was 315 ms; 30 s is far outside normal |
PostgresReplicationSlotRetainingWAL | a slot on the primary holds over 1 GiB for 5 min | the slot drill retained 1.5 GB; max_slot_wal_keep_size invalidates at 2 GB |
PostgresWALArchivingFailing | any archive failure in 10 min | without the archive there is no point-in-time recovery, and pg_wal grows |
PostgresWALArchiveStale | the primary hasn't archived for 10 min | archive_timeout = 60 means at least once a minute |
PostgresBackupTooOld | newest backup older than 26 h | backups run daily; 2 h of slack |
PostgresBackupMetricsStale | no backup status pushed for 30 min | the loop pushes every 5 min; silence means it died |
PostgresConnectionsNearLimit | over 80% of max_connections for 5 min | at 200 the load test failed outright |
PostgresLongRunningTransaction | a transaction open over 10 min | the MVCC lab: one open snapshot blocks VACUUM completely |
PostgresDeadlocks | more than 5 deadlocks in 10 min | an occasional one is normal; a stream is a code bug |
PatroniFailsafeActive | failsafe mode in effect | the primary is running without etcd; fix etcd |
PostgresEtcdQuorumAtRisk | fewer than 3 healthy etcd members for 1 min | at 1 member left the primary demotes (failsafe off) |
PgBouncerDown | PgBouncer unreachable for 1 min | it's the only way in for applications |
PgBouncerClientsWaiting | clients waiting over 1 s for 2 min | the pool is too small for the load |
PostgresDown | an exporter can't connect to its node for 1 min | a 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 == 0The 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.
The old dashboard for this module filtered on datname="learning", the
database of the single-instance setup. On the cluster every one of those
panels would have been empty, without any error. Dashboards rot the same
way alerts do: check them against live data whenever the system changes.