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 1uvgXg-00HKsX-9l for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Sep 2025 18:24:41 +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 1uvgXf-006noq-8e for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Sep 2025 18:24:39 +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 1uvgXe-006noi-UR for pgsql-hackers@lists.postgresql.org; Mon, 08 Sep 2025 18:24:39 +0000 Received: from mail-io1-xd31.google.com ([2607:f8b0:4864:20::d31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uvgXb-001FU6-2L for pgsql-hackers@postgresql.org; Mon, 08 Sep 2025 18:24:38 +0000 Received: by mail-io1-xd31.google.com with SMTP id ca18e2360f4ac-88432e29adcso151367939f.2 for ; Mon, 08 Sep 2025 11:24:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757355875; x=1757960675; 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=G8AyuUx9RusoSni+yzVvxVWQpYcFAXNG5ZbbmFEjthU=; b=nrAw09zudPWIK+dE+xQ7PqLRREUITSQs5sW9ROBfDuAMBHNdm9wPCMd9srCY0CTJME 9493ieSecjK9vPN82Fy+cmI7BzjO7Kllpg+w5eX0e09cqpkIfsi7//ctFRUNjcTpo1xj 9lisNe8S556dSXuGSQGNfMx/UlwoMHEIJKCB+DlxNHgpdefpg8Sc4ZMY9r3o/3MhMTuG L0Bn4+pU/OYYYZN5fdQlu3RSP83XczejBMd1S3pq2EBmStmRwA36it66LYuhX+jiovWf DBMisVH/kNGwZgiDhwPQcTFXCijQd9yUoKXmVv33YHmI84a/Q7cXD6zeaQeICGOSNPeS O16w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757355875; x=1757960675; 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=G8AyuUx9RusoSni+yzVvxVWQpYcFAXNG5ZbbmFEjthU=; b=t6D0jjo8EslhFDhZH8yeIwrmXMua1hFcIHG2QU0zxlRBmT11SKoaLlBYXys0OlQheJ rgLuL0OkD1BKJhg7tM9JaA4ThZJNfoaspfKCNWtgP31BxJ5TCPA4XjTwBiXFMgal11BO 8Gl30ztjVkm5pRSKWVza8xM92Of/MqHqWZb2K3HD3lHBBZWNW6QwiiA5CBaLPe6yW8mP avdR8w1fFnypriZZHcLnqSJxoocEFvvD/7oNw/d2QqCKzaElb8bnBb5nLthOUvHdiBOg 3POPRYxzRRlDoCYBWzl5b1+dIQzj2+3igznW08WgEAy+Cpw6aosNC7LsdwT5BLQqDPze ej+w== X-Gm-Message-State: AOJu0YwZrDqkyab5cxmXajWV/bsCAwv6Tpee6apWrNx0AF+PK9gKW8OX Yh2pGMg0aWoK9ecSgq4ZMmBWBED4C6wkDO3eglT+ZVz+xEY57MOpnv1z X-Gm-Gg: ASbGncvH7OWS5S/NDtmOQPd53pEAsk56B6iHMmjzc/rL5e59IHKlQT/tHO3N4oBRCwG MScf1fWD0Mh2BYQpUyPgMo5wRFBw0/Zbyh9/O41GYlam5izG24Ez9Tdl7UsP7BoQYlAoHXdP+ch EZkAvzptYYRz7pvaq8N/rYbKf53Hm8RFYRFeV6QUlBU1UllBeYpAsF7VnwNbKLUOGfNL6QxOEpw 5ZRkB1t6yX2rsDMasK2IFV96uX+zdhJ+SyciBlweKa91uhIcEQlf4WSTjlC+AY+f4FLY4PpyIIE y1SLeEmB+fjTvRgjY/drnaOdfpomtOaqShUBM2oVCN726Vz3E4LlZE99ATMJPeigCg/GK2X45cy +PqYO1BDkmopHPnKgeMSZpUBlxE9bzEGiegPruGwehbHftn+caarUoFJe3f04QtKccRLxSRJ0FP vij8sqjGberFhg X-Google-Smtp-Source: AGHT+IEYTvn3M2al84d7YMWsxJvPwUN/MKuvbEtrNxYznpln7FAIXRQlit0XeOU2FOIYa6QuNPnXtg== X-Received: by 2002:a05:6e02:3c04:b0:40f:be7d:bb1e with SMTP id e9e14a558f8ab-40fbe7dbd7emr1687125ab.24.1757355874909; Mon, 08 Sep 2025 11:24:34 -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-407bd63d73asm13539005ab.43.2025.09.08.11.24.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Sep 2025 11:24:34 -0700 (PDT) Date: Mon, 8 Sep 2025 13:24:32 -0500 From: Nathan Bossart To: Sami Imseih Cc: pgsql-hackers Subject: Re: GetNamedLWLockTranche crashes on Windows in normal backend Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="iBaQjJ+ngGQq9h44" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --iBaQjJ+ngGQq9h44 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Sep 08, 2025 at 11:55:14AM -0500, Nathan Bossart wrote: > I've started preparing this for commit, and I realized that restricting > GetNamedLWLockTranche() to shmem_startup_hook is not sufficient. > EXEC_BACKEND builds will run this hook in every backend, so unless it's > guarded behind some sort of "if (!found)" condition (i.e., only run in the > postmaster), it'll still crash. I think we just need to add some extra > notes to the docs and check for !IsUnderPostmaster, as discussed upthread. Or... what if we just moved the request array to shared memory? -- nathan --iBaQjJ+ngGQq9h44 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0001-Move-named-LWLock-tranche-request-array-to-shared.patch From 5abfc98ef5dd26e2f3c822cbe51c6038fb4e37bf Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 8 Sep 2025 13:18:59 -0500 Subject: [PATCH v3 1/1] Move named LWLock tranche request array to shared memory. --- src/backend/postmaster/launch_backend.c | 3 +++ src/backend/storage/lmgr/lwlock.c | 31 +++++++++++++++++++++---- src/include/storage/lwlock.h | 4 ++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c index a38979c50e4..c5ef14e1eaa 100644 --- a/src/backend/postmaster/launch_backend.c +++ b/src/backend/postmaster/launch_backend.c @@ -101,6 +101,7 @@ typedef struct struct InjectionPointsCtl *ActiveInjectionPoints; #endif int NamedLWLockTrancheRequests; + NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray; char **LWLockTrancheNames; int *LWLockCounter; LWLockPadded *MainLWLockArray; @@ -761,6 +762,7 @@ save_backend_variables(BackendParameters *param, #endif param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests; + param->NamedLWLockTrancheRequestArray = NamedLWLockTrancheRequestArray; param->LWLockTrancheNames = LWLockTrancheNames; param->LWLockCounter = LWLockCounter; param->MainLWLockArray = MainLWLockArray; @@ -1022,6 +1024,7 @@ restore_backend_variables(BackendParameters *param) #endif NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests; + NamedLWLockTrancheRequestArray = param->NamedLWLockTrancheRequestArray; LWLockTrancheNames = param->LWLockTrancheNames; LWLockCounter = param->LWLockCounter; MainLWLockArray = param->MainLWLockArray; diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c index fcbac5213a5..46c82c63ca5 100644 --- a/src/backend/storage/lmgr/lwlock.c +++ b/src/backend/storage/lmgr/lwlock.c @@ -184,14 +184,13 @@ typedef struct NamedLWLockTrancheRequest int num_lwlocks; } NamedLWLockTrancheRequest; -static NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray = NULL; - /* - * NamedLWLockTrancheRequests is the valid length of the request array. This - * variable is non-static so that postmaster.c can copy them to child processes - * in EXEC_BACKEND builds. + * NamedLWLockTrancheRequests is the valid length of the request array. These + * variables are non-static so that launch_backend.c can copy them to child + * processes in EXEC_BACKEND builds. */ int NamedLWLockTrancheRequests = 0; +NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray = NULL; /* shared memory counter of registered tranches */ int *LWLockCounter = NULL; @@ -407,6 +406,14 @@ LWLockShmemSize(void) size = add_size(size, mul_size(MAX_NAMED_TRANCHES, sizeof(char *))); size = add_size(size, mul_size(MAX_NAMED_TRANCHES, NAMEDATALEN)); + /* + * Make space for named tranche requests. This is done for the benefit of + * EXEC_BACKEND builds, which otherwise wouldn't be able to call + * GetNamedLWLockTranche() outside postmaster. + */ + size = add_size(size, mul_size(NamedLWLockTrancheRequests, + sizeof(NamedLWLockTrancheRequest))); + /* Space for the LWLock array, plus room for cache line alignment. */ size = add_size(size, LWLOCK_PADDED_SIZE); size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded))); @@ -443,6 +450,20 @@ CreateLWLocks(void) ptr += NAMEDATALEN; } + /* + * Move named tranche requests to shared memory. This is done for the + * benefit of EXEC_BACKEND builds, which otherwise wouldn't be able to + * call GetNamedLWLockTranche() outside postmaster. + */ + if (NamedLWLockTrancheRequests > 0) + { + memcpy(ptr, NamedLWLockTrancheRequestArray, + NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest)); + pfree(NamedLWLockTrancheRequestArray); + NamedLWLockTrancheRequestArray = (NamedLWLockTrancheRequest *) ptr; + ptr += NamedLWLockTrancheRequests * sizeof(NamedLWLockTrancheRequest); + } + /* Ensure desired alignment of LWLock array */ ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE; MainLWLockArray = (LWLockPadded *) ptr; diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h index 0e9cf81a4c7..8e0d0d233b4 100644 --- a/src/include/storage/lwlock.h +++ b/src/include/storage/lwlock.h @@ -73,8 +73,12 @@ typedef union LWLockPadded extern PGDLLIMPORT LWLockPadded *MainLWLockArray; +/* forward declaration of private type for use only by lwlock.c */ +typedef struct NamedLWLockTrancheRequest NamedLWLockTrancheRequest; + extern PGDLLIMPORT char **LWLockTrancheNames; extern PGDLLIMPORT int NamedLWLockTrancheRequests; +extern PGDLLIMPORT NamedLWLockTrancheRequest *NamedLWLockTrancheRequestArray; extern PGDLLIMPORT int *LWLockCounter; /* -- 2.39.5 (Apple Git-154) --iBaQjJ+ngGQq9h44--