Skip to content

Replication lag

For a primary with replicas: how far behind each one is, in bytes and in time. Empty on a server with no replicas.

PostgreSQL · SQL
SELECT client_addr,       application_name,       state,       sync_state,       pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes,       EXTRACT(EPOCH FROM replay_lag)::numeric(10,2) AS replay_lag_secondsFROM pg_stat_replicationORDER BY replay_lag_bytes DESC

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What this query shows

A replica that falls behind serves stale reads, and in a failover it loses the transactions it never received. Replication lag is how far each standby is behind the primary, in bytes of WAL and in time.

The query runs on the primary and reads pg_stat_replication, one row per connected standby. It compares the primary's current WAL position with the position each replica has replayed. On a server without replicas it returns nothing.

How to read each column

  • client_addr and application_name: which standby this is. Set application_name in the replica's primary_conninfo to make it readable.
  • state: streaming is normal. startup, catchup, backup and stopping appear during transitions.
  • sync_state: async, sync, potential or quorum, depending on your synchronous replication setup.
  • replay_lag_bytes: the gap between the primary's current WAL position and replay_lsn, the position the standby has applied.
  • replay_lag_seconds: the replay_lag interval in seconds, an estimate of how long recent commits took to become visible on the standby.

Healthy and worrying numbers

On a healthy setup, state is streaming and lag stays in the low seconds or less, with bytes in the megabytes. These are rules of thumb. What matters is your tolerance for stale reads and how much data you can lose in a failover.

Look at the trend. Lag that grows and does not come back means the replica cannot keep up. Spikes after a big batch job or a VACUUM that settle down are usual. A replica in catchup that never reaches streaming needs attention.

What to do about it

Find which stage is slow. In pg_stat_replication, compare sent_lsn, write_lsn, flush_lsn and replay_lsn. If replay is far behind the others, the standby is the bottleneck: slow disk, CPU, or replay waiting on a long query. A standby query that conflicts with replay can pause it, up to max_standby_streaming_delay.

Check replication slots too. An inactive slot makes the primary keep WAL until the disk fills.

SELECT slot_name, active, pg_size_pretty(  pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_walFROM pg_replication_slots;

If a slot is abandoned, drop it with pg_drop_replication_slot. From PostgreSQL 13, max_slot_wal_keep_size caps how much WAL a slot may hold. To measure lag from the standby itself, use:

SELECT now() - pg_last_xact_replay_timestamp() AS replay_delay;

Limits of this query

  • Run it on the primary. pg_current_wal_lsn() cannot be called on a standby, and pg_stat_replication is empty there unless it has its own cascading replicas.
  • replay_lag is NULL when there has been no recent WAL activity, and the seconds column can look empty on an idle system.
  • The seconds value is an estimate, which is why bytes and seconds are shown together.
  • Managed services may hide this view or return their own metrics. Check your provider.

Run it on a schedule in DataRunner

Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Every 5 to 15 minutes if a replica serves production reads. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.

Every run is kept in the run history, so you can set today's result next to last week's. A schedule carries its own time zone, so a Monday at 8:00 is your Monday.

If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.

Frequently asked questions

How do I check replication lag in PostgreSQL?
On the primary, query pg_stat_replication and compare pg_current_wal_lsn() with replay_lsn for bytes, and read the replay_lag column for time. On the standby you can use now() - pg_last_xact_replay_timestamp().
What is an acceptable replication lag?
It depends on what the replica is for. A reporting replica can lag a minute, a replica used for failover should be near zero. There is no universal number, so set one from how stale a read you can accept.
Why is my PostgreSQL replica lagging?
The common causes are a slow disk or network on the standby, a long query on the standby that conflicts with replay, a large batch of writes on the primary, or a standby that is too small. Compare the sent, flushed and replayed positions to see which stage is behind.
Why does pg_stat_replication return no rows?
Because no standby is connected to this server. It also happens when you run the query on a replica. Connect to the primary and check that the standby is streaming.