Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-------------------------------------------------------------------------
 *
 * aio_types.h
 *    AIO related types that are useful to include separately, to reduce the
 *    "include burden".
 *
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/include/storage/aio_types.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef AIO_TYPES_H
#define AIO_TYPES_H
 
#include "storage/block.h"
#include "storage/relfilelocator.h"
 
 
typedef struct PgAioHandle PgAioHandle;
typedef struct PgAioHandleCallbacks PgAioHandleCallbacks;
typedef struct PgAioTargetInfo PgAioTargetInfo;
 
/*
 * A reference to an IO that can be used to wait for the IO (using
 * pgaio_wref_wait()) to complete.
 *
 * These can be passed across process boundaries.
 */
typedef struct PgAioWaitRef
{
    /* internal ID identifying the specific PgAioHandle */
    uint32      aio_index;
 
    /*
     * IO handles are reused. To detect if a handle was reused, and thereby
     * avoid unnecessarily waiting for a newer IO, each time the handle is
     * reused a generation number is increased.
     *
     * To avoid requiring alignment sufficient for an int64, split the
     * generation into two.
     */
    uint32      generation_upper;
    uint32      generation_lower;
} PgAioWaitRef;
 
 
/*
 * Information identifying what the IO is being performed on.
 *
 * This needs sufficient information to
 *
 * a) Reopen the file for the IO if the IO is executed in a context that
 *    cannot use the FD provided initially (e.g. because the IO is executed in
 *    a worker process).
 *
 * b) Describe the object the IO is performed on in log / error messages.
 */
typedef union PgAioTargetData
{
    struct
    {
        RelFileLocator rlocator;    /* physical relation identifier */
        BlockNumber blockNum;   /* blknum relative to begin of reln */
        BlockNumber nblocks;
        ForkNumber  forkNum:8;  /* don't waste 4 byte for four values */
        bool        is_temp:1;  /* proc can be inferred by owning AIO */
        bool        skip_fsync:1;
        /* [PG_CUSTOM: CFS]
         * Set by the CFS submit paths only: the pages of this IO were
         * delivered verified by the CFS CRC chain (header, payload and
         * original-image CRCs), so the cluster page-checksum in
         * PageIsVerified() is redundant and may be skipped (gated by
         * the cfs_skip_page_checksum GUC at submit time).
         */
        bool        cfs_skip_checksum:1;
    }           smgr;
    /* [PG_CUSTOM: CFS]
     * Target data for a CFS span readv: the worker reopens the .cfp
     * segment by identity (no cross-process fd), the completion callback
     * fetches the span plan by the shared arena slot id.  'generation'
     * is the relation map generation the plan's offsets belong to: a
     * compaction bump between planning and reopening means the reopened
     * file no longer matches the plan, so the reopen fails the IO
     * transiently instead of reading wrong bytes.  The ID space is
     * never persisted.  CFS relations are never temp.
     *
     * ALIASING CONTRACT: the leading fields (rlocator, blockNum,
     * nblocks) deliberately mirror the smgr member's layout, and the
     * smgr member's trailing bitfield unit (is_temp, skip_fsync,
     * cfs_skip_checksum) falls into this struct's alignment padding
     * before 'generation'.  The planning backend zeroes the whole
     * union before filling cfs fields, so the buffer completion's
     * report callback (buffer_readv_report), which reads the union
     * through its smgr lens, sees the right relation, block range,
     * fork 0 (CFS serves the main fork) and is_temp false.  The CFS
     * span submit path likewise writes cfs_skip_checksum through the
     * smgr lens AFTER filling the cfs fields; the zeroed padding
     * keeps that write confined to dead bytes of this struct.  The
     * asserts below turn this coupling into a checked contract;
     * reorder either struct only together with them.
     */
    struct
    {
        RelFileLocator rlocator;    /* physical relation identifier */
        BlockNumber blockNum;   /* first block, for identity reports */
        BlockNumber nblocks;    /* blocks served by the span plan */
        uint64      generation; /* relation map generation of the plan */
        uint32      plan_slot;  /* scratch arena slot with the plan */
        uint32      segment;    /* .cfp segment index */
        ForkNumber  forkNum:8;
    }           cfs;
} PgAioTargetData;
 
/*
 * [PG_CUSTOM: CFS]
 * Checked half of the aliasing contract above: the shared prefix must
 * stay byte-identical, and every smgr byte past it (the
 * forkNum/is_temp/skip_fsync/cfs_skip_checksum bitfield unit) must
 * land inside this struct's zeroed padding, never on a live cfs
 * field.
 */
StaticAssertDecl(offsetof(PgAioTargetData, smgr.blockNum) ==
                 offsetof(PgAioTargetData, cfs.blockNum) &&
                 offsetof(PgAioTargetData, smgr.nblocks) ==
                 offsetof(PgAioTargetData, cfs.nblocks) &&
                 offsetof(PgAioTargetData, cfs.generation) >=
                 sizeof(((PgAioTargetData *) 0)->smgr),
                 "smgr/cfs aliasing contract of PgAioTargetData is broken");
 
 
/*
 * The status of an AIO operation.
 */
typedef enum PgAioResultStatus
{
    PGAIO_RS_UNKNOWN,           /* not yet completed / uninitialized */
    PGAIO_RS_OK,
    PGAIO_RS_PARTIAL,           /* did not fully succeed, no warning/error */
    PGAIO_RS_WARNING,           /* [partially] succeeded, with a warning */
    PGAIO_RS_ERROR,             /* failed entirely */
} PgAioResultStatus;
 
 
/*
 * Result of IO operation, visible only to the initiator of IO.
 *
 * We need to be careful about the size of PgAioResult, as it is embedded in
 * every PgAioHandle, as well as every PgAioReturn. Currently we assume we can
 * fit it into one 8 byte value, restricting the space for per-callback error
 * data to PGAIO_RESULT_ERROR_BITS.
 */
#define PGAIO_RESULT_ID_BITS 6
#define PGAIO_RESULT_STATUS_BITS 3
#define PGAIO_RESULT_ERROR_BITS 23
typedef struct PgAioResult
{
    /*
     * This is of type PgAioHandleCallbackID, but can't use a bitfield of an
     * enum, because some compilers treat enums as signed.
     */
    uint32      id:PGAIO_RESULT_ID_BITS;
 
    /* of type PgAioResultStatus, see above */
    uint32      status:PGAIO_RESULT_STATUS_BITS;
 
    /* meaning defined by callback->report */
    uint32      error_data:PGAIO_RESULT_ERROR_BITS;
 
    int32       result;
} PgAioResult;
 
 
StaticAssertDecl(PGAIO_RESULT_ID_BITS +
                 PGAIO_RESULT_STATUS_BITS +
                 PGAIO_RESULT_ERROR_BITS == 32,
                 "PgAioResult bits divided up incorrectly");
StaticAssertDecl(sizeof(PgAioResult) == 8,
                 "PgAioResult has unexpected size");
 
/*
 * Combination of PgAioResult with minimal metadata about the IO.
 *
 * Contains sufficient information to be able, in case the IO [partially]
 * fails, to log/raise an error under control of the IO issuing code.
 */
typedef struct PgAioReturn
{
    PgAioResult result;
    PgAioTargetData target_data;
} PgAioReturn;
 
 
#endif                          /* AIO_TYPES_H */