From 4e521d460e1ed6fa963c2266c8ee02ba519acf6d Mon Sep 17 00:00:00 2001
From: Joe Adams <github@joeadams.io>
Date: Wed, 13 Sep 2023 09:19:21 -0400
Subject: [PATCH] Fix bugs mentioned in #908 (#910)

* Fix bugs mentioned in #908

These collectors are disabled by default, so unless enabled, they are not tested regularly.

Signed-off-by: Joe Adams <github@joeadams.io>

---------

Signed-off-by: Joe Adams <github@joeadams.io>
---
 collector/pg_stat_activity_autovacuum.go |  2 +-
 collector/pg_stat_walreceiver.go         |  8 +++++---
 collector/pg_xlog_location.go            | 13 ++++++++++++-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/collector/pg_stat_activity_autovacuum.go b/collector/pg_stat_activity_autovacuum.go
index 5e2d2d2c..6cf8cdce 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 3134c025..db533ab5 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
@@ -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
diff --git a/collector/pg_xlog_location.go b/collector/pg_xlog_location.go
index 92ac44ac..237204f7 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)