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 1w5mXB-003ckb-0U for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 15:22:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5mX9-003PHJ-1H for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 15:22:07 +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 1w5mX9-003PHB-08 for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 15:22:07 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w5mX6-00000001IyH-19cr for pgsql-hackers@postgresql.org; Thu, 26 Mar 2026 15:22:07 +0000 Received: from [10.0.2.15] (unknown [137.83.235.84]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4fhSDp6KM8z49Q53; Thu, 26 Mar 2026 17:22:02 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1774538523; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1CQQ3zUAC0HhcsLbr27w2WlDWXgvZqmC77V/FATt/Ms=; b=u+nA8CwJcElKndsffHmnDhBS8x/+P9F2AeX+E1CsvMxXYLmREcko2DCvy/hJ7zK7HAqaCs 6kWnuoYNvHWRLWAYptUbjKUt47MQD9kVwdpkgta9dkXEGyGlnA4s3TasX540O6vNd3V6Sd q5YbnJY9Ujd3y93HN8gnecpyF9J71epwmzdKzgXaBrrONk03XEG+xMZDWAtbjnZhr5HsVq CaYdjaWR/twV2j6aa5LTXX9xQq9eW0mGQnnCZY3UDRakS/spfA0dGgRT1aAtRaZM80iw24 Ap6HtYi1qqU13ue5gLfIRdfiMRZBP5HFW7M1I94jOUz/AEmRA/YeG07KPa8RkA== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1774538523; b=VwGjzk6iAi9qVBdfFUi2KQBNse9swWRNwZ7jg9yNxRzaGVP266aeynob8rZJnlpvUKv1/k b978hBLpYFVAD1+V4S7mECJq9O/TDaCj3tJOqhmYMjVt/YrubFPL51zO4b7uyDcz1yLVqz oCeoTEc6iKB9QBA6wvF4LHQLfmnsDZx3VQX+wBHzhn3aKufPtDNko+rLjyLR6kU4doxtWH kYkBw4q/bXSD/jFB+dfoaJ30A7KJai2c4Am0tDxg5pcMbRKE5F9CO1IjWu74NoReMGsvku n4d0dPhfARc+vOvXk22KGKGW7G0HIjsVrXIUPrX6zKZQ0qhyX6citnh/vk8QeA== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1774538523; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1CQQ3zUAC0HhcsLbr27w2WlDWXgvZqmC77V/FATt/Ms=; b=kG+DPgGgBZLxEV22bm8hp1ey/86NLZiCnAHBX583TMwRNx1Pwddsdy3tS6fO/wv9SkfjAA 1LZ2wOLlIY2AEnYoPqQwU9HkHgxmuloa+Tr/x7AotrA/cFRrng220+b0k9v3mBmafRZOAE Yae6W94U6gyBh4hqNQREK/TLOdAzbVcU6SLUsCVn1XBXlK85kz5xkq0QHPul3Q68GJWg1G ibCyAA7NDO2/EB2dDrNiLYk4S/KLcGfgeUds4EOrS7GIWRUUWSebVRbk9mszRA/7Fe/q13 DKfh9hKZZ4AQ4ssz0zokkEWsPxVxxx3DNTGB9aK8jfaEsfWEXyq+v5FBuCsG5Q== Message-ID: Date: Thu, 26 Mar 2026 17:21:58 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Clean up NamedLWLockTranche stuff To: Nathan Bossart Cc: Robert Haas , "pgsql-hackers@postgresql.org" References: <47aaf57e-1b7b-4e12-bda2-0316081ff50e@iki.fi> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 26/03/2026 16:37, Nathan Bossart wrote: > On Thu, Mar 26, 2026 at 02:16:52PM +0200, Heikki Linnakangas wrote: >> 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. There's already this check in RequestNamedLWLockTranche(): if (!process_shmem_requests_in_progress) elog(FATAL, "cannot request additional LWLocks outside shmem_request_hook"); shmem_request_hooks are only called early at postmaster startup. > + SpinLockAcquire(ShmemLock); > + LocalNumUserDefinedTranches = LWLockTranches->num_user_defined; > + SpinLockRelease(ShmemLock); > > Not critical, but it might be worth making num_user_defined an atomic. Yeah I considered that. The lock is still needed in LWLockNewTrancheId(), though, to prevent two concurrent LWLockNewTrancheId() calls from running concurrently. Using an atomic would allow the extra optimization of reading the value without acquiring spinlock, but it seems more clear to have a clear-cut rule that you must always hold the spinlock whenever accessing the field. > 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? Yeah. LWLockAcquire has this: /* * We can't wait if we haven't got a PGPROC. This should only occur * during bootstrap or shared memory initialization. Put an Assert here * to catch unsafe coding practices. */ Assert(!(proc == NULL && IsUnderPostmaster)); To be honest I didn't realize we tolerate that, calling LWLockAcquire in postmaster, until I started to work on this. It might be worth having some extra sanity checks here, to e.g. to throw an error if LWLockAcquire is called from postmaster after startup. But this isn't new. > 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? Makes sense. Thanks for the review! - Heikki