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.96) (envelope-from ) id 1w5lqY-003bvj-0s for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 14:38:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5lqW-003DLz-2c for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 14:38:05 +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.96) (envelope-from ) id 1w5lq2-0039y0-1N for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 14:37:34 +0000 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5lq0-00000001Idt-1eXN for pgsql-hackers@postgresql.org; Thu, 26 Mar 2026 14:37:34 +0000 Received: by mail-oi1-x22b.google.com with SMTP id 5614622812f47-46704177508so593204b6e.0 for ; Thu, 26 Mar 2026 07:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774535851; x=1775140651; 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=hF2zSHdsCwMsGjRS94kcD9yqtNGwZ9AX8St4IKEWx10=; b=VagPvBB0b+62bC8SHRAI6fU9f7hFcygPaamiDrox2RIFes/Nz+QMRT6BCs7iko/5KB Bgds60RGAcXmqXmMU8QKnVgtXZflEuyS68Bs2VIUXiIeYMuOxCKafJzu/ER4n/ERm3IA e15IgveTyAvVBqIDkRi3S0cx57YfcLp9+1WRK9ko+HKQg2tglQ+J1CnMIYoS1C4Z1vbs qSqss6uYh9lv8vzZ8qv2qdHbOmfXrTTXfw70WcRCm6owOC7wHd2qBheiCsFGuJLG73JI U1UPFU4MXghEXSwZURxUc1lmdR6ylfrrOQxCKMZBy4NIkykCK19n9W616nFWPB2DddfD QPrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774535851; x=1775140651; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hF2zSHdsCwMsGjRS94kcD9yqtNGwZ9AX8St4IKEWx10=; b=pvGyUFYJ8oqVUdBdfNwq/bzDz18G1zyaUm+ULloW14oEjMyLJCdRl/qHP2IGJUsqzH lv6fA7NpAtZsEHuL7K3k68MsdZBmBcKiF08ydu2b+Jr78Q87JW0VgFkijmCu21qdUs53 1Y+YAcns9nUVOyGdHTz0VUFJnDwhj8VSofXehzW5nFdtqoyY+afMptDH6qe+6O5VXk8K //2xtXAKVqMqzXptJyezJJwR3jfMJEK/mYavfmA4Pp7/t6Uc+tidLX9zlbOfIsMxeRZV 9AczK1VRfQCrIscL1bkjkYS6Tk08E+5nx+aSRIb6pjUyJIUM6IOQyC1RJgGG7ncWsBj+ sxaQ== X-Forwarded-Encrypted: i=1; AJvYcCVusZmuK8Kj8GdPN8VXO6tFisq50D/gBeMov+Gha7Vr9e4R8UoQmgVjZKHE+o1yp1faGXh+cSib7Fzg7OTu@postgresql.org X-Gm-Message-State: AOJu0YwDWBOumTKbjg+UF+8cIbs8PzbfosrDYE+vOiTD+L4u2t7zV/68 cZMbel2IfXwDQV0hqXov5WXtx2jkuHXai4Q2hrktVXIZKyUAoj3ipkcQ X-Gm-Gg: ATEYQzwRcB1sGyuW1lZRCQyVpNV54ufEK6wu2ujss7Lt3RAH1lMRUEOEvRkGyZp84ke 7iJx7clNPPJCGw9OPBCPmIoJp+4lu4FBsnO/KZJ/XnSQ1E0aPFvA6/lI/atSwpUeIBm9fV16ALq 35gLJ0lrB9FyBhSGiBLODBrpLdC/Wn3iYnuWoWKSKYvneIHh4hSWdvnG7ToW+ERG3WZdDGNrokb n2OhCvf1ks2S3ZilXc1RsBbypvqOFQ6fpkq79JgeELRUqUlu4M6UQsuM1vy0CUvD/5i3nCNjSsQ 9lHYtJU+E89Cckj7qvXQvJYti5CRict6n1zDLMvbrWZg+Bjc/AhYKhfSoGfDaKrqwD/deCbZ8kK NDHoC2WBr2I9a0hIiR3E8lbYpWMTPg2JvnvwUY1p+mpb0S1jp6pTnA/Y1b6s1EmLyQYMmxWmSzA EIYBUo8l4JaMh+pPvctdFlisxwuRr6qMgEn2Q+1eaZlSIw+0TyRDl3FaIvUjywRRslsO0MFPPSb gNNCR701KjpILYvBCeppA== X-Received: by 2002:a05:6808:6d97:b0:467:4939:9666 with SMTP id 5614622812f47-46a5c784560mr3483785b6e.47.1774535850957; Thu, 26 Mar 2026 07:37:30 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 5614622812f47-46a7063c2fdsm1803869b6e.4.2026.03.26.07.37.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2026 07:37:30 -0700 (PDT) Date: Thu, 26 Mar 2026 09:37:28 -0500 From: Nathan Bossart To: Heikki Linnakangas Cc: Robert Haas , "pgsql-hackers@postgresql.org" Subject: Re: Clean up NamedLWLockTranche stuff Message-ID: References: <47aaf57e-1b7b-4e12-bda2-0316081ff50e@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <47aaf57e-1b7b-4e12-bda2-0316081ff50e@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Mar 26, 2026 at 02:16:52PM +0200, Heikki Linnakangas wrote: > At postmaster startup, NamedLWLockTrancheRequests points to a > backend-private array. But after startup, and always in backends, it points > to a copy in shared memory and LocalNamedLWLockTrancheRequestArray is used > to hold the original. It took me a while to realize that > NamedLWLockTrancheRequests in shared memory is *not* updated when you call > LWLockNewTrancheId(), it only holds the requests made with > RequestNamedLWLockTranche() before startup. Right. LocalNamedLWLockTrancheRequestArray is needed so that we can re-initialize shared memory after a crash. See commit c3cc2ab87d. > I propose the attached refactorings to make this less confusing. See commit > messages for details. Thanks for doing this, Heikki. I agree that we ought to make this stuff cleaner. I've asked Sami Imseih, who worked on LWLocks with me last year, to look at this patch set, too. > Subject: [PATCH v1 1/5] Rename MAX_NAMED_TRANCHES to MAX_USER_DEFINED_TRANCHES Seems fine to me. 0002: > + foreach(lc, NamedLWLockTrancheRequests) nitpick: These foreach loops seem like good opportunities to use foreach_ptr. The comment atop NumLWLocksForNamedTranches might benefit from mentioning RequestNamedLWLockTranche() and the fact that it only works in the postmaster. Perhaps an assertion is warranted, too. + SpinLockAcquire(ShmemLock); + LocalNumUserDefinedTranches = LWLockTranches->num_user_defined; + SpinLockRelease(ShmemLock); Not critical, but it might be worth making num_user_defined an atomic. Overall, 0002 looks reasonable to me upon a first read-through. > Subject: [PATCH v1 3/5] Use a separate spinlock to protect LWLockTranches Seems fine to me. 0004: > +++ b/src/backend/storage/ipc/shmem.c > @@ -379,7 +379,8 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr) > > Assert(ShmemIndex != NULL); > > - LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE); > + if (IsUnderPostmaster) > + LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE); Am I understanding that we assume ShmemInitStruct() is only called by the postmaster when there are no other backends yet? 0005: > - if (IsUnderPostmaster) > - LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE); > + LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE); Oh, this reverts many of these changes from 0004. Maybe the patches could be reordered to avoid this? -- nathan