pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: Rahila Syed <rahilasyed90@gmail.com>
Cc: Sami Imseih <samimseih@gmail.com>
Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Andres Freund <andres@anarazel.de>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: Improve LWLock tranche name visibility across backends
Date: Thu, 4 Sep 2025 10:44:00 -0500
Message-ID: <aLmzwC2dRbqk14y6@nathan> (raw)
In-Reply-To: <aLiQeizOQKbKF-ae@nathan>
References: <CAA5RZ0ve-fHuNYW-ruMwg1y1v7-aCqMm_MiNq1KOdg2Y2-pKDw@mail.gmail.com>
	<aLAFhwpzEwbjiWLG@ip-10-97-1-34.eu-west-3.compute.internal>
	<CAA5RZ0s4ip08Lsk=aYuXGCkBr78jhH=gOau939mhAp-rGuNC3Q@mail.gmail.com>
	<aLCEo0ZMV3vIGRWk@nathan>
	<CAA5RZ0vw-r1qjHw5hUNTzAyCiK3KQ-r+G5ECfWSn+qjdu-fpnw@mail.gmail.com>
	<aLHO-jh-spXWG1pt@nathan>
	<aLJnOnTFSweb2pud@nathan>
	<CAH2L28sz2NcLCS2v1MNkBNUiTiO=-CegFGdAvO24nE+hbjzgjQ@mail.gmail.com>
	<aLcPbyWLawp5_rdt@nathan>
	<aLiQeizOQKbKF-ae@nathan>

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 <nathan@postgresql.org>
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)

Attachments:

v1-0001-Revert-some-recent-changes-to-RequestNamedLWLockT.patch (text/plain, 1.9K) download From 6fe6590f86427c55ac831c5b48784d3d259fe1c8 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <nathan@postgresql.org> 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)

view thread (107+ messages)  latest in thread

Message-ID: <aLmzwC2dRbqk14y6@nathan>
Permalink:  ../aLmzwC2dRbqk14y6@nathan/
Also on:    postgresql.org/message-id/aLmzwC2dRbqk14y6@nathan

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, rahilasyed90@gmail.com, samimseih@gmail.com, bertranddrouvot.pg@gmail.com, tgl@sss.pgh.pa.us, andres@anarazel.de
  Subject: Re: Improve LWLock tranche name visibility across backends
  In-Reply-To: <aLmzwC2dRbqk14y6@nathan>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox