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 1v6AkL-000PBj-GI for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Oct 2025 16:41:05 +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 1v6AkJ-001Ppb-6E for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Oct 2025 16:41:04 +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 1v6AkI-001PpQ-SS for pgsql-hackers@lists.postgresql.org; Tue, 07 Oct 2025 16:41:03 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v6AkG-000WYe-3A for pgsql-hackers@postgresql.org; Tue, 07 Oct 2025 16:41:02 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-57e03279bfeso96086e87.0 for ; Tue, 07 Oct 2025 09:41:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759855260; x=1760460060; 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=3GAByL5iQmeKFtAQf2JIG5p8YN+F+VTfgbIXFvBAhWc=; b=U9Bp9rZpKJDX0xr6gAyW/+oYJK9AdFoaYutgX/cO0sVkTw5GIePmpUfTwQsk3m8BR6 0AZ3DW+s6gpeKjMqCOg5abPqce6S4DAR0rpdfl2S3tplAc6eaE7tSzB1YohjrEOUuibB j0HkINkSxpJIgy0JLjNATBuojUNfGAbBUQyn4AX0oKpZHYAKLtkrDR/vyAcfaKcCDILg lmQVH/Qkqi/iYlH6eq8h1JYc7D9DiYcQcv4iqmE5k0OtHF5cZXGrWQqZwpscJiu115WX Vl96NdpiRNWfXZtXmvm+yIwIhjLMHKRwxVLgQPCzAlsbd7SCJ3uWSwNEX91iSqwn5PJu r7wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759855260; x=1760460060; 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=3GAByL5iQmeKFtAQf2JIG5p8YN+F+VTfgbIXFvBAhWc=; b=seoDJdYp/xxkjfvmlWDVWuzm0285Jkg8KhsFejF6/VlsdxFCutV50AYYGAI8FWw35b +ttRO41S5uoM/bpGldT1sLEV/YNFgmwTe39EHM30ns90MBwQ/WunVZYg5Vkjbe2wHALE 9DAJSGVL8oUfe0Y8kpI9Tv4j9Ur66i44N5dX18+AeZD3pIfznXUqu++wvu6bvtghN8uI x2iS0i1fOWKG6OqVYLHSG6Y79EO/uCCqinjBcrEiqxf+q7N09ydTw8+y3iAyl/gD5w+J 1pa0kYjJZSeonj6422M3m75LRFQsWHESyuV4a3YB1OVqnBPk5Uj0LPdz8zAsbuL0Skdd kSWA== X-Gm-Message-State: AOJu0Ywc80FoGxAeLwZHT+TQeTRtamAvdo54VHMjcryET0YevaY45kaa dxaYmmUc/DXnj2Xl+43+igrOkZxcZ4q+QOORJ5IFZ/XRbueeT/LyEQmD32izXn5qDIVURWZDl3b 6QzX/mAxT6gu+uK3YlfpZ3p795O9XUio= X-Gm-Gg: ASbGnctBhBdmEWPwBTo6hgikI5AL6DbH7YjFmiswAhpXRi9gacrXszVj9Ch4XBVeIlv BfHAWOlISgVTfdhETdNtn597aouW8y29K+zI8p0H7AyfR0bSy2giY7U39iFoxTLYocCOhGo4peZ hGq1yfFi6/923dpPjcorPM/ZMw0DJHBRDWNGJSp3CoL7r0w2PbqCvVbwI98DXyeEyABdNkafNf3 8Ana0WKgW8soGBtpClnWUMiJwrfxbU= X-Google-Smtp-Source: AGHT+IFfK8uuLzIiT+UNHT7QTO+IQIohqhK3VpepqmeG612U1AK63kCJckajOTgA1xRfoipICWE8rcBkNgq+euGRfhU= X-Received: by 2002:a05:6512:31c3:b0:583:9860:1405 with SMTP id 2adb3069b0e04-5905e1d2d85mr1280158e87.4.1759855259909; Tue, 07 Oct 2025 09:40:59 -0700 (PDT) MIME-Version: 1.0 References: <6kmid26do57ykqfpvq6iieniy4djsymhrypkjccazq5g4bbe6a@2y6owwv7qpex> In-Reply-To: From: Matthias van de Meent Date: Tue, 7 Oct 2025 18:40:48 +0200 X-Gm-Features: AS18NWDVyeHJBc-HQNmGfuQSyigTiyoPukO7MkjnT2axhZxP_u8uB-5rPCjfJMo 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 Hi, On Tue, 7 Oct 2025 at 00:55, Andres Freund wrote: > On 2025-10-04 09:05:45 +0200, Matthias van de Meent wrote: > > 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? Eventually, yes, but not necessarily now or with this patchset. > 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. I think it's rather strange that there is no Extended variant of ReadRecentBuffer, like how there is a ReadBufferExtended for ReadBuffer. Yes, ReadRecentBuffer has more arguments to fill and so has a smaller difference versus ReadBufferExtended, but it's no complete replacement for ReadBuffer[Ext] when you're aware of a recent buffer of the page. I'm not saying it will definitely happen, but I could see that e.g. amcheck might want to keep track of buffer IDs of recent heap pages it accessed to verify index's results, without holding a pin on all the pages; and instead using ReadRecentBuffer[Extended] with a BufferAccessStrategy to allow re-acquiring the buffer pin without blowing out shared buffers or making parts of the pool take forever to evict again. > > 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... Fair enough; I guess we'll see if further optimization would have much impact once this all has been committed. Kind regards, Matthias van de Meent Databricks