Skip to content

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

Key points

  • pg_stat_replication breaks lag into stages.
  • Long queries on a standby can pause replay.
  • Inactive replication slots can fill the primary's disk.
  • Measure in bytes and seconds, because an idle primary misleads time-based checks.

What it means

The primary streams WAL to each standby, which writes, flushes and replays it. Lag at each stage has a different cause: network between primary and standby for write_lag, disk on the standby for flush_lag, and apply conflicts or CPU for replay_lag.

Common causes

  • Large write bursts, bulk loads, index builds.
  • A slow standby disk or a smaller instance than the primary.
  • Network limits between regions.
  • Standby query conflicts, where replay waits for a long-running read.
  • A single-threaded replay process that cannot keep up.

Diagnose it

On the primary, compare positions. On the standby, see how old the last replayed commit is.

bash
-- 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.

Frequently asked questions

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.

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.