Database Kernel (6): WAL, Replay and Checkpoint

Transactions make concurrency correct, but machines crash. What recovers the system? An append-only log, plus a marker for where to start replaying. The final post in the plan series. 1. What’s in the WAL walog is a file storing redo log entries sequentially: Type Written when insert / delete / update a transaction writes flush a transaction commits checkpoint everything before it is already applied to tables create table / schema / use table DDL and table switching Key convention: entries between two flushes belong to one transaction, so replay needs no extra transaction-boundary metadata. ...

October 22, 2026 · 2 min · daviszhen

Database Kernel (5): Transactions, Timestamps and GC

Multiple versions make concurrent reads and writes possible, but versions aren’t free: who creates them, who can see them, and when they can be dropped all need explicit answers. Part five of the plan series. 1. TxnMgr holds very little TxnMgr{ currentStartTs uint64 // starts at 2, +1 per txn, < 2^62 currentTxnId uint64 // starts at 2^62, +1 per txn lowestActiveId uint64 lowestActiveStart uint64 activeTxns []Txn // created, not committed/rolled back committedTxns []Txn // committed, not GC'd oldTxns []Txn // GC-able, not GC'd } The two counters start in different ranges so timestamps and transaction ids never collide: ...

October 22, 2026 · 2 min · daviszhen

Database Kernel (4): Storage — Row Groups, Column Segments and Four Layers

Execution deals in chunks; storage answers a different question: where those chunks live, at what granularity, and how updates and versions fit in. Part four of the plan series: the four-layer in-memory layout. 1. Four layers table └── row group (122,880 rows) └── column data (one column) └── column segment (256 KB block, up to 60 vectors) └── vector (2,048 rows) The numbers are aligned on purpose: 60 × 2,048 = 122,880 — one row group is exactly 60 full vectors; a segment holds up to 60 vectors, about 256 KB; a vector is the smallest read/write unit and the executor’s batch size. Alignment pays off: scans skip whole segments and read whole vectors, with no cross-block stitching. ...

October 22, 2026 · 2 min · daviszhen

Database Kernel (3): Execution — Vectors, Chunks and the Vectorized Volcano Model

The plan tree settles what to do; execution settles how to do it batch by batch. The key here isn’t the algorithm — it’s memory layout: the same data arranged differently can be an order of magnitude faster. Part three of the plan series. 1. vector: logically an array, physically maybe not A vector is logically a fixed-size array, but it has several physical representations: Representation Use case Stores FLAT default contiguous array, same as logical CONSTANT whole column identical one value DICTIONARY low-cardinality strings indices + dictionary SEQUENCE increasing sequence base + increment UNIFIED generic algorithms contiguous data + selection Why so many? Because many columns during execution are constant or repetitive: ...

October 22, 2026 · 3 min · daviszhen

Database Kernel (2): From Syntax Tree to Plan Tree — Filter Pushdown and Join Ordering

Bind produces scopes and expressions; execution needs a plan tree. The conversion in between decides whether a query merely runs or runs fast. This is part two of the plan series: how the plan tree is built, and the two optimizations that matter most. 1. The minimal plan node type LogicalOperator struct { Typ LOT // scan / filter / project / join / agg / order / limit Children []*LogicalOperator // node-specific extra fields } Generation follows the bind order: ...

October 22, 2026 · 3 min · daviszhen

Database Kernel (1): Bind Is Semantic Analysis, Not Syntax Checking

People often assume SQL parsing means “turn the string into a tree” and then execution can start. The step in between — bind — is the most underrated: it decides which table and column every name refers to, in which scope. This is the first post in the plan implementation series: what bind does, and why the order matters. 1. Four components Component Role Builder The driver for semantic analysis, one per select; also holds partial results BindContext A scope: which relations are visible here; scopes nest Binding The definition of a relation (table or subquery) and its columns Expr Everything except relations becomes an expression The key design is nested BindContexts: a subquery isn’t “a new parser”, it’s a new scope on top of the parent. Name lookup walks up until it finds something or errors. ...

October 22, 2026 · 3 min · daviszhen

The Optimizer's Cost Model: From Formulas to Calibrated Numbers

The search space decides how many plans you can think of; the rules decide what shapes are reachable; and the cost model decides who wins. Whether an optimizer is usable depends heavily on whether its cost model “makes sense”. This post takes COLUMBIA’s cost model apart, plus the traps we hit when calibrating it on TPC-H. 1. What COLUMBIA’s cost model looks like Minimal: a cost model is a set of constants plus a local-cost formula per physical operator. ...

October 20, 2026 · 3 min · daviszhen

Data Version Control: Why a Database Needs Git

Git gave code cheap branching, rollback and merge. Data never got the same deal — because data is huge; one copy is a terabyte. But a trend is changing that: the people editing data are turning into AI clusters. When the scale and frequency of writes go up an order of magnitude, branch / merge / rollback move from “nice feature” to “the only way this can be safe”. 1. What data versioning needs Strip the product narrative and a data version system needs at least three things: ...

October 20, 2026 · 3 min · daviszhen

Writing a Query Optimizer: From COLUMBIA to TPC-H

Query optimizers are the component everyone uses and few people write. You can recite Join Order, subquery unnesting and cost models in an interview — but actually writing an optimizer that survives all 22 TPC-H queries teaches you that the hard part isn’t the vocabulary. It’s the way the details constrain each other. This series is a record of one such attempt: from porting a textbook Cascades implementation, to making it digest TPC-H, to admitting the traps it fell into. ...

October 20, 2026 · 3 min · daviszhen

Implementing Git for Data on DuckDB (Part 1): Feasibility — Why an Analytical Database Can Grow Version Control

Git gave code cheap rollbacks, parallel branches and merges. Data never got the same deal — because data is huge; one copy is a terabyte. But a trend is changing that: the people editing data are turning into AI clusters. Once scale and frequency go up an order of magnitude, branching, rollback and merge move from “nice feature” to “the only way this can be safe.” That’s the starting point of Git4Data. MatrixOne has already built these primitives into a database. My question: can DuckDB do the same? ...

October 20, 2026 · 3 min · daviszhen

Rewriting Lucene in C++ (Part 1): Byte-Level Compatibility — When Java Reads a C++-Written Index

The easiest way to go wrong when porting Lucene to C++ is to say: “I’ll invent my own index format, and just be self-consistent.” That works, but it has no verification standard: you can’t prove your implementation is correct, only that it agrees with itself. I chose the other path: byte-level compatibility. The goal is that Java Lucene 10.4 can read a C++-written index (CheckIndex OK), C++ can read a Java-written index, and the dumps from both sides are byte-for-byte identical. ...

October 20, 2026 · 3 min · daviszhen

Reading gporca (Part 3): The Key of a DP Cell — (Group, Required Properties, Stage)

The first two posts covered the framework mechanics and the scheduler. This one goes into the heart of the memo: the dynamic-programming cell. 1. Correcting an intuition Many people assume the optimizer’s job is to “find one optimal plan per group.” But gporca’s problem definition is: For each group, under the given requirements, find an optimal plan. Because the best implementation for the same group differs completely under different requirements: ...

October 20, 2026 · 4 min · daviszhen

Reading gporca (Part 2): The Reference-Count Gate That Turns Recursive Search into a Pausable Task Network

Query-optimizer search is inherently recursive: explore a group, derive plans, recurse into each input. The naive implementation is plain depth-first recursion. gporca doesn’t do that — it flattens the recursion into a task network, driven by a ready queue and a single reference counter. This post takes apart the most subtle piece: the m_ulpRefs reference-count gate. 1. m_ulpRefs is not a lifetime counter CJob::m_ulpRefs has three sources that must be read together: ...

October 20, 2026 · 4 min · daviszhen

Reading gporca (Part 1): Only ~11K of Its 400K Lines Are Actually Framework

gporca is Greenplum’s query optimizer: about 401K lines across 935 compilation units. Opening it for the first time is intimidating. But if you slice it by “is this a framework design decision?”, the part that is actually framework is under 10K lines. 1. Four libraries, four roles Many people assume libgpopt is the framework and the other three are “foundational components.” That’s imprecise. They are four different kinds of thing: ...

October 20, 2026 · 4 min · daviszhen

Making an Optimizer Run TPC-H: Lossless Conversion and Execution Reconciliation for 22 Queries

An optimizer is worth using when it can digest all 22 TPC-H queries — not when it can run a couple of toy examples. This post records how we took COLUMBIA (see the previous post) from “runs Chain2” to “handles the whole TPC-H suite.” 1. Principle: convert as-is, never rewrite for the optimizer The first step wasn’t code, it was rules: As-is conversion: no restructuring of TPC-H SQL to make the optimizer’s life easier; Record what’s unsupported: syntax the optimizer can’t express yet goes on a TODO list, never gets deleted from the query; Reconcilable: the conversion must be provably equivalent to the original. The value of this discipline only shows up later: it turns “capability gaps” into a quantified list instead of a vague pile of failures. ...

October 20, 2026 · 4 min · daviszhen

Porting a 1999 Cascades Optimizer to Modern C++: The COLUMBIA Story

If you want to hand-write a query optimizer, the cheapest starting point isn’t inventing one from scratch — it’s understanding a real Cascades implementation first, then getting it to run on your own machine. My sparring partner was COLUMBIA — the master’s thesis implementation by Yongwen Xu at Portland State University (1998–2000, advised by Shapiro and Maier), a textbook-grade Cascades/Volcano optimizer in 19,787 lines across 17 .cpp and 17 .h files. ...

October 20, 2026 · 4 min · daviszhen