Skip to content

Instantly share code, notes, and snippets.

@mowings
Last active January 20, 2023 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mowings/8d4b72eef0e9ef2fc808adf0cde25c51 to your computer and use it in GitHub Desktop.
Save mowings/8d4b72eef0e9ef2fc808adf0cde25c51 to your computer and use it in GitHub Desktop.
Monitor postgres replication lag

Montioring postgres replication status

Use the following query at the source cluster to check status and lag (lag is in bytes):

select   application_name, pid, client_addr, state, sync_state,
         pg_wal_lsn_diff(sent_lsn, write_lsn) as write_lag,
         pg_wal_lsn_diff(sent_lsn, flush_lsn) as flush_lag,
         pg_wal_lsn_diff(sent_lsn, replay_lsn) as replay_lag
from pg_stat_replication;

To get replication lag by duration (postgres duration):

select application_name, pid, client_addr, state, sync_state, write_lag, flush_lag, replay_lag  from pg_stat_replication;
column lag type
write_lag Data not yet received by client
flush_lag Data received by client, but not yet flushed to disk (durable)
replay_lag Data flushed to disk at client, but not yet replayed (available for read)

A missing PID means replication has broken somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment