@@ -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