postgres: fix bg_stats query on PostgreSQL 17+ (pg_stat_checkpointer) - #449
Open
Edzilla2000 wants to merge 1 commit into
Open
postgres: fix bg_stats query on PostgreSQL 17+ (pg_stat_checkpointer)#449Edzilla2000 wants to merge 1 commit into
Edzilla2000 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
postgres/postgres.pyqueriespg_stat_bgwriterusing the pre-PostgreSQL-17 columnlist. PostgreSQL 17 relocated the checkpoint counters into a new
pg_stat_checkpointerview and dropped two others, so the
bg_statsquery fails on every collection againstPG 17 or newer:
The failure is caught per-query by the
exceptblock inmetricCollector(), whichappends to
self._msgand rolls back. Collection continues and the monitor stillreports 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_cleanandbuffers_allocdo still exist in PG 17 —they are lost only as collateral damage, because the whole
SELECTfails on the firstmissing column.
Unlike the activity and session queries in the same function, this query was not
version-gated.
Column mapping in PostgreSQL 17
pg_stat_bgwriter)checkpoints_timedpg_stat_checkpointer.num_timedcheckpoints_reqpg_stat_checkpointer.num_requestedcheckpoint_write_timepg_stat_checkpointer.write_timecheckpoint_sync_timepg_stat_checkpointer.sync_timebuffers_checkpointpg_stat_checkpointer.buffers_writtenbuffers_backendpg_stat_iobuffers_backend_fsyncpg_stat_ioChange
Gates
str_bgStatson major version, following the style already used forstr_usageActiveStatandstr_Sessionsin the same function. Pre-17 behaviour isunchanged.
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_versionas already computed ininititializeQueries()evaluates to17forserver_version170010, so no extra parsing was needed.buffers_backendandbuffers_backend_fsynchave no direct equivalent and areomitted on PG 17+, so those two metrics stop appearing there. They could be
approximated from
pg_stat_io(writes/fsyncsfor theclient backendandbackground workerbackend types), but that is a behavioural change rather than alike-for-like mapping and seemed better left as a separate enhancement — happy to add
it here instead if you would prefer.
Testing
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
msgfield is clean.unaffected. The same instance ran this plugin correctly on 16.13 before its upgrade.
python3 -m py_compile postgres/postgres.pypasses.You may also want to increment
PLUGIN_VERSION(currently1) so deployed agents pickup the corrected script.
Related
The same PostgreSQL 17 change affected comparable projects, for reference: