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 1v4wLE-009NzH-JC for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Oct 2025 07:06:04 +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 1v4wLC-00GVyv-HO for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Oct 2025 07:06:03 +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 1v4wLC-00GVyn-52 for pgsql-hackers@lists.postgresql.org; Sat, 04 Oct 2025 07:06:02 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v4wL9-000JxO-39 for pgsql-hackers@postgresql.org; Sat, 04 Oct 2025 07:06:02 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-3682ac7f33fso32811391fa.0 for ; Sat, 04 Oct 2025 00:05:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759561558; x=1760166358; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=93i0La8cjQ8pUgZVUyfY9NT/DsoEs30Zm5sgpROVLD4=; b=NxY4yNeEQc1UD1S4JLUCBtTYhjO2Y6l9jgM6F2AsB/6DZV1B+ArRt8YHyisC/5PN+E aNmL2LBE6QT5b3yy22h+ApPDS+TrsUSkviqVvs44YqLXKEOQjkRYMnFaWsalR6iR9XGT K7yW8W1wJZbJlCtyZV9j6OCynUSX/RXW23Qdf/Cue5amRVPD2pb6jZoT8Ni+cxEATABQ hk9Aw/6ghdXhZZNxSG9Y2toSEROdt8S1AWMSsEeDdg2ImKiwbV1u8qgn7Byl7EAh/pOz ebabeHQbYMlSFV1T4WAUI2rP5YFBl1mF15D7FKEsciOM62c352f5i2kICljeuRvPpoRU 3eTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759561558; x=1760166358; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=93i0La8cjQ8pUgZVUyfY9NT/DsoEs30Zm5sgpROVLD4=; b=hLkSFya8SmuZhQr+GhGvOajGSUgREOn+lXBkR9z/f4SKlg5/zCK4MWB/x7vNZCrsq8 PArfkIg2+CfxnaevcNuq7ZqgyjgRI9uLkBLov9S10vhV+VFq7Ku/QFGXqiDUaqQTuqB0 dR/WudDMgueX0CnjPes8lxvu88zwprDl06yI7V9MwGsOJKptnWND4ngWqGN9GxyA99lt BEmS/tDTDXJto4N3VJAt5SkH2S/le31hslSMGoqtmCntSbE4vbqZqdrmO1cXR6W2x/C4 L/R1fgvTZOm/50LMXxHf58yh1SdPnqywaez6/bymbCgbBLGuU+Oylo6haozN4tw+um13 SFDA== X-Gm-Message-State: AOJu0YyPinwfF+VmEAghhCbw7BdcgniVGKTn9t+X1Y75p4qwpeAGry9E LOZFlBV7kMrXjvC6SBIy1dMpyg1jv+pozONzP82Ye2VTHgQLmBTCHKpKJWe2jYp0oBxKgoh1B1Z JPXcGyz5V9XzHbZyFRVrqHXGP8/ptQcA= X-Gm-Gg: ASbGncuVd0dHUeTv34fwrM8WipfZjJJlLj8aElIjEtO4OYtcr873nw8Ybox7iL/cHEf 2YJC2W3ITUdJtGK+tc5R9kw3TIfNrwHkDAkNbGe2cFVzA+6OcejokbkoUIfccgCC+wa///Qktst fW+I327WrXneHajntqvWUeZFwO+Y34k+FfDHcUgAmFQk7FbDYtc3q+1b5uXyMa+NU4IbRJ5T3xV /TqeAZnS4TKlSQNIp8/yYJnGSBpdhYfOUSy5UAs X-Google-Smtp-Source: AGHT+IHnXOyyEt3LaTozAskPPtpUTrXlMkaqtKBLarYTzgMPuXCt+N43pWWzfEAQSIssMGs1YshozMXU1pzQv1On8k0= X-Received: by 2002:a05:651c:2113:b0:364:c083:2fec with SMTP id 38308e7fff4ca-374c2483d35mr15918351fa.7.1759561557467; Sat, 04 Oct 2025 00:05:57 -0700 (PDT) MIME-Version: 1.0 References: <6kmid26do57ykqfpvq6iieniy4djsymhrypkjccazq5g4bbe6a@2y6owwv7qpex> In-Reply-To: <6kmid26do57ykqfpvq6iieniy4djsymhrypkjccazq5g4bbe6a@2y6owwv7qpex> From: Matthias van de Meent Date: Sat, 4 Oct 2025 09:05:45 +0200 X-Gm-Features: AS18NWD6fw3_ttRLdha0AcYZms9PRej2MNQXMypMgQPadHBFmiCmSnqe4ynAZEo Message-ID: Subject: Re: Buffer locking is special (hints, checksums, AIO writes) To: Andres Freund Cc: pgsql-hackers@postgresql.org, Melanie Plageman , Thomas Munro , Heikki Linnakangas , Noah Misch , Robert Haas , Michael Paquier Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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: 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 0002 has a FIXME in a comment in GetVictimBuffer. Assuming it's about the comment itself needing updates, 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. 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? 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? 0004: LGTM 0005: LGTM 0006: LGTM Kind regards, Matthias van de Meent