The first three posts were about features. This one is about the engineering of the metadata itself: where snapshots and branches are stored, and how they survive.
1. The problem: a metadata block is not transactional
The first implementation wrote the reference registry (snapshots/branches/pins) into the checkpoint metadata block. It persisted, but had two fatal issues:
- Not in a transaction: if
CREATE SNAPSHOTfailed or rolled back, the block might already have been written; - No WAL: if the process crashed before the next checkpoint, the change was gone.
The actual requirement: reference changes must commit or roll back with the caller’s transaction, like ordinary data, and go through WAL replay.
2. The design: a hidden schema + four tables
We turned the registry into internal tables:
schema git4data
├── snapshots -- name, pin, created
├── branches -- name, pin, parent, base_line, ...
├── storage -- materialized tables referenced by branches (source / storage / oid)
└── deltas -- deltas referenced by branches (added / removed)
Key decisions:
| Decision | Reason |
|---|---|
| Changes go through ordinary DML (INSERT/UPDATE/DELETE) | Transactions, WAL and rollback come for free |
| Load the tables back into memory after WAL replay | The in-memory registry is a cache; the table is the truth |
Keep the start_timestamp counter in the file header / metadata block | Reading a table row needs a transaction, and the transaction’s start/commit id is issued from it — it can’t move |
| Keep the old metadata block as a fallback for old files | Migration compatibility; later removed by decision |
3. Difficulty 1: hidden but readable
The engine’s “internal object” flag only applies to the system catalog or a schema named main — it is unusable for persisted ordinary tables. So hiding and protection had to be hand-written:
- Filter the schema out of
duckdb_tables/duckdb_schemas/information_schema/pg_catalog; - Add guards in the insert/update/delete/drop/alter binders, refusing direct user writes;
- But allow reads — table functions like
git4data_snapshots()need to read it.
A subtle trap here: the feature’s own generated statements were blocked by the guards too. The fix was to mark statements returned by pragmas with SQLStatement::internal; the guards only stop non-internal statements. Later we found IMPORT DATABASE could exploit that channel, so the marking was narrowed to “only statements produced by git4data_* pragmas.”
4. Difficulty 2: crash consistency
The verification is blunt: kill -9, then reopen.
| Scenario | WAL | After reopen |
|---|---|---|
| Create a snapshot | 1,000 bytes | Reference still there, reads 100 rows |
| Create branch + materialize + delta | 3,324 bytes | 2 branches, 1 materialization, 1 delta mapping all present |
This grew into a 47-check crash-consistency suite, all passing.
5. Difficulty 3: identity
“Is this table the one that was bound?” is harder than it sounds:
table_oidsurvivesRENAME(measured), but oids are not persisted — after restart you can’t tell;- So we introduced a persisted table identity (
persisted_table_id, a UUID minted at creation and written into the table’s metadata), recorded by mappings and checked on read; - Plus mapping age:
set_atrecords the counter value when the mapping was created; reading at an older pin may see a schema that doesn’t match, and is refused.
Identity is the deepest hidden invariant in a version system. Most bugs eventually come down to “you thought it was still the same table, and it wasn’t.”
6. Summary
- Metadata needs transactions too: DML + WAL + rollback, like ordinary data;
- The reason the counter can’t move is worth writing down (it exists before any transaction);
- “Hidden but readable” requires hand-written guards, with a channel for the feature’s own statements;
- Identity (table, database, mapping) is the dimension most easily forgotten.
Together, the four posts describe a complete data version system in four layers: feasibility → retention → primitives → metadata engineering. The result: snapshots, branches, diff, pick, merge, clone, PITR and quotas — 85 test files, 4,592 assertions, zero feature failures in the full regression.
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 2): Pinning History — Snapshots, Retention Watermarks and AT SNAPSHOT
- 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.