Reading EXPLAIN in PostgreSQL: the same query at 56,198 pages and at 23
The same query read 56,198 pages with no index, 62 pages with the obvious index and 23 pages with the right one. All three plans returned the same 20 rows. The difference is visible in the output of EXPLAIN ANALYZE, if you know which lines to read.
To show those lines we built a test table: 5 million orders, 439 MB, 100,000 customers, on PostgreSQL 18.6 with default settings on an Apple M4 laptop. Every number below comes from that test, measured in November 2026. It is not a client system, and you can repeat it on your own machine.
Read the plan from the inside out
The query is a common one: the latest 20 orders of one customer.
SELECT * FROM orders
WHERE customer_id = 4242
ORDER BY created_at DESC
LIMIT 20;
With an index on customer_id, EXPLAIN ANALYZE prints this plan (trimmed):
Limit (actual time=0.021..0.022 rows=20 loops=1)
Buffers: shared hit=62
-> Sort (actual time=0.021..0.021 rows=20 loops=1)
Sort Key: created_at DESC
Sort Method: top-N heapsort Memory: 29kB
-> Bitmap Heap Scan on orders (cost=4.83..204.85 rows=51)
(actual time=0.007..0.017 rows=59 loops=1)
Heap Blocks: exact=59
-> Bitmap Index Scan on orders_customer (rows=51) (actual rows=59)
Index Cond: (customer_id = 4242)
A plan is a tree, and the most indented node runs first. Here the index finds 59 entries, the heap scan fetches those 59 rows from the table, the sort orders them, and the limit keeps 20.
Four things on each node are worth your attention:
- cost is the planner's estimate in its own units, not milliseconds. It is only useful for comparing plans with each other.
- rows appears twice: the estimate (51) and the actual count (59). When the two differ by a factor of ten or more, the planner chose the plan from wrong information.
- loops says how many times the node ran. Actual time and rows are averages per loop, so multiply them.
- Buffers counts the 8 kB pages the node touched:
hitcame from PostgreSQL's cache,readcame from outside it.
According to the PostgreSQL 18 release announcement (September 2025), EXPLAIN ANALYZE now prints buffer counts automatically. On older versions write EXPLAIN (ANALYZE, BUFFERS). And remember that ANALYZE really executes the statement: wrap an UPDATE or DELETE in BEGIN and ROLLBACK.
One query, three plans
We ran the query three times, adding an index each time.
| Index | What the plan does | Pages |
|---|---|---|
| None | Parallel Seq Scan in 3 processes, then Sort | 56,198 |
| (customer_id) | Bitmap scan finds 59 rows, then Sort | 62 |
| (customer_id, created_at) | Index Scan, stops after 20 rows, no Sort | 23 |
Without an index PostgreSQL started three processes, and each one read a third of the table and threw away 1,666,647 rows to keep about 20. The line Rows Removed by Filter is the clearest sign of a missing index: work was done on rows nobody asked for. 43 ms looks harmless on an idle laptop. On a server it is three busy cores for every call of that query.
The single-column index fixes the search but not the order. PostgreSQL still fetches all 59 orders of the customer and sorts them. The composite index stores each customer's entries already ordered by date, so the scan reads the first 20 and stops. The Sort node disappears from the plan. With 59 orders the gain is small. For a customer with 50,000 orders it is the difference between reading 20 rows and reading 50,000.
The rule for column order: columns compared with = go first, the column you sort or range over goes last.
When the planner ignores your index
An index that exists is not an index that is used. We measured three cases on the same table.
- WHERE lower(email) = ...
- Index on email is skipped: Seq Scan, 56,122 pages, about 150 ms
- Estimated 25,000 rows, actual 56
- Fix: an index on lower(email). 59 pages
- WHERE status = 'shipped'
- 95% of rows match
- Seq Scan although an index on status exists
- No fix needed: the planner is right
- WHERE status = 'pending'
- 1% of rows match, the index is used
- 50,179 rows cost 33,337 pages
- Fix: a partial index. 52 pages for the oldest 50
The first case is the most common one in code review. An index on email stores the values of email, not of lower(email), so the planner cannot use it. The same happens with created_at::date and with a comparison between different types. The plan also shows a second problem: with no statistics for the expression the planner guessed 25,000 rows where 56 exist. An expression index solves both, because PostgreSQL collects statistics for it.
The second case is not a bug. Reading 95% of a table through an index means jumping to almost every page in random order, which is slower than reading the table from start to end. An index on a column with a few values helps only for the rare ones.
The third case is the rare value. The index on status works, but the 50,179 pending orders are spread over the whole table, so nearly every row costs its own page. If the real question is "the oldest 50 pending orders", a partial index answers it:
CREATE INDEX orders_pending ON orders (created_at)
WHERE status = 'pending';
It contains only the pending rows, already sorted. In our test it takes 1.1 MB, against 33 MB for the full index on status.
One more classic cause: a query that filters only on the second column of a composite index. PostgreSQL 18 added skip scan, which according to its release notes lets multicolumn B-tree indexes be used in more of these cases. It helps when the leading column has few distinct values. We would not design a schema around it.
Every index is paid for on write
After the experiments the table had seven indexes, the primary key included. Together they take 397 MB, almost as much as the 439 MB of data. And every INSERT has to update each of them.
The same load took about four times longer. That is why "add an index" is not a free answer to every slow query. Before adding one, check pg_stat_user_indexes: an index whose idx_scan is still zero after weeks in production costs writes and disk and speeds up nothing.
What to take from it
- Run EXPLAIN ANALYZE before you add an index. Look for Seq Scan with a large Rows Removed by Filter, and for a Sort node under a Limit.
- Compare page counts, not only milliseconds. Time depends on cache and load. Buffers show how much work the query really does.
- Compare estimated and actual rows. A gap of ten times or more means stale or missing statistics. Run ANALYZE on the table or index the expression.
- Order composite index columns by use. Equality columns first, then the sort or range column.
- Keep functions off indexed columns, or index the expression itself.
- Delete indexes nobody reads. Each one slows every write.
An index is a guess about your queries. EXPLAIN ANALYZE is how you check the guess: fewer pages, no surprise Sort, estimates close to reality.
Query and index reviews like this one are part of what our dedicated development team and DevOps does for products that have started to slow down as their data grows.
Building something similar?
Tell us what you are building and leave with a plan, or see what we do and how we work.
