Skip to content

postgres: fix bg_stats query on PostgreSQL 17+ (pg_stat_checkpointer) - #449

Open
Edzilla2000 wants to merge 1 commit into
site24x7:masterfrom
Edzilla2000:fix/postgres-pg17-checkpointer
Open

postgres: fix bg_stats query on PostgreSQL 17+ (pg_stat_checkpointer)#449
Edzilla2000 wants to merge 1 commit into
site24x7:masterfrom
Edzilla2000:fix/postgres-pg17-checkpointer

Conversation

@Edzilla2000

Copy link
Copy Markdown

Problem

postgres/postgres.py queries pg_stat_bgwriter using the pre-PostgreSQL-17 column
list. PostgreSQL 17 relocated the checkpoint counters into a new pg_stat_checkpointer
view and dropped two others, so the bg_stats query fails on every collection against
PG 17 or newer:

column "checkpoints_timed" does not exist
LINE 1: SELECT checkpoints_timed,checkpoints_req,checkpoint_write_ti...
               ^

The failure is caught per-query by the except block in metricCollector(), which
appends to self._msg and rolls back. Collection continues and the monitor still
reports normally, so the breakage is not obvious from the monitor state — only from
the message field and the ten absent metrics:

Checkpoints Timed, Checkpoints Req, Checkpoint Write Time, Checkpoint Sync Time,
Buffers Checkpoint, Buffers Clean, Maxwritten Clean, Buffers Backend,
Buffers Backend Fsync, Buffers Alloc.

Note buffers_clean, maxwritten_clean and buffers_alloc do still exist in PG 17 —
they are lost only as collateral damage, because the whole SELECT fails on the first
missing column.

Unlike the activity and session queries in the same function, this query was not
version-gated.

Column mapping in PostgreSQL 17

Pre-17 (pg_stat_bgwriter) PostgreSQL 17+
checkpoints_timed pg_stat_checkpointer.num_timed
checkpoints_req pg_stat_checkpointer.num_requested
checkpoint_write_time pg_stat_checkpointer.write_time
checkpoint_sync_time pg_stat_checkpointer.sync_time
buffers_checkpoint pg_stat_checkpointer.buffers_written
buffers_backend removed — superseded by pg_stat_io
buffers_backend_fsync removed — superseded by pg_stat_io

Change

Gates str_bgStats on major version, following the style already used for
str_usageActiveStat and str_Sessions in the same function. Pre-17 behaviour is
unchanged.

The new columns are aliased back to their historical names, so the metric names the
plugin generates are identical and existing dashboards and thresholds are unaffected.

major_version as already computed in inititializeQueries() evaluates to 17 for
server_version 170010, so no extra parsing was needed.

buffers_backend and buffers_backend_fsync have no direct equivalent and are
omitted on PG 17+, so those two metrics stop appearing there. They could be
approximated from pg_stat_io (writes / fsyncs for the client backend and
background worker backend types), but that is a behavioural change rather than a
like-for-like mapping and seemed better left as a separate enhancement — happy to add
it here instead if you would prefer.

Testing

  • Validated against PostgreSQL 17.10 (PostgreSQL 17.10 on x86_64-pc-linux-gnu),
    GitLab Omnibus 19.2.0 bundled instance: the new query returns one row with all eight
    columns populated, and the msg field is clean.
  • Pre-17 branch is byte-identical to the previous query, so PostgreSQL 16 and older are
    unaffected. The same instance ran this plugin correctly on 16.13 before its upgrade.
  • python3 -m py_compile postgres/postgres.py passes.

You may also want to increment PLUGIN_VERSION (currently 1) so deployed agents pick
up the corrected script.

Related

The same PostgreSQL 17 change affected comparable projects, for reference:

  • prometheus-community/postgres_exporter — issue #1060, fixed in PR #1072
  • netdata/netdata — issue #19412
  • bitnami/charts — issue #31349

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant