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 1v5u79-00DiZm-79 for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Oct 2025 22:55:31 +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 1v5u77-00Bm0R-07 for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Oct 2025 22:55:29 +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.94.2) (envelope-from ) id 1v5u75-00Bm0I-Sj for pgsql-hackers@lists.postgresql.org; Mon, 06 Oct 2025 22:55:29 +0000 Received: from fhigh-b1-smtp.messagingengine.com ([202.12.124.152]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1v5u73-000OTb-1m for pgsql-hackers@postgresql.org; Mon, 06 Oct 2025 22:55:27 +0000 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfhigh.stl.internal (Postfix) with ESMTP id 2383A7A045B; Mon, 6 Oct 2025 18:55:25 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Mon, 06 Oct 2025 18:55:25 -0400 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=fm2; t=1759791324; x=1759877724; bh=hmGu55S42k JHzDQBdeCeV38Wej5mFXDfIo2NUXRTuw0=; b=wBq3WTMk0Gaj3dHvddxBZWxM9T uNypZkCV3uP1JmKpQHzCM0uMRVVj7C1/46xkfgyz4QwIJNAHKSOSTSddoFA1HRGW a4CF0lrSYGepv6HK9j49s9e7NqqA8jqEp7fybsKPwhAzUevQ4PIoefCFBwqqNvpN qRlxuZq4D4wr8b5FX0RXtDEjVpV64x0fpj7ZGweK5k+9LxrstvKbW0Dvgf/esk5k T8JQJk55TtfuQ0hFeh/rjXfaoZb8ZLUz9YjnuyOA7NLngC6AHH5ycTYnIxz/G/oz G7xAILml/LAk0CTgih1B0YxgZdUgt3e2/eyFmKehqtvNgjIZ7pOX6SQEXLtQ== 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=fm2; t= 1759791324; x=1759877724; bh=hmGu55S42kJHzDQBdeCeV38Wej5mFXDfIo2 NUXRTuw0=; b=Cw6Oqaj39LlAwoVpw3zAO2gYVKo9oGUg83pswNu0OC6lDOwi6Yo q90i80jXyGmve2x+CrdbgYW9xJ0FXyGPQaXRUuGswxMaMQwb3si3zddiGv7lvodO Y5P/smO7qhNw8dpOejbu44VTXlmx/CjTojtjfOYvjacmyD56AT4XSMv5UNUj1LGR AwCAFtxRfzX+YhxSSJQFNwGu9Rwyo5JeOZd0Vg1AZ8AdSuO2+x4/1rWuDjXgMWV5 zOx8APYSc/hZ++bd6uj6WiWQkflsfTNLBgeVRCntKJSZcpCXUAI2p4z8AKMw153s SCqN6VSA1mL3J+HnmVSofHBWVRj9m0njQIw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdelkeejlecutefuodetggdotefrod 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; Mon, 6 Oct 2025 18:55:24 -0400 (EDT) Date: Mon, 6 Oct 2025 18:55:23 -0400 From: Andres Freund To: Matthias van de Meent Cc: pgsql-hackers@postgresql.org, Melanie Plageman , Thomas Munro , Heikki Linnakangas , Noah Misch , Robert Haas , Michael Paquier Subject: Re: Buffer locking is special (hints, checksums, AIO writes) Message-ID: References: <6kmid26do57ykqfpvq6iieniy4djsymhrypkjccazq5g4bbe6a@2y6owwv7qpex> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2025-10-04 09:05:45 +0200, Matthias van de Meent wrote: > On Tue, 23 Sept 2025 at 00:14, Andres Freund wrote: > > On 2025-09-15 19:05:37 -0400, Andres Freund wrote: > > > Here are the first few cleaned up patches implementing the above steps, as > > > well as some cleanups. I included a commit from another thread, as it > > > conflicts with these changes, and we really should apply it - and it's > > > arguably required to make the changes viable, as it removes one more use of > > > PinBuffer_Locked(). > > > > > > Another change included is to not return the buffer with the spinlock held > > > from StrategyGetBuffer(), and instead pin the buffer in freelist.c. The reason > > > for that is to reduce the most common PinBuffer_locked() call. By definition > > > PinBuffer_locked() will become a bit slower due to 0003. But even without 0003 > > > it 0002 is faster than master. And the previous approach also just seems > > > pretty unclean. I don't love that it requires the new TrackNewBufferPin(), > > > but I don't really have a better idea. > > > > > > I invite particular attention to the commit message for 0003 as well as the > > > comment changes in buf_internals.h within. > > > > Robert looked at the patches while we were chatting, and I addressed his > > feedback in this new version. > > I like these changes, and have some minor comments: Thank for reviewing! > 0001 ensures that ReadRecentBuffer increments the usage counter, which > someone who uses an access strategy may want to prevent. I know this > isn't exactly new behaviour, but something I noticed anyway. Apart > from that observation, LGTM Are you proposing to change behaviour? Right now ReadRecentBuffer doesn't even accept a strategy, so I don't really see this as something that needs to be tackled at this point. I'm not sure I see any real use cases for ReadRecentBuffer() that would benefit from a strategy, but I very well might just not be thinking wide enough. > 0002 has a FIXME in a comment in GetVictimBuffer. Assuming it's about > the comment itself needing updates Indeed. > , how about: > > + * Ensure, before we pin a victim buffer, that there's a free refcount > + * entry, and a resource owner slot for the pin. > > Again, LGTM. WFM. > 0003's UnlockBufHdrExt: > This is implemented with CAS, even when we only want to change bits we > know the state of (or could know, if we spent the effort). > Given its inline nature, wouldn't it be better to use atomic_sub > instructions? Or is this to handle cases where the bits we want to > (un)set might be (un)set by a concurrent process? Yes, it's to handle concurrent changes to the buffer state. > If the latter, could we specialize this to do a single atomic_sub > whenever we want to change state bits that we know can be only changed > whilst holding the spinlock? We probably could optimize some cases as an atomic-sub, some others as an atomic-and and others again as an atomic-or. The latter to however are implemented as a CAS on x86 anyway... After 0004 I don't think any of the paths using this are actually particularly hot, so I'm somewhat doubtful it's worth to try to optimize this too much. If there are hot paths, we really should try to work towards not even needing the buffer header spinlock, that has a bigger impact that improving the code for unlocking the buffer header... Greetings, Andres Freund