Issue-26: What Happens Before Your Query Touches a Single Page
Issue-26: What Happens Before Your Query Touches a Single Page
Bytes & B-trees · Issue #026 — Query Parsing | Parse tree → analyzer → rewriter → planner
You already know indexes, WAL, and MVCC decide how work lands on disk. This lesson opens the door before all of that: the moment a SQL string becomes a plan the executor can run.
Big shops burn real CPU here. Oracle calls a cache miss a hard parse. MySQL’s
Performance_schematracksSQL/parse. Postgres caches paths for prepared statements so the same text does not rebuild a tree on every execute. If you only readEXPLAIN, you are looking at the end of this pipeline—not the start.What you will walk out with: a working local simulator of lexer → parse tree → bind → rewrite → plan choice, plus a live metrics board you can poke.
Success criteria
You are done when all of these are true:
python3 query_parsing_demo.pyfinishes and writes non-zero metrics undermetrics/../start.shserves the board at
http://127.0.0.1:8096/
and Run query parsing demo moves the counters.
You can point at one sample query and name which stage flattened
IN (SELECT …)and which stage chose IndexScan vs SeqScan.python3 -m pytest tests -qis green.
The pipeline in one breath
SQL text is not a plan. It is ink. The engine walks five jobs in order:
Industry wrinkle most tutorials skip: parse ≠ plan. Postgres’s raw parse tree is almost never what you see in EXPLAIN (FORMAT JSON). Production debugging that mixes “bad SQL text” with “bad plan” wastes hours. Separate the stages and the bug class becomes obvious.
Another quiet tax: search_path and privileges fail in analyze, not in the planner. A missing relation error is a bind failure. A slow query with a correct tree is a later problem.
Diagrams
Diagram 1 — Pipeline architecture
Read it: SQL enters on the left.
Parse packs lex + tree + bind.
Rewrite reshapes the tree.
Plan is the handoff into the rest of the engine (indexes, buffers, WAL from earlier issues). The dashed line is the bridge to the storage stack you already studied—not a new product, the same database continuing its day...
Continue reading this post for free in the Substack app
© 2026 System Design Roadmap
548 Market Street PMB 72296, San Francisco, CA 94104