Fix double-free in pg_stat_autovacuum_scores.
Nathan Bossart <nathan@postgresql.org>
Fix double-free in pg_stat_autovacuum_scores. Presently, relation_needs_vacanalyze() unconditionally frees the pgstat entry returned by pgstat_fetch_stat_tabentry_ext(). This behavior was first added by commit 02502c1bca to avoid memory leakage in autovacuum. While this is fine for autovacuum since it forces stats_fetch_consistency to "none", it is not okay for other callers that use "cache" or "snapshot". This manifests as a double-free when pg_stat_autovacuum_scores is called multiple times in the same transaction. To fix, add a "bool *may_free" parameter to pgstat_fetch_stat_tabentry_ext() that returns whether it is safe for the caller to explicitly pfree() the result. If a caller would rather leave it to the memory context machinery to free the result, it can pass NULL as the "may_free" argument (or just ignore its value). Oversight in commit 87f61f0c82. Reported-by: Tender Wang <tndrwang@gmail.com> Reported-by: Alexander Lakhin <exclusion@gmail.com> Suggested-by: Andres Freund <andres@anarazel.de> Suggested-by: Tom Lane <tgl@sss.pgh.pa.us> Author: Sami Imseih <samimseih@gmail.com> Discussion: https://postgr.es/m/CAHewXNkJKdwb3D5OnksrdOqzqUnXUEMpDam1TPW0vfUkW%3D7jUw%40mail.gmail.com Discussion: https://postgr.es/m/5684f479-858e-4c5d-b8f5-bcf05de1f909%40gmail.com
Files
| Path | Change | +/− |
|---|---|---|
| src/backend/postmaster/autovacuum.c | modified | +5 −2 |
| src/backend/utils/activity/pgstat_backend.c | modified | +2 −1 |
| src/backend/utils/activity/pgstat.c | modified | +17 −1 |
| src/backend/utils/activity/pgstat_database.c | modified | +1 −1 |
| src/backend/utils/activity/pgstat_function.c | modified | +1 −1 |
| src/backend/utils/activity/pgstat_relation.c | modified | +7 −5 |
| src/backend/utils/activity/pgstat_replslot.c | modified | +2 −1 |
| src/backend/utils/activity/pgstat_subscription.c | modified | +1 −1 |
| src/include/pgstat.h | modified | +2 −1 |
| src/include/utils/pgstat_internal.h | modified | +2 −1 |
| src/test/modules/test_custom_stats/test_custom_var_stats.c | modified | +2 −1 |
Discussion
- Add pg_stat_autovacuum_priority 61 messages · 2026-03-27 → 2026-04-09
- Fix "detected double pfree in PgStat Snapshot 0x557d9926b400" error 1 message · 2026-04-08