pgsql: Allocate pgstats entry body before insert into shared hashtable - Mailing list pgsql-committers

From Michael Paquier
Subject pgsql: Allocate pgstats entry body before insert into shared hashtable
Date
Msg-id E1x9Wk3-00000000zEb-3B6C@gemulon.postgresql.org
Whole thread
List 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.grigorev@ftdata.ru>
Author: Michael Paquier <michael@paquier.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(-)


pgsql-committers by date:

Previous
From: Melanie Plageman
Date:
Subject: pgsql: Add VM corruption check to verify_heapam()
Next
From: Michael Paquier
Date:
Subject: pgsql: Forbid pg_get_multixact_stats() during recovery