Skip to content

Commit b9486ad

Browse files
committed
Refactoring
1 parent 53a399c commit b9486ad

File tree

6 files changed

+80
-171
lines changed

6 files changed

+80
-171
lines changed

collector/pg_stat_bgwriter.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,31 +216,21 @@ const statBGWriterQueryPost17 = `SELECT
216216
,stats_reset
217217
FROM pg_stat_bgwriter;`
218218

219-
const statCheckpointerQueryPre18 = `SELECT
219+
const statCheckpointerQuery = `SELECT
220220
num_timed
221221
,num_requested
222-
,NULL::bigint as num_done
222+
CASE WHEN current_setting('server_version_num')::int >= 180000
223+
THEN COALESCE(num_done, 0)
224+
ELSE 0 END as num_done,
223225
,restartpoints_timed
224226
,restartpoints_req
225227
,restartpoints_done
226228
,write_time
227229
,sync_time
228230
,buffers_written
229-
,NULL::bigint as slru_written
230-
,stats_reset
231-
FROM pg_stat_checkpointer;`
232-
233-
const statCheckpointerQuery18Plus = `SELECT
234-
num_timed
235-
,num_requested
236-
,num_done
237-
,restartpoints_timed
238-
,restartpoints_req
239-
,restartpoints_done
240-
,write_time
241-
,sync_time
242-
,buffers_written
243-
,slru_written
231+
,CASE WHEN current_setting('server_version_num')::int >= 180000
232+
THEN COALESCE(slru_written, 0)
233+
ELSE 0 END as slru_written,
244234
,stats_reset
245235
FROM pg_stat_checkpointer;`
246236

@@ -263,13 +253,7 @@ func (p PGStatBGWriterCollector) Update(ctx context.Context, instance *instance,
263253
var rpt, rpr, rpd sql.NullInt64
264254
var csr sql.NullTime
265255

266-
// Use version-specific checkpointer query for PostgreSQL 18+
267-
checkpointerQuery := statCheckpointerQueryPre18
268-
if after18 {
269-
checkpointerQuery = statCheckpointerQuery18Plus
270-
}
271-
272-
row = db.QueryRowContext(ctx, checkpointerQuery)
256+
row = db.QueryRowContext(ctx, statCheckpointerQuery)
273257
err = row.Scan(&cpt, &cpr, &cpd, &rpt, &rpr, &rpd, &cpwt, &cpst, &bcp, &slruw, &csr)
274258
if err != nil {
275259
return err

collector/pg_stat_database.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ var (
226226
prometheus.Labels{},
227227
)
228228

229-
statDatabaseQueryPrePG18 = `
229+
statDatabaseQuery = `
230230
SELECT
231231
datid
232232
,datname
@@ -247,34 +247,12 @@ var (
247247
,blk_read_time
248248
,blk_write_time
249249
,stats_reset
250-
,NULL::bigint as parallel_workers_to_launch
251-
,NULL::bigint as parallel_workers_launched
252-
FROM pg_stat_database;
253-
`
254-
255-
statDatabaseQueryPG18 = `
256-
SELECT
257-
datid
258-
,datname
259-
,numbackends
260-
,xact_commit
261-
,xact_rollback
262-
,blks_read
263-
,blks_hit
264-
,tup_returned
265-
,tup_fetched
266-
,tup_inserted
267-
,tup_updated
268-
,tup_deleted
269-
,conflicts
270-
,temp_files
271-
,temp_bytes
272-
,deadlocks
273-
,blk_read_time
274-
,blk_write_time
275-
,stats_reset
276-
,parallel_workers_to_launch
277-
,parallel_workers_launched
250+
CASE WHEN current_setting('server_version_num')::int >= 180000
251+
THEN parallel_workers_to_launch
252+
ELSE NULL END as parallel_workers_to_launch
253+
CASE WHEN current_setting('server_version_num')::int >= 180000
254+
THEN parallel_workers_launched
255+
ELSE NULL END as parallel_workers_launched
278256
FROM pg_stat_database;
279257
`
280258
)
@@ -283,13 +261,8 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
283261
db := instance.getDB()
284262

285263
after18 := instance.version.GTE(semver.Version{Major: 18})
286-
// Use version-specific query for PostgreSQL 18+
287-
query := statDatabaseQueryPrePG18
288-
if after18 {
289-
query = statDatabaseQueryPG18
290-
}
291264

292-
rows, err := db.QueryContext(ctx, query)
265+
rows, err := db.QueryContext(ctx, statDatabaseQuery)
293266
if err != nil {
294267
return err
295268
}
@@ -410,7 +383,7 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
410383
}
411384

