]> git.ipfire.org Git - thirdparty/postgresql.git/commit
aio: Don't wait for already in-progress IO
authorAndres Freund <andres@anarazel.de>
Fri, 27 Mar 2026 23:51:53 +0000 (19:51 -0400)
committerAndres Freund <andres@anarazel.de>
Fri, 27 Mar 2026 23:53:32 +0000 (19:53 -0400)
commit999dec9ec6a81668057427c2e9312b20635fba02
treed1f9589f5e9f1ba5bc4bbd13eb1cd0c8a8532158
parent74eafeab1a576620879e889ec70bf826e3e7fce8
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@gmail.com>
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: Nazir Bilal Yavuz <byavuz81@gmail.com>
Discussion: https://postgr.es/m/flat/zljergweqti7x67lg5ije2rzjusie37nslsnkjkkby4laqqbfw%403p3zu522yykv
src/backend/storage/buffer/bufmgr.c
src/include/storage/bufmgr.h
src/test/modules/test_aio/t/001_aio.pl
src/test/modules/test_aio/test_aio--1.0.sql
src/test/modules/test_aio/test_aio.c