Skip to content

Troubleshooting · Databases

Postgres replica lag growing? 6 causes, ranked

6 causes, most common first · last reviewed

TL;DR

Find out which stage is slow. pg_stat_replication splits lag into write, flush and replay: replay lag alone usually means the replica cannot apply WAL fast enough or is pausing for its own queries; lag at every stage points at the network or the replica's disk. Then decide whether the application needs to read its own writes from the primary.

On this page

Quick triage: the first five minutes

  1. On the primary, see each replica's state and where the lag is.

    SQL
    SELECT application_name, client_addr, state,
           write_lag, flush_lag, replay_lag,
           pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn)) AS replay_bytes_behind
    FROM pg_stat_replication;
    state should be streaming. replay_lag much larger than write_lag means applying, not receiving, is the bottleneck.
  2. On the replica, compare what has been received with what has been applied.

    SQL
    SELECT pg_is_in_recovery(),
           pg_last_wal_receive_lsn(), pg_last_wal_replay_lsn(),
           now() - pg_last_xact_replay_timestamp() AS since_last_replayed_txn;
    since_last_replayed_txn also grows when the primary is idle, so read it together with the LSNs.
  3. Check whether replay is being held back by queries running on the replica.

    SQL
    SELECT datname, confl_snapshot, confl_lock, confl_bufferpin
    FROM pg_stat_database_conflicts;
    SHOW max_standby_streaming_delay;
    SHOW hot_standby_feedback;

The causes, ranked

Most common first. The ranking is a judgement from how these failures usually turn out, not a measured statistic.

Most common

1. The primary writes faster than the replica can replay

What it looks like

  • replay_lag grows during bulk updates, data migrations, index builds or batch jobs, then recovers afterwards.
  • write_lag and flush_lag stay small; the WAL arrives promptly but is applied slowly.
  • The replica's startup (recovery) process is busy on CPU or I/O.

How to confirm it

Recovery on a physical replica is applied by a single process, while the primary may have many sessions writing in parallel. Correlate the lag with jobs on the primary and with the WAL generation rate there. If replay_bytes_behind keeps rising while the job runs and falls once it stops, the replica is simply out-applied; nothing is broken, it is a matter of pace.

Fix it now

Throttle or batch the heavy job, and route reads that must be fresh to the primary until the replica catches up. For index creation, a CONCURRENTLY build spreads the work but still generates WAL to replay. If several replicas lag, take the most important one out of read traffic first so it catches up without competing with queries.

Prevent it

Run large data changes in batches with pauses, during quiet periods, and watch replica lag as part of every migration's runbook.

Most common

2. Long queries on the replica pause replay

What it looks like

  • Lag rises while analytics or reporting queries run on the replica.
  • pg_stat_database_conflicts counters rise, or queries fail with 'canceling statement due to conflict with recovery'.
  • Lag stops at about the value of max_standby_streaming_delay, then drops suddenly.

How to confirm it

When WAL would remove rows a running query still needs, or needs a lock it holds, the replica either waits, up to max_standby_streaming_delay (30 seconds by default), or cancels the query. Waiting shows as lag; cancelling shows as errors.

SQL
SELECT pid, now() - query_start AS running, left(query, 70) AS query
FROM pg_stat_activity
WHERE state = 'active' AND backend_type = 'client backend'
ORDER BY query_start LIMIT 10;

Fix it now

Move long analytical queries to a dedicated replica with a high delay, and keep the replicas that serve the application on a short delay. hot_standby_feedback on reduces cancellations but makes the primary keep dead rows longer, which causes bloat there.

Prevent it

Separate replicas by purpose: one tuned for fresh reads, one for long queries, so neither setting compromises the other. Then alert on lag per replica against what its readers can tolerate.

Common

3. The network between primary and replica is the bottleneck

What it looks like

  • write_lag, flush_lag and replay_lag are all high and close to each other.
  • The replica is in another region or availability zone, or shares a busy link.
  • Lag tracks the primary's WAL volume closely during busy periods.

How to confirm it

Compare the WAL generation rate on the primary with measured throughput on the replication connection. If the link is saturated, every stage after sending waits. Latency matters too: with synchronous replication, every commit on the primary waits for the replica, so a slow link also slows writes, not just reads.

