Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
/*-------------------------------------------------------------------------
 * cfs_recovery.h
 *    Compressed File Store (CFS) — WAL / recovery / replication
 *    coordination.
 *
 * This header declares the functions that coordinate CFS state during
 * server startup, crash recovery, and process lifecycle.
 *
 * Crash-safety model summary
 * --------------------------
 * CFS does not add its own WAL resource manager.  The WAL stream always
 * contains standard uncompressed page images; CFS compression happens
 * only at the smgr write boundary (FlushBuffer → smgrwrite).  Durability
 * of the CFS sidecar files (.cfp / .cfsmap / .cfsjrn) is achieved
 * through:
 *
 *   1. Append-only .cfp records.
 *   2. An idempotent journal (.cfsjrn) that records every map update.
 *   3. Checkpoint: fsync .cfp → fsync .cfsjrn → fold .cfsmap rows in
 *    place → truncate .cfsjrn.
 *
 * On crash recovery the startup process folds every .cfsjrn into its
 * page map row by row before backends can access relations.  The fold
 * is idempotent and handles torn final records gracefully.
 *
 * Damage model (what recovery does with corrupt sidecar state)
 * ----------------------------------------------------------------
 *   - corrupt .cfsmap header: hard ERROR in every mode (the header is a
 *     synchronisation point WAL does not cover);
 *   - corrupt .cfsjrn header: hard ERROR in every mode (a header that
 *     fails validation means the file cannot be trusted to describe
 *     itself; the failed startup leaves the files untouched).  Both
 *     header checks run at startup for every relation, including those
 *     whose empty journal makes the scan skip the full open, and again
 *     on every later open; in all cases a full-sized header that fails
 *     validation is fatal, while a header shorter than its full size is
 *     a legal fresh state, not corruption;
 *   - torn final journal record: the normal crash artifact — the fold
 *     stops there (DEBUG1) and the torn tail is dropped with the journal
 *     reset;
 *   - torn journal record with data following it: WARNING, or ERROR in
 *     strict mode (a legal append cannot produce it);
 *   - skipped journal record (referenced .cfp data missing): WARNING,
 *     or ERROR in strict mode (WAL redo re-applies the change);
 *   - a partial sidecar set (debris of a commit-time removal): WARNING,
 *     the leftovers are removed and startup continues;
 *   - a corrupt .cfsmap row no journal record covers: not startup's
 *     business anymore - the fold never rewrites uncovered rows, so the
 *     damage surfaces at the first access of the block through the
 *     backend checksum policy.
 *
 * Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
 *
 * IDENTIFICATION
 *    src/include/storage/cfs/cfs_recovery.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef CFS_RECOVERY_H
#define CFS_RECOVERY_H
 
#include "postgres.h"
 
#include "storage/block.h"
#include "storage/cfs/cfs_storage.h"
#include "storage/relfilelocator.h"
 
/* ----------------------------------------------------------------
 * Lifecycle initialisation
 * ---------------------------------------------------------------- */
 
/*
 * CfsSubsystemInit — one-time initialisation of the CFS subsystem.
 * Called from CreateSharedMemoryAndSemaphores() after the shared-memory
 * structures are allocated.  Sets up the codec dispatch table and
 * any global state needed before backends start.
 */
extern void CfsSubsystemInit(void);
 
/*
 * CfsBackendInit — per-backend initialisation.
 * Called from InitPostgres() after the backend is ready to do smgr I/O.
 * Initialises per-backend codec states.
 */
extern void CfsBackendInit(void);
 
/*
 * CfsRecoveryInit — called early in crash / archive recovery (after the
 * file system is available, before relations are opened for normal use).
 *
 * A single unified scan out of this routine (cfs_scan_all_db_dirs) covers
 * every database directory across all tablespaces and, per directory:
 *   * completes interrupted compaction swaps (commit marker present),
 *   * removes orphan candidates / temp files matching the sidecar name
 *     template,
 *   * folds the .cfsjrn journals that hold records into their page maps.
 *
 * In a clean restart the walk finds empty journals (truncated at the last
 * checkpoint): no sidecar handles are kept and no map is loaded, only the
 * .cfsmap/.cfsjrn headers are read and validated.
 */
extern void CfsRecoveryInit(void);
 
/*
 * CfsAtProcExit — registered as an on_proc_exit callback; closes any
 * backend-local CFS handles that the smgr layer has not already released.
 */
extern void CfsAtProcExit(int code, Datum arg);
 
/* ----------------------------------------------------------------
 * Crash recovery journal replay
 * ---------------------------------------------------------------- */
 
/*
 * CfsReplayAllJournals — the recovery scan as a standalone entry point:
 * scan every database directory in the cluster for CFS sidecar journals
 * (.cfsjrn) and fold each one into its page map row by row.  After a
 * successful fold the journal is truncated.
 *
 * Returns the total number of journal records applied across all
 * relations.  Used by the test_cfs module to drive the scan directly.
 */
extern int64 CfsReplayAllJournals(void);
 
/* ----------------------------------------------------------------
 * Centralised corruption handling
 * ---------------------------------------------------------------- */
 
/*
 * cfs_report_corruption — report a CFS checksum or format error using
 * the configured severity (cfs_checksum_failure GUC for normal reads,
 * cfs_recovery_strict for recovery).  The caller passes a pre-formatted
 * detail string and the relation / block context.  'blocknum' is the
 * damaged block, or InvalidBlockNumber when the damage has no single
 * block to name (journal-wide); with InvalidBlockNumber the message
 * omits the block part.
 *
 * 'during_recovery' selects between the two policies: when true the
 * function consults cfs_recovery_strict; otherwise cfs_checksum_failure.
 */
extern void cfs_report_corruption(RelFileLocatorBackend rlocator,
                                  ForkNumber forknum,
                                  BlockNumber blocknum,
                                  const char *detail,
                                  bool during_recovery);
 
/*
 * cfs_corruption_guc_applies - may this process take the
 * cfs_checksum_failure-driven severity for structural corruption?
 * True only in a normal backend session of a primary outside WAL
 * replay (QDB-1507 U8: recovery, standby and auxiliary processes keep
 * the historical hard ERROR).
 */
extern bool cfs_corruption_guc_applies(void);
 
#endif                          /* CFS_RECOVERY_H */