Database monitoring · 7 min read · PostgreSQLreplication

Monitor PostgreSQL replication lag

Keep an eye on how far behind your read replicas are, before an application starts reading stale data.

What you'll need

  • A primary PostgreSQL server already added in Zynty
  • At least one streaming replica configured
1

Confirm pg_monitor has replication visibility

The pg_monitor role (see Monitor PostgreSQL performance and availability) already includes access to pg_stat_replication, no extra grant needed.

2

Understand what Zynty measures

Lag is calculated from the byte difference between the primary's write position and each replica's replay position, converted to an estimated time delay.

SELECT client_addr, state,
       pg_wal_lsn_diff(sent_lsn, replay_lsn) AS lag_bytes
FROM pg_stat_replication;
3

Set a lag threshold alert

Alert when lag exceeds a few seconds for synchronous-sensitive workloads, or a few minutes for read replicas serving reporting queries where a little staleness is fine.

Zynty application screenshot
1