PostgreSQL EXPLAIN ANALYZE: How to Read It
Seq scan, index scan, bitmap scan, nested loop, hash and merge joins, estimated vs actual rows, and buffer hits vs reads in EXPLAIN (ANALYZE, BUFFERS).
In this chapter
Reading PostgreSQL EXPLAIN (ANALYZE, BUFFERS)
More on PostgreSQL
Keep learning
Overview
Nested Loop Outer side small, inner side an index lookup. loops= on the inner node in the thousands while the outer estimate said a handful. Hash Join Equality join, no useful index, one side fits in memory. Batches above 1 on the Hash node: it spilled to disk. Raise work_mem for that session. Merge Join Both inputs already sorted on the join key (indexes), or large and sortable. An explicit Sort under it with external merge: the sort costs more than the join. Planner units: startup..total. Compare plans, not machines. rows=200 vs actual rows=10000 Estimate 50× off. ANALYZE the table, raise the statistics target, or CREATE STATISTICS. Multiply. A cheap node × many loops is a common culprit. Rows Removed by Filter Rows read and thrown away. An index or a better one on the filtered column. Heap Fetches: 90000 Index-only scan visiting the heap. Sort Method: external merge Disk Sort spilled to temp files. More work_mem for this query, or an index that returns rows sorted. Batches: 8 (Hash) Hash table spilled. More work_mem, or a smaller build side. shared read=20000 Pages not in shared_buffers. Lossy heap blocks Bitmap exceeded work_mem and keeps pages, not rows. Seq Scan on orders (cost=0.00..41520.00 rows=98200 width=16) (actual time=0.02..118.6 rows=101240 loops=1) startup cost before 1st row total cost all rows estimated rows 1st row ms last row ms actual rows per loop EXPLAIN: the estimate ANALYZE: what happened Compare estimated rows with actual rows × loops on every node. width is the average row size in bytes. Seq Scan every page, in order in order Index Scan index, then heap per row Index Only Scan Bitmap Index + Heap collect pages, read in order Four scan types. Seq Scan reads every heap page in order. Index Scan walks the index and jumps to the heap for each row. Index Only Scan reads only the index and the visibility map, visiting the heap rarely. Bitmap scan collects matching pages from the index, then reads them in physical order. index → bitmap no index visibility map The planner moves from Index Scan to Bitmap to Seq Scan as the share of matching rows grows. chapters · postgresql Reading PostgreSQL EXPLAIN (ANALYZE, BUFFERS) A plan is a tree of nodes. Read it from the deepest node out, and compare what the planner expected with what happened.
2. Read the tree inside out
Each -> is a child that feeds rows to the node above it. Children run before parents, and a node with loops=N reports time and rows per loop. Planning Time and Execution Time at the bottom are for the whole statement.
3. Scan nodes and join nodes
Index Scan pays one random heap visit per row. Bitmap Heap Scan sorts the visits by page first, which wins from roughly a few percent of the table. Index Only Scan reports Heap Fetches: rows it still had to check in the heap. The three join methods Chosen when Warning sign The algorithms themselves are drawn in join methods.
4. When the estimate is wrong, the plan is wrong
Estimates come from pg_statistic, filled by ANALYZE (autovacuum runs it after enough changes). Each column gets a most-common-values list and a histogram, 100 buckets by default. Per-column statistics assume columns are independent, so city AND zip multiplies two selectivities. Extended statistics record that one column predicts the other. They only take effect after the next ANALYZE.
Recap
Read the tree from the deepest node outward. Multiply per-loop numbers by loops. Seq, Index, Index Only and Bitmap scans trade random heap visits against reading more pages. Nested Loop for few outer rows, Hash Join for big unsorted equality joins, Merge Join for sorted inputs. A large gap between estimated and actual rows is the usual cause of a bad plan: ANALYZE, raise statistics, or CREATE STATISTICS. external merge, Batches above 1 and Disk Usage mean work_mem was too small for that node.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.