On Wed, Sep 03, 2025 at 02:01:14PM -0500, Nathan Bossart wrote:
> Committed.

I'm having some regrets about the changes to RequestNamedLWLockTranche().
Specifically, when it is first called, it immediately allocates an array
big enough to hold 256 requests (~17 KB), whereas it used to only allocate
space for 16 requests (~1 KB) and resize as needed.  Furthermore, the
MAX_NAMED_TRANCHES check isn't actually needed because InitializeLWLocks()
will do the same check via its calls to LWLockNewTrancheId() for all the
named tranche requests.

-- 
nathan
>From 6fe6590f86427c55ac831c5b48784d3d259fe1c8 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <[email protected]>
Date: Thu, 4 Sep 2025 10:36:06 -0500
Subject: [PATCH v1 1/1] Revert some recent changes to
 RequestNamedLWLockTranche().

---
 src/backend/storage/lmgr/lwlock.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/backend/storage/lmgr/lwlock.c 
b/src/backend/storage/lmgr/lwlock.c
index 258cdebd0f5..a5f6b81806d 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -610,6 +610,7 @@ void
 RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
 {
        NamedLWLockTrancheRequest *request;
+       static int      NamedLWLockTrancheRequestsAllocated;
 
        if (!process_shmem_requests_in_progress)
                elog(FATAL, "cannot request additional LWLocks outside 
shmem_request_hook");
@@ -628,17 +629,22 @@ RequestNamedLWLockTranche(const char *tranche_name, int 
num_lwlocks)
 
        if (NamedLWLockTrancheRequestArray == NULL)
        {
+               NamedLWLockTrancheRequestsAllocated = 16;
                NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *)
                        MemoryContextAlloc(TopMemoryContext,
                                                           MAX_NAMED_TRANCHES
                                                           * 
sizeof(NamedLWLockTrancheRequest));
        }
 
-       if (NamedLWLockTrancheRequests >= MAX_NAMED_TRANCHES)
-               ereport(ERROR,
-                               (errmsg("maximum number of tranches already 
registered"),
-                                errdetail("No more than %d tranches may be 
registered.",
-                                                  MAX_NAMED_TRANCHES)));
+       if (NamedLWLockTrancheRequests >= NamedLWLockTrancheRequestsAllocated)
+       {
+               int                     i = 
pg_nextpower2_32(NamedLWLockTrancheRequests + 1);
+
+               NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *)
+                       repalloc(NamedLWLockTrancheRequestArray,
+                                        i * sizeof(NamedLWLockTrancheRequest));
+               NamedLWLockTrancheRequestsAllocated = i;
+       }
 
        request = &NamedLWLockTrancheRequestArray[NamedLWLockTrancheRequests];
        strlcpy(request->tranche_name, tranche_name, NAMEDATALEN);
-- 
2.39.5 (Apple Git-154)

Reply via email to