412385
if after18 {
413-
if !parallelWorkersToLaunch.Valid && after18 {
386+
if !parallelWorkersToLaunch.Valid {
414387
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no parallel_workers_to_launch")
415388
continue
416389
}

collector/pg_stat_database_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestPGStatDatabaseCollector(t *testing.T) {
9090
2,
9191
)
9292

93-
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPG18)).WillReturnRows(rows)
93+
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
9494

9595
ch := make(chan prometheus.Metric)
9696
go func() {
@@ -221,7 +221,7 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
221221
3,
222222
2,
223223
)
224-
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPG18)).WillReturnRows(rows)
224+
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
225225

226226
ch := make(chan prometheus.Metric)
227227
go func() {
@@ -375,7 +375,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
375375
3,
376376
2,
377377
)
378-
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPG18)).WillReturnRows(rows)
378+
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
379379

380380
ch := make(chan prometheus.Metric)
381381
go func() {
@@ -499,7 +499,7 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
499499
2,
500500
)
501501

502-
mock.ExpectQuery(sanitizeQuery(statDatabaseQueryPG18)).WillReturnRows(rows)
502+
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
503503

504504
ch := make(chan prometheus.Metric)
505505
go func() {

collector/pg_stat_io.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,27 @@ var (
134134
prometheus.Labels{},
135135
)
136136

137-
// PostgreSQL 18+ query with byte statistics and WAL I/O
138-
StatIOQuery18Plus = `
137+
statIOQuery = `
139138
SELECT
140139
backend_type,
141140
io_object,
142141
io_context,
143142
reads,
144-
read_bytes,
143+
CASE WHEN current_setting('server_version_num')::int >= 180000
144+
THEN read_bytes
145+
ELSE NULL END as read_bytes,
145146
read_time,
146147
writes,
147-
write_bytes,
148+
CASE WHEN current_setting('server_version_num')::int >= 180000
149+
THEN write_bytes
150+
ELSE NULL END as write_bytes,
148151
write_time,
149152
writebacks,
150153
writeback_time,
151154
extends,
152-
extend_bytes,
155+
CASE WHEN current_setting('server_version_num')::int >= 180000
156+
THEN extend_bytes
157+
ELSE NULL END as extend_bytes,
153158
extend_time,
154159
hits,
155160
evictions,
@@ -158,31 +163,6 @@ var (
158163
fsync_time,
159164
FROM pg_stat_io
160165
`
161-
162-
// Pre-PostgreSQL 18 query without byte statistics
163-
StatIOQueryPre18 = `
164-
SELECT
165-
backend_type,
166-
io_object,
167-
io_context,
168-
reads,
169-
NULL::bigint as read_bytes,
170-
read_time,
171-
writes,
172-
NULL::bigint as write_bytes,
173-
write_time,
174-
writebacks,
175-
writeback_time,
176-
extends,
177-
NULL::numeric as extend_bytes,
178-
extend_time,
179-
hits,
180-
NULL::bigint as evictions,
181-
reueses,
182-
fsyncs
183-
fsync_time
184-
FROM pg_stat_io
185-
`
186166
)
187167

188168
func (c *PGStatIOCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
@@ -194,13 +174,8 @@ func (c *PGStatIOCollector) Update(ctx context.Context, instance *instance, ch c
194174
db := instance.getDB()
195175

196176
after18 := instance.version.GTE(semver.Version{Major: 18})
197-
// Use version-specific query for PostgreSQL 18+
198-
query := StatIOQueryPre18
199-
if after18 {
200-
query = StatIOQuery18Plus
201-
}
202177

203-
rows, err := db.QueryContext(ctx, query)
178+
rows, err := db.QueryContext(ctx, statIOQuery)
204179
if err != nil {
205180
return err
206181
}

collector/pg_stat_user_tables.go

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ var (
182182
prometheus.Labels{},
183183
)
184184

185-
statUserTablesQueryPrePG18 = `SELECT
185+
statUserTablesQuery = `SELECT
186186
current_database() datname,
187187
schemaname,
188188
relname,
@@ -206,41 +206,18 @@ var (
206206
analyze_count,
207207
autoanalyze_count,
208208
pg_total_relation_size(relid) as total_size,
209-
NULL::double precision as total_vacuum_time,
210-
NULL::double precision as total_autovacuum_time,
211-
NULL::double precision as total_analyze_time,
212-
NULL::double precision as total_autoanalyze_time
213-
FROM
214-
pg_stat_user_tables`
215-
216-
statUserTablesQueryPG18 = `SELECT
217-
current_database() datname,
218-
schemaname,
219-
relname,
220-
seq_scan,
221-
seq_tup_read,
222-
idx_scan,
223-
idx_tup_fetch,
224-
n_tup_ins,
225-
n_tup_upd,
226-
n_tup_del,
227-
n_tup_hot_upd,
228-
n_live_tup,
229-
n_dead_tup,
230-
n_mod_since_analyze,
231-
COALESCE(last_vacuum, '1970-01-01Z') as last_vacuum,
232-
COALESCE(last_autovacuum, '1970-01-01Z') as last_autovacuum,
233-
COALESCE(last_analyze, '1970-01-01Z') as last_analyze,
234-
COALESCE(last_autoanalyze, '1970-01-01Z') as last_autoanalyze,
235-
vacuum_count,
236-
autovacuum_count,
237-
analyze_count,
238-
autoanalyze_count,
239-
pg_total_relation_size(relid) as total_size,
240-
total_vacuum_time,
241-
total_autovacuum_time,
242-
total_analyze_time,
243-
total_autoanalyze_time
209+
CASE WHEN current_setting('server_version_num')::int >= 180000
210+
THEN total_vacuum_time
211+
ELSE NULL precision END as total_vacuum_time,
212+
CASE WHEN current_setting('server_version_num')::int >= 180000
213+
THEN total_autovacuum_time
214+
ELSE NULL precision END as total_autovacuum_time,
215+
CASE WHEN current_setting('server_version_num')::int >= 180000
216+
THEN total_analyze_time
217+
ELSE NULL precision END as total_analyze_time,
218+
CASE WHEN current_setting('server_version_num')::int >= 180000
219+
THEN total_autoanalyze_time
220+
ELSE NULL precision END as total_autoanalyze_time
244221
FROM
245222
pg_stat_user_tables`
246223
)
@@ -249,13 +226,8 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
249226
db := instance.getDB()
250227

251228
after18 := instance.version.GTE(semver.Version{Major: 18})
252-
// Use version-specific query for PostgreSQL 18+
253-
query := statUserTablesQueryPrePG18
254-
if after18 {
255-
query = statUserTablesQueryPG18
256-
}
257229

258-
rows, err := db.QueryContext(ctx, query)
230+
rows, err := db.QueryContext(ctx, statUserTablesQuery)
259231

260232
if err != nil {
261233
return err
@@ -508,33 +480,38 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
508480

509481
if after18 {
510482
// PostgreSQL 18+ vacuum/analyze timing metrics
511-
ch <- prometheus.MustNewConstMetric(
512-
statUserTablesTotalVacuumTime,
513-
prometheus.CounterValue,
514-
totalVacuumTime.Float64,
515-
datnameLabel, schemanameLabel, relnameLabel,
516-
)
517-
518-
ch <- prometheus.MustNewConstMetric(
519-
statUserTablesTotalAutovacuumTime,
520-
prometheus.CounterValue,
521-
totalAutovacuumTime.Float64,
522-
datnameLabel, schemanameLabel, relnameLabel,
523-
)
524-
525-
ch <- prometheus.MustNewConstMetric(
526-
statUserTablesTotalAnalyzeTime,
527-
prometheus.CounterValue,
528-
totalAnalyzeTime.Float64,
529-
datnameLabel, schemanameLabel, relnameLabel,
530-
)
531-
532-
ch <- prometheus.MustNewConstMetric(
533-
statUserTablesTotalAutoanalyzeTime,
534-
prometheus.CounterValue,
535-
totalAutoanalyzeTime.Float64,
536-
datnameLabel, schemanameLabel, relnameLabel,
537-
)
483+
if totalVacuumTime.Valid {
484+
ch <- prometheus.MustNewConstMetric(
485+
statUserTablesTotalVacuumTime,
486+
prometheus.CounterValue,
487+
totalVacuumTime.Float64,
488+
datnameLabel, schemanameLabel, relnameLabel,
489+
)
490+
}
491+
if totalAutovacuumTime.Valid {
492+
ch <- prometheus.MustNewConstMetric(
493+
statUserTablesTotalAutovacuumTime,
494+
prometheus.CounterValue,
495+
totalAutovacuumTime.Float64,
496+
datnameLabel, schemanameLabel, relnameLabel,
497+
)
498+
}
499+
if totalAnalyzeTime.Valid {
500+
ch <- prometheus.MustNewConstMetric(
501+
statUserTablesTotalAnalyzeTime,
502+
prometheus.CounterValue,
503+
totalAnalyzeTime.Float64,
504+
datnameLabel, schemanameLabel, relnameLabel,
505+
)
506+
}
507+
if totalAutoanalyzeTime.Valid {
508+
ch <- prometheus.MustNewConstMetric(
509+
statUserTablesTotalAutoanalyzeTime,
510+
prometheus.CounterValue,
511+
totalAutoanalyzeTime.Float64,
512+
datnameLabel, schemanameLabel, relnameLabel,
513+
)
514+
}
538515
}
539516
}
540517

0 commit comments

Comments
 (0)