Skip to content

Add a collector for stat_wal#1167

Open
sfc-gh-pnuttall wants to merge 1 commit into
prometheus-community:masterfrom
Snowflake-Labs:stat_wal
Open

Add a collector for stat_wal#1167
sfc-gh-pnuttall wants to merge 1 commit into
prometheus-community:masterfrom
Snowflake-Labs:stat_wal

Conversation

@sfc-gh-pnuttall

Copy link
Copy Markdown
Contributor

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.

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>
@SuperQ SuperQ changed the title Add a collector for stat_wal. Add a collector for stat_wal Jul 9, 2025
Comment thread collector/pg_stat_wal.go
db := instance.getDB()

columns := []string{
"wal_records", // bigint

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PGStatWALCollector to scrape pg_stat_wal fields and export them as Prometheus metrics.
  • Adds unit tests using sqlmock to 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.

Comment thread collector/pg_stat_wal.go
defer rows.Close()

for rows.Next() {
var walRecords, walFPI, walBytes, walBuffersFull, walWrite, walSync sql.NullInt64
Comment thread collector/pg_stat_wal.go
prometheus.CounterValue,
resetMetric,
)
}
Comment thread collector/pg_stat_wal.go
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 thread collector/pg_stat_wal.go
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"),
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.

3 participants