Исходный код СУБД 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*-------------------------------------------------------------------------
 *
 * cfs_sync.c
 *    The CFS side of the checkpointer sync queue: fsync/unlink request
 *    registration and the file-tag callbacks storage/sync/sync.c calls.
 *    Moved out of cfs_smgr.c so that the storage layer can register its
 *    writes (cfs_register_dirty_file) without depending on the smgr
 *    bridge — this module's dependencies are the sync queue, the sidecar
 *    path builder and raw fsync, nothing else.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 2026, Diasoft
 *
 * src/backend/storage/cfs/cfs_sync.c
 *
 *-------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "access/xlog.h"
#include "miscadmin.h"
#include "storage/cfs/cfs_path.h"
#include "storage/cfs/cfs_sync.h"
#include "storage/fd.h"
#include "storage/sync.h"
 
 
/* ================================================================
 * Sync handler callbacks
 *
 * CFS sidecar files (.cfp/.cfsmap/.cfsjrn) register fsync and unlink
 * requests through the standard sync queue so that the checkpointer
 * fsyncs them at checkpoint time, exactly like md.c does for base
 * relation segments.
 * ================================================================ */
 
/*
 * Resolve a CfsFileKind from a file tag's 'segno' field.  Returns false when
 * the value is out of range.
 */
static bool
cfs_kind_from_tag(const FileTag *ftag, CfsFileKind *kind)
{
    if (ftag->segno >= (uint64) CFS_FILE_KIND_COUNT)
        return false;
    *kind = (CfsFileKind) ftag->segno;
    return true;
}
 
int
cfs_syncfiletag(const FileTag *ftag, char *path)
{
    RelFileLocatorBackend rlocator;
    CfsFileKind kind;
    char       *sidecar_path;
    uint32      segno;
    int         result = 0;
    int         save_errno = 0;
 
    if (!cfs_kind_from_tag(ftag, &kind))
    {
        errno = EINVAL;
        return -1;
    }
 
    rlocator.locator = ftag->rlocator;
    rlocator.backend = INVALID_PROC_NUMBER;
 
    /*
     * The .cfp data file is segmented (".cfp", ".cfp.1", ...): fsync every
     * existing segment, stopping at the first gap.  Map and journal are
     * single-segment files.
     */
    for (segno = 0;; segno++)
    {
        int         fd;
 
        sidecar_path = cfs_make_segment_filename(rlocator, MAIN_FORKNUM,
                                                 kind, segno);
        if (sidecar_path == NULL)
        {
            errno = EINVAL;
            return -1;
        }
 
        if (segno == 0)
            strlcpy(path, sidecar_path, MAXPGPATH);
 
        fd = BasicOpenFile(sidecar_path, O_RDWR | PG_BINARY);
        pfree(sidecar_path);
        if (fd < 0)
        {
            if (errno == ENOENT && segno > 0)
                break;          /* end of the segment chain */
            return -1;
        }
 
        result = pg_fsync(fd);
        save_errno = errno;
 
        close(fd);
        errno = save_errno;
        if (result != 0)
            return result;
 
        if (kind != CFS_FILE_DATA)
            break;              /* single-segment kinds */
    }
    return result;
}
 
int
cfs_unlinkfiletag(const FileTag *ftag, char *path)
{
    RelFileLocatorBackend rlocator;
    CfsFileKind kind;
    char       *sidecar_path;
    uint32      segno;
 
    if (!cfs_kind_from_tag(ftag, &kind))
    {
        errno = EINVAL;
        return -1;
    }
 
    rlocator.locator = ftag->rlocator;
    rlocator.backend = INVALID_PROC_NUMBER;
 
    /*
     * Like cfs_syncfiletag, walk the whole .cfp segment chain.  Segments are
     * contiguous, so the first missing one ends the loop.
     */
    for (segno = 0;; segno++)
    {
        sidecar_path = cfs_make_segment_filename(rlocator, MAIN_FORKNUM,
                                                 kind, segno);
        if (sidecar_path == NULL)
        {
            errno = EINVAL;
            return -1;
        }
 
        if (segno == 0)
            strlcpy(path, sidecar_path, MAXPGPATH);
 
        if (unlink(sidecar_path) != 0)
        {
            int         save_errno = errno;
 
            pfree(sidecar_path);
            if (save_errno == ENOENT)
                break;          /* end of the segment chain */
            errno = save_errno;
            return -1;
        }
        pfree(sidecar_path);
 
        if (kind != CFS_FILE_DATA)
            break;              /* single-segment kinds */
    }
    return 0;
}
 
