pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Allocate pgstats entry body before insert into shared hashtable
Date: Tue, 24 Nov 2026 03:27:43 +0000
Message-ID: <E1xVhC2-000000005yi-4Bt7@gemulon.postgresql.org> (raw)

Allocate pgstats entry body before insert into shared hashtable

8191e0c16a03 made pgstat_init_entry() return NULL on DSA allocation
failure so callers could delete a just-inserted hash entry.  That works
when dsa_allocate_extended(DSA_ALLOC_NO_OOM) returns InvalidDsaPointer,
but not when the creation of a new DSM segment raises ERROR.

Under this type of failure, pgstat_init_entry() would mark the hash
entry live before allocating the body and cause an inconsistent state in
shared memory.  The next backend trying to acquire the existing,
partially-initialized entry in pgstat_acquire_entry_ref() would crash,
taking down the server.  Some of the public reports mentioned ENOSPC on
POSIX shared memory as one initial cause.

This commit refactors the pgstats entry initialization to happen in two
phases:
- Allocate the DSA chunk.
- Insert the hash entry with a valid segment.

If a concurrent insert wins, the allocation of the first phase needs an
extra set of dsa_free() calls (pulling back a DSM into its free list).

This change has been done on HEAD as of d9de60c5e47a, without a
backpatch as the API signature of pgstat_init_entry() has changed, and
is now backpatched following more complaints that this code leads to a
corrupted shared memory state, which is likely to crash a server at the
end.  This backpatch guarantees ABI compatibility with a new
pgstat_init_entry_ext(), pgstat_init_entry() remaining as it is.

Colin has authored this part, while I have adjusted a few things to be
more consistent with HEAD.  There are no more direct callers of
pgstat_init_entry() in all the stable branches.  It is recommended that
out-of-core code adjusts to pgstat_init_entry_ext(), pgstat_init_entry()
should not be used when dealing with versions of Postgres up to v19.

Author: Yuriy Grigoryev <ju.grigorev@ftdata.ru>
Author: Michael Paquier <michael@paquier.xyz>
Author: Colin McDonnell <colinmcd94@gmail.com>
Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain
Discussion: https://postgr.es/m/CALdYYtTg2x4nNLsjX99acjbQj=oTo40CBXgDM1qzdzG7UbwzGQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/978888fafaccc4ebd404898004c828d18bcf121b

Modified Files
--------------
src/backend/utils/activity/pgstat.c       | 28 ++++++-----
src/backend/utils/activity/pgstat_shmem.c | 78 +++++++++++++++++++++----------
src/include/utils/pgstat_internal.h       |  4 ++
3 files changed, 74 insertions(+), 36 deletions(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1xVhC2-000000005yi-4Bt7@gemulon.postgresql.org>
Permalink:  ../E1xVhC2-000000005yi-4Bt7@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xVhC2-000000005yi-4Bt7@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: michael@paquier.xyz
  Subject: Re: pgsql: Allocate pgstats entry body before insert into shared hashtable
  In-Reply-To: <E1xVhC2-000000005yi-4Bt7@gemulon.postgresql.org>
  References: <E1xVhC2-000000005yi-4Bt7@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 DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox