From de2e4108632f27fceae0a4a5f1e5af8bb473abe8 Mon Sep 17 00:00:00 2001 From: Jean Cardona Date: Wed, 5 Aug 2026 11:27:57 -0600 Subject: [PATCH] postgres: fix bg_stats query on PostgreSQL 17+ PostgreSQL 17 moved the checkpoint counters from pg_stat_bgwriter to the new pg_stat_checkpointer view and dropped buffers_backend and buffers_backend_fsync, so the ungated bg_stats query failed on every collection against PG 17+ with 'column "checkpoints_timed" does not exist', losing all ten metrics in that query. Gate the query on major version, matching the existing style for str_usageActiveStat and str_Sessions. New columns are aliased to their historical names so generated metric names are unchanged. Pre-17 behaviour is untouched. Validated against PostgreSQL 17.10. --- postgres/postgres.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/postgres/postgres.py b/postgres/postgres.py index 3467d70c..fbdf7de5 100644 --- a/postgres/postgres.py +++ b/postgres/postgres.py @@ -124,7 +124,12 @@ def inititializeQueries(): str_MaxConn = "select setting::int max_connections from pg_settings where name=$$max_connections$$;" str_dbStats = "SELECT sum(numbackends) as active_connections, sum(xact_commit) as total_commits, sum(xact_rollback) as total_rollbacks, sum(conflicts) as total_conflicts FROM pg_stat_database;" str_iostats = "SELECT sum(tup_inserted) as total_rows_inserted, sum(tup_updated) as total_rows_updated ,sum(tup_deleted) as total_rows_deleted, sum(tup_fetched) as total_rows_fetched ,sum(tup_returned) as total_rows_returned,sum(blks_read) as total_block_reads , sum(blks_hit) as total_block_hits FROM pg_stat_database;" - str_bgStats = "SELECT checkpoints_timed,checkpoints_req,checkpoint_write_time,checkpoint_sync_time,buffers_checkpoint,buffers_clean,maxwritten_clean,buffers_backend,buffers_backend_fsync,buffers_alloc FROM pg_stat_bgwriter;" + if major_version >= 17: + # PG17 moved the checkpoint counters to pg_stat_checkpointer; aliased back to + # their historical names so reported metric names stay unchanged. + str_bgStats = "SELECT c.num_timed AS checkpoints_timed, c.num_requested AS checkpoints_req, c.write_time AS checkpoint_write_time, c.sync_time AS checkpoint_sync_time, c.buffers_written AS buffers_checkpoint, b.buffers_clean, b.maxwritten_clean, b.buffers_alloc FROM pg_stat_checkpointer c, pg_stat_bgwriter b;" + else: + str_bgStats = "SELECT checkpoints_timed,checkpoints_req,checkpoint_write_time,checkpoint_sync_time,buffers_checkpoint,buffers_clean,maxwritten_clean,buffers_backend,buffers_backend_fsync,buffers_alloc FROM pg_stat_bgwriter;" str_idxStats = "SELECT sum(idx_scan) as index_scans,sum(idx_tup_read) as index_rows_read, sum(idx_tup_fetch) as index_rows_fetched FROM pg_stat_user_indexes;" str_uptime ="SELECT FLOOR(EXTRACT(EPOCH FROM current_timestamp - pg_postmaster_start_time())) as uptime;" str_databaseCount = "SELECT COUNT(*) AS database_count FROM pg_database WHERE datname <> 'template0' AND datname <> 'template1';"