pgsql: Use OOM-safe routine for pgstats shared hashtable insert

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Use OOM-safe routine for pgstats shared hashtable insert
Date: 2026-08-09 23:38:01
Message-ID: E1wtD5d-00000000szU-2cTR@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Use OOM-safe routine for pgstats shared hashtable insert

pgstat_get_entry_ref() called dshash_find_or_insert() to insert a new
entry into the shared pgstats hashtable. If the allocation required for
the new entry fails, a local cache entry would be left in
pgStatEntryRefHash with a NULL shared_entry. This code is switched to
use dshash_find_or_insert_extended(), so as local hash entries can be
cleaned up before failuring. This change makes pgstat_get_entry_ref()
more robust on re-entry.

Taken in isolation within a single backend, local entries are handled
fine on HEAD as pgstat_get_entry_ref() is able to handle the case of a
NULL shared_entry on re-entry. However it is possible to finish with
some NULL pointer dereference in more complex scenarios, like:
- First backend fails insert into the shared hashtable on OOM.
- Second backend bumps the same entry's refcount (due to
reinitialization of the same entry, for example).
- First backend calls pgstat_gc_entry_refs(), uses its local entry
reference with the NULL shared_entry, crashes when checking if the
shared entry has been dropped.

6f0738ddec85 has introduced dshash_find_or_insert_extended() for the
same reason as what we are dealing with here: the backend could be left
in an inconsistent stat, and clean up actions need to be taken before
issuing an error. The failure is unlikely going to show up in practice,
so no backpatch is done. This hash insert extended routine is new as of
v19.

Author: Michael Paquier <michael(at)paquier(dot)xyz>
Discussion: https://postgr.es/m/anV8ftNMW3xyKSMG@paquier.xyz

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/127ce776983f19e81ba8abda1984f6f0e229c1b5

Modified Files
--------------
src/backend/utils/activity/pgstat_shmem.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-08-10 00:14:26 pgsql: Use explicit fetching for digests in cryptohash (OpenSSL >= 3.0)
Previous Message Tom Lane 2026-08-09 16:53:05 pgsql: Release notes for 18.5, 17.11, 16.15, 15.19, 14.24.