From e562b32b0a35577984865a9b9007e3bec50345fb Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Fri, 7 Aug 2026 15:24:01 +0900 Subject: [PATCH] Use OOM-safe routine for pgstats shared hashtable insert On failure, a NULL error is reported, giving a way to clean up the local reference that could have been inserted. HEAD-only change, no backpatch. --- src/backend/utils/activity/pgstat_shmem.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c index 95cb4c6f4ae1..d8ac9d6f5e8c 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -551,7 +551,23 @@ pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, * lookup. If so, fall through to the same path as if we'd have if it * already had been created before the dshash_find() calls. */ - shhashent = dshash_find_or_insert(pgStatLocal.shared_hash, &key, &shfound); + shhashent = dshash_find_or_insert_extended(pgStatLocal.shared_hash, + &key, &shfound, + DSHASH_INSERT_NO_OOM); + if (!shhashent) + { + /* + * Clean up the local reference when failing insert into the + * shared hashtable. + */ + pgstat_release_entry_ref(key, entry_ref, false); + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"), + errdetail("Failed while inserting entry %u/%u/%" PRIu64 ".", + key.kind, key.dboid, key.objid))); + } + if (!shfound) { shheader = pgstat_init_entry(kind, shhashent); -- 2.55.0