Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

NodeWhat it doesYou’ve seen it
Seq Scanreads every page in order, tests every rowlesson 1, before the index
Index Scanwalks the index, jumps to each matching row’s page; rows come out in index orderPractical 1, Index Scan Backward
Bitmap Index Scan + Bitmap Heap Scancollects 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 Scananswers from the index alone, without reading the tablebelow

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:

statusrowsshare
delivered1,800,35290.0%
shipped120,1276.0%
pending59,6063.0%
cancelled19,9151.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 choseTimeForced alternativeTime
'cancelled' (1%)Bitmap scan57 msParallel Seq Scan218 ms
'shipped' (6%)Bitmap scan214 msParallel Seq Scan214 ms
'delivered' (90%)Seq Scan784 msBitmap scan1,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:

ConditionWhat happensFix
lower(status) = 'pending'Seq Scan, 815 ms (Practical 1)compare the column itself, or index lower(status)
customer_id = 4242.0Seq Scan, 259 msuse 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'Plan5 runs
default (random_page_cost = 4)Bitmap scan245–283 ms
SET random_page_cost = 1.1Index Scan433–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).
2. WHERE status = 'shipped' returns 6% of the rows, and the Bitmap Heap Scan shows Heap Blocks: exact=16657. Why isn't an index much help here? 16,657 is nearly every one of the 16,667 pages. The index finds the rows, but they're on almost every page, so the scan reads nearly the whole table anyway. It measured the same as a parallel Seq Scan (214 ms).
3. Your plan shows Index Only Scan with Heap Fetches: 48000. What does that tell you? For 48,000 rows the visibility map didn't mark the page all-visible, so Postgres read the table after all. The table has had many recent changes not yet vacuumed; the scan is an Index Scan in disguise.

Next: lesson 4 · Estimates and joins.