pgsql: aio: Don't wait for already in-progress IO

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: aio: Don't wait for already in-progress IO
Date: 2026-03-28 00:01:37
Message-ID: E1w6H7Q-001lTS-2X@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

aio: Don't wait for already in-progress IO

When a backend attempts to start a read IO and finds the first buffer already
has I/O in progress, previously it waited for that I/O to complete before
initiating reads for any of the subsequent buffers.

Although it must wait for the I/O to finish when acquiring the buffer, there's
no reason for it to wait when setting up the read operation. Waiting at this
point prevents starting I/O on subsequent buffers and can significantly reduce
concurrency.

This matters in two workloads:
1) When multiple backends scan the same relation concurrently.
2) When a single backend requests the same block multiple times within the
readahead distance.

Waiting each time an in-progress read is encountered effectively degenerates
the access pattern into synchronous I/O.

To fix this, when encountering an already in-progress IO for the head buffer,
the wait reference is now recorded and waiting is deferred until
WaitReadBuffers(), when the buffer actually needs to be acquired.

In rare cases, a backend may still need to wait synchronously at IO
start time: If another backend has set BM_IO_IN_PROGRESS on the buffer
but has not yet set the wait reference. Such windows should be brief and
uncommon.

Author: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Author: Andres Freund <andres(at)anarazel(dot)de>
Reviewed-by: Andres Freund <andres(at)anarazel(dot)de>
Reviewed-by: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Reviewed-by: Nazir Bilal Yavuz <byavuz81(at)gmail(dot)com>
Discussion: https://postgr.es/m/flat/zljergweqti7x67lg5ije2rzjusie37nslsnkjkkby4laqqbfw%403p3zu522yykv

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/999dec9ec6a81668057427c2e9312b20635fba02

Modified Files
--------------
src/backend/storage/buffer/bufmgr.c | 89 ++++++++++--
src/include/storage/bufmgr.h | 4 +-
src/test/modules/test_aio/t/001_aio.pl | 215 +++++++++++++++++++++++++---
src/test/modules/test_aio/test_aio--1.0.sql | 2 +-
src/test/modules/test_aio/test_aio.c | 16 ++-
5 files changed, 287 insertions(+), 39 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2026-03-28 14:57:34 pgsql: Make cast functions from jsonb error safe
Previous Message Andres Freund 2026-03-27 23:17:19 pgsql: bufmgr: Improve StartBufferIO interface