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 1vWM94-00HT87-15 for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Dec 2025 22:06:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWM92-004QPc-1u for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Dec 2025 22:06:49 +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.96) (envelope-from ) id 1vWM91-004QPU-1p for pgsql-hackers@lists.postgresql.org; Thu, 18 Dec 2025 22:06:49 +0000 Received: from fout-b8-smtp.messagingengine.com ([202.12.124.151]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vWM90-001Oqn-1p for pgsql-hackers@postgresql.org; Thu, 18 Dec 2025 22:06:47 +0000 Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfout.stl.internal (Postfix) with ESMTP id 5BAB11D000D3; Thu, 18 Dec 2025 17:06:44 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Thu, 18 Dec 2025 17:06:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1766095604; x=1766182004; bh=j1pA9eddK6 77N9HUnpn/mnuGbrhQOFiiKHKplRIvaSs=; b=Io77dyZnzwHLiatUMa9MjtR1gx lxATCkKpaxlIKa6nU/CTMAML1Y29nCIYwLSZVkdWqMSwfgjCltwldOsmKcgolhlo X1Stb6jOtGYyOmpX6YZAgkQ9WojBUqVV+fXjmS5SsMFhEEr5ykz1ssUPp6kNRUMw oze/xatSbayr58m7kVi6HFVgfzqbE0WBZScoFQsvZEmLbhw0OAG4HthdEu1eMK4e qyXqoDNyehc82aOcxmy/mVs58lOc7NX65BGm512vR2hPOb3eOzZa9eNIiTCZInJK Vd+pUIloHnkzgUsj4nHtqd2dUaFzuuqRynWalMqP8tLPE4Fqga4EfntRRZwg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1766095604; x=1766182004; bh=j1pA9eddK677N9HUnpn/mnuGbrhQOFiiKHK plRIvaSs=; b=L8hnv2x6jc6Csws9i/TuhfzOBr4yWG9T11SKep8HtB6OH90ZDLj iDBkzaCCcAp/vqSK0QMXC5Q5Ce3ffvATXwDSYp7vwI7bgFDjr8pR888BXawe9jkX T1oC0oMVUQQnd4m7Hofz3Yw1oLFlu8IiMRnxEJKHsgTTARxVmRx0RxHZvDhiwpIs diqlA0LoQdWO4iUoghQ1tROpRxExIrmfO9w3HPcEw7TNQQk/MTPlpHtP3MPIfmmN e2z8/RCoMcAwR6T21YZnzG36lgtDTPhKN/TOm4sHfjGMh0OBtImjycoCdOo2QCNs dJO19BjGDZvK5UYLCGkKsoaQNDEXMlGYedg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgdegieehkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtuggjsehttdfstddttddvnecuhfhrohhmpeetnhgurhgvshcu hfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucggtffrrghtth gvrhhnpeeffffgledvffegtdevlefgtdeggffhvdekgfegteeiveejkeetudelveejhfeu geenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrnh gurhgvshesrghnrghrrgiivghlrdguvgdpnhgspghrtghpthhtohepkedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepsghovghkvgifuhhrmhdophhoshhtghhrvghssehgmh grihhlrdgtohhmpdhrtghpthhtohepmhgvlhgrnhhivghplhgrghgvmhgrnhesghhmrghi lhdrtghomhdprhgtphhtthhopehmihgthhgrvghlrdhprghquhhivghrsehgmhgrihhlrd gtohhmpdhrtghpthhtoheprhhosggvrhhtmhhhrggrshesghhmrghilhdrtghomhdprhgt phhtthhopehthhhomhgrshdrmhhunhhrohesghhmrghilhdrtghomhdprhgtphhtthhope hhlhhinhhnrghkrgesihhkihdrfhhipdhrtghpthhtohepnhhorghhsehlvggruggsohgr thdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehpohhsthhgrhgvsh hqlhdrohhrgh X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Dec 2025 17:06:43 -0500 (EST) Date: Thu, 18 Dec 2025 17:06:43 -0500 From: Andres Freund To: Heikki Linnakangas Cc: Melanie Plageman , Matthias van de Meent , pgsql-hackers@postgresql.org, Thomas Munro , Noah Misch , Robert Haas , Michael Paquier Subject: Re: Buffer locking is special (hints, checksums, AIO writes) Message-ID: <5dwlfu2jyzkyf3nrlzxxblxctb6xio5es73ptgsahjnmfu5miu@772rc764hfhi> References: <3je3ahgf7rrmmurxo6hnlhg5d3ffwfrtjwjxd6jm5srlv5iebp@vxqk5qtgmowr> <3w7v3w6a57jnssokap4k7thoekig72flnyhd4wp3yftzdd7lm7@f6lpcfen6hr7> <6rgb2nvhyvnszz4ul3wfzlf5rheb2kkwrglthnna7qhe24onwr@vw27225tkyar> <3nce7i72ayzkunai6mkz24ckbxk74jodz4ua2chcdrwppxlxcd@w6x5kfkjrkru> <1108f18d-cf7c-4f17-b29c-a119fe42f7e5@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1108f18d-cf7c-4f17-b29c-a119fe42f7e5@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2025-12-18 19:20:38 +0200, Heikki Linnakangas wrote: > On 18/12/2025 19:03, Andres Freund wrote: > > On 2025-12-17 09:54:32 -0500, Andres Freund wrote: > > > On 2025-12-17 11:25:50 +0200, Heikki Linnakangas wrote: > > > > - LWLockWaitListLock() uses pg_atomic_read_u32() after spinning, > > > > LockBufHdr() retries directly with pg_atomic_fetch_or_u32(). > > > > > > I think here LWLockWaitListLock() is likely right - but it seems like a change > > > to LockBufHdr() that I would probably make in a separate commit? > > > > FWIW, I couldn't come up with a scenario where it makes a performance > > difference - exclusive content locks just aren't *that* frequent. And because > > of that the wait list lock doesn't have similar contention as some non-content > > lwlocks (like XidGenLock). The most extreme workload I could think of was > > pgbench hammering a single sequence across many sessions. While the exclusive > > locks show up in wait events, the buffer header spinlock itself doesn't.. > > > > So I'm inclined to not change anything about this for now. > > Ok. My thinking was just that LockBufHdr() and LWLockWaitListLock() should > be consistent with each other. Otherwise anyone reading the code will ask > the question "why are they different?". They're the only two things using > the spin delay mechanism in our codebase, in addition to actual spinlocks. I guess for me it didn't really seem like this patch's job to fix that. Regardless of that, here's a version that tries to make them more similar. I did check, adding a likely() to LWLockWaitListLock()'s break does improve code generation (verified by looking at the generated code) and seems to improve performance in some very extreme workloads (e.g. [1]) a bit. I'll try to come up with a combined patch that applies the optimizations in LWLockWaitListLock() and LockBufHdr() to each other. > BTW, I wonder if it would be worthwhile to have an inlineable fast-path of > LockBufHdr() for the common case that the lock is free? I see that > UnlockBufHdr() is already a static inline function. I tried that a while ago and couldn't see any improvement, I think because all the performance relevant callers are in bufmgr.c and thus can already inline [parts of] the implementation. I guess you could make the generated code a bit smaller if you use pg_noinline on the slowpath, but that seems like a separate project / effort to me. Greetings, Andres Freund [1] Many connections doing DO $do$ BEGIN FOR i IN 1 .. 1000 LOOP PERFORM txid_current(); COMMIT; END LOOP; END; $do$;