3 · How rows are found
In one sentence: Postgres finds rows by reading the whole table, by jumping through an index, or by a mix of the two, and it picks by estimating how much of the table you need; an index it can’t match to your condition is never used at all.
The problem
After Practical 1, orders has an index on (status, created_at). Yet this query ignores it and reads the whole
table:
SELECT * FROM orders WHERE status = 'delivered';
Seq Scan on orders (cost=0.00..41667.00 rows=1797533 width=37) (actual time=0.055..606.950 rows=1800352.00 loops=1)
Filter: (status = 'delivered'::text)
...
Execution Time: 784.485 ms
Is the planner wrong? And why did lower(status) make the index useless in Practical 1?
Four ways to find rows
| Node | What it does | You’ve seen it |
|---|---|---|
Seq Scan | reads every page in order, tests every row | lesson 1, before the index |
Index Scan | walks the index, jumps to each matching row’s page; rows come out in index order | Practical 1, Index Scan Backward |
Bitmap Index Scan + Bitmap Heap Scan | collects the pages from the index first, then reads them in page order, each once, testing the condition again (Recheck Cond) | lesson 1, after the index |
Index Only Scan | answers from the index alone, without reading the table | below |
Only the index scans (Index Scan, Index Only Scan) hand rows over in index order, so they can replace a Sort
and, with a LIMIT, stop early: Practical 1’s LIMIT 20 touched 10 pages. A Bitmap
scan returns rows in page order, but reads each page once, which pays off when many rows match.
How much of the table you need decides
status has four values, in very different amounts:
| status | rows | share |
|---|---|---|
| delivered | 1,800,352 | 90.0% |
| shipped | 120,127 | 6.0% |
| pending | 59,606 | 3.0% |
| cancelled | 19,915 | 1.0% |
For three of them, here is the plan the planner chose, and the same query with that plan forbidden
(SET enable_bitmapscan = off; SET enable_indexscan = off; or SET enable_seqscan = off;, test switches only):
WHERE status = | Planner chose | Time | Forced alternative | Time |
|---|---|---|---|---|
'cancelled' (1%) | Bitmap scan | 57 ms | Parallel Seq Scan | 218 ms |
'shipped' (6%) | Bitmap scan | 214 ms | Parallel Seq Scan | 214 ms |
'delivered' (90%) | Seq Scan | 784 ms | Bitmap scan | 1,099 ms |
The planner was right each time, or level. For 90% of the table, the forced Bitmap scan read 25,538 pages: all 16,667 of the table plus 8,871 of the index.
The surprising row is the first. 1% of the rows were spread over 70% of the pages:
Bitmap Heap Scan on orders (cost=508.58..18182.99 rows=18600 width=37) (actual time=10.966..54.187 rows=19915.00 loops=1)
Recheck Cond: (status = 'cancelled'::text)
Heap Blocks: exact=11673
19,915 rows on 11,673 of 16,667 pages: cancelled orders are scattered through the table. Pages are what costs,
so the planner estimates both how many rows match (rows=18600) and how many pages they’ll take.
When the index can’t be used at all
An index can only answer conditions on the column as it is stored:
| Condition | What happens | Fix |
|---|---|---|
lower(status) = 'pending' | Seq Scan, 815 ms (Practical 1) | compare the column itself, or index lower(status) |
customer_id = 4242.0 | Seq Scan, 259 ms | use the column’s type: customer_id = 4242 |
The second has no function in it. 4242.0 is a numeric, customer_id a bigint, so Postgres converts the
column to compare them:
Filter: ((customer_id)::numeric = 4242.0)
Look for :: on the column side of a Filter.
Settings that tilt the choice
The planner prices reading a page in order at seq_page_cost = 1 and jumping to a page at
random_page_cost = 4 (Planner cost constants).
Lowering it makes index scans look cheaper. Five alternating runs of each on the Raspberry Pi 4 (alternating, so
the cache differs from the single runs in the table above):
WHERE status = 'shipped' | Plan | 5 runs |
|---|---|---|
default (random_page_cost = 4) | Bitmap scan | 245–283 ms |
SET random_page_cost = 1.1 | Index Scan | 433–448 ms |
A cost setting is a guess about your hardware; change it
only when EXPLAIN ANALYZE on your own queries shows the new plans are faster.
Try it on your computer
Try the three status queries on your data, with and without the switches above
(RESET ALL; afterwards).
Go deeper: Index Only Scan and the visibility map
Counting the cancelled orders of 2026 needs only status and created_at, both in the index:
SELECT count(*) FROM orders WHERE status = 'cancelled' AND created_at >= '2026-01-01';
-> Index Only Scan using orders_status_created_at_idx on orders (cost=0.43..241.57 rows=6057 width=0) (actual time=0.155..2.272 rows=6526.00 loops=1)
Heap Fetches: 0
...
Execution Time: 3.476 ms
3.5 ms, 37 pages. The index doesn’t store whether a row is visible to your transaction. Postgres keeps a visibility map, one bit per table page meaning “every row here is visible to everyone”, and skips the table only for pages with the bit set (Index-Only Scans).
Heap Fetches: 0 means it never had to look. VACUUM, Postgres’s cleanup pass, sets those bits; autovacuum runs it
in the background (Routine Vacuuming); on a table with many recent changes,
Heap Fetches climbs and the advantage goes.
Go deeper: skip scan, an index used without its first column
Lesson 2’s revenue query filters on created_at alone. Since Practical 1, it uses the (status, created_at) index,
whose first column isn’t in the query at all:
-> Bitmap Index Scan on orders_status_created_at_idx (cost=0.00..1763.04 rows=59688 width=0) (actual time=8.549..8.550 rows=56987.00 loops=1)
Index Searches: 9
Index Searches: 9: instead of one search, the index was searched 9 times for the 4 status values, skipping
between values and looking up October within them
(Multicolumn Indexes). The query went from 584–683 ms
to 250 ms, with parallel workers off as in lesson 2. It works because status has only four values; with thousands,
an index starting with created_at would be the one to build.
Common misunderstandings
- “A Seq Scan means a missing index.” For 90% of the table it’s the fastest plan. Check how many rows the scan returns before adding an index.
- “An index on a column helps any condition on that column.” Only conditions on the column as stored: a function or a type conversion on the column side turns it off.
Check yourself
1. A plan shows Seq Scan on users with Filter: (lower(email) = 'a@b.c') and an index exists on users (email). Why isn't it used, and what are your two options?
The index stores email, the condition is on lower(email). Either compare email
directly (if emails are stored lowercase), or create an index on lower(email).