The idea · Lab 3.1.1EXPLAIN alone shows what the planner intends to do and what it estimates that will cost — useful, but entirely theoretical. EXPLAIN ANALYZE actually runs the query and reports what really happened at every node: real row counts, real timing, and — with BUFFERS added — exactly how many blocks came from shared_buffers versus how many had to be read from disk.
That last distinction is the entire explanation behind this lab's opening scenario. The exact same query, run twice against this course's own beer_db data, tells two different stories: the first run pays for real disk reads because nothing is cached yet, and the second run — identical query, identical data — is served entirely from memory. This is "fast locally, slow in production" reproduced directly, not asserted: a warmed-up development database and a cold production one are not running different queries, they are paying a completely different price for the same one.
The Cost Model
Seq Scan on beers b (cost=0.00..31.50 rows=650 width=64)
cost=0.00..31.50 reads as startup cost..total cost, in arbitrary planner units, not milliseconds. 0.00 means this node can start producing rows immediately (a sequential scan needs no setup); 31.50 is the estimated total cost to read every row. rows=650 is the planner's estimate of how many rows this node will produce — worth remembering as an estimate, not a promise, before the next lab shows exactly how wrong it can be.
Adding ANALYZE: What Actually Happened
EXPLAIN ANALYZE SELECT * FROM beers WHERE brewery_id = '...';
Seq Scan on beers (cost=0.00..32.00 rows=8 width=64) (actual time=0.012..0.045 rows=8.00 loops=1)
actual time=0.012..0.045 is real milliseconds elapsed for this node specifically, and rows=8.00 is what genuinely came out — comparing this against the estimate a few characters to its left is the single most useful habit in this entire block.
Adding BUFFERS: Cache or Disk
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
Buffers: shared hit=103
-- or, on a cold cache:
Buffers: shared read=100
hit means the block was already sitting in shared_buffers; read means PostgreSQL genuinely had to fetch it from disk (or the OS page cache underneath, which BUFFERS cannot distinguish from a true physical read). A query dominated by read on its first run and by hit on every run after is not two different queries — it is the same query paying a real, one-time cost to warm the cache.
Key concepts
Cost vs Actual Time
Cost is the planner's own internal unit for comparing candidate plans to each other before any of them run — it is not a time prediction and should never be read as one. Actual time (only present with ANALYZE) is real, measured milliseconds for that specific node. A plan with a low cost can still have a high actual time if the planner's underlying assumptions — about row counts, cache state, or correlations — turn out to be wrong.
shared hit vs shared read
Both count 8KB buffer accesses during query execution. hit means the block was already resident in shared_buffers; read means PostgreSQL had to request it from the OS, which may itself still be served from the OS page cache or may hit physical disk — BUFFERS alone cannot tell those two apart, only that shared_buffers itself did not already have it.
What to remember
- EXPLAIN alone shows the planner's intent and estimates; EXPLAIN ANALYZE actually runs the query and reports real timing and row counts
- Cost is a planner-internal unit for comparing candidate plans against each other for the same query — never a time prediction, and never comparable across different queries
- BUFFERS distinguishes shared hit (already in shared_buffers) from shared read (fetched from the OS, possibly all the way from physical disk)
- The identical query against identical data can show a dramatically different execution time purely based on cache state — reproduced directly here as a real ~2x difference
- A genuine "fast locally, slow in production" gap is usually cache state plus something else — a bad estimate or a poor join strategy, both covered in this block's next two labs