skip to content

why isn't my index used?

a function on the column, a type mismatch, a leading wildcard, an OR, too many matches. a checklist.

address
everything.indexes.index_not_used
part
part 8, indexes, from the outside
chapter
chapter 9 of 12
reading time
9 min

when postgres ignores your index, it's one of two things. either the index can't answer the query as written (a function on the column, a type mismatch, a leading wildcard, an OR with an unindexed column), or it could but the planner thinks it's slower (too many matches, or statistics that are wrong about how many). EXPLAIN tells you which: the condition shows up as a Filter on a seq scan instead of an Index Cond.

the picture

you know how a phone book is useless for "everyone whose name ends in -son"? the book is sorted, but not by the thing you're asking about. most index mysteries are that: the question doesn't match the order the index is in. the rest are the planner deciding that the book isn't worth opening, because the answer is half the book.

here are the usual suspects, each as written and fixed, from one capture. every one of them is a real plan from the postgres on this page.

cause =
version =
SELECT id FROM customers WHERE lower(name) = 'ada lovelace'

no index. a function wrapped around the column: the index has name, not lower(name).

real output, postgres 18.6 (PGlite 0.5.12) · not-used
pick a cause, then flip between the query as written and the fix. the tell is the same every time: the condition moves from a Filter on a Seq Scan to an Index Cond. plain EXPLAIN, so only estimated rows.

the checklist

all of these run on shop with an index on customers(name), and on orders(customer_id) and orders(status):

1. a function on the column

the index holds name. it doesn't hold lower(name), so a condition on lower(name) can't search it:

sqldb=shop · session nu
continues session nu (1 earlier block run first)
EXPLAIN (COSTS OFF) SELECT id FROM customers WHERE lower(name) = 'ada lovelace';
output, from postgres 18.6
EXPLAIN
Seq Scan on customers Filter: (lower(name) = 'ada lovelace'::text)

the same goes for name || '', customer_id + 0, date(ordered_at), ordered_at::date. compare the bare column, or index the expression itself: CREATE INDEX ON customers (lower(name)) (expression indexes). for dates, rewrite the function as a range on the column: ordered_at >= '2026-01-01' AND ordered_at < '2026-01-02'.

2. a type mismatch

customer_id is an int. 42.0 is a numeric. postgres has no int = numeric operator in the index's operator class, so it casts the column up to numeric, and the condition is on (customer_id)::numeric, which nothing indexes:

sqldb=shop · session nu
continues session nu (2 earlier blocks run first)
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = 42.0;
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = '42';
output, from postgres 18.6
#1 · EXPLAIN
Seq Scan on orders Filter: ((customer_id)::numeric = 42.0)
#2 · EXPLAIN
Bitmap Heap Scan on orders Recheck Cond: (customer_id = 42) -> Bitmap Index Scan on orders_customer Index Cond: (customer_id = 42)

the quoted '42' works: an untyped literal takes the column's type. in application code this shows up as a driver that binds every number as numeric, or a bigint key compared with a text parameter. the plan shows the cast; types and casts has the rules for which way it goes.

3. a leading wildcard

a sorted index can find everything that starts with something: it's a range. LIKE 'Ada%' becomes name >= 'Ada' AND name < 'Adb'. but LIKE '%Lovelace' can start with anything, so the order is no help:

sqldb=shop · session nu
continues session nu (3 earlier blocks run first)
EXPLAIN (COSTS OFF) SELECT id FROM customers WHERE name LIKE '%Lovelace';
EXPLAIN (COSTS OFF) SELECT id FROM customers WHERE name LIKE 'Ada%';
output, from postgres 18.6
#1 · EXPLAIN
Seq Scan on customers Filter: (name ~~ '%Lovelace'::text)
#2 · EXPLAIN
Index Scan using customers_name on customers Index Cond: ((name >= 'Ada'::text) AND (name < 'Adb'::text)) Filter: (name ~~ 'Ada%'::text)

for "contains" searches, a trigram index does work (full-text and fuzzy search). ILIKE and case-insensitive regexes have the same problem as a function: index lower(name) and search lower(name) LIKE 'ada%'.

4. OR

an OR is fine when every side has an index: postgres builds a bitmap from each and ORs them together. it isn't when one side has no index, because that side alone needs the whole table:

sqldb=shop · session nu
continues session nu (4 earlier blocks run first)
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = 42 OR status = 'placed';
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = 42 OR shipped_at IS NULL;
output, from postgres 18.6
#1 · EXPLAIN
Bitmap Heap Scan on orders Recheck Cond: ((customer_id = 42) OR (status = 'placed'::text)) -> BitmapOr -> Bitmap Index Scan on orders_customer Index Cond: (customer_id = 42) -> Bitmap Index Scan on orders_status Index Cond: (status = 'placed'::text)
#2 · EXPLAIN
Seq Scan on orders Filter: ((customer_id = 42) OR (shipped_at IS NULL))

BitmapOr in the first, a seq scan in the second. the fix is an index for the other side, or a UNION of two queries that each use one. and an OR on the same column is the easy case: postgres 18 turns customer_id = 42 OR customer_id = 43 into customer_id = ANY ('{42,43}') and does a single index scan pg 18+:

sqldb=shop · session nu
continues session nu (5 earlier blocks run first)
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = 42 OR customer_id = 43;
output, from postgres 18.6
EXPLAIN
Bitmap Heap Scan on orders Recheck Cond: ((customer_id = 42) OR (customer_id = 43)) -> Bitmap Index Scan on orders_customer Index Cond: (customer_id = ANY ('{42,43}'::integer[]))

