Measure replication lag in PostgreSQL
How far behind are the standbys? The primary reports it in bytes and time per standby; the standby can report its own delay.
The task
Replication lag is the gap between the WAL the primary has written and what each standby has received, flushed and replayed. A little lag is normal. Growing lag means a standby cannot keep up, because of network, disk, or queries on the standby holding up replay. With asynchronous replication, the unreceived part is what a failover would lose, and the unreplayed part is how stale reads on the standby are. pg_stat_replication on the primary has one row per connected standby.
The snippet
Written for PostgreSQL. Versions for MySQL are under Variations.
SELECT client_addr, application_name, state, sync_state,
pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn)) AS replay_backlog,
write_lag, flush_lag, replay_lag
FROM pg_stat_replication
ORDER BY replay_lag_bytes DESC;
So funktioniert es
Run it on the primary. pg_wal_lsn_diff gives the byte distance between the primary's current WAL position and the standby's replay position. write_lag, flush_lag and replay_lag are the times between the primary writing recent WAL and the standby reporting it written, flushed to disk and replayed. Data-loss exposure on failover depends on flush, and read staleness on replay. A standby with small flush_lag and large replay_lag is receiving WAL fine but applying it slowly, often because a long query on the standby is blocking replay. The *_lag columns go NULL after the standby has caught up and the primary has been idle for a moment, so NULL there is not an error.
Variations
On the standby: time since the last replayed transaction
SELECT pg_is_in_recovery() AS in_recovery,
pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() AS caught_up,
now() - pg_last_xact_replay_timestamp() AS since_last_replayed_commit;
MySQL: replica status (run on the replica)
MySQLSHOW REPLICA STATUS;
-- MySQL 8.0.22 and later. Key columns: Seconds_Behind_Source,
-- Replica_IO_Running, Replica_SQL_Running, Last_IO_Error, Last_SQL_Error.
-- Older versions: SHOW SLAVE STATUS with Seconds_Behind_Master.
Questions that come up
The primary shows no lag, but a standby is behind. How?
pg_stat_replication lists only standbys that are connected right now. A standby whose connection dropped has no row, so it shows no lag at all while it falls further behind; if it uses a replication slot, pg_replication_slots shows it with active = false and an old restart_lsn. The reverse confusion happens on the standby side: since_last_replayed_commit keeps growing on a quiet primary even though nothing is waiting to be applied, so check caught_up in the same variant before alerting on it. The primary-side byte count is the better alert for a quiet system.
What makes replay lag grow suddenly?
Common causes: a bulk load, index build or large UPDATE on the primary producing WAL faster than the network or the standby's disk can take it; queries on the standby that conflict with replay, which pause replay for up to max_standby_streaming_delay (30 seconds by default, -1 means indefinitely); and replay being single-threaded, so one standby CPU core can be the limit. Frequent checkpoints on the primary make it worse, because each one triggers full-page images and more WAL; see the checkpoint-warnings recipe.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.