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:

LibraryRoleSizeNotes
libgposGeneric infrastructure248 files / 34,585 linesMemory pools, tasks, exceptions, home-grown containers, test framework; the only truly ORCA-agnostic library
libnaucratesDomain data and models868 files / 127,881 lines70.5% is DXL (ORCA’s private query/plan exchange format), plus metadata and statistics models
libgpoptOptimizer framework911 files / 205,695 linesMemo, scheduling, property system, 171 operators, 119 transformation rules, DP, engine
libgpdbcostPolicy plug-in9 files / 4,943 linesImplements 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

DirectoryLines (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/~19KBoundary: 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)

#MechanismCore classesSize
M1Task engine (gporca’s most distinctive piece)CJob CJobStateMachine CJobQueue CScheduler CJobFactory CSchedulerContext~2,251 lines
M2Search space (memo)CMemo CGroup CGroupExpression CGroupProxy CBinding~3,935 lines
M3DP and memoizationCOptimizationContext CCostContext~1,151 lines
M4Property system (mirror + gap-filling)CReqdProp* CDrvdProp* CEnfd* CPropSpec*~2K lines
M5Engine and search stagesCEngine, search stages, pruning~1.5K lines
M6Transformation rule protocolCXform, 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:

ObjectStates
CGroupUnexplored → Exploring → Explored → Implementing → Implemented → Optimizing → Optimized
CGroupExpressionUnexplored → … → Implemented (no Optimized)
COptimizationContextUnoptimized → Optimizing → Optimized
CCostContextUncosted → 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:

  1. Know libgpos by contract only (memory pools, tasks, exceptions, refcounts) — don’t read the implementations;
  2. Know libnaucrates by interface only (IMD*, IStatistics) — skip DXL details;
  3. 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.

Get the next essay

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