Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds pg_stat_progress_vacuum collector #1141

Merged

Conversation

ianbibby
Copy link
Contributor

@ianbibby ianbibby commented Mar 26, 2025

This pull request adds a new collector that exposes metrics from pg_stat_progress_vacuum, enabling visibility into in-progress VACUUM operations. The collected metrics include:

  • Current phase of the vacuum (folllowing the State Set pattern)
  • Heap blocks scanned / vacuumed
  • Index vacuum cycle count
  • Dead tuple counts and limits

This collector queries the underlying function pg_stat_get_progress_info('VACUUM'::text) directly rather than using the pg_stat_progress_vacuum view.

Testing

  • Added unit tests, following the same structure as existing collectors (e.g. pg_stat_user_tables_test.go)
  • Manually tested against live PostgreSQL instances with vacuum activity, using controlled workloads to generate observable vacuum progress
  • Verified that metrics appear and update correctly on the Prometheus /metrics endpoint

Example output:

# HELP pg_stat_progress_vacuum_heap_blks Total number of heap blocks in the table being vacuumed.
# TYPE pg_stat_progress_vacuum_heap_blks gauge
pg_stat_progress_vacuum_heap_blks{datname="postgres",relname="foo"} 638708
# HELP pg_stat_progress_vacuum_heap_blks_scanned Number of heap blocks scanned so far.
# TYPE pg_stat_progress_vacuum_heap_blks_scanned gauge
pg_stat_progress_vacuum_heap_blks_scanned{datname="postgres",relname="foo"} 8448
# HELP pg_stat_progress_vacuum_heap_blks_vacuumed Number of heap blocks vacuumed so far.
# TYPE pg_stat_progress_vacuum_heap_blks_vacuumed gauge
pg_stat_progress_vacuum_heap_blks_vacuumed{datname="postgres",relname="foo"} 0
# HELP pg_stat_progress_vacuum_index_vacuums Number of completed index vacuum cycles.
# TYPE pg_stat_progress_vacuum_index_vacuums gauge
pg_stat_progress_vacuum_index_vacuums{datname="postgres",relname="foo"} 0
# HELP pg_stat_progress_vacuum_max_dead_tuples Maximum number of dead tuples that can be stored before cleanup is performed.
# TYPE pg_stat_progress_vacuum_max_dead_tuples gauge
pg_stat_progress_vacuum_max_dead_tuples{datname="postgres",relname="foo"} 1.1184809e+07
# HELP pg_stat_progress_vacuum_num_dead_tuples Current number of dead tuples found so far.
# TYPE pg_stat_progress_vacuum_num_dead_tuples gauge
pg_stat_progress_vacuum_num_dead_tuples{datname="postgres",relname="foo"} 228096
# HELP pg_stat_progress_vacuum_phase Current vacuum phase (1 = active, 0 = inactive). Label 'phase' is human-readable.
# TYPE pg_stat_progress_vacuum_phase gauge
pg_stat_progress_vacuum_phase{datname="postgres",phase="cleaning up indexes",relname="foo"} 0
pg_stat_progress_vacuum_phase{datname="postgres",phase="initializing",relname="foo"} 0
pg_stat_progress_vacuum_phase{datname="postgres",phase="performing final cleanup",relname="foo"} 0
pg_stat_progress_vacuum_phase{datname="postgres",phase="scanning heap",relname="foo"} 1
pg_stat_progress_vacuum_phase{datname="postgres",phase="truncating heap",relname="foo"} 0
pg_stat_progress_vacuum_phase{datname="postgres",phase="vacuuming heap",relname="foo"} 0
pg_stat_progress_vacuum_phase{datname="postgres",phase="vacuuming indexes",relname="foo"} 0

Compatibility

  • This collector supports PostgreSQL versions 9.6 through 16.
  • The param mappings used from pg_stat_get_progress_info are consistent across these versions

Postgres 17 support

PostgreSQL 17 introduces breaking changes (pgpedia):

  • max_dead_tuples and num_dead_tuples were removed
  • Replaced by max_dead_tuple_bytes, dead_tuple_bytes, and num_dead_item_ids

This collector is not currently compatible with Postgres 17, but this is a known limitation. A future update can address this by adjusting param mappings based on the server version.

Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

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

A couple of minor nits, and one feature suggestion.

Otherwise, looking good.

@SuperQ
Copy link
Contributor

SuperQ commented Mar 26, 2025

This needs a DCO sign-off. You can use git commit -s --amend to add it.

@ianbibby ianbibby force-pushed the bibby_pg_stat_progress_vacuum branch from b47ef94 to 780dc68 Compare March 26, 2025 19:17
README.md Outdated
@@ -144,6 +144,9 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
* `[no-]collector.stat_database`
Enable the `stat_database` collector (default: enabled).

* `[no-]collector.stat_progress_vacuum`
Enable the `stat_progress_vacuum` collector (default: disabled).
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about making this one enabled by default. Are vacuum stats useful enough for most users?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think enabling this by default is acceptable here. I'll make it so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done.

@ianbibby ianbibby requested a review from SuperQ March 27, 2025 06:22
Co-authored-by: Ben Kochie <superq@gmail.com>
Signed-off-by: Ian Bibby <ian.bibby@reddit.com>
@ianbibby ianbibby force-pushed the bibby_pg_stat_progress_vacuum branch from 9b9866b to a50603f Compare March 27, 2025 06:48
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

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

LGTM

@SuperQ SuperQ merged commit 9e86f1e into prometheus-community:master Apr 3, 2025
11 checks passed
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.

2 participants