Forty Seconds: Your First EXPLAIN ANALYZE

Where the Time Went: BUFFERS

12 min

Timing showed you how much each node along the route costs. But one mystery remains: the same query with the same plan can be fast on one run and noticeably slower on the next, as if the machine were in a different mood.

QUERY leads you farther into the engine room, where the hum grows steadier and the air colder. Racks of identical containers line the walls. The station's entire archive is divided into blocks like these: eight-kilobyte pages.

QUERY: Machines don't have moods. They have cache. A stopwatch measures the weather: a warm page takes milliseconds; a cold page means waiting for disk. Don't measure work in seconds. Measure it in pages.

Today you'll add a third to timing: I/O. You'll see how many pages the query moved, where they came from, and how many were fetched for nothing.

A conveyor of identical metal page-slabs runs through the compartment and disappears out of frame in both directions; a cadet holds a tiny tray with a few shavings at the exit; on the left, glowing slabs arrive from a warm brass storage rack, while on the right, frost-covered slabs hang from chains leading out of cold storage; a robot cat walks beside the conveyor, and a parrot sits on the tray.
Work is measured in pages: almost twelve megabytes processed to return only a few dozen kilobytes of results.

Pages: the currency of I/O

Postgres never reads “one row.” A table is stored in 8 KB pages, and a page is the smallest unit it can read: whether you need one row from it or all one hundred twenty, the whole container comes off the rack. The ls_readings log contains one hundred eighty thousand rows across 1513 pages, or about one hundred twenty rows per page.

Between disk and the executor is the buffer cache, an area of memory that holds recently accessed pages. That means the same page can come at two different costs:

  • shared hit — the page was found in cache. Fast: it's already in memory.
  • read — the page wasn't in cache, so it had to be fetched from disk. Slower, sometimes by orders of magnitude.

The BUFFERS flag adds these counters to every node in the plan:

EXPLAIN (ANALYZE, BUFFERS) <your_query>;

The plan becomes an I/O map: you can see both where a node spent time and how many pages it moved to do the work.

Open this map for a familiar route: a full scan of the log. The condition is deliberately selective. Readings with the warn status make up only two percent of the log. But there is no index on status, so the machine has only one route: pull every page off the racks.

Query work is measured in pages: shared hit — from cache, read — from disk, temp — spilled to a file
EXPLAIN (ANALYZE, BUFFERS)
SELECT reading_id, reading_ts, value
FROM ls_readings
WHERE status = 'warn';

Reading the manifest

Break down the three lines beneath Seq Scan. They are the manifest for this trip:

  • Filter: (status = 'warn') — the condition the executor checked against every row.
  • Rows Removed by Filter: 176400 — rows that were read and discarded. The query needs 3600 rows, but the entire log was read: ninety-eight out of every hundred rows pulled from storage went straight to waste. This is the “read for nothing” count, the main measure of how wasteful the route is.
  • Buffers: shared hit=1513 — all 1513 pages of the log. Multiply that by eight kilobytes: the query processed almost twelve megabytes to return only a few dozen kilobytes of results. This also explains the cost from two lessons ago: 3763 parrots for the scan comes from 1513 pages at the per-page cost, plus the CPU work of processing one hundred eighty thousand rows.

Notice that there is no read counter in the output at all. The sandbox has just seeded the log, so the whole thing is hot: every page was found in cache, and the full scan finished in just a few milliseconds. Production isn't always so forgiving. The first query in the morning may find a cold cache, turning those same 1513 pages from hit into read, and execution time can jump severalfold. Same plan, same work, different weather. That's why engineers compare routes by pages rather than stopwatch time.

One small detail: the Planning: block has its own Buffers. Those are pages the planner touched while consulting the system catalog. They are not part of the execution route.

A point lookup

Now look at the opposite extreme. The only index already present on the log is the primary-key index on reading_id. Ask for one specific row, and the navigator plots a route through the index staircase instead of scanning the racks: floor by floor, one page per step, until it reaches the single log page that contains the row itself.

The cell contains two commands, a pattern you'll see throughout this course. The first runs the query itself. While preparing it, the navigator also touches its own catalog pages and warms them in cache. The second captures the map for the same query. This warm-up doesn't change the node's own Buffers, but without it you'd also see a Planning: block below the map with around thirty system pages. You already know those pages don't belong to the route. A clean manifest with four pages and nothing extra makes the comparison clearer.

SELECT * FROM ls_readings WHERE reading_id = 137137;
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM ls_readings
WHERE reading_id = 137137;

Megabytes versus kilobytes

Put the two manifests side by side. Full scan: 1513 pages, almost twelve megabytes of archive processed. Point lookup: four pages, or thirty-two kilobytes. The two routes to the same table differ in I/O by almost four hundred times.

Yet both queries look “instant” to the eye: milliseconds for one and fractions of a millisecond for the other. A warm cache hides the difference, so the stopwatch tells you very little here. The I/O map does better: it shows how much work the machine performs regardless of cache state. A query that moves megabytes to return kilobytes creates a cost that will eventually show up on a cold morning, after the log has grown, or when neighboring queries are competing for resources.

QUERY: A bad route on a warm cache is like a leak in the hold during calm seas. Invisible. Right up until the first storm.

One last detail. In the first line of the full-scan map, the navigator estimated several thousand rows and came close: exactly 3600 arrived. For now, estimate and reality agree. In the next lesson, you'll see what happens to routes when they don't, and why this pair is the most important one on the entire plan.

Check yourself
The full-scan map contains the line Buffers: shared hit=1513. Does that mean the query read 1513 pages from disk?