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:
| Library | Role | Size | Notes |
|---|---|---|---|
| libgpos | Generic infrastructure | 248 files / 34,585 lines | Memory pools, tasks, exceptions, home-grown containers, test framework; the only truly ORCA-agnostic library |
| libnaucrates | Domain data and models | 868 files / 127,881 lines | 70.5% is DXL (ORCA’s private query/plan exchange format), plus metadata and statistics models |
| libgpopt | Optimizer framework | 911 files / 205,695 lines | Memo, scheduling, property system, 171 operators, 119 transformation rules, DP, engine |
| libgpdbcost | Policy plug-in | 9 files / 4,943 lines | Implements ICostModel only; fully replaceable |
There’s also the GPDB adapter layer (src/backend/gpopt, 20 files / 28,366 lines).
Key insight: libnaucrates is not a “base layer” — it’s conceptually on the same plane as libgpopt. Compile-time dependencies run both ways (statistics code includes gpopt’s CColRef, CMDAccessor, etc.).
2. Inside libgpopt: the framework is a small fraction
| Directory | Lines (src+include) | Nature |
|---|---|---|
search/ | 9,288 + 4,836 | ★★★ Framework core: scheduler, memo, groups |
engine/ | 3,227 + 1,236 | ★★★ Engine entry and search stages |
optimizer/ | 710 + 242 | ★★★ The single entry COptimizer::PdxlnOptimize |
base/ | 24,392 + 12,319 | ★★★ Half framework (contexts/properties/costs), half domain (columns/constraints/partitions) |
operators/ | 51,443 + 28,997 | ★ Content: 171 operators, highly homogeneous |
xforms/ | 27,445 + 16,922 | ★ Content: 119 rules, equally homogeneous |
mdcache/ translate/ | ~19K | Boundary: metadata and DXL translation |
Of 205,695 lines, the framework proper is under 40K; the rest is “operator and rule content.” Read three operators and you understand a class of them; read three rules and you understand a batch.
3. Six mechanisms worth reading (~11K lines total)
| # | Mechanism | Core classes | Size |
|---|---|---|---|
| M1 | Task engine (gporca’s most distinctive piece) | CJob CJobStateMachine CJobQueue CScheduler CJobFactory CSchedulerContext | ~2,251 lines |
| M2 | Search space (memo) | CMemo CGroup CGroupExpression CGroupProxy CBinding | ~3,935 lines |
| M3 | DP and memoization | COptimizationContext CCostContext | ~1,151 lines |
| M4 | Property system (mirror + gap-filling) | CReqdProp* CDrvdProp* CEnfd* CPropSpec* | ~2K lines |
| M5 | Engine and search stages | CEngine, search stages, pruning | ~1.5K lines |
| M6 | Transformation rule protocol | CXform, CXformFactory, rule sets | ~1.5K lines |
M1 is the “task network” (next post), M2 is equivalence-class compression, M3 is the DP cell, M4 is the required/derived duality.
4. A pattern that runs through the whole codebase: three-state machines
Almost every ORCA object follows estXxx → estXxxing → estXxxed:
| Object | States |
|---|---|
CGroup | Unexplored → Exploring → Explored → Implementing → Implemented → Optimizing → Optimized |
CGroupExpression | Unexplored → … → Implemented (no Optimized) |
COptimizationContext | Unoptimized → Optimizing → Optimized |
CCostContext | Uncosted → Costing → Costed |
An “ing” state = the marker left when a task suspended after spawning children; an “ed” state = written when the task finished. Once this clicks, all the job state-advance logic makes sense.
5. A practical reading order
scheduler / tasks (M1)
→ memo (M2)
→ DP cells (M3)
→ property system (M4)
→ engine and stages (M5)
→ rule protocol (M6)
Three accompanying rules:
- Know libgpos by contract only (memory pools, tasks, exceptions, refcounts) — don’t read the implementations;
- Know libnaucrates by interface only (
IMD*,IStatistics) — skip DXL details; - Read only samples of operators and rules — they are content, not framework.
6. Implications for porting
If you want to bring this framework into your own project, compute the dependency closure first: a single header, COptimizationContext.h, drags in 89 files; with its .cpp it’s 361 files across 54 layers.
That number tells you: “port class by class following dependencies” does not work. The viable path is vendoring upstream + a thin adapter layer + trimming (a single-node engine doesn’t need the whole MPP distribution/motion machinery).
Next: the cleverest piece of the M1 task engine — the reference-count gate.
Related
- Reading gporca (Part 3): The Key of a DP Cell — (Group, Required Properties, Stage)
- Reading gporca (Part 2): The Reference-Count Gate That Turns Recursive Search into a Pausable Task Network
- Porting a 1999 Cascades Optimizer to Modern C++: The COLUMBIA Story
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.