Skip to content

Fix bugs mentioned in #908 #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collector/pg_stat_activity_autovacuum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions collector/pg_stat_walreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are used in fmt.Sprintf below and I think the % is causing the problem. I can't actually get this collector to finish running the query against my local docker.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to reproduce this issue, and I found that calling hasFlushedLSNRows.Close() before running the second query fixes it. Presumably there is some deadlock waiting for one cursor to be closed before opening another on the same connection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth switching these to prepared statements.

(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
Expand All @@ -127,14 +127,16 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
return err
}

defer hasFlushedLSNRows.Close()
hasFlushedLSN := hasFlushedLSNRows.Next()
var query string
if hasFlushedLSN {
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "(flushed_lsn - '0/0') % (2^52)::bigint as flushed_lsn,\n")
} else {
query = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "")
}

hasFlushedLSNRows.Close()

rows, err := db.QueryContext(ctx, query)
if err != nil {
return err
Expand Down
13 changes: 12 additions & 1 deletion collector/pg_xlog_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand Down