From 6356f4d29bb87cfedaddf6667396b329daa9d1ef Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 26 Nov 2026 13:41:52 -0500 Subject: [PATCH 3/4] Release references to dropped stats entries before writing stats file The checkpointer will hold references to tablespace entries, and may not look up one again before it shuts down. --- src/backend/utils/activity/pgstat.c | 8 ++++++++ src/backend/utils/activity/pgstat_shmem.c | 6 ++---- src/include/utils/pgstat_internal.h | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c index 0f150cd2363..1f55c2be673 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -634,6 +634,14 @@ pgstat_before_server_shutdown(int code, Datum arg) /* flush out our own pending changes before writing out */ pgstat_report_stat(true); + /* + * Release our references to the entries that have been dropped. That + * otherwise only happens when looking up an entry, which this process may + * not do again, and the stats file is written from the entries left. + */ + if (pgstat_need_entry_refs_gc()) + pgstat_gc_entry_refs(); + /* * Only write out file during normal shutdown. Don't even signal that * we've shutdown during irregular shutdowns, because the shutdown diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c index 8db33ac2fdf..38c17710798 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -50,8 +50,6 @@ static void pgstat_drop_database_and_contents(Oid dboid); static void pgstat_free_entry(PgStatShared_HashEntry *shent, dshash_seq_status *hstat); static void pgstat_release_entry_ref(PgStat_HashKey key, PgStat_EntryRef *entry_ref, bool discard_pending); -static bool pgstat_need_entry_refs_gc(void); -static void pgstat_gc_entry_refs(void); static void pgstat_release_all_entry_refs(bool discard_pending); typedef bool (*ReleaseMatchCB) (PgStat_EntryRefHashEntry *, Datum data); static void pgstat_release_matching_entry_refs(bool discard_pending, ReleaseMatchCB match, Datum match_data); @@ -898,7 +896,7 @@ pgstat_request_entry_refs_gc(void) pg_atomic_fetch_add_u64(&pgStatLocal.shmem->gc_request_count, 1); } -static bool +bool pgstat_need_entry_refs_gc(void) { uint64 curage; @@ -914,7 +912,7 @@ pgstat_need_entry_refs_gc(void) return pgStatSharedRefAge != curage; } -static void +void pgstat_gc_entry_refs(void) { pgstat_entry_ref_hash_iterator i; diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h index 208ea083200..7204b84884f 100644 --- a/src/include/utils/pgstat_internal.h +++ b/src/include/utils/pgstat_internal.h @@ -861,6 +861,8 @@ extern void pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEnt TimestampTz ts); extern void pgstat_request_entry_refs_gc(void); +extern bool pgstat_need_entry_refs_gc(void); +extern void pgstat_gc_entry_refs(void); extern dsa_pointer pgstat_alloc_entry_body(PgStat_Kind kind); extern PgStatShared_Common *pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent, -- 2.37.1 (Apple Git-137.1)