Index vs full read: clustering over selectivity
The rule "use the index below some percent" is only half right. How closely the rows found through the index sit together in the table (clustering) decides it. PostgreSQL has a bitmap scan in between.
SQL
Situation
A query looks at the count and amount of canceled orders this month. Following an old review that said "canceled orders are as much as 3%, so a full read beats the index", index scans were turned off in the session that runs this query (enable_indexscan = off and so on). But the query is now called thousands of times a day and has risen to the top of the load list.
Tables
| Table | Column | Description |
|---|---|---|
orders | status | DONE 90%, PAID 7%, CANCELED 3% (mixed evenly regardless of date) |
orders | ord_dt | Order date and time. Data is stored in date order |
orders | amt | Order amount |
| Index | Columns |
|---|---|
orders_x2 | status, ord_dt, ord_id |
orders has 2 million rows, about 18,750 blocks.
Query
BEFORE · Session with indexes off
SET LOCAL enable_indexscan = off;
SET LOCAL enable_bitmapscan = off;
SET LOCAL enable_indexonlyscan = off;
SELECT count(*), sum(o.amt)
FROM orders o
WHERE o.status = 'CANCELED'
AND o.ord_dt >= DATE '2025-09-01'
Run it and check how many blocks it read relative to the result rows (A-Rows).
Approach
1. Compute selectivity from "all the conditions"
status = 'CANCELED' alone is 3%, but with the date condition added, this month's canceled orders are about 1,600 rows, 0.08%.
2. Judge the cost of index access by "how many table blocks are read"
If the rows found through the index sit in a few blocks, it is cheap; if they are scattered across the whole table, it is as expensive as a full read. PostgreSQL estimates this with the correlation statistic (correlation between value order and storage order).
3. Consider the order in which the data was stored
Orders are stored in date order. "This month" rows sit together at the end of the table, while "all canceled orders" are scattered evenly over three years.
Explanation
Full read → index (default settings)
AFTER · Default settings (index)
SELECT count(*), sum(o.amt)
FROM orders o
WHERE o.status = 'CANCELED'
AND o.ord_dt >= DATE '2025-09-01'
Why it got faster
- Before reads the whole table with
Seq Scan(Buffers about 18,750) to keep 1,559 rows. - In After,
Index Scanreads only the section oforders_x2from('CANCELED', 2025-09-01)on, and only the blocks at the end of the table where those rows sit. Buffers about 490.
Why this is optimal
- It reads only the blocks where the needed rows sit. If
amtwere in the index too, the table would not be read at all, but that is a structural change.
Variation: when the 3% is scattered
TRY · Forced index: all canceled orders (3%)
SET LOCAL enable_seqscan = off;
SET LOCAL enable_bitmapscan = off;
SELECT count(*), sum(o.amt)
FROM orders o
WHERE o.status = 'CANCELED'
TRY · Default settings: all canceled orders (3%)
SELECT count(*), sum(o.amt)
FROM orders o
WHERE o.status = 'CANCELED'
- The 59,493 canceled orders are scattered evenly over three years of blocks, so reading through the index gives Buffers about 18,400, nearly the same as a full read (about 18,750). This is because nearly every block holds at least one such row.
- With default settings the planner picks
Bitmap Heap Scan. It sorts the locations of rows found through the index into block order and reads one block at a time in sequence, so it never reads the same block twice and reads the disk in order (Buffers about 18,400). For reading scattered rows it beats a plain index scan.
Variation: the index wins even at 24%
TRY · Forced index: completed orders in 2025 (24%)
SET LOCAL enable_seqscan = off;
SET LOCAL enable_bitmapscan = off;
SELECT count(*), sum(o.amt)
FROM orders o
WHERE o.status = 'DONE'
AND o.ord_dt >= DATE '2025-01-01'
TRY · Full read: completed orders in 2025 (24%)
SET LOCAL enable_indexscan = off;
SET LOCAL enable_bitmapscan = off;
SET LOCAL enable_indexonlyscan = off;
SELECT count(*), sum(o.amt)
FROM orders o
WHERE o.status = 'DONE'
AND o.ord_dt >= DATE '2025-01-01'
- At 484,261 rows (24% of the total), the index side's Buffers is about 7,400, less than a full read (about 18,750). This is because 2025 rows sit together toward the end of the table. However, when reading from disk, a full read that reads in order can be faster, so check execution time too.
Key points
- "Use the index below some percent" is only a starting point. The real cost is the number of table blocks that hold the rows found through the index.
- PostgreSQL has a bitmap scan between an index scan and a full read. It gathers scattered rows and reads them in block order.
- DB difference: Oracle forces with the
INDEX/FULLhints and judges clustering byCLUSTERING_FACTOR(Oracle scenario 14). PostgreSQL judges it bypg_stats.correlationand forces withenable_*settings.