| From: | Michael Paquier <michael(at)paquier(dot)xyz> |
|---|---|
| To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
| Subject: | pgsql: Allocate pgstats entry body before insert into shared hashtable |
| Date: | 2026-09-23 23:51:11 |
| Message-ID: | E1x9Wk3-00000000zEb-3B6C@gemulon.postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-committers |
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).
As this changes the signature of pgstat_init_entry(), no backpatch is
done.
Author: Yuriy Grigoryev <ju(dot)grigorev(at)ftdata(dot)ru>
Author: Michael Paquier <michael(at)paquier(dot)xyz>
Discussion: https://postgr.es/m/ddc3ecfb01ce4e9698b23cc59767f016@localhost.localdomain
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/d9de60c5e47a7f7a5dcee050d119bda08ea85d55
Modified Files
--------------
src/backend/utils/activity/pgstat.c | 46 ++++++++++++++------
src/backend/utils/activity/pgstat_shmem.c | 72 ++++++++++++++++++-------------
src/include/utils/pgstat_internal.h | 4 +-
3 files changed, 76 insertions(+), 46 deletions(-)
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Melanie Plageman | 2026-09-23 22:23:02 | pgsql: Add VM corruption check to verify_heapam() |