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:

WatermarkComputed fromUsed for
LowestVisibilityBound()Active transactions onlyDeciding 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:

  1. Pass options.retention_bound = LowestRetentionBound() to checkpoint, and let reclamation skip when it’s unsafe;
  2. Write delete information to disk: DuckDB originally stored only a boolean delete mask, no ids. We extended ChunkInfoType with CHUNK_INFO_WITH_IDS to 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 in grammar_types.yml, or the generated parser won’t accept it;
  • Binding: detect AT in bind_basetableref and swap the table’s logical_get for 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

TrapSymptomFix
Self-deadlockSnapshot scan and a normal query wait on each otherThe read path must not take SegmentLock (EXCLUSIVE) — it’s for index builds
Virtual columnsVirtual columns require projection pushdownClear logical_get->virtual_columns at bind time
Empty vectorSIGBUSGetSelVector returns capacity without filling the selection vector when nothing is deleted; guard before iterating

6. Measured retention

After the fixes, retention measured by script:

ScenarioResult
Deletes, pinnedOld data still readable: 1,000 rows
Deletes, pin releasedOnly 600 rows (400 reclaimed)
Updates, pinnedOld version readable: 2,000 rows
Updates, no pinOnly 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.

Get the next essay

New essays on products, business and tech — plus what the experiment's numbers actually say.