Forty Seconds: Your First EXPLAIN ANALYZE

Timing: EXPLAIN ANALYZE

12 min

Query plans no longer look like secret code: cost, rows, width—you read them like a navigator's chart. But on your way down to the engine room, an uncomfortable thought occurs to you: all of those numbers are estimates. The navigator PROMISES a route that costs so many parrots. But how will the trip actually go?

QUERY is waiting for you at the test bench, a heavy slab with clamps and an ancient mechanical stopwatch hanging above it on a chain.

QUERY: The plan tells you how the navigator intended to travel. Timing tells you how the trip actually went. An engineer who trusts plans alone will eventually build a bridge out of paper. Start the stopwatch.

The same machines are now running: belts turn and steam bursts out, while the chalk outline of the estimate appears faintly over them; the cadet holds a brass stopwatch, a mechanical counter clicks nearby as chalk marks record repeated passes, a robot cat sits on a warm pipe above, and a parrot is perched on the stopwatch.
The same route, now actually traveled: actual measurements appear beside the estimate, along with the number of rows and the number of executions.

The second set of parentheses: actual results beside the estimate

Add one word to the familiar EXPLAIN:

EXPLAIN ANALYZE <your query>;

With ANALYZE, the machine doesn't stop at an estimate—it ACTUALLY RUNS the query, times every node, and prints the same plan with real measurements added. You've already encountered this command in Vault-9; now we'll break down its output number by number. Each node gets a second set of parentheses:

(cost=… rows=… width=…)                ← navigator's estimate, parrots
(actual time=start..finish rows=… loops=…) ← stopwatch readings, actual results
  • actual time=start..finish — follows the same pattern as cost from the previous lesson, but the units are real: MILLISECONDS. The first number tells you when the node produced its first row; the second tells you when it finished.
  • rows — how many rows the node ACTUALLY returned. Together with the estimated rows in the first set of parentheses, this is the most important pair of numbers in the plan. We'll devote a separate lesson to it.
  • loops — how many times the node ran during the trip. For now, remember that it's usually 1; a little later, you'll see this number explode.

Two administrative lines appear at the bottom of the plan: Planning Time tells you how long the navigator spent planning, and Execution Time tells you how long the executor spent running the query.

ANALYZE means a real execution. EXPLAIN ANALYZE UPDATE … does not merely “show the update plan”—it UPDATES the rows and then shows you the plan for what it did. The same applies to DELETE. In Vault-9, you learned the protective wrapper: BEGIN; EXPLAIN ANALYZE UPDATE …; ROLLBACK;—you get the plan, then roll back the changes. Transactions are disabled in the station sandbox, but that's safe here: the archive is rebuilt from scratch before every cell run. On a database, the rule is absolute: under ANALYZE must run inside a with ROLLBACK.

The estimate meets reality

Our test subject is temperature sensor No. 388. It hangs directly above the engine-room hatch on deck J. The query collects its entire timeline of readings: a full scan of the log with a filter, followed by a sort.

First, look at the plan without running the query, just as you already know how to do:

EXPLAIN
SELECT reading_id, reading_ts, value
FROM ls_readings
WHERE sensor_id = 388
ORDER BY reading_ts;
EXPLAIN ANALYZE
SELECT reading_id, reading_ts, value
FROM ls_readings
WHERE sensor_id = 388
ORDER BY reading_ts;

Reading the stopwatch

Three observations from this timing run:

  1. The navigator was almost exactly right. The estimate predicted about 360 rows; the actual result was exactly 361. When the estimate and the actual result agree, the navigator is working from up-to-date charts. A tenfold mismatch is a warning sign, and we'll devote a separate lesson in this chapter to it.
  2. actual time reveals how a node behaves. starts near zero, while its finish time accounts for almost the entire trip: it produces its first row immediately and then keeps streaming steadily, like the stream-like node from the previous lesson. With Sort, the start and finish times are almost identical: sorting cannot return a SINGLE row until it has seen them all. It's the same dam as before: it holds everything back, then releases the result all at once. Those two numbers already tell you what kind of node you're looking at.
  3. Milliseconds fluctuate. Run the cell again and the times will change slightly because of caches, neighboring processes, and the station's mood. But rows=361 and Rows Removed by Filter: 179639 won't move by even one row. The result of the trip is deterministic; only the stopwatch needle trembles, and, as you remember from the previous lesson, the navigator's estimate can shift too. That's why engineers report rows and nodes precisely, while timings are usually discussed only by order of magnitude.

The node that ran forty times

In the plans above, every node had loops=1: each node ran once during the trip. That isn't always the case. Remember the report that hung: the machine reran the counting for EVERY sensor. Now you'll see what that looks like on the stopwatch.

We'll take forty sensors from deck J, the same deck whose hatch brought you down here, and ask for each one: when did it last check in?

EXPLAIN ANALYZE
SELECT
  s.sensor_id,
  (SELECT max(r.reading_ts)
   FROM ls_readings r
   WHERE r.sensor_id = s.sensor_id) AS last_seen
FROM ls_sensors s
WHERE s.deck = 'J';
The parentheses describe one of forty executions: 361 rows become 14,440 rows passed upward, while seven million rows are scanned

The key rule for the parentheses: multiply by loops

The numbers in the actual parentheses are AVERAGES PER ONE execution of the node (rounded), not totals. To calculate the node's full contribution, multiply:

  • Aggregate: rows=1 × loops=40 → forty rows over the entire trip, one “last check-in” for each sensor.
  • on ls_readings: rows=361 × loops=40 ≈ 14,440 rows returned. But FAR more rows were scanned: every execution goes through the entire log, so 40 × 180,000 = 7,200,000 rows—seven million rows for forty answers.
  • actual time — also an average per execution: the node's total time is approximately its finish time × loops.

That's why an engineer checks loops before judging a node by its milliseconds. A node that takes only a few milliseconds can consume minutes if it runs thousands of times. The forty seconds in the morning report came from exactly this pattern; that report simply had more sensors.

QUERY: The stopwatch doesn't accuse—it testifies. A node that takes only a few milliseconds is innocent. Look closely at whoever made it run forty times.

The stopwatch told us HOW MUCH time was spent. The engineer's next question is WHERE it went: what exactly was the machine scanning during all those milliseconds? The answer is measured in pages rather than rows. In the next lesson, we'll open BUFFERS and measure the I/O.

Check yourself
A node in the timing output shows (actual … rows=1 loops=40). How many rows did this node return over the entire query?