Read a query plan
The order history endpoint slows down as the table grows. Find where PostgreSQL spends its work before changing the query.
You will learn to read a plan from its children upward, distinguish estimates from observations, and explain why a selective query can still inspect every row. You need basic filtering and sorting.
The page loads a fresh order dataset automatically. Select Run SQL in each numbered editor in order. The editors share one database, so later experiments use changes from earlier ones.
Sections on this page
The incident
A support agent opens the newest orders for tenant 42. The application needs twenty rows containing
an order ID, creation time, and total. The table has a primary-key index on id, but no index on
tenant_id or created_at.
In SQL 01, inspect indexname and indexdef. An index is a separate structure PostgreSQL can use
to locate rows. Declaring a primary key creates a unique index automatically.
Predict: Does an index on id help locate one tenant's newest orders? Write down what PostgreSQL
must do to find and order the rows before you run the query.
SQL 02 returns the endpoint's twenty orders. In SQL 03, find the steps PostgreSQL plans to use to produce them.
Ask for the plan
EXPLAIN prints the planner's chosen execution strategy without running the query. A plan is a tree:
each node consumes rows from a child, or obtains rows directly from storage.
Read the deepest indented node first. On the fresh fixture, look for a sequential scan, then a sort, then a limit. A sequential scan visits the table's pages and tests rows against the filter. The sort puts matching rows into creation-time order. The limit returns at most twenty.
A node's cost=startup..total values are planner units, not milliseconds. Startup cost estimates
work before the node can emit its first row; total cost estimates work if it runs to completion.
The parent cost includes its children's work. Do not add all node costs together.
rows estimates the rows emitted by a node, not necessarily the rows it inspects. width estimates
the average size in bytes of an output row at that node.
Measure the work
Predict: The limit returns twenty orders. Will the scan inspect twenty, all of this tenant's orders, or the whole table? Now let the executor run the plan.
ANALYZE in EXPLAIN executes the statement. actual rows and loops report observations.
For a node executed multiple times, actual rows is an average per loop; multiply by loops when
reasoning about total rows. TIMING OFF avoids per-node clock measurements while retaining row
counts and overall execution time.
Find the scan's actual rows and Rows Removed by Filter. On this fixture, their sum accounts for
the whole table when the scan runs once. The filter emits one tenant's orders and rejects the rest.
The limit does not let the scan stop early because the executor still needs to find the newest orders.
Look for Sort Method. PostgreSQL can use a top-N sort to retain a small set of candidates.
Keeping fewer candidates saves sort memory; the sort still consumes all qualifying input rows.
An implementation or version may choose another sort strategy, so use the actual output as evidence.
BUFFERS describes PostgreSQL buffer activity. A shared hit means the needed block was already in
PostgreSQL's shared buffer cache. A shared read means PostgreSQL loaded it into that cache. Neither
is a direct measurement of physical device I/O. Parent buffer figures include descendants; do not
sum them across the tree. Browser memory and host caching make server I/O comparisons especially weak.
Change the selectivity
Selectivity is the fraction of rows a predicate matches. Predict the plan if the predicate matches almost the entire table instead of one tenant. Compare the scan's emitted and rejected rows.
A similar plan can do different amounts of downstream work. Both scans inspect the table, but the broad predicate sends more rows to the sort. A plan's node names alone are not enough to diagnose it.
Select Run SQL again without editing this query. Compare Execution Time with the previous run. Cache warming and browser scheduling can change the time even when the query is identical. Use rows, filters, sort, and buffer activity to describe the work.
Explain your evidence
Before opening the explanation, answer these questions in your own words:
- Which node first obtains rows, and which node establishes the requested order?
- Why does returning twenty rows still require inspecting the table?
- Which values were predictions, and which came from execution?
- If the query returns no rows for tenant 101, does that make the scan cheap?
Hint: start at the leaves
A scan must prove a row matches or fails its filter. A sort cannot identify the newest matching rows until it has seen its input. A limit controls what its parent receives, not every child's work.
Worked explanation
The sequential scan obtains and filters rows. The sort establishes creation-time order, and the limit takes the first twenty. No existing index groups a tenant's orders in that order, so the scan cannot stop after finding twenty matches.
The cost, estimated rows, and width values come from planning. Actual rows, loops, removed rows,
and buffers describe execution. The estimate can differ from observation because statistics sample
the data. Tenant 101 still requires a full scan in this plan: PostgreSQL must check the table to
prove no rows match. A small output does not imply a small search.
Go deeper: how the engine works
How do logical plans, physical operators, and execution profiles fit together? Read The plan is the map (opens in a new tab) in Query Engine Book. Its DuckDB examples and teaching engine explain the general principle; PostgreSQL's operators and implementation details can differ. This reading is optional.
Transfer the idea
Return to the SQL editor under Measure the work. Record the scan type and
Rows Removed by Filter from the first run. Keep WHERE tenant_id = 42 and LIMIT 20. Replace
ORDER BY created_at DESC with ORDER BY id DESC. Predict whether the primary-key index can
supply this order and stop early. Select Run SQL and compare the same evidence. If the plan
uses the primary-key index, which work does it handle and which work does the tenant filter
still need to do?
What this cannot tell you
This fixture is deliberately small and uniform across tenants. It does not model concurrent users, production storage latency, or a workload larger than memory. A sequential scan is often the right choice when many rows match. The goal is to explain the work, not to remove every sequential scan.
Takeaway: A limit bounds returned rows. The chosen access path determines how much work is needed to find them. Design an index for this query →.
Primary sources: Using EXPLAIN and EXPLAIN reference.