Skip to content

Commit

Permalink
Checkpoint related columns in PG 17 have been moved from pg_stat_bgwr…
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Oct 7, 2024
1 parent 98f75c7 commit 9b9bbc5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 15 additions & 3 deletions collector/pg_stat_bgwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"database/sql"

"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -101,7 +102,7 @@ var (
prometheus.Labels{},
)

statBGWriterQuery = `SELECT
statBGWriterQueryBefore17 = `SELECT
checkpoints_timed
,checkpoints_req
,checkpoint_write_time
Expand All @@ -114,12 +115,23 @@ var (
,buffers_alloc
,stats_reset
FROM pg_stat_bgwriter;`

statBGWriterQueryAfter17 = `SELECT
buffers_clean
,maxwritten_clean
,buffers_alloc
,stats_reset
FROM pg_stat_bgwriter;`
)

func (PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
query := statBGWriterQueryBefore17
if instance.version.GE(semver.MustParse("17.0.0")) {
query = statBGWriterQueryAfter17
}

db := instance.getDB()
row := db.QueryRowContext(ctx,
statBGWriterQuery)
row := db.QueryRowContext(ctx, query)

var cpt, cpr, bcp, bc, mwc, bb, bbf, ba sql.NullInt64
var cpwt, cpst sql.NullFloat64
Expand Down
4 changes: 2 additions & 2 deletions collector/pg_stat_bgwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestPGStatBGWriterCollector(t *testing.T) {

rows := sqlmock.NewRows(columns).
AddRow(354, 4945, 289097744, 1242257, int64(3275602074), 89320867, 450139, 2034563757, 0, int64(2725688749), srT)
mock.ExpectQuery(sanitizeQuery(statBGWriterQuery)).WillReturnRows(rows)
mock.ExpectQuery(sanitizeQuery(statBGWriterQueryBefore17)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestPGStatBGWriterCollectorNullValues(t *testing.T) {

rows := sqlmock.NewRows(columns).
AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
mock.ExpectQuery(sanitizeQuery(statBGWriterQuery)).WillReturnRows(rows)
mock.ExpectQuery(sanitizeQuery(statBGWriterQueryBefore17)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
Expand Down

0 comments on commit 9b9bbc5

Please sign in to comment.