postgres.git / summary / log / commit / refs
commit 06d859aaf4e988ddef8adc55efb21324c5e30092
Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu Mar 26 21:47:33 2026 +0000
First released in: 19.0
Move ShmemIndexLock into ShmemAllocator
This makes shmem.c independent of the main LWLock array. That makes it
possible to stop passing MainLWLockArray through BackendParameters in
the next commit.
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://www.postgresql.org/message-id/47aaf57e-1b7b-4e12-bda2-0316081ff50e@iki.fi
src/backend/storage/ipc/shmem.c | 15 ++++++++++-----
src/backend/utils/activity/wait_event_names.txt | 2 +-
src/include/storage/lwlocklist.h | 3 ++-
3 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index b3199082de2..3cc38450949 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -90,12 +90,16 @@
typedef struct ShmemAllocatorData
{
Size free_offset; /* offset to first free space from ShmemBase */
- HASHHDR *index; /* location of ShmemIndex */
- /* protects shared memory and LWLock allocation */
+ /* protects 'free_offset' */
slock_t shmem_lock;
+
+ HASHHDR *index; /* location of ShmemIndex */
+ LWLock index_lock; /* protects ShmemIndex */
} ShmemAllocatorData;
+#define ShmemIndexLock (&ShmemAllocator->index_lock)
+
static void *ShmemAllocRaw(Size size, Size *allocated_size);
/* shared memory global variables */
@@ -154,15 +158,16 @@ InitShmemAllocator(PGShmemHeader *seghdr)
/*
* In postmaster or stand-alone backend, initialize the shared memory
- * allocator and the spinlock so that we can allocate shared memory for
- * ShmemIndex using ShmemAlloc(). In a regular backend just set up the
- * pointers required by ShmemAlloc().
+ * allocator so that we can allocate shared memory for ShmemIndex using
+ * ShmemAlloc(). In a regular backend just set up the pointers required
+ * by ShmemAlloc().
*/
ShmemAllocator = (ShmemAllocatorData *) ((char *) seghdr + seghdr->content_offset);
if (!IsUnderPostmaster)
{
SpinLockInit(&ShmemAllocator->shmem_lock);
ShmemAllocator->free_offset = offset;
+ LWLockInitialize(&ShmemAllocator->index_lock, LWTRANCHE_SHMEM_INDEX);
}
ShmemSegHdr = seghdr;
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 4aa864fe3c3..6be80d2daad 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -321,7 +321,6 @@ ABI_compatibility:
Section: ClassName - WaitEventLWLock
-ShmemIndex "Waiting to find or allocate space in shared memory."
OidGen "Waiting to allocate a new OID."
XidGen "Waiting to allocate a new transaction ID."
ProcArray "Waiting to access the shared per-process data structures (typically, to get a snapshot or report a session's transaction ID)."
@@ -412,6 +411,7 @@ SubtransSLRU "Waiting to access the sub-transaction SLRU cache."
XactSLRU "Waiting to access the transaction status SLRU cache."
ParallelVacuumDSA "Waiting for parallel vacuum dynamic shared memory allocation."
AioUringCompletion "Waiting for another process to complete IO via io_uring."
+ShmemIndex "Waiting to find or allocate space in shared memory."
# No "ABI_compatibility" region here as WaitEventLWLock has its own C code.
diff --git a/src/include/storage/lwlocklist.h b/src/include/storage/lwlocklist.h
index e94ebce95b9..59ee097977d 100644
--- a/src/include/storage/lwlocklist.h
+++ b/src/include/storage/lwlocklist.h
@@ -32,7 +32,7 @@
*/
/* 0 is available; was formerly BufFreelistLock */
-PG_LWLOCK(1, ShmemIndex)
+/* 1 was ShmemIndex */
PG_LWLOCK(2, OidGen)
PG_LWLOCK(3, XidGen)
PG_LWLOCK(4, ProcArray)
@@ -137,3 +137,4 @@ PG_LWLOCKTRANCHE(SUBTRANS_SLRU, SubtransSLRU)
PG_LWLOCKTRANCHE(XACT_SLRU, XactSLRU)
PG_LWLOCKTRANCHE(PARALLEL_VACUUM_DSA, ParallelVacuumDSA)
PG_LWLOCKTRANCHE(AIO_URING_COMPLETION, AioUringCompletion)
+PG_LWLOCKTRANCHE(SHMEM_INDEX, ShmemIndex)
[parent: 12e3e0f2c8f7]