summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Bossart <nathan@postgresql.org>2025-09-11 16:13:55 -0500
committerNathan Bossart <nathan@postgresql.org>2025-09-11 16:13:55 -0500
commited1aad15e09d7d523f4ef413e3c4d410497c8065 (patch)
tree0b3d2d5a04a00d0ec3ca12c37b25d49c78c378e3
parenta0b99fc12203fa179d5b4218a21de30e0e91a7b8 (diff)
Move named LWLock tranche requests to shared memory.
In EXEC_BACKEND builds, GetNamedLWLockTranche() can segfault when called outside of the postmaster process, as it might access NamedLWLockTrancheRequestArray, which won't be initialized. Given the lack of reports, this is apparently unusual, presumably because it is usually called from a shmem_startup_hook like this: mystruct = ShmemInitStruct(..., &found); if (!found) { mystruct->locks = GetNamedLWLockTranche(...); ... } This genre of shmem_startup_hook evades the aforementioned segfaults because the struct is initialized in the postmaster, so all other callers skip the !found path. We considered modifying the documentation or requiring GetNamedLWLockTranche() to be called from the postmaster, but ultimately we decided to simply move the request array to shared memory (and add it to the BackendParameters struct), thereby allowing calls outside postmaster on all platforms. Since the main shared memory segment is initialized after accepting LWLock tranche requests, postmaster builds the request array in local memory first and then copies it to shared memory later. Given the lack of reports, back-patching seems unnecessary. Reported-by: Sami Imseih <samimseih@gmail.com> Reviewed-by: Sami Imseih <samimseih@gmail.com> Discussion: https://postgr.es/m/CAA5RZ0v1_15QPg5Sqd2Qz5rh_qcsyCeHHmRDY89xVHcy2yt5BQ%40mail.gmail.com
-rw-r--r--src/backend/postmaster/launch_backend.c3
-rw-r--r--src/backend/storage/lmgr/lwlock.c31
-rw-r--r--src/include/storage/lwlock.h4
3 files changed, 33 insertions, 5 deletions
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index a38979c50e4..c5ef14e1eaa 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -101,6 +101,7 @@ typedef struct
struct InjectionPointsCtl *ActiveInjectionPoints;
#endif
int NamedLWLockTrancheRequests;
+ NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray;
char **LWLockTrancheNames;
int *LWLockCounter;
LWLockPadded *MainLWLockArray;
@@ -761,6 +762,7 @@ save_backend_variables(BackendParameters *param,
#endif
param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
+ param->NamedLWLockTrancheRequestArray = NamedLWLockTrancheRequestArray;
param->LWLockTrancheNames = LWLockTrancheNames;
param->LWLockCounter = LWLockCounter;
param->MainLWLockArray = MainLWLockArray;
@@ -1022,6 +1024,7 @@ restore_backend_variables(BackendParameters *param)
#endif
NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
+ NamedLWLockTrancheRequestArray = param->NamedLWLockTrancheRequestArray;
LWLockTrancheNames = param->LWLockTrancheNames;
LWLockCounter = param->LWLockCounter;
MainLWLockArray = param->MainLWLockArray;
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index fcbac5213a5..46c82c63ca5 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -184,14 +184,13 @@ typedef struct NamedLWLockTrancheRequest
int num_lwlocks;
} NamedLWLockTrancheRequest;
-static NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray = NULL;
-
/*
- * NamedLWLockTrancheRequests is the valid length of the request array. This
- * variable is non-static so that postmaster.c can copy them to child processes
- * in EXEC_BACKEND builds.
+ * NamedLWLockTrancheRequests is the valid length of the request array. These
+ * variables are non-static so that launch_backend.c can copy them to child
+ * processes in EXEC_BACKEND builds.
*/
int NamedLWLockTrancheRequests = 0;
+NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray = NULL;
/* shared memory counter of registered tranches */
int *LWLockCounter = NULL;
@@ -407,6 +406,14 @@ LWLockShmemSize(void)
size = add_size(size, mul_size(MAX_NAMED_TRANCHES, sizeof(char *)));
size = add_size(size, mul_size(MAX_NAMED_TRANCHES, NAMEDATALEN));
+ /*
+ * Make space for named tranche requests. This is done for the benefit of
+ * EXEC_BACKEND builds, which otherwise wouldn't be able to call
+ * GetNamedLWLockTranche() outside postmaster.
+ */
+ size = add_size(size, mul_size(NamedLWLockTrancheRequests,
+ sizeof(NamedLWLockTrancheRequest)));
+
/* Space for the LWLock array, plus room for cache line alignment. */
size = add_size(size, LWLOCK_PADDED_SIZE);
size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded)));
@@ -443,6 +450,20 @@ CreateLWLocks(void)
ptr += NAMEDATALEN;
}
+ /*
+ * Move named tranche requests to shared memory. This is done for the
+ * benefit of EXEC_BACKEND builds, which otherwise wouldn't be able to
+ * call GetNamedLWLockTranche() outside postmaster.
+ */
+ if (NamedLWLockTrancheRequests > 0)
+ {
+ memcpy(ptr, NamedLWLockTrancheRequestArray,
+ NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest));
+ pfree(NamedLWLockTrancheRequestArray);
+ NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *) ptr;
+ ptr += NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest);
+ }
+
/* Ensure desired alignment of LWLock array */
ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
MainLWLockArray = (LWLockPadded *) ptr;
diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h
index 0e9cf81a4c7..8e0d0d233b4 100644
--- a/src/include/storage/lwlock.h
+++ b/src/include/storage/lwlock.h
@@ -73,8 +73,12 @@ typedef union LWLockPadded
extern PGDLLIMPORT LWLockPadded *MainLWLockArray;
+/* forward declaration of private type for use only by lwlock.c */
+typedef struct NamedLWLockTrancheRequest NamedLWLockTrancheRequest;
+
extern PGDLLIMPORT char **LWLockTrancheNames;
extern PGDLLIMPORT int NamedLWLockTrancheRequests;
+extern PGDLLIMPORT NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray;
extern PGDLLIMPORT int *LWLockCounter;
/*