Skip to content

Commit 047cba7

Browse files
committed
bufmgr: Implement AIO read support
This commit implements the infrastructure to perform asynchronous reads into the buffer pool. To do so, it: - Adds readv AIO callbacks for shared and local buffers It may be worth calling out that shared buffer completions may be run in a different backend than where the IO started. - Adds an AIO wait reference to BufferDesc, to allow backends to wait for in-progress asynchronous IOs - Adapts StartBufferIO(), WaitIO(), TerminateBufferIO(), and their localbuf.c equivalents, to be able to deal with AIO - Moves the code to handle BM_PIN_COUNT_WAITER into a helper function, as it now also needs to be called on IO completion As of this commit, nothing issues AIO on shared/local buffers. A future commit will update StartReadBuffers() to do so. Buffer reads executed through this infrastructure will report invalid page / checksum errors / warnings differently than before: In the error case the error message will cover all the blocks that were included in the read, rather than just the reporting the first invalid block. If more than one block is invalid, the error will include information about the range of the read, the first invalid block and the number of invalid pages, with a HINT towards the server log for per-block details. For the warning case (i.e. zero_damaged_buffers) we would previously emit one warning message for each buffer in a multi-block read. Now there is only a single warning message for the entire read, again referring to the server log for more details in case of multiple checksum failures within a single larger read. Reviewed-by: Noah Misch <noah@leadboat.com> Reviewed-by: Melanie Plageman <melanieplageman@gmail.com> Reviewed-by: Nazir Bilal Yavuz <byavuz81@gmail.com> Discussion: https://postgr.es/m/uvrtrknj4kdytuboidbhwclo4gxhswwcpgadptsjvjqcluzmah%40brqs62irg4dt Discussion: https://postgr.es/m/20210223100344.llw5an2aklengrmn@alap3.anarazel.de Discussion: https://postgr.es/m/stj36ea6yyhoxtqkhpieia2z4krnam7qyetc57rfezgk4zgapf@gcnactj4z56m
1 parent ef64fe2 commit 047cba7

File tree

10 files changed

+885
-66
lines changed

10 files changed

+885
-66
lines changed

src/backend/storage/aio/aio_callback.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "miscadmin.h"
1919
#include "storage/aio.h"
2020
#include "storage/aio_internal.h"
21+
#include "storage/bufmgr.h"
2122
#include "storage/md.h"
2223

2324

@@ -40,6 +41,10 @@ static const PgAioHandleCallbacksEntry aio_handle_cbs[] = {
4041
CALLBACK_ENTRY(PGAIO_HCB_INVALID, aio_invalid_cb),
4142

4243
CALLBACK_ENTRY(PGAIO_HCB_MD_READV, aio_md_readv_cb),
44+
45+
CALLBACK_ENTRY(PGAIO_HCB_SHARED_BUFFER_READV, aio_shared_buffer_readv_cb),
46+
47+
CALLBACK_ENTRY(PGAIO_HCB_LOCAL_BUFFER_READV, aio_local_buffer_readv_cb),
4348
#undef CALLBACK_ENTRY
4449
};
4550

src/backend/storage/buffer/README

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ in the buffer. It is used per the rules above.
147147

148148
* The BM_IO_IN_PROGRESS flag acts as a kind of lock, used to wait for I/O on a
149149
buffer to complete (and in releases before 14, it was accompanied by a
150-
per-buffer LWLock). The process doing a read or write sets the flag for the
151-
duration, and processes that need to wait for it to be cleared sleep on a
152-
condition variable.
150+
per-buffer LWLock). The process starting a read or write sets the flag. When
151+
the I/O is completed, be it by the process that initiated the I/O or by
152+
another process, the flag is removed and the Buffer's condition variable is
153+
signalled. Processes that need to wait for the I/O to complete can wait for
154+
asynchronous I/O by using BufferDesc->io_wref and for BM_IO_IN_PROGRESS to be
155+
unset by sleeping on the buffer's condition variable.
153156

154157

155158
Normal Buffer Replacement Strategy

src/backend/storage/buffer/buf_init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
#include "postgres.h"
1616

17+
#include "storage/aio.h"
1718
#include "storage/buf_internals.h"
1819
#include "storage/bufmgr.h"
1920

@@ -125,6 +126,8 @@ BufferManagerShmemInit(void)
125126

126127
buf->buf_id = i;
127128

129+
pgaio_wref_clear(&buf->io_wref);
130+
128131
/*
129132
* Initially link all the buffers together as unused. Subsequent
130133
* management of this list is done by freelist.c.

0 commit comments

Comments
 (0)