I added the perfect index. Postgres still sorted, and still read the whole table.
I had a simple query: get one user’s completed lessons, newest first.
SELECT lesson_id, completed_at
FROM lesson_progress
WHERE user_id = 142 AND completed_at IS NOT NULL
ORDER BY completed_at DESC;
The table has 100,000 rows. User 142 has 1,000 of them. So I added what looked like the perfect index, one that matches both the filter and the sort order:
CREATE INDEX ON lesson_progress (user_id, completed_at DESC);
The query got about 5× faster. I was ready to call it done. Then I read the plan properly.
What the plan actually said
| Scenario | Plan | Sort? | Buffers read | Median time |
|---|---|---|---|---|
| No index | Seq Scan | yes | 544 | 2.52 ms |
| With the index | Bitmap Heap Scan | yes | 544 | 0.46 ms |
Two things were wrong with my story:
- There’s still a sort. The index is already sorted by
completed_at DESC, so why sort again? - It read 544 buffers, the same as the sequential scan. The “fast” plan touched as many pages as reading the whole table.
So the 5× speedup wasn’t from reading less data. Postgres read the same pages; it just checked fewer rows on each one.
Why: where the rows live on disk
My seed data assigns users round-robin: row 1 goes to user 101, row 2 to user 102, and so on. Every user’s rows are spread evenly through the table. User 142’s 1,000 rows sit on 541 different pages, which is basically every page in the table (Heap Blocks: exact=541).
Postgres had two ways to use the index:
- Index Scan: walk the index in order and fetch each row’s page as you go. The output is already sorted, but it jumps around the table, one random page read per row.
- Bitmap Heap Scan: collect every matching row’s location from the index first, then read the pages in physical order. Fewer random jumps, but the index’s order is lost, so it has to sort afterwards.
The planner picked the bitmap scan. That’s where the sort comes from.
The planner was wrong, and why
First, a thing I didn’t expect: having an index doesn’t mean Postgres will use it. The planner is cost-based. It estimates every option (sequential scan, bitmap scan, index scan) and picks the one it thinks is cheapest. When I turned off bitmap scans only, Postgres didn’t fall back to my index. It went straight to a sequential scan of the whole table (2.5 ms), because it estimated that as cheaper than walking the index.
Then I forced the index scan to compare:
| Scenario | Plan | Sort? | Median time |
|---|---|---|---|
| Planner’s choice | Bitmap Heap Scan | yes | 0.46 ms |
Forced index scan (enable_bitmapscan = off, enable_seqscan = off) |
Index Scan | no | 0.36 ms |
The plan the planner rejected was faster. It estimated the index scan’s cost at about 2,206, against about 640 for the bitmap plan. Why so pessimistic?
Postgres’s default random_page_cost = 4 assumes random reads are 4× more expensive than sequential ones. That made sense for spinning disks. My data was entirely in memory. When I told the planner random reads are cheap:
SET random_page_cost = 1.1;
it picked the index scan on its own: no sort, 0.37 ms.
Lesson: the planner doesn’t know your hardware unless you tell it.
Fix the layout, not just the index
The real problem was data scattered across every page. CLUSTER rewrites the table in index order:
CLUSTER lesson_progress USING idx_lesson_progress_user_completed;
| Scenario | Heap pages | Buffers | Median time |
|---|---|---|---|
| Index, scattered rows | 541 | 544 | 0.46 ms |
| Index, clustered rows | 6 | 9 | 0.15 ms |
Same index, same query, 60× fewer pages read. User 142’s rows now sit together.
One catch: CLUSTER is a one-time rewrite. New rows go wherever there’s space, so the layout decays over time, and it locks the table while it runs.
Skip the table entirely
The query only needs lesson_id and completed_at. If the index holds both, Postgres doesn’t have to visit the table at all:
CREATE INDEX ON lesson_progress (user_id, completed_at DESC) INCLUDE (lesson_id);
VACUUM ANALYZE lesson_progress;
| Scenario | Plan | Buffers | Median time |
|---|---|---|---|
| Covering index + VACUUM | Index Only Scan, Heap Fetches: 0 |
1 | 0.09 ms |
| Covering index, no VACUUM | Bitmap Heap Scan | 544 | 0.55 ms |
With VACUUM: 1 buffer, no sort, 26× faster than no index.
Without VACUUM, the exact same index gave no benefit at all. An index doesn’t store whether a row version is visible to your transaction; that’s in the table (MVCC). Postgres can skip the table only for pages marked “all visible” in the visibility map, and VACUUM is what sets those marks. On a freshly written table, none are set, so the planner doesn’t even try an index-only scan.
Everything in one table
| # | Scenario | Plan | Sort? | Buffers | Median (n=20) |
|---|---|---|---|---|---|
| 1 | No index | Seq Scan | yes | 544 | 2.52 ms |
| 2 | Composite index | Bitmap Heap Scan | yes | 544 | 0.46 ms |
| 2b | + random_page_cost = 1.1 |
Index Scan | no | 548 | 0.37 ms |
| 3b | Forced index scan | Index Scan | no | 548 | 0.36 ms |
| 4 | + CLUSTER |
Bitmap Heap Scan | yes | 9 | 0.15 ms |
| 5 | Covering index + VACUUM | Index Only Scan | no | 1 | 0.09 ms |
| 6 | Covering index, no VACUUM | Bitmap Heap Scan | yes | 544 | 0.55 ms |
What I took away
- An index is an option, not a guarantee. The planner can still choose a sequential or bitmap scan if it estimates that’s cheaper. Always confirm with
EXPLAIN ANALYZE. - Read
BUFFERS, not just time. Time said “5× faster”. Buffers said “you still read the whole table”. - An index can only help as much as the data layout allows. Scattered rows mean scattered page reads, whatever the index looks like.
- The planner’s cost settings assume old hardware.
random_page_costmatters on SSDs and in-memory data. - Index-only scans depend on VACUUM. The visibility map is the link between MVCC and indexes.
- My first benchmark was wrong. My original “before” query didn’t have the
completed_at IS NOT NULLfilter, so I was comparing two different queries. Same query, repeated runs, median: that’s the minimum.
Caveats
- Small table (100k rows, ~4 MB), fully in memory. On disk with a cold cache, random reads cost far more and the gaps would be bigger.
- Times are medians of 20 runs after 3 warm-up runs, on an Apple Silicon Mac, PostgreSQL 17.12 in Docker.
- Sub-millisecond differences matter at high volume, not for a single request.
Code and raw plans: github.com/MinitJain/postgres-performance-lab (experiments/run.sh).