Lesson 4 of 12

Paths vs Plans: How the Optimizer Chooses, Then Commits

Applies to PostgreSQL 13–17 Last reviewed Jun 2026 Grounded in source

The one thing to understand first

The <a class="sev1-termlink" href="https://thesev1database.com/glossary/query-planner/" title="Query planner">optimizer does its work in two different currencies. First it sketches many lightweight candidates called Paths — cheap to create, cheap to compare, annotated only with cost. Then it converts the single winning Path into a heavyweight Plan — the detailed, executable form. Understanding why there are two representations explains almost everything about how PostgreSQL picks one strategy over another, and why a more expensive-looking option sometimes wins.

Why two representations at all?

Think of planning a drive. Comparing routes on a map — distance, estimated time — is fast, and you might weigh dozens. Writing out the turn-by-turn directions is detailed work you only do once, for the route you actually pick. Paths are the routes on the map; the Plan is the turn-by-turn directions. Generating thousands of Paths and costing them has to be cheap, so a Path carries only what is needed to compare alternatives. Only the survivor pays the price of becoming a full Plan.

Stage 1 — Path generation

Path search lives in src/backend/optimizer/path/. For every relation (a RelOptInfo), set_rel_pathlist() in allpaths.c generates candidate access paths: a sequential-scan path, one index-scan path per usable index, bitmap-scan paths, and so on. Each is built by a helper in pathnode.ccreate_seqscan_path(), create_index_path() — and costed by functions in costsize.c (cost_seqscan(), cost_index()). Joins add their own combinatorics in joinpath.c: nested loop, merge join, and hash join paths across different join orders.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • Stage 2 — Keeping only what matters: add_path
  • The cost model in one paragraph
  • Stage 3 — Commit: create_plan
  • Layer 3 — Watch path selection on your own database
  • Layer 4 — The levers this hands you
  • Layer 5 — What an Oracle DBA should expect vs what they get
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Was this helpful?

← Back to 05 — The Engine Room: How PostgreSQL Actually Executes