Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1us0KC-00Fyyy-1c for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Aug 2025 14:43:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1us0KB-00AVGs-Bf for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Aug 2025 14:43:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1us0KA-00AVGk-UU for pgsql-hackers@lists.postgresql.org; Fri, 29 Aug 2025 14:43:31 +0000 Received: from mail-il1-x12a.google.com ([2607:f8b0:4864:20::12a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1us0K8-002NiE-2r for pgsql-hackers@postgresql.org; Fri, 29 Aug 2025 14:43:30 +0000 Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3ea8b3a64beso10521965ab.0 for ; Fri, 29 Aug 2025 07:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756478609; x=1757083409; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yEZ9AWKv0VIFjqc+qDqga7doQgPGslxFu/URlYc2ylQ=; b=F8QpxMRBWCjy0XOc056LuMjZQnMk3u6/0XedTil3g63ZMjK0OyEYvyQiFiR7VHrNOf Q9HtbBQy5AYbxkrL77jyhK4nWg7TkgodFz5jva6P7v1iwJQxCzBesfgHDbY7qIyJtIDV B0fHnLboljttQd7niTyYFcGNjKFNXbgxboa/Q8Q9wCnqxIxoYwpn9CFn8fL1HDp6wDia Ibgs4RblQpLeGsXUZ+iMVCF+vzxpXzXH3QUrhz9Nm/8/uh2EObbVo88P8IHT6mvPJmbN x41YhMSHSzx6SorhOe3/hxxnzFMVLhQ+Elw4IsVtyyNoHjqL+d6QOPqUGmKC30jaJpgX VaKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756478609; x=1757083409; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yEZ9AWKv0VIFjqc+qDqga7doQgPGslxFu/URlYc2ylQ=; b=c5ucR3Y05X4wh7zigZl7RlNXnvYg+2XKk6v+RrFoaOiB2YZ7qweCdY8/cckfGvMGPz MZswkELdzPIhfT+fDDH2slMIkXoC8RwT8TstLTKq9oKOSaqOjHa7gJp4i/FM6s9NdGSx 9neI42xQZBBLzseywVRwT9jPhKkAnE1VC/EKhj5CdceZR2/xokFKQJVV6YzB2f/RlBY3 gME+DSwUqQXVxVCjvl1w2JxYkcLXyYckVCugC2C0vRToA6ZCqJXp5Z2TopIEg44Ei3gl D9yuD4lWggFHOdl1jSe62LLL/QFgCr4kOsKD1rMV9pyv2BB0h/8/TP8lTNhEEmrfRb2I VtCQ== X-Forwarded-Encrypted: i=1; AJvYcCXqpOzclyP3C7flf3HEqF0br00sHpTLgZ6wjdOn3H5n18y4wxOe2MqpRmpEzrfvVRmL/B2wX43NG7MO8aDP@postgresql.org X-Gm-Message-State: AOJu0YypqyjQ+SnSQHe9EJd600e3sb4/3lTsiFJMeSCYv9CI2XN3UtP/ 82bsC+tlo/sKcsonhoV846yVSyQTGX4cnfQfLRT1a+jEGSS7He3zKVqw X-Gm-Gg: ASbGncvZsoq2X20/gYcgg2F8094G+OBm4sLxEv/lCHOQcLZt3G7RejkhHIu7pVTl/0j 2ZVuHKaKeI/aHKZV7XB3RlxksPvQE68Sgl/TruIo+QbOHrAigrMWiGZ2Utu0AwDQqFJWaXt+5JY 76AVnMA7e1FuOx/f3hGDjsWzzjRVFLMDsKT/zikDS//KlNaNSzlZgrOqZ5deT4teJJCmtK+HgJO ZTk/6AINvss8gs6LLjMCtbsd2zgYtgd0dg3imBrk4Fx6+tSPr18+dxh2YIl/9fxKm0Eq17F5mTx KnaHMedfX1C2xvbZF6VrGGVcKKlIa2ThjZZ7ZI+tyzu9Yr+J81ez5LyXUB+H10rLMUo/s3pXcqm 6/ca7Ysd9S9a6f9dUbe2XgnqNwEq0ZLAALzp4OWtC+cfNHE3ANT0lM8ofVzn54HSwbr4F6KegQ+ 1UD2iwurwpSevPai3PyCFxx1zbfbVKSP9Drw== X-Google-Smtp-Source: AGHT+IEA38M2jeNqIZpwucspbObK42wzlKrQHYuFlKLiaJTRrfamIM2iQgM/H0tiJPfeDlzZChYEHA== X-Received: by 2002:a05:6e02:3c03:b0:3e5:5e09:4484 with SMTP id e9e14a558f8ab-3e9201fd78bmr431256355ab.6.1756478608541; Fri, 29 Aug 2025 07:43:28 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3f2a18897a3sm7769315ab.37.2025.08.29.07.43.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Aug 2025 07:43:27 -0700 (PDT) Date: Fri, 29 Aug 2025 09:43:26 -0500 From: Nathan Bossart To: Bertrand Drouvot Cc: Tom Lane , pgsql-hackers@postgresql.org Subject: Re: make LWLockCounter a global variable Message-ID: References: <1943925.1756418167@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="+59QXQE4tc7/REGN" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --+59QXQE4tc7/REGN Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Aug 29, 2025 at 06:52:48AM +0000, Bertrand Drouvot wrote: > On Thu, Aug 28, 2025 at 05:56:07PM -0400, Tom Lane wrote: >> No objection here. As a small improvement, perhaps you could swap >> around the code in LWLockShmemSize so that the order in which it >> considers size contributions matches the physical layout, more >> or less like >> >> [...] >> >> I find it a little confusing that that code doesn't line up >> exactly with what CreateLWLocks does. > > +1. Good idea. Here's a new version of the patch. If CI is happy with it, I'll go ahead and commit it. > Another option could be to not change CreateLWLocks() at all, except removing the > local variable: > > @@ -423,7 +424,6 @@ CreateLWLocks(void) > if (!IsUnderPostmaster) > { > Size spaceLocks = LWLockShmemSize(); > - int *LWLockCounter; > > to use the global variable. That way we preserve the current memory layout and > there is no need to change LWLockShmemSize(). That would make the patch smaller, but IMHO it kind-of defeats the purpose, which is to make this stuff simpler and easier to follow. -- nathan --+59QXQE4tc7/REGN Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v2-0001-Make-LWLockCounter-a-global-variable.patch From babaa2bfedd24932b6df13bfea0f4b01e911a311 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 29 Aug 2025 09:33:55 -0500 Subject: [PATCH v2 1/1] Make LWLockCounter a global variable. In lwlock.c, using the LWLockCounter requires first calculating its address in shared memory like this: LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int)); Commit 82e861fbe1 started this trend in order to fix EXEC_BACKEND builds, but it could also be fixed by adding it to the BackendParameters struct. The current approach is somewhat difficult to follow, so this commit switches to the latter approach. While at it, swap around the code in LWLockShmemSize() to match the order of the assignments in CreateLWLocks() for added readability. Reviewed-by: Tom Lane Reviewed-by: Bertrand Drouvot Discussion: https://postgr.es/m/aLDLnan9gNCS9fHx%40nathan --- src/backend/postmaster/launch_backend.c | 3 +++ src/backend/storage/lmgr/lwlock.c | 24 ++++++++---------------- src/include/storage/lwlock.h | 1 + 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c index bf6b55ee830..cd9547b03a3 100644 --- a/src/backend/postmaster/launch_backend.c +++ b/src/backend/postmaster/launch_backend.c @@ -102,6 +102,7 @@ typedef struct #endif int NamedLWLockTrancheRequests; NamedLWLockTranche *NamedLWLockTrancheArray; + int *LWLockCounter; LWLockPadded *MainLWLockArray; slock_t *ProcStructLock; PROC_HDR *ProcGlobal; @@ -761,6 +762,7 @@ save_backend_variables(BackendParameters *param, param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests; param->NamedLWLockTrancheArray = NamedLWLockTrancheArray; + param->LWLockCounter = LWLockCounter; param->MainLWLockArray = MainLWLockArray; param->ProcStructLock = ProcStructLock; param->ProcGlobal = ProcGlobal; @@ -1021,6 +1023,7 @@ restore_backend_variables(BackendParameters *param) NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests; NamedLWLockTrancheArray = param->NamedLWLockTrancheArray; + LWLockCounter = param->LWLockCounter; MainLWLockArray = param->MainLWLockArray; ProcStructLock = param->ProcStructLock; ProcGlobal = param->ProcGlobal; diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c index c80b43f1f55..a4aecd1fbc3 100644 --- a/src/backend/storage/lmgr/lwlock.c +++ b/src/backend/storage/lmgr/lwlock.c @@ -196,6 +196,7 @@ int NamedLWLockTrancheRequests = 0; /* points to data in shared memory: */ NamedLWLockTranche *NamedLWLockTrancheArray = NULL; +int *LWLockCounter = NULL; static void InitializeLWLocks(void); static inline void LWLockReportWaitStart(LWLock *lock); @@ -397,11 +398,11 @@ LWLockShmemSize(void) /* Calculate total number of locks needed in the main array. */ numLocks += NumLWLocksForNamedTranches(); - /* Space for the LWLock array. */ - size = mul_size(numLocks, sizeof(LWLockPadded)); - /* Space for dynamic allocation counter, plus room for alignment. */ - size = add_size(size, sizeof(int) + LWLOCK_PADDED_SIZE); + size = sizeof(int) + LWLOCK_PADDED_SIZE; + + /* Space for the LWLock array. */ + size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded))); /* space for named tranches. */ size = add_size(size, mul_size(NamedLWLockTrancheRequests, sizeof(NamedLWLockTranche))); @@ -423,27 +424,20 @@ CreateLWLocks(void) if (!IsUnderPostmaster) { Size spaceLocks = LWLockShmemSize(); - int *LWLockCounter; char *ptr; /* Allocate space */ ptr = (char *) ShmemAlloc(spaceLocks); - /* Leave room for dynamic allocation of tranches */ + /* Initialize the dynamic-allocation counter for tranches */ + LWLockCounter = (int *) ptr; + *LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED; ptr += sizeof(int); /* Ensure desired alignment of LWLock array */ ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE; - MainLWLockArray = (LWLockPadded *) ptr; - /* - * Initialize the dynamic-allocation counter for tranches, which is - * stored just before the first LWLock. - */ - LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int)); - *LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED; - /* Initialize all LWLocks */ InitializeLWLocks(); } @@ -574,9 +568,7 @@ int LWLockNewTrancheId(void) { int result; - int *LWLockCounter; - LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int)); /* We use the ShmemLock spinlock to protect LWLockCounter */ SpinLockAcquire(ShmemLock); result = (*LWLockCounter)++; diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h index 5e717765764..f9cf57f8d26 100644 --- a/src/include/storage/lwlock.h +++ b/src/include/storage/lwlock.h @@ -82,6 +82,7 @@ typedef struct NamedLWLockTranche extern PGDLLIMPORT NamedLWLockTranche *NamedLWLockTrancheArray; extern PGDLLIMPORT int NamedLWLockTrancheRequests; +extern PGDLLIMPORT int *LWLockCounter; /* * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS -- 2.39.5 (Apple Git-154) --+59QXQE4tc7/REGN--