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>
This commit is contained in:
Joe Adams 2023-09-13 09:19:21 -04:00 committed by GitHub
parent 31ef4ed5a2
commit 4e521d460e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -45,7 +45,7 @@ var (
statActivityAutovacuumQuery = ` statActivityAutovacuumQuery = `
SELECT SELECT
SPLIT_PART(query, '.', 2) AS relname, SPLIT_PART(query, '.', 2) AS relname,
EXTRACT(xact_start) AS timestamp_seconds EXTRACT(EPOCH FROM xact_start) AS timestamp_seconds
FROM FROM
pg_catalog.pg_stat_activity pg_catalog.pg_stat_activity
WHERE WHERE

View File

@ -107,13 +107,13 @@ var (
trim(both '''' from substring(conninfo from 'host=([^ ]*)')) as upstream_host, trim(both '''' from substring(conninfo from 'host=([^ ]*)')) as upstream_host,
slot_name, slot_name,
status, 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 %s
receive_start_tli, receive_start_tli,
received_tli, received_tli,
extract(epoch from last_msg_send_time) as last_msg_send_time, extract(epoch from last_msg_send_time) as last_msg_send_time,
extract(epoch from last_msg_receipt_time) as last_msg_receipt_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, extract(epoch from latest_end_time) as latest_end_time,
substring(slot_name from 'repmgr_slot_([0-9]*)') as upstream_node substring(slot_name from 'repmgr_slot_([0-9]*)') as upstream_node
FROM pg_catalog.pg_stat_wal_receiver FROM pg_catalog.pg_stat_wal_receiver
@ -127,7 +127,6 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
return err return err
} }
defer hasFlushedLSNRows.Close()
hasFlushedLSN := hasFlushedLSNRows.Next() hasFlushedLSN := hasFlushedLSNRows.Next()
var query string var query string
if hasFlushedLSN { if hasFlushedLSN {
@ -135,6 +134,9 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
} else { } else {
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "") query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "")
} }
hasFlushedLSNRows.Close()
rows, err := db.QueryContext(ctx, query) rows, err := db.QueryContext(ctx, query)
if err != nil { if err != nil {
return err return err

View File

@ -16,7 +16,9 @@ package collector
import ( import (
"context" "context"
"github.com/blang/semver/v4"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "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() 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, rows, err := db.QueryContext(ctx,
xlogLocationQuery) xlogLocationQuery)