Reading a Query Plan
Learn to read PostgreSQL query plans on a real million-row stand — EXPLAIN vs EXPLAIN ANALYZE, the scan types, and BUFFERS — so you understand what a query does instead of guessing.
Most people add an index by superstition. This course fixes that from the ground up: we stand up a real Postgres with a million orders and learn to read what the planner is actually doing. We start with the shape of a plan — cost, rows, width — then turn on EXPLAIN ANALYZE to compare the estimate with what really happened. We walk every access type the planner can choose — sequential scan, index scan, index-only scan, and the bitmap pair — each on a query built to trigger it. We finish with BUFFERS, so you can tell a genuinely cheap plan from one that only looked fast because the data was already cached. Reading the plan and running it are taught as separate moves, so each idea lands on its own.
What you'll build
- Read a query plan top-down and find the most expensive node
- Tell EXPLAIN (estimate) apart from EXPLAIN ANALYZE (measured)
- Recognise Seq Scan, Index Scan, Index Only Scan and the Bitmap pair
- Read cost, rows, width, actual time, actual rows and loops
- Use BUFFERS to separate a cheap plan from a warm cache
Contents
- A database to read plans on
- Wait for it to be ready
- One command to bring it up
- Start the server
- A clean slate when you need one
- A short way into the database
- The first table
- The table we will actually study
- A small lookup table
- Create the tables
- Seed the users, repeatably
- Seed the products
- Seed a million orders
- Load the data
- Fresh statistics, single-threaded plans
- Confirm what we are sitting on
- Ask for a plan, don't run the query
- Read the shape
- Now actually run it
- Compare the guess with the truth
- When there is no index, read everything
- A sequential scan
- Index a near-unique column
- A query the index can answer
- An index scan
- Index a column with repeats
- A handful of scattered rows
- The bitmap pair
- Carry a column inside the index
- Ask only for what the index holds
- An index-only scan
- Ask where the pages came from
- Cold: read from disk
- Warm: served from memory
- Your own plan-reading tool
- Reading a plan in sixty seconds