Skip to content

Commit

Permalink
Fixed using pg_current_wal_lsn if pg_is_in_recovery is true
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Grigorev committed Jan 10, 2025
1 parent 26faa80 commit 331bced
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/collector/postgres_replication_slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import (

const (
// Query for Postgres version 9.6 and older.
postgresReplicationSlotQuery96 = "SELECT database, slot_name, slot_type, active, pg_current_xlog_location() - restart_lsn AS since_restart_bytes FROM pg_replication_slots"
postgresReplicationSlotQuery96 = "SELECT database, slot_name, slot_type, active, " +
"CASE WHEN pg_is_in_recovery() THEN pg_xlog_location_diff(pg_current_xlog_location(), restart_lsn) " +
"ELSE pg_xlog_location_diff(pg_current_xlog_location(), restart_lsn) END AS since_restart_bytes " +
"FROM pg_replication_slots"

// Query for Postgres versions from 10 and newer.
postgresReplicationSlotQueryLatest = "SELECT database, slot_name, slot_type, active, pg_current_wal_lsn() - restart_lsn AS since_restart_bytes FROM pg_replication_slots"
postgresReplicationSlotQueryLatest = "SELECT database, slot_name, slot_type, active, " +
"CASE WHEN pg_is_in_recovery() THEN pg_wal_lsn_diff(pg_last_wal_receive_lsn(), restart_lsn) " +
"ELSE pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) END AS since_restart_bytes " +
"FROM pg_replication_slots"
)

type postgresReplicationSlotCollector struct {
Expand Down

0 comments on commit 331bced

Please sign in to comment.