Cookbook recipe

Streaming replication lag is growing

Applies to PostgreSQL 13–17 Last reviewed Nov 2025 Grounded in source
Estimated investigation4 min

Scenario

The standby is falling behind the primary — replay_lag is growing and your RPO window is at risk. Diagnose it -- Lag by standby (run on the primary): SELECT application_name, client_addr, state, sync_state, sent_lsn, write_lsn, flush_lsn,…

Investigation Path

The standby is falling behind the primary — replay_lag is growing and your RPO window is at risk.

Diagnose it

-- Lag by standby (run on the primary):
SELECT application_name,
       client_addr,
       state,
       sync_state,
       sent_lsn,
       write_lsn,
       flush_lsn,
       replay_lsn,
       write_lag,
       flush_lag,
       replay_lag,
       pg_size_pretty(
           pg_wal_lsn_diff(sent_lsn, replay_lsn)
       ) AS bytes_behind
FROM pg_stat_replication
ORDER BY replay_lag DESC NULLS LAST;

write_lag is the round-trip network time; flush_lag adds fsync time
on the standby; replay_lag adds WAL apply time. All three were added in
PostgreSQL 10.

Why it happens

Lag can accumulate from: (1) network saturation — more WAL is being generated than the
link can transfer; (2) standby I/O bottleneck — flush_lag and replay_lag
are high while write_lag is low; (3) a long-running transaction or vacuum on the
standby causing max_standby_streaming_delay to kick in and pause WAL apply;
(4) a query conflict on the standby causing a pause (check the standby’s
pg_stat_database_conflicts).

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes