Skip to content

Commit 0284401

Browse files
committed
aio: Basic subsystem initialization
This commit just does the minimal wiring up of the AIO subsystem, added in the next commit, to the rest of the system. The next commit contains more details about motivation and architecture. This commit is kept separate to make it easier to review, separating the changes across the tree, from the implementation of the new subsystem. We discussed squashing this commit with the main commit before merging AIO, but there has been a mild preference for keeping it separate. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Noah Misch <noah@leadboat.com> Discussion: https://postgr.es/m/uvrtrknj4kdytuboidbhwclo4gxhswwcpgadptsjvjqcluzmah%40brqs62irg4dt
1 parent 65db396 commit 0284401

File tree

24 files changed

+356
-0
lines changed

24 files changed

+356
-0
lines changed

doc/src/sgml/config.sgml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,57 @@ include_dir 'conf.d'
26382638
</para>
26392639
</listitem>
26402640
</varlistentry>
2641+
2642+
<varlistentry id="guc-io-max-concurrency" xreflabel="io_max_concurrency">
2643+
<term><varname>io_max_concurrency</varname> (<type>integer</type>)
2644+
<indexterm>
2645+
<primary><varname>io_max_concurrency</varname> configuration parameter</primary>
2646+
</indexterm>
2647+
</term>
2648+
<listitem>
2649+
<para>
2650+
Controls the maximum number of I/O operations that one process can
2651+
execute simultaneously.
2652+
</para>
2653+
<para>
2654+
The default setting of <literal>-1</literal> selects a number based
2655+
on <xref linkend="guc-shared-buffers"/> and the maximum number of
2656+
processes (<xref linkend="guc-max-connections"/>, <xref
2657+
linkend="guc-autovacuum-worker-slots"/>, <xref
2658+
linkend="guc-max-worker-processes"/> and <xref
2659+
linkend="guc-max-wal-senders"/>), but not more than
2660+
<literal>64</literal>.
2661+
</para>
2662+
<para>
2663+
This parameter can only be set at server start.
2664+
</para>
2665+
</listitem>
2666+
</varlistentry>
2667+
2668+
<varlistentry id="guc-io-method" xreflabel="io_method">
2669+
<term><varname>io_method</varname> (<type>enum</type>)
2670+
<indexterm>
2671+
<primary><varname>io_method</varname> configuration parameter</primary>
2672+
</indexterm>
2673+
</term>
2674+
<listitem>
2675+
<para>
2676+
Selects the method for executing asynchronous I/O.
2677+
Possible values are:
2678+
<itemizedlist>
2679+
<listitem>
2680+
<para>
2681+
<literal>sync</literal> (execute asynchronous-eligible I/O synchronously)
2682+
</para>
2683+
</listitem>
2684+
</itemizedlist>
2685+
</para>
2686+
<para>
2687+
This parameter can only be set at server start.
2688+
</para>
2689+
</listitem>
2690+
</varlistentry>
2691+
26412692
</variablelist>
26422693
</sect2>
26432694

src/backend/access/transam/xact.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "replication/origin.h"
5252
#include "replication/snapbuild.h"
5353
#include "replication/syncrep.h"
54+
#include "storage/aio_subsys.h"
5455
#include "storage/condition_variable.h"
5556
#include "storage/fd.h"
5657
#include "storage/lmgr.h"
@@ -2411,6 +2412,8 @@ CommitTransaction(void)
24112412
RESOURCE_RELEASE_BEFORE_LOCKS,
24122413
true, true);
24132414

2415+
AtEOXact_Aio(true);
2416+
24142417
/* Check we've released all buffer pins */
24152418
AtEOXact_Buffers(true);
24162419

@@ -2716,6 +2719,8 @@ PrepareTransaction(void)
27162719
RESOURCE_RELEASE_BEFORE_LOCKS,
27172720
true, true);
27182721

2722+
AtEOXact_Aio(true);
2723+
27192724
/* Check we've released all buffer pins */
27202725
AtEOXact_Buffers(true);
27212726

@@ -2830,6 +2835,8 @@ AbortTransaction(void)
28302835
pgstat_report_wait_end();
28312836
pgstat_progress_end_command();
28322837

2838+
pgaio_error_cleanup();
2839+
28332840
/* Clean up buffer content locks, too */
28342841
UnlockBuffers();
28352842

@@ -2960,6 +2967,7 @@ AbortTransaction(void)
29602967
ResourceOwnerRelease(TopTransactionResourceOwner,
29612968
RESOURCE_RELEASE_BEFORE_LOCKS,
29622969
false, true);
2970+
AtEOXact_Aio(false);
29632971
AtEOXact_Buffers(false);
29642972
AtEOXact_RelationCache(false);
29652973
AtEOXact_TypeCache();
@@ -5232,6 +5240,9 @@ AbortSubTransaction(void)
52325240

52335241
pgstat_report_wait_end();
52345242
pgstat_progress_end_command();
5243+
5244+
pgaio_error_cleanup();
5245+
52355246
UnlockBuffers();
52365247

52375248
/* Reset WAL record construction state */
@@ -5326,6 +5337,7 @@ AbortSubTransaction(void)
53265337
RESOURCE_RELEASE_BEFORE_LOCKS,
53275338
false, false);
53285339

5340+
AtEOXact_Aio(false);
53295341
AtEOSubXact_RelationCache(false, s->subTransactionId,
53305342
s->parent->subTransactionId);
53315343
AtEOSubXact_TypeCache();

src/backend/postmaster/autovacuum.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
#include "postmaster/autovacuum.h"
8989
#include "postmaster/interrupt.h"
9090
#include "postmaster/postmaster.h"
91+
#include "storage/aio_subsys.h"
9192
#include "storage/bufmgr.h"
9293
#include "storage/ipc.h"
9394
#include "storage/latch.h"
@@ -465,6 +466,7 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len)
465466
*/
466467
LWLockReleaseAll();
467468
pgstat_report_wait_end();
469+
pgaio_error_cleanup();
468470
UnlockBuffers();
469471
/* this is probably dead code, but let's be safe: */
470472
if (AuxProcessResourceOwner)

src/backend/postmaster/bgwriter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "postmaster/auxprocess.h"
3939
#include "postmaster/bgwriter.h"
4040
#include "postmaster/interrupt.h"
41+
#include "storage/aio_subsys.h"
4142
#include "storage/buf_internals.h"
4243
#include "storage/bufmgr.h"
4344
#include "storage/condition_variable.h"
@@ -168,6 +169,7 @@ BackgroundWriterMain(const void *startup_data, size_t startup_data_len)
168169
*/
169170
LWLockReleaseAll();
170171
ConditionVariableCancelSleep();
172+
pgaio_error_cleanup();
171173
UnlockBuffers();
172174
ReleaseAuxProcessResources(false);
173175
AtEOXact_Buffers(false);

src/backend/postmaster/checkpointer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "postmaster/bgwriter.h"
5050
#include "postmaster/interrupt.h"
5151
#include "replication/syncrep.h"
52+
#include "storage/aio_subsys.h"
5253
#include "storage/bufmgr.h"
5354
#include "storage/condition_variable.h"
5455
#include "storage/fd.h"
@@ -276,6 +277,7 @@ CheckpointerMain(const void *startup_data, size_t startup_data_len)
276277
LWLockReleaseAll();
277278
ConditionVariableCancelSleep();
278279
pgstat_report_wait_end();
280+
pgaio_error_cleanup();
279281
UnlockBuffers();
280282
ReleaseAuxProcessResources(false);
281283
AtEOXact_Buffers(false);

src/backend/postmaster/pgarch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "postmaster/interrupt.h"
4141
#include "postmaster/pgarch.h"
4242
#include "storage/condition_variable.h"
43+
#include "storage/aio_subsys.h"
4344
#include "storage/fd.h"
4445
#include "storage/ipc.h"
4546
#include "storage/latch.h"
@@ -568,6 +569,7 @@ pgarch_archiveXlog(char *xlog)
568569
LWLockReleaseAll();
569570
ConditionVariableCancelSleep();
570571
pgstat_report_wait_end();
572+
pgaio_error_cleanup();
571573
ReleaseAuxProcessResources(false);
572574
AtEOXact_Files(false);
573575
AtEOXact_HashTables(false);

src/backend/postmaster/walsummarizer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "postmaster/interrupt.h"
3939
#include "postmaster/walsummarizer.h"
4040
#include "replication/walreceiver.h"
41+
#include "storage/aio_subsys.h"
4142
#include "storage/fd.h"
4243
#include "storage/ipc.h"
4344
#include "storage/latch.h"
@@ -289,6 +290,7 @@ WalSummarizerMain(const void *startup_data, size_t startup_data_len)
289290
LWLockReleaseAll();
290291
ConditionVariableCancelSleep();
291292
pgstat_report_wait_end();
293+
pgaio_error_cleanup();
292294
ReleaseAuxProcessResources(false);
293295
AtEOXact_Files(false);
294296
AtEOXact_HashTables(false);

src/backend/postmaster/walwriter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "postmaster/auxprocess.h"
5252
#include "postmaster/interrupt.h"
5353
#include "postmaster/walwriter.h"
54+
#include "storage/aio_subsys.h"
5455
#include "storage/bufmgr.h"
5556
#include "storage/condition_variable.h"
5657
#include "storage/fd.h"
@@ -164,6 +165,7 @@ WalWriterMain(const void *startup_data, size_t startup_data_len)
164165
LWLockReleaseAll();
165166
ConditionVariableCancelSleep();
166167
pgstat_report_wait_end();
168+
pgaio_error_cleanup();
167169
UnlockBuffers();
168170
ReleaseAuxProcessResources(false);
169171
AtEOXact_Buffers(false);

src/backend/replication/walsender.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include "replication/walsender.h"
8080
#include "replication/walsender_private.h"
8181
#include "storage/condition_variable.h"
82+
#include "storage/aio_subsys.h"
8283
#include "storage/fd.h"
8384
#include "storage/ipc.h"
8485
#include "storage/pmsignal.h"
@@ -327,6 +328,7 @@ WalSndErrorCleanup(void)
327328
LWLockReleaseAll();
328329
ConditionVariableCancelSleep();
329330
pgstat_report_wait_end();
331+
pgaio_error_cleanup();
330332

331333
if (xlogreader != NULL && xlogreader->seg.ws_file >= 0)
332334
wal_segment_close(xlogreader);

src/backend/storage/aio/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ top_builddir = ../../../..
99
include $(top_builddir)/src/Makefile.global
1010

1111
OBJS = \
12+
aio.o \
13+
aio_init.o \
1214
read_stream.o
1315

1416
include $(top_srcdir)/src/backend/common.mk

src/backend/storage/aio/aio.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* aio.c
4+
* AIO - Core Logic
5+
*
6+
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
* IDENTIFICATION
10+
* src/backend/storage/aio/aio.c
11+
*
12+
*-------------------------------------------------------------------------
13+
*/
14+
15+
#include "postgres.h"
16+
17+
#include "lib/ilist.h"
18+
#include "storage/aio.h"
19+
#include "storage/aio_subsys.h"
20+
#include "utils/guc.h"
21+
#include "utils/guc_hooks.h"
22+
23+
24+
/* Options for io_method. */
25+
const struct config_enum_entry io_method_options[] = {
26+
{"sync", IOMETHOD_SYNC, false},
27+
{NULL, 0, false}
28+
};
29+
30+
/* GUCs */
31+
int io_method = DEFAULT_IO_METHOD;
32+
int io_max_concurrency = -1;
33+
34+
35+
36+
/*
37+
* Release IO handle during resource owner cleanup.
38+
*/
39+
void
40+
pgaio_io_release_resowner(dlist_node *ioh_node, bool on_error)
41+
{
42+
}
43+
44+
/*
45+
* Perform AIO related cleanup after an error.
46+
*
47+
* This should be called early in the error recovery paths, as later steps may
48+
* need to issue AIO (e.g. to record a transaction abort WAL record).
49+
*/
50+
void
51+
pgaio_error_cleanup(void)
52+
{
53+
}
54+
55+
/*
56+
* Perform AIO related checks at (sub-)transactional boundaries.
57+
*
58+
* This should be called late during (sub-)transactional commit/abort, after
59+
* all steps that might need to perform AIO, so that we can verify that the
60+
* AIO subsystem is in a valid state at the end of a transaction.
61+
*/
62+
void
63+
AtEOXact_Aio(bool is_commit)
64+
{
65+
}
66+
67+
void
68+
assign_io_method(int newval, void *extra)
69+
{
70+
}
71+
72+
bool
73+
check_io_max_concurrency(int *newval, void **extra, GucSource source)
74+
{
75+
if (*newval == -1)
76+
{
77+
/*
78+
* Auto-tuning will be applied later during startup, as auto-tuning
79+
* depends on the value of various GUCs.
80+
*/
81+
return true;
82+
}
83+
else if (*newval == 0)
84+
{
85+
GUC_check_errdetail("Only -1 or values bigger than 0 are valid.");
86+
return false;
87+
}
88+
89+
return true;
90+
}

src/backend/storage/aio/aio_init.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* aio_init.c
4+
* AIO - Subsystem Initialization
5+
*
6+
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
* IDENTIFICATION
10+
* src/backend/storage/aio/aio_init.c
11+
*
12+
*-------------------------------------------------------------------------
13+
*/
14+
15+
#include "postgres.h"
16+
17+
#include "storage/aio_subsys.h"
18+
19+
20+
21+
Size
22+
AioShmemSize(void)
23+
{
24+
Size sz = 0;
25+
26+
return sz;
27+
}
28+
29+
void
30+
AioShmemInit(void)
31+
{
32+
}
33+
34+
void
35+
pgaio_init_backend(void)
36+
{
37+
}

src/backend/storage/aio/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) 2024-2025, PostgreSQL Global Development Group
22

33
backend_sources += files(
4+
'aio.c',
5+
'aio_init.c',
46
'read_stream.c',
57
)

0 commit comments

Comments
 (0)