agora inbox for pgsql-committers@postgresql.org
help / color / mirror / Atom feedFrom: Michael Paquier <michael@paquier.xyz>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Use OOM-safe routine for pgstats shared hashtable insert
Date: Sun, 09 Aug 2026 23:38:01 +0000
Message-ID: <E1wtD5d-00000000szU-2cTR@gemulon.postgresql.org> (raw)
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@paquier.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(-)
Message-ID: <E1wtD5d-00000000szU-2cTR@gemulon.postgresql.org>
Permalink: ../E1wtD5d-00000000szU-2cTR@gemulon.postgresql.org/
Also on: postgresql.org/message-id/E1wtD5d-00000000szU-2cTR@gemulon.postgresql.org
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-committers@postgresql.org
Cc: michael@paquier.xyz, pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Use OOM-safe routine for pgstats shared hashtable insert
In-Reply-To: <E1wtD5d-00000000szU-2cTR@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox