Estimate vs. Actual
After your watch with buffers, you head down to the engine room as if it were already your own: hatch, ladder, steady hum. QUERY isn't waiting by the planner this time. He's at a rack deep in the hall that, until now, you took for an archive cabinet. Inside are maps. Not maps of decks or corridors, but maps of data. The top sheet shows the life-support log: how many rows it contains, which statuses appear at every turn, and which are almost never seen. The navigator doesn't inspect the data itself when plotting a route. That would take longer than the voyage itself. He looks here, at the maps.
You remember yesterday's plan: every node had two rows values, one in the first pair of parentheses and one in the second. Yesterday they agreed: the navigator made a prediction and came very close.
QUERY: The first is what the navigator expected from the maps. The second is what actually happened during the voyage. As long as those numbers stay close, the machine sees the archive as it really is. Today's question: what happens when they drift apart?

Estimate and actual
In EXPLAIN ANALYZE, every plan node contains two pairs of parentheses, each with its own rows:
Seq Scan on ls_readings (cost=0.00..3763.00 rows=<estimate> ...) (actual time=... rows=<actual> loops=1)
- Estimated
rows— how many rows the planner predicts the node will return. This number exists BEFORE execution: it feeds into the parrots incost, the price tag the navigator uses to compare routes and choose the cheapest one. You saw it with plainEXPLAIN. - Actual
rows— how many rows the node actually returned. This number appears only after the voyage; the stopwatch adds it.
You've already seen each number on its own. Today's skill is comparing them: this is the main pair of numbers in a plan, and you'll keep using it throughout the course.
Where does the prediction come from? The planner keeps statistics, a compact map of each table: how many rows it contains, which values are common, and which are rare. The ANALYZE <table> command draws that map by scanning a random sample of rows and rebuilding the statistics. Don't confuse it with EXPLAIN ANALYZE. The names happen to overlap, but the tools do different jobs: ANALYZE draws the maps, while EXPLAIN ANALYZE executes the query and shows what actually happened.
Keep this reading rule handy for the rest of the course:
A several-fold gap between estimate and actual is noise. An ORDER-OF-MAGNITUDE gap is a signal. A prediction based on a random sample doesn't have to match row for row, and being off by a factor of two is routine. But if the navigator expected a thousand rows and twenty thousand arrived, the route was chosen using the wrong map.
Check the station's maps. The sandbox reseeds the log before every run, and the seeding ends with ANALYZE, so the statistics are as fresh as they can be. Ask the log about faults:
Maps go stale
Statistics are a , not a mirror. The ANALYZE command takes a snapshot of the table, and from that moment the map starts falling behind the territory: the data changes, but the snapshot does not until the next ANALYZE. In , a background process rebuilds the maps on its own schedule, once enough changes have accumulated — and the data doesn't wait. There is always a window between “the archive changed” and “the maps were redrawn.” During that window, the planner is blind.
Create that window yourself. Meet sensor 13, the noisiest sensor on the station: it already accounts for every fifth row in the log. Today it went haywire. A cascading failure dumped twenty thousand fault readings into the log in a single morning. The cell below appends them to the end of the log and — without letting the machine redraw its maps — asks the planner the same question about faults:
Why a stale map is dangerous
Today's query survived: there is only one node, and a full scan stays a full scan no matter how many rows pass the filter. The danger starts with larger plans. If the navigator predicts “there are almost no faults,” it can confidently choose routes designed for hundreds of rows — but twenty thousand will actually travel through them. A bad estimate in a lower node poisons every level above it: joins, sorts, table order — every later decision is based on a number that doesn't exist.
The diagnosis is clear, and so is the treatment: redraw the map. Add ANALYZE ls_readings; to the cell above, between the insert and the query, then run it again — the estimate will move much closer to the actual result. What those maps contain internally — which values the navigator remembers by name and how it estimates the rest — we'll cover in the navigator's cabin in Chapter 7.
QUERY: Notice that the machine didn't break. It faithfully executed the best route available on the map lying on the table. If you want a different route, bring it a fresh map.
You now have the full set of instruments: the blueprint, the stopwatch, I/O, and the estimate-versus-actual comparison. In the next lesson, we'll put them into a single sequence of actions — a workflow an engineer can use for any slow query.
EXPLAIN ANALYZE SELECT * FROM ls_readings WHERE status = 'fault';