Sub topic 7
Checking Replication Health and Measuring Lag
This chapter assumes you can edit PostgreSQL configuration, use psql, read basic SQL, and distinguish a primary from a standby. You also need access to both servers. The examples use PostgreSQL physical streaming replication.
What to check
Replication health has three related questions:
- Is the standby connected to the primary?
- How much WAL has the primary sent but the standby has not received?
- How much WAL has the standby received but not replayed?
WAL position is represented by an LSN, or log sequence number. An LSN identifies a location in the write-ahead log. Comparing LSNs shows the amount of outstanding WAL in bytes.
The primary exposes one row per connected standby through pg_stat_replication.
SELECT application_name,
client_addr,
state,
sync_state,
sent_lsn,
write_lsn,
flush_lsn,
replay_lsn,
pg_size_pretty(pg_wal_lsn_diff(sent_lsn, replay_lsn))
AS sent_to_replay_bytes,
write_lag,
flush_lag,
replay_lag
FROM pg_stat_replication;The columns describe stages in the stream:
| Column | Meaning |
|---|---|
| `sent_lsn` | WAL position sent by the primary |
| `write_lsn` | WAL received and written to the standby's WAL files |
| `flush_lsn` | WAL flushed durably on the standby |
| `replay_lsn` | WAL applied to the standby's data files |
| `state` | Connection state, normally `streaming` during normal operation |
| `sync_state` | Whether the standby is asynchronous or part of synchronous replication |
A large difference between sent_lsn and write_lsn usually indicates network or receiver pressure. A large difference between flush_lsn and replay_lsn usually indicates that replay is behind, often because of standby queries or limited standby resources.
Check the standby directly
Run these functions on the standby:
SELECT pg_is_in_recovery() AS is_standby,
pg_last_wal_receive_lsn() AS received_lsn,
pg_last_wal_replay_lsn() AS replayed_lsn,
pg_last_xact_replay_timestamp() AS last_replayed_transaction;pg_is_in_recovery() should return true while the server is a standby. The receive position shows the latest WAL written by the standby's receiver. The replay position shows the latest WAL applied by the recovery process.
The timestamp function can estimate replay delay:
SELECT clock_timestamp()
- pg_last_xact_replay_timestamp() AS approximate_replay_delay;This value can be misleading when the primary is idle. If no transaction has recently replayed, the timestamp is old even when the standby has no meaningful backlog. Treat LSN differences as the primary measurement and timestamps as supporting evidence.
A practical monitoring routine
Check the connection state first. Then compare sent_lsn, flush_lsn, and replay_lsn. Record these values repeatedly so you can see whether the gap is growing or shrinking. Alert on sustained growth rather than on one brief spike.
On the standby, pg_stat_wal_receiver provides connection details such as the upstream host, connection status, and the latest received WAL position. Compare its values with pg_stat_replication when diagnosing whether the delay is in transmission, flushing, or replay.
Exercise
The primary reports state = 'streaming', but the gap between flush_lsn and replay_lsn keeps increasing. Which stage is behind?
Show answer
Replay is behind the standby's WAL receiver. Investigate long-running standby queries, recovery conflicts, CPU or I/O pressure, and replay-related settings.