Skip to content

Commit 4e521d4

Browse files
authored
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 <[email protected]> --------- Signed-off-by: Joe Adams <[email protected]>
1 parent 31ef4ed commit 4e521d4

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

collector/pg_stat_activity_autovacuum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var (
4545
statActivityAutovacuumQuery = `
4646
SELECT
4747
SPLIT_PART(query, '.', 2) AS relname,
48-
EXTRACT(xact_start) AS timestamp_seconds
48+
EXTRACT(EPOCH FROM xact_start) AS timestamp_seconds
4949
FROM
5050
pg_catalog.pg_stat_activity
5151
WHERE

collector/pg_stat_walreceiver.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ var (
107107
trim(both '''' from substring(conninfo from 'host=([^ ]*)')) as upstream_host,
108108
slot_name,
109109
status,
110-
(receive_start_lsn- '0/0') % (2^52)::bigint as receive_start_lsn,
110+
(receive_start_lsn- '0/0') %% (2^52)::bigint as receive_start_lsn,
111111
%s
112112
receive_start_tli,
113113
received_tli,
114114
extract(epoch from last_msg_send_time) as last_msg_send_time,
115115
extract(epoch from last_msg_receipt_time) as last_msg_receipt_time,
116-
(latest_end_lsn - '0/0') % (2^52)::bigint as latest_end_lsn,
116+
(latest_end_lsn - '0/0') %% (2^52)::bigint as latest_end_lsn,
117117
extract(epoch from latest_end_time) as latest_end_time,
118118
substring(slot_name from 'repmgr_slot_([0-9]*)') as upstream_node
119119
FROM pg_catalog.pg_stat_wal_receiver
@@ -127,14 +127,16 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
127127
return err
128128
}
129129

130-
defer hasFlushedLSNRows.Close()
131130
hasFlushedLSN := hasFlushedLSNRows.Next()
132131
var query string
133132
if hasFlushedLSN {
134133
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "(flushed_lsn - '0/0') % (2^52)::bigint as flushed_lsn,\n")
135134
} else {
136135
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "")
137136
}
137+
138+
hasFlushedLSNRows.Close()
139+
138140
rows, err := db.QueryContext(ctx, query)
139141
if err != nil {
140142
return err

collector/pg_xlog_location.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package collector
1616
import (
1717
"context"
1818

19+
"github.com/blang/semver/v4"
1920
"github.com/go-kit/log"
21+
"github.com/go-kit/log/level"
2022
"github.com/prometheus/client_golang/prometheus"
2123
)
2224

@@ -50,8 +52,17 @@ var (
5052
`
5153
)
5254

53-
func (PGXlogLocationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
55+
func (c PGXlogLocationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
5456
db := instance.getDB()
57+
58+
// xlog was renmaed to WAL in PostgreSQL 10
59+
// https://wiki.postgresql.org/wiki/New_in_postgres_10#Renaming_of_.22xlog.22_to_.22wal.22_Globally_.28and_location.2Flsn.29
60+
after10 := instance.version.Compare(semver.MustParse("10.0.0"))
61+
if after10 >= 0 {
62+
level.Warn(c.log).Log("msg", "xlog_location collector is not available on PostgreSQL >= 10.0.0, skipping")
63+
return nil
64+
}
65+
5566
rows, err := db.QueryContext(ctx,
5667
xlogLocationQuery)
5768

0 commit comments

Comments
 (0)