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
//! v5.4.1 — background flusher thread (async-commit mode).
//!
//! In sync-commit mode (`SPG_SYNCHRONOUS_COMMIT=on`, the default)
//! the flusher does NOTHING: every WAL write path already
//! `sync_data`s before acknowledging the client (v4.42 group
//! commit + v5.3.2 CHECKPOINT both call `sync_data` themselves).
//! No flusher thread is spawned in that mode and the env knob is
//! the only user-visible knob.
//!
//! In async-commit mode (`SPG_SYNCHRONOUS_COMMIT=off`), the
//! flusher thread runs the periodic emission of v5.4.0
//! `durability_checkpoint` markers. Each marker tells future
//! crash-recovery readers that "every WAL byte before this
//! marker reached `fsync` at the time the marker was written."
//!
//! v5.4.1 ships the flusher infrastructure but **does not yet
//! change the client write path** — every INSERT still
//! `sync_data`s synchronously. The async write path lands in
//! v5.4.2; until then the flusher's markers are visible-but-
//! decorative (they cost ~17 bytes / interval and one extra
//! `fsync` on the WAL mutex but don't gate any client ack). This
//! sequencing lets the wire format, env knob, /metrics surface,
//! and lifecycle integration all settle before the durability-
//! semantics change in v5.4.2 ships.
//!
//! Cadence default (r176): 200 ms per marker — PG's
//! `wal_writer_delay` default, the documented loss window for
//! `synchronous_commit = off`. The original v5.4.1 default was
//! 200 µs ("the fsync cost is the same at any cadence; only the
//! window changes") — that reasoning missed the DUTY CYCLE: an
//! fsync takes ~5-8 ms on APFS, so a 200 µs interval means the
//! WAL file is being fsynced essentially 100% of the time, and
//! every concurrent client `append_wal` write_all on the same
//! file stalls behind the in-flight fsync (~1-3 ms per statement
//! — the r175/r176 wire-panel tax, root-caused via the interval
//! discriminator). At 200 ms the duty cycle drops to a few
//! percent and the window matches what PG customers already
//! accept for async commit.
//!
//! v7.39 (round 602) — that figure is right, and round 601 briefly
//! replaced it with a wrong one. Measured from Rust on this host
//! with the server's own access pattern (append 128 bytes to a
//! growing file, then `sync_data`, 200 times):
//!
//! write + sync_data 4.18 ms per call
//! write alone 0.008
//!
//! The mistake was measuring with Python's `os.fsync`, which got
//! 0.041 ms and looked like proof that a durable commit's cost
//! lay somewhere else. It does not: `File::sync_data` on macOS
//! asks for `F_FULLFSYNC`, a real barrier to the drive, while
//! `fsync(2)` — which is what Python calls — does not force the
//! drive's cache at all. Two different guarantees, two costs a
//! hundred times apart. `xbench/competitor/src/bin/fsync_probe.rs`
//! measures the one this code actually pays.
//!
//! v7.39 (round 702-SW) — and that 4 ms primitive turned out to BE the
//! write-path red line, so the WAL no longer pays it by default: every
//! commit-path sync goes through `wal::wal_sync`, which on macOS issues
//! `F_BARRIERFSYNC` (0.37 ms measured — ordered, barrier-flushed, and
//! still stronger than PG's own macOS default of plain `fdatasync`).
//! `SPG_WAL_FULLFSYNC=1` opts back into the full media barrier. The
//! numbers above describe `sync_data` itself, which is now the opt-in
//! strict path and the non-macOS path.
use env;
use Arc;
use Ordering;
use ;
use ;
use crate::;
pub const DEFAULT_INTERVAL_US: u64 = 200_000;
/// Lower bound on the per-marker interval — sub-10 µs ticks
/// would spin the WAL mutex faster than `sync_data` can return,
/// burning CPU for no durability-window improvement.
const MIN_INTERVAL_US: u64 = 10;
/// Cap on how long the worker may sleep between shutdown-flag
/// checks. The marker interval can be arbitrarily small, but the
/// shutdown wake-up needs to stay responsive — bound it at 50 ms
/// so server stop doesn't hang waiting for one more marker.
const SHUTDOWN_POLL_CAP: Duration = from_millis;
/// Configuration knobs surfaced via env vars. `async_mode` is
/// derived from `SPG_SYNCHRONOUS_COMMIT` (off/false/0 → async);
/// when false the flusher is never spawned in the first place.
pub
/// Spawn the background flusher. Before v7.37.15 this only spawned
/// when async-commit mode was opted in via
/// `SPG_SYNCHRONOUS_COMMIT=off`; now it always spawns (like PG's
/// wal writer) but stays **dormant** — no markers, no fsync, one
/// atomic load per 50 ms — until either the env opts into async
/// mode or a session's `SET synchronous_commit = off` commit
/// latches `async_commit_dirty`. Sync-mode servers that never see
/// an async commit keep their exact pre-7.37.15 WAL byte stream
/// (zero marker frames).
pub