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 1urkHM-00BhtU-5W for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Aug 2025 21:35: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 1urkHL-004oxo-FV for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Aug 2025 21:35:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1urkHL-004oxg-4u for pgsql-hackers@lists.postgresql.org; Thu, 28 Aug 2025 21:35:31 +0000 Received: from mail-il1-x135.google.com ([2607:f8b0:4864:20::135]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1urkHJ-002NoT-1c for pgsql-hackers@postgresql.org; Thu, 28 Aug 2025 21:35:31 +0000 Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-3f2b6e0b938so32385ab.1 for ; Thu, 28 Aug 2025 14:35:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756416927; x=1757021727; darn=postgresql.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=DyTJxiX/ouyy5Qeow9YSk5RrpMwy0omnt23oeJbOMpQ=; b=RCxTGd7FQa+fNO7dtek3yo8r4jA5xXGplQk4nB1S320npvizkpqRvrN9PitJeodIqr IShFXCOQHfPSKNxL/dFksS4qTCVGmD876bskCyaCVVl43Z4IryKkpqNyNSu2iwTDPCSF QocmcobLcKUyYmYqnqz43hYjlCdHmCheJlCgU5U7canh/6VOe7bouXONGDGXBz/yiAOf 1ZLvSXu4u3+962ja7K2BZ3MeLdryqtiEiAQtNjfwgpSQ4xJgn/iqrjfGoJdE57O79emE Gsxi47KSkslxhQQSDym3jLF6gJ5UVTOIyVZOzGxSmIbyD7HCt3Fo3MUCCh+QbYXICSjI 5QeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756416927; x=1757021727; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DyTJxiX/ouyy5Qeow9YSk5RrpMwy0omnt23oeJbOMpQ=; b=PIb1kApQ93JarcpqHVy0XsKdlN4mGcTO/rSz2Qx1pzFsBIDnzHEs2kv40JPpE6Lstb qPDkNMo4Ta0TeW86AE8A+hA20x2qkrYT4UBoSrNH3gLzS+GU73qZSpALU/5MKFTH8zdG RCTMxT/Y41hK5pxE9DLnMXO2z2jcQAaIFL7uOH7monZEhPI7BJQl7MNTQxUlbJwQjTuQ vsUI4ZER5TI48bluBavpaUalNcWxb/jgFNiFqmChpe5HQPOjDqgFf8sthQLFcNKPsGHG CPfir9Eh5QfDSEHHpK39mGpFaRUExaoPtn7eaaS8vKWuFPad1PV7fLI3gXaLDDCHw+JZ Qnrw== X-Gm-Message-State: AOJu0Yzs915Syngur7OioCUMjT7sUd6SIhMrh+p10NkcayYn6+H+y6H8 onIlIAsXAhzhglZxsLM2HHNIzBDFXW1LDS6+B8/7p1iGwqw0t1vs1ZvTuidpYg== X-Gm-Gg: ASbGncstgddR/3d6ysiQPs0g7pJazLsmzXNbwMcBYMNkfkMUKWLsiljjmWokaZm7Jfk VwxbGRNQ/fPfulIT0cHp85NlbmYSxtLKBkLmlZjkbQCUdC6eR4dl+LnEmmuif5PbYrg0T5N+SMR djRQzlMVoJFWDD83ZVAzhIbkxU8uaWtQ8sQUQvhrvO/5BClWBfbrrivYJErU1ohfXq7zCOrTQPt dx8A4EZjj9Vc8zO/IKcYFTU1nzuHFK6z9UCYB0jQGGSrIOQlgqLQOsf6C1TbPqVR9+xKV8P2jdW jVDp/GQMJ5obqrgCsY1606DgYC91tpb8DVxDYxKZgIuVnv8G8aSi0LXnqXshbwxYUS2OZ/8F0EK 6fRyNnBmrGrTuXKb0AUcpB1B+FTxgSXBgfe8U7C3ttF5fGGixYy/hHA6TuMkGwIFE/CmADOEA9w 5WW7hci9/Rkwp+ X-Google-Smtp-Source: AGHT+IFPogTqJmfkdYJ9AilgbPn/6DQEoCrXo8gBFJW9w4/Gjwce1Y8pPERqanBho8tHq/efbDX0tg== X-Received: by 2002:a05:6e02:1aa2:b0:3f1:f2:1a47 with SMTP id e9e14a558f8ab-3f100f21bb3mr77560515ab.31.1756416927212; Thu, 28 Aug 2025 14:35:27 -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-3f2a12b5d7esm723845ab.31.2025.08.28.14.35.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 14:35:26 -0700 (PDT) Date: Thu, 28 Aug 2025 16:35:25 -0500 From: Nathan Bossart To: pgsql-hackers@postgresql.org Subject: make LWLockCounter a global variable Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="DA0a177PYXmdsqgT" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --DA0a177PYXmdsqgT Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In lwlock.c, uses of LWLockCounter must first calculate its address in shared memory with something like this: LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int)); This appears to have been started by commit 82e861f in order to fix EXEC_BACKEND builds, but it could also be fixed by adding it to the BackendParameters struct. I find the current approach somewhat difficult to read and understand, so I'd like to switch to the latter approach. This is admittedly just nitpicking... -- nathan --DA0a177PYXmdsqgT Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0001-Make-LWLockCounter-a-global-variable.patch From d4b1847f7e7d1ef4b32b4a5304b2a55cabe55f56 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Thu, 28 Aug 2025 16:22:02 -0500 Subject: [PATCH v1 1/1] Make LWLockCounter a global variable. --- src/backend/postmaster/launch_backend.c | 3 +++ src/backend/storage/lmgr/lwlock.c | 15 ++++----------- src/include/storage/lwlock.h | 1 + 3 files changed, 8 insertions(+), 11 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..09401515d3a 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); @@ -423,13 +424,14 @@ 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 */ @@ -437,13 +439,6 @@ CreateLWLocks(void) 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 +569,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) --DA0a177PYXmdsqgT--