From 60f6ab331e436ece58b52abab8a96f60cadcf197 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 25 Aug 2025 21:12:22 +0000 Subject: [PATCH 1/1] Fix EXEC_BACKEND segfault on NamedLWLockTrancheRequestArray access Unlike fork(), EXEC_BACKEND did not pass NamedLWLockTrancheRequestArray down to backends. This caused crashes if backends accessed the array, such as in GetNamedLWLockTranche. This patch fixes the issue by adding a num_lwlocks field to the shared memory NamedLWLockTrancheArray, with the postmaster copying the value from NamedLWLockTrancheRequestArray. GetNamedLWLockTranche now looks up the LWLock from NamedLWLockTrancheArray instead. NamedLWLockTrancheArray already points to shared memory and is passed down to backends in launch_backend.c. NamedLWLockTrancheRequests still acts as the counter for both arrays and is already handled in EXEC_BACKEND, so no further changes are needed there. This issue was discovered during testing of nearby code. No reports have been seen in the field, so backpatching is not required. --- src/backend/storage/lmgr/lwlock.c | 5 +++-- src/include/storage/lwlock.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c index c80b43f1f55..2e7392b633a 100644 --- a/src/backend/storage/lmgr/lwlock.c +++ b/src/backend/storage/lmgr/lwlock.c @@ -514,6 +514,7 @@ InitializeLWLocks(void) strcpy(name, request->tranche_name); tranche->trancheId = LWLockNewTrancheId(); tranche->trancheName = name; + tranche->num_lwlocks = request->num_lwlocks; for (j = 0; j < request->num_lwlocks; j++, lock++) LWLockInitialize(&lock->lock, tranche->trancheId); @@ -554,11 +555,11 @@ GetNamedLWLockTranche(const char *tranche_name) lock_pos = NUM_FIXED_LWLOCKS; for (i = 0; i < NamedLWLockTrancheRequests; i++) { - if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name, + if (strcmp(NamedLWLockTrancheArray[i].trancheName, tranche_name) == 0) return &MainLWLockArray[lock_pos]; - lock_pos += NamedLWLockTrancheRequestArray[i].num_lwlocks; + lock_pos += NamedLWLockTrancheArray[i].num_lwlocks; } elog(ERROR, "requested tranche is not registered"); diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h index 5e717765764..074ac87cbf4 100644 --- a/src/include/storage/lwlock.h +++ b/src/include/storage/lwlock.h @@ -78,6 +78,7 @@ typedef struct NamedLWLockTranche { int trancheId; char *trancheName; + int num_lwlocks; } NamedLWLockTranche; extern PGDLLIMPORT NamedLWLockTranche *NamedLWLockTrancheArray; -- 2.43.0