Troubleshooting guide
PostgreSQL replication lag: causes and how to fix it
Replication lag means a standby has not yet applied what the primary committed. Reads from it are stale, and failing over would lose or delay data.
3 min read10 sectionsUpdated
On this page
Diagnose it
On the primary, compare positions. On the standby, see how old the last replayed commit is.
-- On the primary
SELECT application_name, state, write_lag, flush_lag, replay_lag,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn)) AS behind
FROM pg_stat_replication;
-- On a standby
SELECT now() - pg_last_xact_replay_timestamp() AS replay_delay;
-- Slots that retain WAL
SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained
FROM pg_replication_slots;Fixes
Match the standby's disk and CPU to the primary. Split huge writes into batches. Control standby conflicts with max_standby_streaming_delay, knowing that a higher value lets queries finish but allows greater lag. Move long reporting queries to a dedicated standby. Remove replication slots you no longer use.
Why an idle primary can look lagged
pg_last_xact_replay_timestamp() reports when the last transaction was applied. With no writes on the primary, that time grows even though the standby is fully caught up. Pair it with the byte difference: if the bytes behind are zero, the standby is current. A small heartbeat table that is updated every few seconds gives a reliable, end-to-end measure too. For applications reading from standbys, remember lag can cause users to not see what they just wrote, so route read-after-write traffic to the primary.
Catch it automatically
Alert on bytes behind and on retained WAL per slot, as a dead slot can fill the primary's disk and stop the whole cluster. See PostgreSQL monitoring. Test failover regularly so you know the real effect of lag.
How much lag is acceptable?
It depends on what reads from the standby. Under a second is typical in-region, and any failover plan must account for it.
Does synchronous replication remove lag?
It guarantees WAL is received before commit, but read visibility depends on the setting used.
What does an inactive slot do?
It forces the primary to retain WAL indefinitely until it is removed or consumed.
Related reading
- PostgreSQL idle in transaction: find and fix it: Find and fix PostgreSQL idle in transaction sessions: why they block vacuum and hold locks, which application patterns cause them and which timeout to set.
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- PostgreSQL high CPU usage: how to find the cause: Diagnose PostgreSQL high CPU: use pg_stat_statements to rank queries by total time, spot missing indexes and stale statistics, and read EXPLAIN properly.
- PostgreSQL connection refused: how to fix it: Fix PostgreSQL connection refused on port 5432: server not running, listen_addresses, pg_hba.conf, Docker hostnames and firewalls, with commands for every case.
- 502 Bad Gateway: causes and how to fix it: What a 502 Bad Gateway means, the common causes behind Nginx, load balancers and CDNs, how to find yours and how to stop it happening again.
- ERR_CONNECTION_RESET: causes and how to fix it: Fix ERR_CONNECTION_RESET: crashed backends, firewalls and antivirus resetting traffic, MTU problems and keep-alive races, for both visitors and site owners.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.