PostgreSQL problems
PostgreSQL Replication Lag: Where the Standby Falls Behind, and the Fix
22 November 2026
WAL travels to a standby in stages: the primary sends it, the standby writes it to disk, and the standby replays it. Lag is WAL that waits before one of these stages, and each stage has its own causes. One query on the primary shows where the WAL waits.
Where the WAL waits
Run this on the primary:
SELECT application_name, client_addr, state, sync_state,
pg_size_pretty(pg_wal_lsn_diff(
pg_current_wal_lsn(), sent_lsn)) AS not_sent,
pg_size_pretty(pg_wal_lsn_diff(
sent_lsn, flush_lsn)) AS not_flushed,
pg_size_pretty(pg_wal_lsn_diff(
flush_lsn, replay_lsn)) AS not_replayed,
write_lag, flush_lag, replay_lag
FROM pg_stat_replication;
| What grows | Where to look |
|---|---|
not_sent | The primary writes WAL faster than it leaves: the network between the two servers, or a burst such as a bulk load or an index build. |
not_flushed | The WAL arrived and is not on the standby's disk yet: the disk that holds pg_wal on the standby. |
not_replayed | The WAL is safe on the standby and not applied. Replay is one process, so a standby with slower disks falls behind under load. Or replay waits for a query on the standby. |
| No row for the standby | It is not connected. Nothing is sent, and when it uses a replication slot the primary keeps all WAL for it: see pg_wal fills the disk. |
state is streaming for a standby that follows and catchup for one that works through a backlog. The three lag columns are times, and they are empty on a system that writes nothing.
On the standby
SELECT pg_last_wal_receive_lsn() AS received,
pg_last_wal_replay_lsn() AS replayed,
pg_is_wal_replay_paused() AS replay_paused,
now() - pg_last_xact_replay_timestamp()
AS since_last_replay;
SELECT status, sender_host, flushed_lsn,
last_msg_receipt_time
FROM pg_stat_wal_receiver;
When received and replayed are equal the standby has caught up, and a large since_last_replay only means the primary wrote nothing lately. No row in pg_stat_wal_receiver means the receiver does not run.
Queries that replay had to cancel on this standby:
SELECT datname, confl_snapshot, confl_lock,
confl_bufferpin, confl_tablespace
FROM pg_stat_database_conflicts
ORDER BY confl_snapshot + confl_lock DESC;
The fix, in order
- Make sure it streams. A standby without a row in
pg_stat_replicationhas a reason in its own server log: the primary is unreachable, the password changed, or the WAL it needs is gone and it must be rebuilt. - Resume a paused replay.
SELECT pg_wal_replay_resume();on the standby. Somebody paused it for a reason, and forgot. - Decide what the standby is for. Replay waits for queries on the standby up to
max_standby_streaming_delay, 30 seconds by default, and then cancels them. A standby for failover wants a short delay. A standby for reports can have a long one, and its lag is then a choice and not a fault. - Spread the bursts. A bulk load, a
VACUUM FULLor a large index build writes WAL faster than one replay process applies it. Load in batches, and the standby stays close. - Give the standby the disks of the primary. A standby on slower storage falls behind in every busy hour.
- Send less.
wal_compression = onshrinks the full page images that make up much of the WAL after each checkpoint, which helps a slow link. - Keep a synchronous standby from stopping writes. With
synchronous_standby_namesset, every commit waits for the standby. When it hangs, sessions wait onSyncRepand no write finishes. Repair the standby, or take it out ofsynchronous_standby_namesand reload until it is back.
See it coming
Lag of seconds is normal and differs for every standby, so a fixed number either wakes people for nothing or says nothing. Radar Engine, the detection layer of Scops, measures each standby against its own usual lag, with no agent on the server:
| Detector | When it fires |
|---|---|
| Replication lag | Only when the lag is above 3 times the usual lag of that standby and growing. Watch from 3 minutes, Warning from 10 minutes, Critical from an hour. At least Warning: 1 GB not yet replayed. |
| Standby not streaming | A known standby stopped streaming. Synchronous: Critical. Asynchronous: Warning after 5 minutes, Critical after 60. |
| Synchronous commit waits | Commits wait for a synchronous standby that does not answer. Watch when at least 2 sessions wait for it and they are at least half of the sessions that commit. Warning when it lasts. Critical when the oldest has waited 60 seconds or more: hung, not slow. |
| Slot holding WAL | A replication slot keeps WAL and nobody consumes it. The finding names the slot and the time left to its limit. |
The finding names the standby and says whether the lag still grows. On the standby itself Scops reports a WAL receiver that does not stream while replay is not paused, so the problem is seen from both ends.
Questions
How do I measure replication lag in PostgreSQL?
On the primary, pg_stat_replication gives write_lag, flush_lag and replay_lag as times, and the difference between pg_current_wal_lsn() and replay_lsn as bytes. On the standby, compare pg_last_wal_receive_lsn() with pg_last_wal_replay_lsn().
Why is replay_lag high when the standby has received all the WAL?
Because replay is behind, not the network. Replay is a single process and can wait for queries on the standby up to max_standby_streaming_delay, or be paused with pg_wal_replay_pause().
Does a lagging standby affect the primary?
An asynchronous one does not slow it, but a replication slot makes the primary keep the WAL the standby has not received. A synchronous standby makes every commit wait, so its lag is the primary's commit time.
What does max_standby_streaming_delay do?
It sets how long replay on a standby waits for a query that conflicts with the WAL to apply, 30 seconds by default. After that the query is cancelled. -1 waits forever, which lets one long report hold replay without limit.
Related problems
- A replication slot fills the disk with WAL
- Too many clients: who holds the connections
- All PostgreSQL problems
- PostgreSQL monitoring without agents