pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Yura Sokolov <y.sokolov@postgrespro.ru>
To: Andres Freund <andres@anarazel.de>
To: Melanie Plageman <melanieplageman@gmail.com>
Cc: Noah Misch <noah@leadboat.com>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Kirill Reshke <reshkekirill@gmail.com>
Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: pgsql-hackers@postgresql.org, Thomas Munro <thomas.munro@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Michael Paquier <michael.paquier@gmail.com>
Subject: Re: Buffer locking is special (hints, checksums, AIO writes)
Date: Tue, 31 Mar 2026 19:02:33 +0300
Message-ID: <5bf667f3-5270-4b19-a08f-0facbecdff68@postgrespro.ru> (raw)
In-Reply-To: <q6trx7a5vd3j5tddamwxahrojopboov6eeue2jkjmfpkhdfgjj@w3hapotwxroo>
References: <CALdSSPgyc7VuMLUZ8J7v2G-PebBa_vEi+mp-cLkcOwNycB56Hw@mail.gmail.com>
	<kjdgvws34gpnz4y6xu5aaeul5mspgy3ahvyiw4lndb3ecsacdb@b2ccyowotpqh>
	<jtg5cu4n6h5lib3kzx66ju4yhh6kmviaud7oq6dtut6c4q4rdi@xwsfoagt3c2b>
	<k5j77f3q6ztihnjnx2nqxzyor6fbj2qxcbhzuxhkh2yy63jyfg@p72phigar3n4>
	<5ubipyssiju5twkb7zgqwdr7q2vhpkpmuelxfpanetlk6ofnop@hvxb4g2amb2d>
	<68e89de8-5f6c-4eaf-a800-e16a5e487667@iki.fi>
	<20260215195239.ce.noahmisch@microsoft.com>
	<mheeefrtikvgjnjsenocvo3afj7vlpr5rljkzevssxs77n2zdt@sugqtjhryydo>
	<CAAKRu_Z=xrBWhAB5wRG8x-gnjNa7ZnW-sSsEbBfR1C8gwuTD8g@mail.gmail.com>
	<yn4f45z6wl6wstuwha3qjsicrc6uvymkldtqz2ttchz7zdue3y@n6fll5dcter4>
	<q6trx7a5vd3j5tddamwxahrojopboov6eeue2jkjmfpkhdfgjj@w3hapotwxroo>

27.03.2026 23:00, Andres Freund wrote:
> Hi,
> 
> On 2026-03-25 18:35:55 -0400, Andres Freund wrote:
>> Running it through valgrind and then will work on reading through one more
>> time and pushing them.
> 
> And done.
> 
> Phew, this project took way longer than I'd though it'd take.

In addition to bug with BM_IO_ERROR [1] , I found race condition in
PinBuffer in this lines of code:

	if (unlikely(skip_if_not_valid && !(old_buf_state & BM_VALID)))
		return false;

	/*
	 * We're not allowed to increase the refcount while the buffer
	 * header spinlock is held. Wait for the lock to be released.
	 */
	if (old_buf_state & BM_LOCKED)
		old_buf_state = WaitBufHdrUnlocked(buf);

While we waited for buffer header for being unlocked, it may become
invalid, isn't it?
Therefore, check related to skip_if_not_valid have to happen after waiting.

....

Another question: previously we had to wait for buffer for being unlocked
because UnlockBufHdr wrote to buf->state unconditionally, therefore our pin
increment could be lost.
Now UnlockBufHdr and UnlockBufHdrExt does proper atomic operations and
preserves concurrent changes. Are we still need to wait?

Most of time PinBuffer is called protected by BufTable's partition LWLock,
therefore buffer may not be changed in dramatic way.

But call in ReadRecentBuffer is the exception. It is not protected by
partition lock and have to make additional checks. That is why you
introduced skip_if_not_valid.

Does optimization of ReadRecentBuffer pays for WaitBufHdrUnlocked?

[1]
https://www.postgresql.org/message-id/57a8ea70-32a8-4596-bb68-5d2990b83380%40postgrespro.ru

-- 
regards
Yura Sokolov aka funny-falcon





view thread (120+ messages)  latest in thread

Message-ID: <5bf667f3-5270-4b19-a08f-0facbecdff68@postgrespro.ru>
Permalink:  ../5bf667f3-5270-4b19-a08f-0facbecdff68@postgrespro.ru/
Also on:    postgresql.org/message-id/5bf667f3-5270-4b19-a08f-0facbecdff68@postgrespro.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: y.sokolov@postgrespro.ru, andres@anarazel.de, melanieplageman@gmail.com, noah@leadboat.com, hlinnaka@iki.fi, reshkekirill@gmail.com, boekewurm+postgres@gmail.com, thomas.munro@gmail.com, robertmhaas@gmail.com, michael.paquier@gmail.com
  Subject: Re: Buffer locking is special (hints, checksums, AIO writes)
  In-Reply-To: <5bf667f3-5270-4b19-a08f-0facbecdff68@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox