PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
aio.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * aio.h
4 * Main AIO interface
5 *
6 * This is the header to include when actually issuing AIO. When just
7 * declaring functions involving an AIO related type, it might suffice to
8 * include aio_types.h. Initialization related functions are in the dedicated
9 * aio_init.h.
10 *
11 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
13 *
14 * src/include/storage/aio.h
15 *
16 *-------------------------------------------------------------------------
17 */
18#ifndef AIO_H
19#define AIO_H
20
21#include "storage/aio_types.h"
22#include "storage/procnumber.h"
23
24
25
26/* Enum for io_method GUC. */
27typedef enum IoMethod
28{
32
33/* We'll default to worker based execution. */
34#define DEFAULT_IO_METHOD IOMETHOD_WORKER
35
36
37/*
38 * Flags for an IO that can be set with pgaio_io_set_flag().
39 */
40typedef enum PgAioHandleFlags
41{
42 /*
43 * The IO references backend local memory.
44 *
45 * This needs to be set on an IO whenever the IO references process-local
46 * memory. Some IO methods do not support executing IO that references
47 * process local memory and thus need to fall back to executing IO
48 * synchronously for IOs with this flag set.
49 *
50 * Required for correctness.
51 */
53
54 /*
55 * Hint that IO will be executed synchronously.
56 *
57 * This can make it a bit cheaper to execute synchronous IO via the AIO
58 * interface, to avoid needing an AIO and non-AIO version of code.
59 *
60 * Advantageous to set, if applicable, but not required for correctness.
61 */
63
64 /*
65 * IO is using buffered IO, used to control heuristic in some IO methods.
66 *
67 * Advantageous to set, if applicable, but not required for correctness.
68 */
71
72/*
73 * The IO operations supported by the AIO subsystem.
74 *
75 * This could be in aio_internal.h, as it is not pubicly referenced, but
76 * PgAioOpData currently *does* need to be public, therefore keeping this
77 * public seems to make sense.
78 */
79typedef enum PgAioOp
80{
81 /* intentionally the zero value, to help catch zeroed memory etc */
83
86
98
99#define PGAIO_OP_COUNT (PGAIO_OP_WRITEV + 1)
100
101
102/*
103 * On what is IO being performed?
104 *
105 * PgAioTargetID specific behaviour should be implemented in
106 * aio_target.c.
107 */
108typedef enum PgAioTargetID
109{
110 /* intentionally the zero value, to help catch zeroed memory etc */
113
114#define PGAIO_TID_COUNT (PGAIO_TID_INVALID + 1)
115
116
117/*
118 * Data necessary for support IO operations (see PgAioOp).
119 *
120 * NB: Note that the FDs in here may *not* be relied upon for re-issuing
121 * requests (e.g. for partial reads/writes or in an IO worker) - the FD might
122 * be from another process, or closed since. That's not a problem for staged
123 * IOs, as all staged IOs are submitted when closing an FD.
124 */
125typedef union
126{
127 struct
128 {
129 int fd;
133
134 struct
135 {
136 int fd;
137 uint16 iov_length;
138 uint64 offset;
141
142
143/*
144 * Information the object that IO is executed on. Mostly callbacks that
145 * operate on PgAioTargetData.
146 *
147 * typedef is in aio_types.h
148 */
150{
151 /*
152 * To support executing using worker processes, the file descriptor for an
153 * IO may need to be be reopened in a different process.
154 */
155 void (*reopen) (PgAioHandle *ioh);
156
157 /* describe the target of the IO, used for log messages and views */
158 char *(*describe_identity) (const PgAioTargetData *sd);
159
160 /* name of the target, used in log messages / views */
161 const char *name;
162};
163
164
165/*
166 * IDs for callbacks that can be registered on an IO.
167 *
168 * Callbacks are identified by an ID rather than a function pointer. There are
169 * two main reasons:
170 *
171 * 1) Memory within PgAioHandle is precious, due to the number of PgAioHandle
172 * structs in pre-allocated shared memory.
173 *
174 * 2) Due to EXEC_BACKEND function pointers are not necessarily stable between
175 * different backends, therefore function pointers cannot directly be in
176 * shared memory.
177 *
178 * Without 2), we could fairly easily allow to add new callbacks, by filling a
179 * ID->pointer mapping table on demand. In the presence of 2 that's still
180 * doable, but harder, because every process has to re-register the pointers
181 * so that a local ID->"backend local pointer" mapping can be maintained.
182 */
184{
187
188
189typedef void (*PgAioHandleCallbackStage) (PgAioHandle *ioh, uint8 cb_flags);
190typedef PgAioResult (*PgAioHandleCallbackComplete) (PgAioHandle *ioh, PgAioResult prior_result, uint8 cb_flags);
191typedef void (*PgAioHandleCallbackReport) (PgAioResult result, const PgAioTargetData *target_data, int elevel);
192
193/* typedef is in aio_types.h */
195{
196 /*
197 * Prepare resources affected by the IO for execution. This could e.g.
198 * include moving ownership of buffer pins to the AIO subsystem.
199 */
201
202 /*
203 * Update the state of resources affected by the IO to reflect completion
204 * of the IO. This could e.g. include updating shared buffer state to
205 * signal the IO has finished.
206 *
207 * The _shared suffix indicates that this is executed by the backend that
208 * completed the IO, which may or may not be the backend that issued the
209 * IO. Obviously the callback thus can only modify resources in shared
210 * memory.
211 *
212 * The latest registered callback is called first. This allows
213 * higher-level code to register callbacks that can rely on callbacks
214 * registered by lower-level code to already have been executed.
215 *
216 * NB: This is called in a critical section. Errors can be signalled by
217 * the callback's return value, it's the responsibility of the IO's issuer
218 * to react appropriately.
219 */
221
222 /*
223 * Like complete_shared, except called in the issuing backend.
224 *
225 * This variant of the completion callback is useful when backend-local
226 * state has to be updated to reflect the IO's completion. E.g. a
227 * temporary buffer's BufferDesc isn't accessible in complete_shared.
228 *
229 * Local callbacks are only called after complete_shared for all
230 * registered callbacks has been called.
231 */
233
234 /*
235 * Report the result of an IO operation. This is e.g. used to raise an
236 * error after an IO failed at the appropriate time (i.e. not when the IO
237 * failed, but under control of the code that issued the IO).
238 */
240};
241
242
243
244/*
245 * How many callbacks can be registered for one IO handle. Currently we only
246 * need two, but it's not hard to imagine needing a few more.
247 */
248#define PGAIO_HANDLE_MAX_CALLBACKS 4
249
250
251
252/* --------------------------------------------------------------------------------
253 * IO Handles
254 * --------------------------------------------------------------------------------
255 */
256
257/* functions in aio.c */
258struct ResourceOwnerData;
259extern PgAioHandle *pgaio_io_acquire(struct ResourceOwnerData *resowner, PgAioReturn *ret);
260extern PgAioHandle *pgaio_io_acquire_nb(struct ResourceOwnerData *resowner, PgAioReturn *ret);
261
262extern void pgaio_io_release(PgAioHandle *ioh);
263struct dlist_node;
264extern void pgaio_io_release_resowner(struct dlist_node *ioh_node, bool on_error);
265
267
268extern int pgaio_io_get_id(PgAioHandle *ioh);
270
271extern void pgaio_io_get_wref(PgAioHandle *ioh, PgAioWaitRef *iow);
272
273/* functions in aio_io.c */
274struct iovec;
275extern int pgaio_io_get_iovec(PgAioHandle *ioh, struct iovec **iov);
276
279
280extern void pgaio_io_prep_readv(PgAioHandle *ioh,
281 int fd, int iovcnt, uint64 offset);
282extern void pgaio_io_prep_writev(PgAioHandle *ioh,
283 int fd, int iovcnt, uint64 offset);
284
285/* functions in aio_target.c */
286extern void pgaio_io_set_target(PgAioHandle *ioh, PgAioTargetID targetid);
287extern bool pgaio_io_has_target(PgAioHandle *ioh);
290
291/* functions in aio_callback.c */
293 uint8 cb_data);
297
298
299
300/* --------------------------------------------------------------------------------
301 * IO Wait References
302 * --------------------------------------------------------------------------------
303 */
304
305extern void pgaio_wref_clear(PgAioWaitRef *iow);
306extern bool pgaio_wref_valid(PgAioWaitRef *iow);
307extern int pgaio_wref_get_id(PgAioWaitRef *iow);
308
309extern void pgaio_wref_wait(PgAioWaitRef *iow);
310extern bool pgaio_wref_check_done(PgAioWaitRef *iow);
311
312
313
314/* --------------------------------------------------------------------------------
315 * IO Result
316 * --------------------------------------------------------------------------------
317 */
318
319extern void pgaio_result_report(PgAioResult result, const PgAioTargetData *target_data,
320 int elevel);
321
322
323
324/* --------------------------------------------------------------------------------
325 * Actions on multiple IOs.
326 * --------------------------------------------------------------------------------
327 */
328
329extern void pgaio_enter_batchmode(void);
330extern void pgaio_exit_batchmode(void);
331extern void pgaio_submit_staged(void);
332extern bool pgaio_have_staged(void);
333
334
335
336/* --------------------------------------------------------------------------------
337 * Other
338 * --------------------------------------------------------------------------------
339 */
340
341extern void pgaio_closing_fd(int fd);
342
343
344
345/* GUCs */
346extern PGDLLIMPORT int io_method;
348
349
350#endif /* AIO_H */
void(* PgAioHandleCallbackReport)(PgAioResult result, const PgAioTargetData *target_data, int elevel)
Definition: aio.h:191
PgAioTargetData * pgaio_io_get_target_data(PgAioHandle *ioh)
Definition: aio_target.c:70
PGDLLIMPORT int io_max_concurrency
Definition: aio.c:73
PgAioHandleCallbackID
Definition: aio.h:184
@ PGAIO_HCB_INVALID
Definition: aio.h:185
bool pgaio_wref_valid(PgAioWaitRef *iow)
Definition: aio.c:863
int pgaio_io_get_id(PgAioHandle *ioh)
Definition: aio.c:322
IoMethod
Definition: aio.h:28
@ IOMETHOD_WORKER
Definition: aio.h:30
@ IOMETHOD_SYNC
Definition: aio.h:29
PgAioHandle * pgaio_io_acquire(struct ResourceOwnerData *resowner, PgAioReturn *ret)
Definition: aio.c:165
PgAioTargetID
Definition: aio.h:109
@ PGAIO_TID_INVALID
Definition: aio.h:111
void pgaio_wref_clear(PgAioWaitRef *iow)
Definition: aio.c:856
PgAioOp
Definition: aio.h:80
@ PGAIO_OP_WRITEV
Definition: aio.h:85
@ PGAIO_OP_INVALID
Definition: aio.h:82
@ PGAIO_OP_READV
Definition: aio.h:84
void pgaio_io_set_handle_data_32(PgAioHandle *ioh, uint32 *data, uint8 len)
Definition: aio_callback.c:130
void pgaio_io_prep_readv(PgAioHandle *ioh, int fd, int iovcnt, uint64 offset)
Definition: aio_io.c:78
void(* PgAioHandleCallbackStage)(PgAioHandle *ioh, uint8 cb_flags)
Definition: aio.h:189
PgAioOpData * pgaio_io_get_op_data(PgAioHandle *ioh)
Definition: aio_io.c:58
void pgaio_io_get_wref(PgAioHandle *ioh, PgAioWaitRef *iow)
Definition: aio.c:346
void pgaio_io_register_callbacks(PgAioHandle *ioh, PgAioHandleCallbackID cb_id, uint8 cb_data)
Definition: aio_callback.c:78
void pgaio_closing_fd(int fd)
Definition: aio.c:1107
void pgaio_io_set_flag(PgAioHandle *ioh, PgAioHandleFlags flag)
Definition: aio.c:310
bool pgaio_have_staged(void)
Definition: aio.c:994
PgAioOp pgaio_io_get_op(PgAioHandle *ioh)
Definition: aio_io.c:52
PgAioHandleFlags
Definition: aio.h:41
@ PGAIO_HF_SYNCHRONOUS
Definition: aio.h:62
@ PGAIO_HF_REFERENCES_LOCAL
Definition: aio.h:52
@ PGAIO_HF_BUFFERED
Definition: aio.h:69
PgAioResult(* PgAioHandleCallbackComplete)(PgAioHandle *ioh, PgAioResult prior_result, uint8 cb_flags)
Definition: aio.h:190
bool pgaio_io_has_target(PgAioHandle *ioh)
Definition: aio_target.c:38
uint64 * pgaio_io_get_handle_data(PgAioHandle *ioh, uint8 *len)
Definition: aio_callback.c:145
void pgaio_io_set_handle_data_64(PgAioHandle *ioh, uint64 *data, uint8 len)
Definition: aio_callback.c:113
bool pgaio_wref_check_done(PgAioWaitRef *iow)
Definition: aio.c:897
PGDLLIMPORT int io_method
Definition: aio.c:72
ProcNumber pgaio_io_get_owner(PgAioHandle *ioh)
Definition: aio.c:335
void pgaio_enter_batchmode(void)
Definition: aio.c:968
void pgaio_io_release_resowner(struct dlist_node *ioh_node, bool on_error)
Definition: aio.c:254
void pgaio_submit_staged(void)
Definition: aio.c:1010
char * pgaio_io_get_target_description(PgAioHandle *ioh)
Definition: aio_target.c:81
void pgaio_wref_wait(PgAioWaitRef *iow)
Definition: aio.c:883
void pgaio_io_release(PgAioHandle *ioh)
Definition: aio.c:234
int pgaio_wref_get_id(PgAioWaitRef *iow)
Definition: aio.c:872
void pgaio_io_prep_writev(PgAioHandle *ioh, int fd, int iovcnt, uint64 offset)
Definition: aio_io.c:91
void pgaio_io_set_target(PgAioHandle *ioh, PgAioTargetID targetid)
Definition: aio_target.c:61
int pgaio_io_get_iovec(PgAioHandle *ioh, struct iovec **iov)
Definition: aio_io.c:42
void pgaio_result_report(PgAioResult result, const PgAioTargetData *target_data, int elevel)
Definition: aio_callback.c:162
void pgaio_exit_batchmode(void)
Definition: aio.c:979
PgAioHandle * pgaio_io_acquire_nb(struct ResourceOwnerData *resowner, PgAioReturn *ret)
Definition: aio.c:191
struct PgAioResult PgAioResult
#define PGDLLIMPORT
Definition: c.h:1291
uint8_t uint8
Definition: c.h:500
uint64_t uint64
Definition: c.h:503
uint16_t uint16
Definition: c.h:501
uint32_t uint32
Definition: c.h:502
#define write(a, b, c)
Definition: win32.h:14
#define read(a, b, c)
Definition: win32.h:13
const void size_t len
const void * data
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int ProcNumber
Definition: procnumber.h:24
PgAioHandleCallbackComplete complete_shared
Definition: aio.h:220
PgAioHandleCallbackStage stage
Definition: aio.h:200
PgAioHandleCallbackReport report
Definition: aio.h:239
PgAioHandleCallbackComplete complete_local
Definition: aio.h:232
void(* reopen)(PgAioHandle *ioh)
Definition: aio.h:155
const char * name
Definition: aio.h:161
char * flag(int b)
Definition: test-ctype.c:33
uint64 offset
Definition: aio.h:131
int fd
Definition: aio.h:129
uint16 iov_length
Definition: aio.h:130