How to monitor PostgreSQL replication lag

PostgreSQL Advanced DBA Series: Monitoring PostgreSQL replication lag


There are several ways to monitor replication lag in PostgreSQL:

  1. pg_stat_replication: This is a PostgreSQL system view that provides information about the replication status, including the replication lag. You can use a query like SELECT * FROM pg_stat_replication; to check the replication status, including the replication lag. The pg_stat_replication.replication_lag column shows the time in microseconds that a slave is behind the master.
  2. pg_current_wal_lsn() and pg_last_wal_receive_lsn(): These are PostgreSQL functions that can be used to check the LSN (Log Sequence Number) of the master and the slave. The difference between the LSN on the master and the slave represents the replication lag.
  3. pg_stat_replication_slots: This is another PostgreSQL system view that provides information about the replication slots, including the replication lag. You can use a query like SELECT * FROM pg_stat_replication_slots to check the replication slots, including the replication lag.
  4. pg_replication_slots_advance: This is another PostgreSQL function that can be used to check the replication lag by comparing the position of the replication slot on the master and the slave.
About MinervaDB Corporation 76 Articles
Full-stack Database Infrastructure Architecture, Engineering and Operations Consultative Support(24*7) Provider for PostgreSQL, MySQL, MariaDB, MongoDB, ClickHouse, Trino, SQL Server, Cassandra, CockroachDB, Yugabyte, Couchbase, Redis, Valkey, NoSQL, NewSQL, Databricks, Amazon Resdhift, Amazon Aurora, CloudSQL, Snowflake and AzureSQL with core expertize in Performance, Scalability, High Availability, Database Reliability Engineering, Database Upgrades/Migration, and Data Security.