Add a collector for stat_wal#1167
Open
sfc-gh-pnuttall wants to merge 1 commit into
Open
Conversation
Docs: https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-WAL-VIEW We use this collector in production and have done so for some time. Signed-off-by: Peter Nuttall <peter.nuttall@snowflake.com>
86a7b41 to
21cae0e
Compare
sysadmind
reviewed
May 13, 2026
| db := instance.getDB() | ||
|
|
||
| columns := []string{ | ||
| "wal_records", // bigint |
Contributor
There was a problem hiding this comment.
I think this list was probably valid for 17, but it looks like they took some away in 18. https://www.postgresql.org/docs/18/monitoring-stats.html#MONITORING-PG-STAT-WAL-VIEW
There was a problem hiding this comment.
Pull request overview
Adds a new Prometheus collector for PostgreSQL’s pg_stat_wal view, extending the exporter’s built-in monitoring coverage for WAL generation and I/O timing.
Changes:
- Introduces
PGStatWALCollectorto scrapepg_stat_walfields and export them as Prometheus metrics. - Adds unit tests using
sqlmockto validate metric emission for populated and NULL-valued rows.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| collector/pg_stat_wal.go | New collector implementation querying pg_stat_wal and exporting WAL-related metrics. |
| collector/pg_stat_wal_test.go | Tests for the new collector, including normal and NULL-value scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| defer rows.Close() | ||
|
|
||
| for rows.Next() { | ||
| var walRecords, walFPI, walBytes, walBuffersFull, walWrite, walSync sql.NullInt64 |
| prometheus.CounterValue, | ||
| resetMetric, | ||
| ) | ||
| } |
Comment on lines
+106
to
+126
| func (c *PGStatWALCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { | ||
| db := instance.getDB() | ||
|
|
||
| columns := []string{ | ||
| "wal_records", // bigint | ||
| "wal_fpi", // bigint | ||
| "wal_bytes", // numeric | ||
| "wal_buffers_full", // bigint | ||
| "wal_write", // bigint | ||
| "wal_sync", // bigint | ||
| "wal_write_time", // double precision | ||
| "wal_sync_time", // double precision | ||
| "stats_reset", // timestamp with time zone | ||
| } | ||
|
|
||
| rows, err := db.QueryContext(ctx, | ||
| statWALQuery(columns), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } |
Comment on lines
+47
to
+68
| prometheus.BuildFQName(namespace, statWALSubsystem, "wal_fpi"), | ||
| "Total number of WAL full page images generated", | ||
| []string{}, | ||
| prometheus.Labels{}, | ||
| ) | ||
|
|
||
| var statsWALBytesDesc = prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, statWALSubsystem, "wal_bytes"), | ||
| "Total amount of WAL generated in bytes", | ||
| []string{}, | ||
| prometheus.Labels{}, | ||
| ) | ||
|
|
||
| var statsWALBuffersFullDesc = prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, statWALSubsystem, "wal_buffers_full"), | ||
| "Number of times WAL data was written to disk because WAL buffers became full", | ||
| []string{}, | ||
| prometheus.Labels{}, | ||
| ) | ||
|
|
||
| var statsWALWriteDesc = prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, statWALSubsystem, "wal_write"), |
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.
Docs: https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-WAL-VIEW
We use this collector in production and have done so for some time.