From 9422d8b19fdcd5bb245371c61c7e47ccb17c6dca Mon Sep 17 00:00:00 2001 From: yanboer Date: Fri, 26 May 2023 14:59:07 +0800 Subject: [PATCH] adjust pg_replication_slots metrics --- cmd/postgres_exporter/queries.go | 5 +++-- collector/replication_slots.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/postgres_exporter/queries.go b/cmd/postgres_exporter/queries.go index 14c14b4f8..7b031a500 100644 --- a/cmd/postgres_exporter/queries.go +++ b/cmd/postgres_exporter/queries.go @@ -112,8 +112,9 @@ var queryOverrides = map[string][]OverrideQuery{ { semver.MustParseRange(">=10.0.0"), ` - SELECT slot_name, database, active, pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) - FROM pg_replication_slots + SELECT slot_name, database, active, + (case pg_is_in_recovery() when 't' then pg_last_wal_receive_lsn() - restart_lsn else pg_current_wal_lsn() - restart_lsn end) as pg_wal_lsn_diff + FROM pg_replication_slots; `, }, }, diff --git a/collector/replication_slots.go b/collector/replication_slots.go index a9cad77df..a7459cb2c 100644 --- a/collector/replication_slots.go +++ b/collector/replication_slots.go @@ -53,9 +53,9 @@ var pgReplicationSlot = map[string]*prometheus.Desc{ func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error { rows, err := db.QueryContext(ctx, - `SELECT + `SELECT slot_name, - pg_current_wal_lsn() - '0/0' AS current_wal_lsn, + (case pg_is_in_recovery() when 't' then pg_last_wal_receive_lsn() - '0/0' else pg_current_wal_lsn() - '0/0' end) as current_wal_lsn, coalesce(confirmed_flush_lsn, '0/0') - '0/0', active FROM