bool
cfs_filetagmatches(const FileTag *ftag, const FileTag *candidate)
{
    /*
     * A filter request carries either a complete relation locator or a
     * database-wide locator with relNumber == 0.  The former is used by
     * cfs_forget_sync_requests() to forget all pending sync/unlink requests
     * of one relation fork when it is dropped; the latter by
     * cfs_forget_database_sync_requests() to forget everything belonging to a
     * whole database, like mdfiletagmatches() does for md.c.
     */
    if (ftag->rlocator.relNumber == 0)
        return ftag->rlocator.dbOid == candidate->rlocator.dbOid;
 
    return ftag->rlocator.dbOid == candidate->rlocator.dbOid &&
        ftag->rlocator.spcOid == candidate->rlocator.spcOid &&
        ftag->rlocator.relNumber == candidate->rlocator.relNumber;
}
 
void
cfs_register_dirty_file(RelFileLocatorBackend rlocator, ForkNumber forknum,
                        CfsFileKind kind)
{
    FileTag     tag;
 
    if (RelFileLocatorBackendIsTemp(rlocator))
        return;
 
    INIT_CFS_FILETAG(tag, rlocator.locator, kind);
 
    if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false))
    {
        /*
         * The checkpointer queue is full — fsync locally so durability is
         * not lost, mirroring register_dirty_segment() in md.c.
         *
         * CFS note: md.c can fsync via an already-open fd (mdfd_vfd), but the
         * storage layer here only has the rlocator, so the sidecar path must
         * be rebuilt with cfs_make_filename() (palloc).  That is forbidden
         * inside a critical section.  When invoked from WAL redo (e.g.
         * XLOG_SMGR_TRUNCATE replay via cfs_smgr_truncate -> cfs_truncate),
         * skip the fallback: the journal record is already written, the WAL
         * record guarantees replayability, and the end-of-recovery checkpoint
         * will fsync everything.  Dropping the request is safe — see
         * cfs_storage.h's crash-safety summary.
         */
        if (CritSectionCount > 0)
        {
            ereport(DEBUG1,
                    (errmsg("CFS: sync queue full during critical section; "
                            "deferring fsync of %u/%u/%u kind %d to checkpoint",
                            rlocator.locator.spcOid, rlocator.locator.dbOid,
                            rlocator.locator.relNumber, (int) kind)));
            return;
        }
 
        {
            uint32      segno;
 
            /* Walk the whole segment chain (see cfs_syncfiletag). */
            for (segno = 0;; segno++)
            {
                char       *sidecar_path;
                int         fd;
 
                sidecar_path = cfs_make_segment_filename(rlocator, forknum,
                                                         kind, segno);
                if (sidecar_path == NULL)
                    return;
 
                fd = BasicOpenFile(sidecar_path, O_RDWR | PG_BINARY);
                if (fd < 0)
                {
                    pfree(sidecar_path);
                    break;      /* end of the segment chain */
                }
 
                /*
                 * Close the descriptor before reporting: with data_sync_retry
                 * enabled the ereport is an ERROR, and the raw descriptor
                 * would leak for the rest of the process lifetime.
                 */
                {
                    int         fsync_status = pg_fsync(fd);
                    int         save_errno = errno;
 
                    close(fd);
                    errno = save_errno;
                    if (fsync_status < 0)
                        ereport(data_sync_elevel(WARNING),
                                (errcode_for_file_access(),
                                 errmsg("CFS: could not fsync sidecar \"%s\": %m",
                                        sidecar_path)));
                }
                pfree(sidecar_path);
 
                if (kind != CFS_FILE_DATA)
                    break;      /* single-segment kinds */
            }
        }
    }
}
 
void
cfs_register_unlink_file(RelFileLocatorBackend rlocator, ForkNumber forknum,
                         CfsFileKind kind)
{
    FileTag     tag;
 
    if (RelFileLocatorBackendIsTemp(rlocator))
        return;
 
    INIT_CFS_FILETAG(tag, rlocator.locator, kind);
    RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true);
}
 
void
cfs_forget_sync_requests(RelFileLocatorBackend rlocator, ForkNumber forknum)
{
    FileTag     tag;
 
    if (RelFileLocatorBackendIsTemp(rlocator))
        return;
 
    INIT_CFS_FILETAG(tag, rlocator.locator, CFS_FILE_DATA);
    RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true);
}
 
void
cfs_forget_database_sync_requests(Oid dbid)
{
    FileTag     tag;
    RelFileLocator rlocator;
 
    rlocator.dbOid = dbid;
    rlocator.spcOid = 0;
    rlocator.relNumber = 0;
 
    INIT_CFS_FILETAG(tag, rlocator, CFS_FILE_DATA);
 
    RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true);
}