apps/docs/content/guides/database/replication/manual-replication-monitoring.mdx
Monitoring replication lag is important and there are 3 ways to do this:
The pg_stat_replication table shows the status of any replicas connected to the primary database.
select pid, application_name, state, sent_lsn, write_lsn, flush_lsn, replay_lsn, sync_state
from pg_stat_replication;
A replication slot can be in one of three states:
active - The slot is active and is receiving datainactive - The slot is not active and is not receiving datalost - The slot is lost and is not receiving dataThe state can be checked using the pg_replication_slots table:
select slot_name, active, state from pg_replication_slots;
The WAL size can be checked using the pg_ls_waldir() function:
select * from pg_ls_waldir();
select pg_current_wal_lsn();
The pg_subscription table shows the status of any subscriptions on a replica and the pg_subscription_rel table shows the status of each table within a subscription.
The srsubstate column in pg_subscription_rel can be one of the following:
i - Initializing - The subscription is being initializedd - Data Synchronizing - The subscription is synchronizing data for the first time (i.e. doing the initial copy)s - Synchronized - The subscription is synchronizedr - Replicating - The subscription is replicating dataSELECT
sub.subname AS subscription_name,
relid::regclass AS table_name,
srel.srsubstate AS replication_state,
CASE srel.srsubstate
WHEN 'i' THEN 'Initializing'
WHEN 'd' THEN 'Data Synchronizing'
WHEN 's' THEN 'Synchronized'
WHEN 'r' THEN 'Replicating'
ELSE 'Unknown'
END AS state_description,
srel.srsyncedlsn AS last_synced_lsn
FROM
pg_subscription sub
JOIN
pg_subscription_rel srel ON sub.oid = srel.srsubid
ORDER BY
table_name;
select pg_last_wal_replay_lsn();