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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Per-statement-class write-volume census (W3.2 quantification).
//!
//! Attributes durable write bytes to three key families — the current row
//! (`data:`), the time-travel version chain (`v:`/`v_idx:` and the COPY
//! `vmeta:` marker), and secondary-index maintenance (ART entry keys) — split
//! by statement class (single INSERT, multi-row INSERT, COPY, UPDATE, DELETE).
//! It exists to answer ONE question before any version-format work: what share
//! of INSERT byte volume is the full `v:` duplicate of every row? (The `v:` value
//! is the row's logical value — equal to `data:` for the default row-store, larger
//! under side-storage; `W3_2_DESIGN.md` §1.1. STOP rule: below 15% ⇒ deprioritize.)
//!
//! ## Zero cost when disabled — runtime-only gate, no cargo feature
//!
//! Every recording site is guarded by ONE relaxed load of a process-global
//! `AtomicBool` (`[performance] write_volume_stats`, default `false`), mirroring
//! the `global_txn_active` fast-out (`lib.rs:540`) and the copy-marker `any`
//! fast-out (`storage/copy_marker.rs:53`). Unlike `lock_census` (W3.1) this
//! needs NO `#[cfg(feature)]`: the per-row cost when disabled is only relaxed
//! atomic *loads* — a load of an almost-always-false, cache-resident,
//! uncontended global — never a store, fence, lock, thread-local access, or
//! allocation. Atoms per row, DISABLED (the RAII `stmt_scope` guard loads
//! `enabled()` once on construction, then one gate per write funnel the row
//! crosses; a funnel gates ALL its `add`/`add_row` calls behind a single
//! `enabled()`):
//!
//! * autocommit single INSERT = 4 loads (scope guard `stmt_scope` + data
//! funnel `insert_tuple_fast` + version
//! funnel `append_version_snapshot_to_batch`
//! + index funnel `on_insert_tuple`);
//! 3 loads when time-travel is OFF
//! (`fast`/`fast_ingest` profiles) —
//! the version funnel never runs
//! * autocommit UPDATE / DELETE = 2 loads (scope guard + data funnel only —
//! the fast paths are version-skipping)
//! * COPY / multi-row INSERT = 1 scope load (statement) + 1 load hoisted
//! before the batch loop + 1 index-funnel
//! load; the per-row cost inside the loop
//! is zero (the gate is hoisted out)
//!
//! Each is a predictable not-taken branch over a single `mov`; there is no
//! measurable steady-state cost, which is the claim this comment proves and the
//! reason a cargo feature would be over-engineering here. Atoms per row,
//! ENABLED: one `fetch_add` per (category) recorded plus one `add_row`, all
//! `Relaxed` — a diagnostic aggregate, not a serialization point.
//!
//! Statement class rides a thread-local `Cell` set by a RAII [`stmt_scope`]
//! guard at the DML dispatch boundary. Storage writes run synchronously on the
//! dispatching thread (no `.await` between the scope and the RocksDB write), so
//! the thread-local is exact for autocommit statements. Buffered explicit-
//! transaction writes land at COMMIT time, decoupled from the staging
//! statement, and are attributed to [`StmtClass::Other`] — documented in
//! `W3_2_DESIGN.md` §"instrumentation coverage".
//!
//! ## Buffered autocommit-implicit commits (the multi-row `VALUES` funnel)
//!
//! A literal multi-row `INSERT … VALUES (…),(…),…` and any single-row INSERT
//! the fast literal path rejects (constrained tables, `ON CONFLICT`,
//! `RETURNING`) fall to the generic plan arm, which STAGES rows into an implicit
//! transaction whose durable bytes are written by `txn.commit()` in
//! `execute_with_implicit_transaction` — *after* the staging arm returns, so a
//! bare `stmt_scope` in the arm would drop before the write. These are still
//! autocommit-implicit (never explicit `BEGIN … COMMIT`), so they DO carry their
//! true class: the wrapper takes a [`checkpoint`] spanning the commit and the
//! arm upgrades the class via [`set_current_class_if_other`]
//! (`insert_multi`/`insert_single`). The `if_other` guard preserves an enclosing
//! `Copy` scope so a COPY that routes through the generic fallback (an
//! ineligible/columnar/trigger table, `handler.rs`) still attributes to `copy`
//! rather than being reclassified as `insert_multi`. Explicit `BEGIN … COMMIT`
//! never reaches that wrapper (its COMMIT runs through `handle_transaction_control`),
//! so it stays `Other` — `W3_2_DESIGN.md` §2.3 note 1 holds.
//!
//! ## Known label loss: parameterized / prepared re-execution (W3 follow-up)
//!
//! Prepared-statement re-execution — SQL `EXECUTE`, and the PG-wire extended
//! protocol's pinned-plan Execute — dispatches DML straight through
//! `execute_plan_with_params` → `execute_plan_with_params_inner` (`lib.rs`),
//! BYPASSING the scoped fast paths (`try_autocommit_fast_insert_params`,
//! `try_autocommit_fast_update_delete_params_cached`) that a simple-query
//! statement would take. That funnel sets NO `stmt_scope`, and its
//! autocommit (no active txn) main-branch INSERT lands in
//! `insert_tuple_versioned_with_schema` — the non-fast versioned path that §2.3
//! note 2 leaves UNINSTRUMENTED (recording its version without its `data:` write
//! would skew the ratio), so those rows appear in NO bucket at all; the buffered
//! / session-transaction sub-path instead records at COMMIT under the ambient
//! class (`Other`). Either way the per-class INSERT/UPDATE/DELETE buckets stop
//! growing once a workload shifts from cold simple-query execution (first
//! pgbench sweep, scoped) onto pinned prepared plans (steady state) — the
//! "counters freeze after the first sweep / label loss on cache rebuild" report.
//! The process-global atomics never literally freeze; the CLASS is lost (or the
//! whole write is omitted), not the count. Closing it means giving
//! `execute_plan_with_params_inner`'s autocommit DML its own class scope AND
//! instrumenting the versioned path's `data:`+version writes together, gated to
//! `session_txn.is_none()` so explicit session-transaction COMMITs keep `Other`
//! (note 1). That touches the hot OLTP path and must be validated by a live
//! `heliosdb_write_volume` re-run under pgbench, so it is deferred to the
//! coordinator gate rather than changed blind.
//!
//! Surfaced via the `heliosdb_write_volume` system view
//! (`sql/phase3/system_views.rs`): one row per statement class with the three
//! byte counters and a row-event count.
use Cell;
use ;
/// The statement class a write is attributed to. `Other` is the default and
/// catches buffered-commit and slow-path writes not wrapped in a [`stmt_scope`].
pub
/// Which key family the bytes belong to.
pub
const CLASS_COUNT: usize = 6;
/// Stable per-class display names, in [`class_index`] order.
const CLASS_NAMES: = ;
static ENABLED: AtomicBool = new;
// Explicit array literal (no inline-const) to compile on the repo's pinned
// toolchain regardless of `[const { .. }; N]` availability — matching the
// `lock_census` static (`lock_census.rs:98`).
static COUNTERS: = ;
thread_local!
/// Apply the runtime toggle. Process-global (last config wins — a diagnostic
/// aggregate, like a metrics registry), called from `EmbeddedDatabase`
/// construction.
pub
/// The single relaxed-load fast-out. Write funnels call this ONCE and gate all
/// their `add`/`add_row` calls on the result.
pub
/// Add `bytes` to the current statement class's `cat` counter. Callers MUST
/// have checked [`enabled`] first (so a disabled build pays only that one load).
pub
/// Count one written row (or tombstone) against the current class. Callers MUST
/// have checked [`enabled`] first.
pub
/// RAII statement-class scope. Restores the previous class on drop so nested
/// DML (e.g. a trigger's INSERT inside an UPDATE) attributes correctly. When
/// the census is disabled the guard is inert (one relaxed load, no thread-local
/// write), preserving the zero-cost-disabled contract at statement granularity.
pub
/// Enter a statement-class scope for the duration of the returned guard.
pub
/// Capture the current class and restore it when the returned guard drops,
/// WITHOUT changing it. Used where the durable write of an autocommit statement
/// lands at a `txn.commit()` decoupled from the plan arm that stages it (the
/// literal multi-row `INSERT … VALUES` path funnels through
/// `execute_with_implicit_transaction`, whose commit runs after the staging arm
/// returns — `lib.rs`). The wrapper takes this checkpoint so the arm may upgrade
/// the ambient class via [`set_current_class_if_other`] for the commit, and the
/// original class is restored afterwards — bounding that upgrade to the one
/// statement so a later explicit-transaction COMMIT is not misattributed
/// (`W3_2_DESIGN.md` §2.3 note 1). Inert (one relaxed load) when disabled.
pub
/// Upgrade the current statement class to `class` ONLY when it is still the
/// `Other` default, leaving any already-set enclosing scope untouched. Used by
/// the generic (buffered) INSERT plan arm to attribute the durable bytes it
/// stages — which are written by the enclosing autocommit COMMIT, not the arm —
/// to `insert_single`/`insert_multi`, while preserving an enclosing `Copy`
/// scope when the arm runs as a COPY generic-path fallback chunk. Does NOT
/// capture a restore point (the enclosing [`checkpoint`] owns that); a bare
/// non-restoring set would leak the class past the statement. Inert (no
/// thread-local write) when disabled.
pub
/// One census row for the system view.
pub
/// Snapshot all six per-class counters (always six rows; zeros when the census
/// has never been enabled).
pub