The previous post argued that the counterintuitive gate is retention. This one takes it apart: two watermarks, one measurement, and making AT (SNAPSHOT => 'name') actually read.
1. Two watermarks
DuckDB’s MVCC only answers “can this transaction see that row.” Supporting snapshots means separating two questions:
| Watermark | Computed from | Used for |
|---|---|---|
LowestVisibilityBound() | Active transactions only | Deciding which commits are “past”: releasing locks, cleaning transaction records |
LowestRetentionBound() | min(active transactions, pinned snapshots) | Deciding which historical versions must not be reclaimed |
We initially only had the first. The result: create a snapshot, run a checkpoint, and the old version is reclaimed anyway — because to checkpoint, there are no active transactions, so the old version is garbage.
2. A measurement that proved us wrong: checkpoint ignores pins
Our first assumption was “passing a retention_bound to checkpoint is enough.” To verify, I instrumented CheckpointBlockIdDropper::Visit:
with a pin: 198 blocks dropped
without a pin: 198 blocks dropped
Identical. So the CONCURRENT_CHECKPOINT path doesn’t read the retention watermark the way we assumed.
The real fix has two layers:
- Pass
options.retention_bound = LowestRetentionBound()to checkpoint, and let reclamation skip when it’s unsafe; - Write delete information to disk: DuckDB originally stored only a boolean delete mask, no ids. We extended
ChunkInfoTypewithCHUNK_INFO_WITH_IDSto persist each row’s insert/delete id — otherwise after restart, “when was this row deleted” is lost.
3. Pins must be persisted
Commit ids come from current_start_timestamp++ and are not durable across processes. If pins aren’t written to the file alongside references, snap_01 after a restart points at a past that doesn’t exist.
Our approach: reference changes and checkpoint go into one atomic unit, and loading restores the “last commit” alongside. We hit a subtle bug here: in a session that never restarted, last_commit was 0, so a new pin landed on 0 and a diff showed 3 phantom deletions. Fix: persist last_commit, and after restart use max(persisted, counter - 1).
4. AT SNAPSHOT: grammar and execution
A clause on the table reference:
SELECT * FROM t AT (SNAPSHOT => 'snap_01');
Three implementation points:
- PEG grammar: add
SnapshotAtUnit <- 'SNAPSHOT'and register the type ingrammar_types.yml, or the generated parser won’t accept it; - Binding: detect
ATinbind_basetablerefand swap the table’slogical_getfor a scan-at-pin table function; - Scanning: use a synthetic
TransactionData(MAX_TRANSACTION_ID, bound)over the persistent row groups — “everything committed, but only up to the pin” then holds naturally.
5. Three traps
| Trap | Symptom | Fix |
|---|---|---|
| Self-deadlock | Snapshot scan and a normal query wait on each other | The read path must not take SegmentLock (EXCLUSIVE) — it’s for index builds |
| Virtual columns | Virtual columns require projection pushdown | Clear logical_get->virtual_columns at bind time |
| Empty vector | SIGBUS | GetSelVector returns capacity without filling the selection vector when nothing is deleted; guard before iterating |
6. Measured retention
After the fixes, retention measured by script:
| Scenario | Result |
|---|---|
| Deletes, pinned | Old data still readable: 1,000 rows |
| Deletes, pin released | Only 600 rows (400 reclaimed) |
| Updates, pinned | Old version readable: 2,000 rows |
| Updates, no pin | Only 1,000 rows |
7. Summary
- A snapshot’s promise must be verified at the level where it is made (checkpoint / on-disk format), not by
SELECT count(*)looking right; - Two watermarks, persisted pins, delete ids on disk — all three are required;
- Syntax is the last mile; the real work is in the storage layer.
Next: branches, diff, pick, merge — the core primitives of a data version system.
Related
- Implementing Git for Data on DuckDB (Part 3): Branches, diff, pick, merge — The Version Primitives of Data
- Implementing Git for Data on DuckDB (Part 4): Moving Metadata into Tables — Transactions, Crash Consistency and ‘Hidden but Readable’
- Rewriting Lucene in C++ (Part 1): Byte-Level Compatibility — When Java Reads a C++-Written Index
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.