From be93bff4083b9034d0a7e0bab3036c71ae75d09f Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Tue, 12 Sep 2023 21:53:29 -0400 Subject: [PATCH 1/2] Fix bugs mentioned in #908 These collectors are disabled by default, so unless enabled, they are not tested regularly. Signed-off-by: Joe Adams --- collector/pg_stat_activity_autovacuum.go | 2 +- collector/pg_stat_walreceiver.go | 4 ++-- collector/pg_xlog_location.go | 13 ++++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/collector/pg_stat_activity_autovacuum.go b/collector/pg_stat_activity_autovacuum.go index 5e2d2d2ca..6cf8cdcec 100644 --- a/collector/pg_stat_activity_autovacuum.go +++ b/collector/pg_stat_activity_autovacuum.go @@ -45,7 +45,7 @@ var ( statActivityAutovacuumQuery = ` SELECT SPLIT_PART(query, '.', 2) AS relname, - EXTRACT(xact_start) AS timestamp_seconds + EXTRACT(EPOCH FROM xact_start) AS timestamp_seconds FROM pg_catalog.pg_stat_activity WHERE diff --git a/collector/pg_stat_walreceiver.go b/collector/pg_stat_walreceiver.go index 3134c025b..245571a07 100644 --- a/collector/pg_stat_walreceiver.go +++ b/collector/pg_stat_walreceiver.go @@ -107,13 +107,13 @@ var ( trim(both '''' from substring(conninfo from 'host=([^ ]*)')) as upstream_host, slot_name, status, - (receive_start_lsn- '0/0') % (2^52)::bigint as receive_start_lsn, + (receive_start_lsn- '0/0') %% (2^52)::bigint as receive_start_lsn, %s receive_start_tli, received_tli, extract(epoch from last_msg_send_time) as last_msg_send_time, extract(epoch from last_msg_receipt_time) as last_msg_receipt_time, - (latest_end_lsn - '0/0') % (2^52)::bigint as latest_end_lsn, + (latest_end_lsn - '0/0') %% (2^52)::bigint as latest_end_lsn, extract(epoch from latest_end_time) as latest_end_time, substring(slot_name from 'repmgr_slot_([0-9]*)') as upstream_node FROM pg_catalog.pg_stat_wal_receiver diff --git a/collector/pg_xlog_location.go b/collector/pg_xlog_location.go index 92ac44acb..237204f7d 100644 --- a/collector/pg_xlog_location.go +++ b/collector/pg_xlog_location.go @@ -16,7 +16,9 @@ package collector import ( "context" + "github.com/blang/semver/v4" "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" ) @@ -50,8 +52,17 @@ var ( ` ) -func (PGXlogLocationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { +func (c PGXlogLocationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { db := instance.getDB() + + // xlog was renmaed to WAL in PostgreSQL 10 + // https://wiki.postgresql.org/wiki/New_in_postgres_10#Renaming_of_.22xlog.22_to_.22wal.22_Globally_.28and_location.2Flsn.29 + after10 := instance.version.Compare(semver.MustParse("10.0.0")) + if after10 >= 0 { + level.Warn(c.log).Log("msg", "xlog_location collector is not available on PostgreSQL >= 10.0.0, skipping") + return nil + } + rows, err := db.QueryContext(ctx, xlogLocationQuery) From d2a0225959b88062f49982807694b3e3a8df7549 Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Wed, 13 Sep 2023 08:59:29 -0400 Subject: [PATCH 2/2] Fix db rows.Close in pg_stat_walreceiver Signed-off-by: Joe Adams --- collector/pg_stat_walreceiver.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/collector/pg_stat_walreceiver.go b/collector/pg_stat_walreceiver.go index 245571a07..db533ab55 100644 --- a/collector/pg_stat_walreceiver.go +++ b/collector/pg_stat_walreceiver.go @@ -127,7 +127,6 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta return err } - defer hasFlushedLSNRows.Close() hasFlushedLSN := hasFlushedLSNRows.Next() var query string if hasFlushedLSN { @@ -135,6 +134,9 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta } else { query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "") } + + hasFlushedLSNRows.Close() + rows, err := db.QueryContext(ctx, query) if err != nil { return err