Understanding Cost, Rows, Width, Buffers, Cache Hits & Disk Reads
Decode cost, row estimates, width, and buffer hit ratios while tuning.
In plain words
Cost is the guess, rows is how many rows it expects, and buffers count how many pages it really touched.
Think of it like…
A delivery quote vs the actual trip: cost is the quoted effort, rows is how many parcels the planner expects, and buffers are the odometer reading, how many stops the van actually made, and whether it had to go back to the depot (disk) for stock.
Where you’ve seen it
A report takes 14 seconds at 9 a.m. and 2 seconds at 9:05. EXPLAIN (ANALYZE, BUFFERS) shows shared read=910,000 on the first run (about 7 GB from disk) and shared hit=910,000 on the second. The query isn't randomly fast; it's reading 7 GB either way. A composite index cuts it to shared hit=42, and both runs take under 5 ms.
Words you’ll hear
- cost=a..b
- Estimated startup and total cost, in units where one sequential page read = 1.0.
- rows / width
- Estimated output rows per loop / average bytes per row.
- shared hit / read
- Pages found in shared_buffers / fetched from the OS cache or disk.
- temp read / written
- Pages spilled to temporary files because work_mem was too small.
Visual diagram
Theory & example queries
Cost, rows and width
Costs are relative units built from settings: seq_page_cost = 1.0 for a sequential page read, random_page_cost = 4.0 for a random one, cpu_tuple_cost = 0.01 per row processed, plus smaller CPU costs per index entry and operator. Startup cost is the work before the first row comes out (a Sort must sort everything first); total cost is the work for all rows. The planner picks the plan with the lowest total, or lowest startup when a LIMIT means only a few rows are needed.
rows is the estimate that drives everything else: a node estimated at 120 rows but actually producing 96,000 likely chose a nested loop that should have been a hash join. Compare estimated and actual rows node by node, remembering that actual rows and time are per loop, so a node with loops=5,000 did 5,000 times the work shown. On SSD storage, many teams lower random_page_cost to around 1.1 so the planner stops overvaluing sequential scans.
Index Scan using order_items_order_idx on order_items (cost=0.43..8.6 rows=4 width=32) (actual time=0.004..0.006 rows=3 loops=50000) means 150,000 rows and about 300 ms in total, not 3 rows in 0.006 ms.
Buffers, cache hits and spills
EXPLAIN (ANALYZE, BUFFERS) reports pages per node (PostgreSQL 18 includes buffers in EXPLAIN ANALYZE by default). shared hit pages were already in PostgreSQL's cache; shared read pages came from the operating system, which may itself have had them cached or may have gone to disk; with track_io_timing on, I/O Timings shows how long those reads took. Multiply by 8 kB for volume: read=125,000 is about 1 GB.
temp read and written mean a sort or hash didn't fit in work_mem and spilled to disk, also shown as Sort Method: external merge Disk: 120MB. Raising work_mem for that query or reducing the rows being sorted fixes it. Database-wide, SELECT round(100.0 * sum(blks_hit) / nullif(sum(blks_hit) + sum(blks_read), 0), 2) FROM pg_stat_database; gives the cache hit ratio; OLTP systems usually sit above 99%, and a falling ratio means the working set no longer fits in memory.
Sort (actual time=2,410..2,980 rows=4,200,000 loops=1) Sort Key: created_at Sort Method: external merge Disk: 186,432kB Buffers: shared hit=310 read=61,200, temp read=23,300 written=23,304
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Run with ANALYZE and BUFFERS
Use EXPLAIN (ANALYZE, BUFFERS) on a realistic dataset (in a transaction you roll back if the query writes).
EXPLAIN (ANALYZE, BUFFERS)SELECT …When to use it & trade-offs
What it is
Each node in a PostgreSQL plan shows cost=0.43..8.45 rows=1 width=72: cost is the planner's estimate in arbitrary units (startup..total), rows the estimated rows it will output, and width the average row size in bytes. EXPLAIN ANALYZE adds actual time, actual rows and loops, and BUFFERS adds how many 8 kB pages were found in memory (shared hit) or fetched from the OS or disk (read), and how much spilled to temporary files.
Why it matters: Timings vary with load and caching; buffer counts show how much work a query really does. A query that touches 900,000 pages is slow even when it happens to be cached today. Reading cost, rows and buffers correctly tells you whether the planner misestimated, whether data came from disk, and whether a sort spilled, which decides the fix.
When to reach for it
- Explaining why a query is slow only sometimes
- Confirming an index reduced the work done
- Finding sorts and hashes that spill to disk
When to avoid or be careful
- Comparing plans by cost across different queries
- Judging a fix by one timing on a warm cache
- Reading per-loop numbers as totals
What each number tells you
Strengths: Available without running the query.
Watch out: Only as good as the statistics.
Strengths: Real measurements per node.
Watch out: Varies with cache state and load; must run the query.
Strengths: Stable across runs; shows cache misses and spills.
Watch out: Doesn't show CPU-heavy work like complex expressions.
Interview signals & numbers to know
When this topic comes up
- “The query is fast the second time”
- “What does cost=… mean in EXPLAIN?”
- “Why is this sort so slow?”
- Cache hit ratio
- work_mem
Benchmarks & rules of thumb
- 1.0 / 4.0
- default seq_page_cost / random_page_cost
- 8 kB
- per buffer
- > 99%
- typical OLTP cache hit ratio
- PostgreSQL 18
- BUFFERS on by default with ANALYZE
Common mistakes & follow-ups
Common interview mistakes
- Declaring a query fixed because the second run was fast, when it simply read the same pages from cache.
- Reading actual rows=3 on an inner node with loops=50,000 as "only 3 rows".
- Comparing the cost numbers of two different queries as if they were milliseconds.
Likely interviewer follow-ups
- What's the difference between shared hit and shared read?
- How do you tell that a sort spilled to disk?
- What do the two numbers in cost=0.43..8.45 mean?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Understanding Cost, Rows, Width, Buffers, Cache Hits & Disk Reads with hands-on SQL practice and interview Q&A.