Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Postgres: Reading EXPLAIN in Depth

Tested 2026-11-25 on Debian GNU/Linux 13 (trixie), Raspberry Pi 4 Model B (4 GB, 4 cores, aarch64); Docker Engine 29.4.0, no VM: containers share the 4 CPUs and 4 GB · with PostgreSQL 19.0 (Docker image postgres:19.0), Docker 29.4.0 · by Vivek Sahu (@viv1) · CC BY 4.0

Your shop’s My orders page asks Postgres for one customer’s orders: 7 rows out of 2,000,000.

It takes 148 ms on a Raspberry Pi 4, because Postgres goes through all 16,667 pages of the table to find them.

The SQL looks fine. The reason is in the plan, and EXPLAIN shows it to you.

One index later, the same query touches 13 pages and takes 0.414 ms.

Reading the plan is how you find out why a query is slow. This course teaches you to read one, on a database you set up and break yourself, with every plan and number in it taken from real runs.

You should know how to write a SELECT with WHERE, JOIN, GROUP BY and ORDER BY, and how to run a command in a terminal. You don’t need to know Postgres: lesson 1 explains the few pieces reading a plan depends on (pages, indexes, the planner).

By the end you’ll be able to:

  • take a slow query from your app’s logs, run EXPLAIN ANALYZE on it, and say which step costs the time and why;
  • tell whether an index would help, which one, and prove it with before/after numbers;
  • recognise when the planner guessed the row count wrong, and fix it;
  • find the slowest queries of a running database before anyone reports them.

How it works

Each lesson starts from an animated, step-by-step picture (press Play, or step with the arrow keys), then explains in plain words, with optional Go deeper sections for the internals. Each practical has you do it yourself on your own Postgres: requirements, a hint, a hidden solution, a “predict first” question and a part where you break it on purpose. One script, ./lab, does the rest:

  • ./lab setup pN gets what a practical needs that isn’t its topic: the Postgres container, the data, the files it hands out. It says one line per step (every command and its output go to lab.log), and keeps what you already have.
  • ./lab check pN tells you whether your work is right. It only reads.
  • ./lab reset pN starts you at a practical with the earlier ones’ solutions in place, after a Break it gone too far or a month away. It asks first, removes only the container and volume that ./lab setup made (it labels them), and never touches your files.

All you need is Docker on macOS or Linux. Postgres 19 runs in a container that ./lab setup p0 starts.

Path

LessonPracticalYou’ll be able to
1 · Why a query is slow0 · Your Postgressee a plan, and why the same query takes 148 ms or 0.414 ms
2 · Reading a plan1 · Where the time goesfind the node that costs the time; fix it with an index
3 · How rows are foundtell when an index is used, and when it rightly isn’t
4 · Estimates and joins2 · When the planner guesses wrongspot a bad row estimate and the plan it causes; fix statistics and memory
5 · In production3 · Capstone: slow-query huntfind and fix the slowest queries of a running app
What your folder will look like
 ~/pg-explain/
 ├── lab              the lab script: setup, check, reset (from Practical 0)
 ├── report.md        your capstone report (Practical 3)
 └── sql/             mounted in the container at /sql
     ├── shop.sql     creates and fills customers and orders
     ├── cities.sql   adds a city to every customer (Practical 2)
     └── app/         the app's three queries (Practical 3)