5. too many matches

the index works, and it isn't worth it:

sqldb=shop · session nu
continues session nu (6 earlier blocks run first)
SELECT status, count(*) FROM orders GROUP BY status ORDER BY count(*) DESC;
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE status = 'delivered';
output, from postgres 18.6
#1 · SELECT 6
statustextcountint8
delivered9352
cancelled317
shipped120
refunded98
paid62
placed51
(6 rows)
#2 · EXPLAIN
Seq Scan on orders Filter: (status = 'delivered'::text)

9,352 of 10,000 orders are delivered. fetching them one index entry at a time would cost far more than reading the 83 pages in order. this isn't a bug to fix. when postgres uses an index shows where the line falls and why. if what you really query is the rare values, a partial index on just those is smaller and better.

predict the output
WHERE status = 'paid': 62 rows of 10,000. what plan?
sql
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE status = 'paid';
sqldb=shop · session nu
continues session nu (7 earlier blocks run first)
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE status = 'paid';
SELECT most_common_vals, most_common_freqs FROM pg_stats
WHERE tablename = 'orders' AND attname = 'status';
output, from postgres 18.6
#1 · EXPLAIN
Index Scan using orders_status on orders Index Cond: (status = 'paid'::text)
#2 · SELECT 1
most_common_valsanyarraymost_common_freqsfloat4[]
{delivered,cancelled,shipped,refunded,paid,placed}{0.9352,0.0317,0.012,0.0098,0.0062,0.0051}
(1 row)

6. stale or missing statistics

the planner's row counts come from ANALYZE. if the table changed a lot since, it plans for the old table. here's a job queue: when it was analyzed, 20 of 10,000 jobs were done. then almost all of them finished:

sqldb=shop · session nu
continues session nu (8 earlier blocks run first)
CREATE TABLE jobs (id int PRIMARY KEY, state text NOT NULL);
INSERT INTO jobs SELECT g, CASE WHEN g <= 20 THEN 'done' ELSE 'new' END
FROM generate_series(1, 10000) AS g ORDER BY g;
CREATE INDEX jobs_state ON jobs (state);
ANALYZE jobs;
UPDATE jobs SET state = 'done' WHERE id <= 9980;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT id FROM jobs WHERE state = 'done';
ANALYZE jobs;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT id FROM jobs WHERE state = 'done';
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 10000
#3 · CREATE INDEX
#4 · ANALYZE
#5 · UPDATE 9980
#6 · EXPLAIN
Index Scan using jobs_state on jobs (actual rows=9980.00 loops=1) Index Cond: (state = 'done'::text) Index Searches: 1 Buffers: shared hit=66 Planning: Buffers: shared hit=3
#7 · ANALYZE
#8 · EXPLAIN
Seq Scan on jobs (actual rows=9980.00 loops=1) Filter: (state = 'done'::text) Rows Removed by Filter: 20 Buffers: shared hit=99 Planning: Buffers: shared hit=24
statistics =
SELECT id FROM jobs WHERE state = 'done'
pages read by the whole query (Buffers: shared hit + read)
66 pages
99 pages

stale statistics: they say 0.2% of jobs are done, so postgres expects a few dozen rows and walks the index. there are 9,980.

real output, postgres 18.6 (PGlite 0.5.12) · not-used
the same query before and after ANALYZE. with stale statistics postgres expects 44 rows and gets 9,980, so it walks the index for most of the table. after ANALYZE, a seq scan.

the reverse happens too: a value that used to be common and now is rare gets a seq scan it doesn't deserve. on a real server autovacuum runs ANALYZE for you after enough changes (autovacuum); a big load or update followed straight away by queries can beat it to the table, so run ANALYZE yourself after bulk changes. a table that was never analyzed is worse: the planner falls back on built-in default selectivities, which are guesses (estimating rows).

7. it was used, just not how you expected

sometimes the index is there in the plan and still looks unused: a Bitmap Index Scan instead of an Index Scan, or a different index than the one you made, or an Index Scan with a big Rows Removed by Filter because only the first column of a multicolumn index could narrow the search. read the Index Cond line: it says exactly which conditions the index searched on, and everything in Filter was checked row by row afterwards.

how to check, in order

  • ├──
  • │ ├── a function or a cast1, 2
  • │ ├── LIKE '%...'3
  • │ └── OR, one side unindexed4
  • ├── an Index Cond, not the one you hoped7
  • └──
  • ├── estimate far from actual6: ANALYZE
  • └── estimate right, many rows5: believe it
the checklist as one path through EXPLAIN. every branch is a line you can read off the plan.
  1. EXPLAIN (ANALYZE, BUFFERS) the real query, with the real parameter values.
  2. is the condition in an Index Cond? if it's in a Filter, the index can't search on it as written: look for a function, a cast, a wildcard or an OR (1 to 4).
  3. compare rows estimated with actual. off by 10x or more: statistics (6), or correlated columns (extended statistics).
  4. estimates right and still a seq scan: it really is cheaper (5). check the plan with SET enable_seqscan = off in a test session to see what the index plan would cost, and believe the numbers. steering the planner is about what you can actually change.

keylines

  • Filter instead of Index Cond: the index can't search the condition as written.
  • functions, casts, leading wildcards and OR with an unindexed side all do that.
  • too many matches: the index works, and a seq scan is cheaper. not a bug.
  • wrong estimates come from stale statistics. ANALYZE after big changes.
  • LIKE 'abc%' needs C collation or text_pattern_ops on the index.