Skip to content

Commit f4d0730

Browse files
anarazelmacdice
andcommitted
aio: Basic read_stream adjustments for real AIO
Adapt the read stream logic for real AIO: - If AIO is enabled, we shouldn't issue advice, but if it isn't, we should continue issuing advice - AIO benefits from reading ahead with direct IO - If effective_io_concurrency=0, pass READ_BUFFERS_SYNCHRONOUSLY to StartReadBuffers() to ensure synchronous IO execution There are further improvements we should consider: - While in read_stream_look_ahead(), we can use AIO batch submission mode for increased efficiency. That however requires care to avoid deadlocks and thus done separately. - It can be beneficial to defer starting new IOs until we can issue multiple IOs at once. That however requires non-trivial heuristics to decide when to do so. Reviewed-by: Noah Misch <noah@leadboat.com> Co-authored-by: Andres Freund <andres@anarazel.de> Co-authored-by: Thomas Munro <thomas.munro@gmail.com>
1 parent b27f863 commit f4d0730

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/backend/storage/aio/read_stream.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "postgres.h"
7373

7474
#include "miscadmin.h"
75+
#include "storage/aio.h"
7576
#include "storage/fd.h"
7677
#include "storage/smgr.h"
7778
#include "storage/read_stream.h"
@@ -99,6 +100,8 @@ struct ReadStream
99100
int16 pinned_buffers;
100101
int16 distance;
101102
int16 initialized_buffers;
103+
int read_buffers_flags;
104+
bool sync_mode; /* using io_method=sync */
102105
bool advice_enabled;
103106
bool temporary;
104107

@@ -250,7 +253,7 @@ read_stream_start_pending_read(ReadStream *stream)
250253
Assert(stream->next_buffer_index == stream->oldest_buffer_index);
251254

252255
/* Do we need to issue read-ahead advice? */
253-
flags = 0;
256+
flags = stream->read_buffers_flags;
254257
if (stream->advice_enabled)
255258
{
256259
if (stream->pending_read_blocknum == stream->seq_blocknum)
@@ -261,7 +264,7 @@ read_stream_start_pending_read(ReadStream *stream)
261264
* then stay of the way of the kernel's own read-ahead.
262265
*/
263266
if (stream->seq_until_processed != InvalidBlockNumber)
264-
flags = READ_BUFFERS_ISSUE_ADVICE;
267+
flags |= READ_BUFFERS_ISSUE_ADVICE;
265268
}
266269
else
267270
{
@@ -272,7 +275,7 @@ read_stream_start_pending_read(ReadStream *stream)
272275
*/
273276
stream->seq_until_processed = stream->pending_read_blocknum;
274277
if (stream->pinned_buffers > 0)
275-
flags = READ_BUFFERS_ISSUE_ADVICE;
278+
flags |= READ_BUFFERS_ISSUE_ADVICE;
276279
}
277280
}
278281

@@ -613,27 +616,33 @@ read_stream_begin_impl(int flags,
613616
stream->per_buffer_data = (void *)
614617
MAXALIGN(&stream->ios[Max(1, max_ios)]);
615618

619+
stream->sync_mode = io_method == IOMETHOD_SYNC;
620+
616621
#ifdef USE_PREFETCH
617622

618623
/*
619-
* This system supports prefetching advice. We can use it as long as
620-
* direct I/O isn't enabled, the caller hasn't promised sequential access
621-
* (overriding our detection heuristics), and max_ios hasn't been set to
622-
* zero.
624+
* Read-ahead advice simulating asynchronous I/O with synchronous calls.
625+
* Issue advice only if AIO is not used, direct I/O isn't enabled, the
626+
* caller hasn't promised sequential access (overriding our detection
627+
* heuristics), and max_ios hasn't been set to zero.
623628
*/
624-
if ((io_direct_flags & IO_DIRECT_DATA) == 0 &&
629+
if (stream->sync_mode &&
630+
(io_direct_flags & IO_DIRECT_DATA) == 0 &&
625631
(flags & READ_STREAM_SEQUENTIAL) == 0 &&
626632
max_ios > 0)
627633
stream->advice_enabled = true;
628634
#endif
629635

630636
/*
631-
* For now, max_ios = 0 is interpreted as max_ios = 1 with advice disabled
632-
* above. If we had real asynchronous I/O we might need a slightly
633-
* different definition.
637+
* Setting max_ios to zero disables AIO and advice-based pseudo AIO, but
638+
* we still need to allocate space to combine and run one I/O. Bump it up
639+
* to one, and remember to ask for synchronous I/O only.
634640
*/
635641
if (max_ios == 0)
642+
{
636643
max_ios = 1;
644+
stream->read_buffers_flags = READ_BUFFERS_SYNCHRONOUSLY;
645+
}
637646

638647
/*
639648
* Capture stable values for these two GUC-derived numbers for the
@@ -777,6 +786,11 @@ read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
777786

778787
if (likely(next_blocknum != InvalidBlockNumber))
779788
{
789+
int flags = stream->read_buffers_flags;
790+
791+
if (stream->advice_enabled)
792+
flags |= READ_BUFFERS_ISSUE_ADVICE;
793+
780794
/*
781795
* Pin a buffer for the next call. Same buffer entry, and
782796
* arbitrary I/O entry (they're all free). We don't have to
@@ -792,8 +806,7 @@ read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
792806
if (likely(!StartReadBuffer(&stream->ios[0].op,
793807
&stream->buffers[oldest_buffer_index],
794808
next_blocknum,
795-
stream->advice_enabled ?
796-
READ_BUFFERS_ISSUE_ADVICE : 0)))
809+
flags)))
797810
{
798811
/* Fast return. */
799812
return buffer;

0 commit comments

Comments
 (0)