From e330f9256c1e686f8e6438db87575cf31efc475b Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Fri, 20 Nov 2026 11:11:40 -0800 Subject: [PATCH v1] Allocate pgstats entry body before insert into shared hashtable pgstat_get_entry_ref() inserted a new entry into the shared hashtable, then allocated its body with DSA_ALLOC_NO_OOM. When the DSA needs a new segment, dsm_create() raises an ERROR instead of returning, as when posix_fallocate() fails with ENOSPC on /dev/shm. That ERROR escaped with the entry already live and its body set to InvalidDsaPointer, and the next process to use the entry crashed: an autovacuum worker in pgstat_fetch_entry(), a backend flushing its pending stats, or the checkpointer writing the stats file at shutdown. This is a back-patch of d9de60c5e4 for REL_17_STABLE. The body is now allocated with pgstat_alloc_entry_body() before the entry is inserted, and pgstat_init_entry_ext() links it to the entry. If a concurrent insert wins, the body is freed. pgstat_read_statsfile() does the same. Unlike on master, pgstat_init_entry() keeps its signature and behavior, since pgstat_internal.h exports it, and core no longer calls it. Author: Yuriy Grigoryev Author: Michael Paquier Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain --- src/backend/utils/activity/pgstat.c | 28 +++++--- src/backend/utils/activity/pgstat_shmem.c | 84 ++++++++++++++++------- src/include/utils/pgstat_internal.h | 4 ++ 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c index b7cf6fa88f..42cda5ade5 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -1599,6 +1599,7 @@ pgstat_read_statsfile(void) PgStat_HashKey key; PgStatShared_HashEntry *p; PgStatShared_Common *header; + dsa_pointer chunk; CHECK_FOR_INTERRUPTS(); @@ -1646,30 +1647,35 @@ pgstat_read_statsfile(void) * This intentionally doesn't use pgstat_get_entry_ref() - * putting all stats into checkpointer's * pgStatEntryRefHash would be wasted effort and memory. + * + * Allocate the DSA body before inserting the hash entry. */ + chunk = pgstat_alloc_entry_body(key.kind); + if (chunk == InvalidDsaPointer) + { + /* + * It would be tempting to switch this ERROR to a + * WARNING, but it would mean that all the statistics + * are discarded when the environment fails on OOM. + */ + elog(ERROR, "could not allocate entry %d/%u/%u", + key.kind, key.dboid, key.objoid); + } + p = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &found); /* don't allow duplicate entries */ if (found) { dshash_release_lock(pgStatLocal.shared_hash, p); + dsa_free(pgStatLocal.dsa, chunk); elog(WARNING, "found duplicate stats entry %d/%u/%u", key.kind, key.dboid, key.objoid); goto error; } - header = pgstat_init_entry(key.kind, p); + header = pgstat_init_entry_ext(key.kind, p, chunk); dshash_release_lock(pgStatLocal.shared_hash, p); - if (header == NULL) - { - /* - * It would be tempting to switch this ERROR to a - * WARNING, but it would mean that all the statistics - * are discarded when the environment fails on OOM. - */ - elog(ERROR, "could not allocate entry %d/%u/%u", - key.kind, key.dboid, key.objoid); - } if (!read_chunk(fpin, pgstat_get_entry_data(key.kind, header), diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c index 06be884fba..1a317d5bb7 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -263,21 +263,62 @@ pgstat_detach_shmem(void) * ------------------------------------------------------------ */ +/* + * Allocate the DSA body for a new variable-numbered pgstats entry. + * + * Returns InvalidDsaPointer in the event of an allocation failure. + */ +dsa_pointer +pgstat_alloc_entry_body(PgStat_Kind kind) +{ + const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind); + + return dsa_allocate_extended(pgStatLocal.dsa, + kind_info->shared_size, + DSA_ALLOC_ZERO | DSA_ALLOC_NO_OOM); +} + /* * Initialize entry newly-created. * * Returns NULL in the event of an allocation failure, so as callers can * take cleanup actions as the entry initialized is already inserted in the * shared hashtable. + * + * This allocates the body after the entry has been inserted, so an ERROR + * raised while allocating it, as when the DSA needs a new segment that + * cannot be created, leaves a live entry without a body. It is kept for + * compatibility; the callers in core allocate the body first with + * pgstat_alloc_entry_body() and use pgstat_init_entry_ext(). */ PgStatShared_Common * pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent) { - /* Create new stats entry. */ dsa_pointer chunk; + + chunk = pgstat_alloc_entry_body(kind); + if (chunk == InvalidDsaPointer) + return NULL; + + return pgstat_init_entry_ext(kind, shhashent, chunk); +} + +/* + * Initialize variable-numbered pgstats entry newly-created. + * + * "chunk" must be a valid pointer, allocated previously by + * pgstat_alloc_entry_body(). + */ +PgStatShared_Common * +pgstat_init_entry_ext(PgStat_Kind kind, + PgStatShared_HashEntry *shhashent, + dsa_pointer chunk) +{ PgStatShared_Common *shheader; + Assert(DsaPointerIsValid(chunk)); + /* * Initialize refcount to 1, marking it as valid / not dropped. The entry * can't be freed before the initialization because it can't be found as @@ -292,12 +333,6 @@ pgstat_init_entry(PgStat_Kind kind, pg_atomic_init_u32(&shhashent->generation, 0); shhashent->dropped = false; - chunk = dsa_allocate_extended(pgStatLocal.dsa, - pgstat_get_kind_info(kind)->shared_size, - DSA_ALLOC_ZERO | DSA_ALLOC_NO_OOM); - if (chunk == InvalidDsaPointer) - return NULL; - shheader = dsa_get_address(pgStatLocal.dsa, chunk); shheader->magic = 0xdeadbeef; @@ -485,6 +520,19 @@ pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, if (create && !shhashent) { bool shfound; + dsa_pointer chunk; + + /* Allocate the stats body before inserting a hash entry. */ + chunk = pgstat_alloc_entry_body(kind); + if (chunk == InvalidDsaPointer) + { + pgstat_release_entry_ref(key, entry_ref, false); + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"), + errdetail("Failed while allocating entry %d/%u/%u.", + key.kind, key.dboid, key.objoid))); + } /* * It's possible that somebody created the entry since the above @@ -494,24 +542,7 @@ pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound); if (!shfound) { - shheader = pgstat_init_entry(kind, shhashent); - if (shheader == NULL) - { - /* - * Failed the allocation of a new entry, so clean up both the - * local reference and the shared hashtable before giving up. - * Clean the local state first, since releasing the dshash - * lock can process a pending interrupt. - */ - pgstat_release_entry_ref(key, entry_ref, false); - dshash_delete_entry(pgStatLocal.shared_hash, shhashent); - - ereport(ERROR, - (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory"), - errdetail("Failed while allocating entry %d/%u/%u.", - key.kind, key.dboid, key.objoid))); - } + shheader = pgstat_init_entry_ext(kind, shhashent, chunk); pgstat_acquire_entry_ref(entry_ref, shhashent, shheader); if (created_entry != NULL) @@ -519,6 +550,9 @@ pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, return entry_ref; } + + /* Concurrent insert won; drop the unused body. */ + dsa_free(pgStatLocal.dsa, chunk); } if (!shhashent) diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h index 84b52305d1..8533959d1d 100644 --- a/src/include/utils/pgstat_internal.h +++ b/src/include/utils/pgstat_internal.h @@ -640,6 +640,10 @@ extern void pgstat_reset_matching_entries(bool (*do_reset) (PgStatShared_HashEnt extern void pgstat_request_entry_refs_gc(void); extern PgStatShared_Common *pgstat_init_entry(PgStat_Kind kind, PgStatShared_HashEntry *shhashent); +extern dsa_pointer pgstat_alloc_entry_body(PgStat_Kind kind); +extern PgStatShared_Common *pgstat_init_entry_ext(PgStat_Kind kind, + PgStatShared_HashEntry *shhashent, + dsa_pointer chunk); /* -- 2.54.0 (Apple Git-157)