Fix it now

Reduce WAL volume (batch jobs, avoid unnecessary full-row updates), or move the replica closer. Enabling wal_compression reduces the size of full-page images in WAL at some CPU cost.

Prevent it

Size cross-region links for peak WAL rate, not the average, and alert on write_lag as well as replay_lag.

Common

4. The replica is smaller or slower than the primary

What it looks like

  • The replica's disk shows high utilisation or latency while the primary's does not.
  • The replica has a smaller instance type or cheaper storage, chosen to save cost.
  • Lag appears under load that the primary handles easily.

How to confirm it

Check I/O and CPU on the replica while it is lagging. Replay is often bound by random reads of pages it must update, so slower storage shows up directly as lag.

Shell
iostat -x 5 3

Fix it now

Give the replica storage and memory comparable to the primary, especially if it may be promoted. Take reporting load off it while it catches up. A replica with too little memory also rereads pages from disk that the primary keeps cached, so it falls behind on exactly the workloads the primary finds easy.

Prevent it

Treat a replica that can be promoted as production capacity, sized like the primary.

Less common

5. The application reads its own writes from a replica

What it looks like

  • Users save something and immediately see the old value, though measured lag is only a second or two.
  • Bugs appear in flows that write then read straight away, such as checkout or profile updates.
  • Reads go through a proxy or ORM setting that sends all SELECTs to replicas.

How to confirm it

Trace one request that writes and then reads. Any non-zero lag means a read issued straight after a write on a replica can miss it.

Fix it now

Route reads that follow a write in the same user flow to the primary, or wait until the replica has replayed past the write's LSN before reading from it.

Prevent it

Decide per query whether stale data is acceptable, and make read-your-writes the default for user-facing flows.

Rare but nasty

6. The replica lost streaming and fell back to catching up

What it looks like

  • The replica is missing from pg_stat_replication, or its state is catchup instead of streaming.
  • Logs on the replica say the requested WAL segment has already been removed.
  • Lag grows without limit and never recovers on its own.

How to confirm it

If the primary recycled WAL the replica still needed, and there is no slot or archive to fetch it from, the replica cannot continue and must be rebuilt.

Shell
grep -i 'already been removed\|could not receive data' /var/log/postgresql/*.log | tail -5

Fix it now

Rebuild the replica from a fresh base backup (for example with pg_basebackup), or let it restore missing WAL from the archive if restore_command is configured. Until it is back, remove it from the pool of read replicas so applications do not read data that is falling further and further behind.

Prevent it

Use a replication slot with max_slot_wal_keep_size, or a WAL archive with restore_command, so a briefly disconnected replica can always catch up.

See it happen in a real outage

90-second puzzles built from public postmortems. Diagnose it first; the company is revealed after you answer.

Related concepts

FAQ

How do I check replication lag in Postgres?

On the primary, query pg_stat_replication for write_lag, flush_lag and replay_lag per replica. On a replica, compare pg_last_wal_receive_lsn() with pg_last_wal_replay_lsn(), and now() minus pg_last_xact_replay_timestamp() for time since the last applied transaction.

Why does replay lag increase when nothing is wrong?

now() - pg_last_xact_replay_timestamp() keeps growing when the primary has no new transactions, so an idle database looks like it is lagging. Check whether the receive and replay positions differ before treating it as lag.

What does 'canceling statement due to conflict with recovery' mean?

A query on the replica needed rows or a lock that incoming WAL had to remove, and it ran longer than max_standby_streaming_delay allows, so the replica cancelled it to keep replaying.

Should I enable hot_standby_feedback?

It reduces query cancellations on the replica by telling the primary not to clean up rows those queries need. The cost is more bloat on the primary while long replica queries run.

All troubleshooting guides

Reference documentation: PostgreSQL documentation: monitoring statistics, pg_stat_replication ↗ · PostgreSQL documentation: hot standby and query conflicts ↗ · PostgreSQL documentation: system administration functions ↗ · Report a problem

Commands and settings are checked against the documentation linked above. Defaults change between versions; check the documentation for the version you run.