How to Read an Unfamiliar Codebase: gporca as an Example

Opening a 400K-line optimizer, the first question is usually “where do I start”. The answer isn’t line one — it’s one complete call path. 1. Four steps Step Goal How 1. Find the entry know where execution starts main → init → handling one query 2. Read the data structures know where state lives structs, containers, memo/group objects 3. Follow the control flow see one task complete breakpoints or logging on a minimal input 4. Change one thing prove you understood flip a branch, check the output changes as expected Step 4 is the point: reading without changing is usually an illusion of understanding. ...

October 22, 2026 · 2 min · daviszhen

gporca's Rule System: How to Read 119 Rules

The previous post covered gporca’s DP cells. This one covers rules: xforms/ contains 119 files, roughly 44K lines. At first it looks like required reading — it isn’t. Rules are highly homogeneous; understanding three of them teaches you a whole class. 1. There are only three kinds of rules Kind Role Examples Transformation Produce equivalent logical expressions inside the same group Join commutativity, associativity, predicate pushdown Implementation Turn a logical expression into an executable physical operator HashJoin, IndexScan, Sort Enforcer Insert operators to satisfy parent requirements Sort (order), Motion (distribution) Search works like this: transformations grow groups, implementations propose candidates, DP picks the cheapest per group. ...

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