![[chap_3_planner.png]]
# Chapter 3: The Query Planner (The Lazy General)
When you hand an incredibly complex request to Postgres, the engine does not immediately grab a shovel and start digging. That would be rash, and rashness leads to unnecessary physical exertion.
Instead, it hands your request up the chain of command to the **Query Planner**—the Lazy General.
The Lazy General sits comfortably in its tent, far away from the physical labor of scanning pages or building hash tables. Its sole responsibility is to stare at a wobbly, abstract map of the data and calculate the absolute cheapest, fastest, most effortless path to achieve your goal.
It does this by making deeply pessimistic, paranoid assumptions about how terrible the real world is. It maintains massive probabilistic histograms and statistics about your data. If you ask for all rows where `age > 99`, the General checks its cheat sheet, realizes almost no one is over 99, and calculates that reading an index instead of the whole table is much less effort.
To make these decisions, the General uses a rigid "Price List" grounded in the harsh reality of [[_Resources|Elephant Time]]. He knows that while a thought in his head takes only a second, a single trip to the frozen basement ([[Disk IO]]) is the equivalent of a **4-month-long winter expedition**. He assigns points to every action—deciding that walking down a hallway costs 1 point, but digging through a file folder costs 100 points. He calculates exactly how much it "costs" to haul bytes into memory, whether it's cheaper to sort a list mentally or scribble it onto a temporary notepad, and ultimately writes a flawlessly lazy battle plan.
Crucially, the General’s plan is not some infinite, mysterious set of instructions. Every battle plan is composed of a finite, **enumerable** set of specialized soldiers. These are the [[_Postgres Operations|Operation Nodes]]—the building blocks of every query ever executed. Each soldier has a specific job and a specific cost. You can find the entire roster of these soldiers in the [[Operations/]] folder.
The General's army is a perfectly modular machine. Because every soldier is trained to respect the **Types** of the data, they all click together perfectly. This is his private **Algebra**.
Think of a query not as a list of commands, but as a **Path** through a multi-dimensional landscape. Every operation is a **Warp** that changes the data's shape—a [[Sort]] warps it from 'Chaotic' to 'Ordered', while a [[HashJoin|Matchmaker]] merges two distant landscapes into one. Because every soldier agrees on these **Shapes (Types)**, the General can treat your query as a series of topological moves. If two different paths lead to the same result, the General knows they are **Equivalent**, and his only job is to find the path that requires the least amount of heavy lifting.
This simple rule—that we can warp and transform the data as long as we maintain its 'Type'—allows the General to build infinite complex paths out of a finite, enumerable toolkit.
Only when the math proves that a specific path is the absolute smoothest way through the world does the General issue the orders.