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.
no index. a function wrapped around the column: the index has name, not lower(name).
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:
EXPLAIN (COSTS OFF) SELECT id FROM customers WHERE lower(name) = 'ada lovelace';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:
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = 42.0;
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE 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:
EXPLAIN (COSTS OFF) SELECT id FROM customers WHERE name LIKE '%Lovelace';
EXPLAIN (COSTS OFF) SELECT id FROM customers WHERE name LIKE 'Ada%';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:
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;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+:
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE customer_id = 42 OR customer_id = 43;5. too many matches
the index works, and it isn't worth it:
SELECT status, count(*) FROM orders GROUP BY status ORDER BY count(*) DESC;
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE status = 'delivered';| statustext | countint8 |
|---|---|
| delivered | 9352 |
| cancelled | 317 |
| shipped | 120 |
| refunded | 98 |
| paid | 62 |
| placed | 51 |
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.
EXPLAIN (COSTS OFF) SELECT id FROM orders WHERE status = 'paid';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';| most_common_valsanyarray | most_common_freqsfloat4[] |
|---|---|
| {delivered,cancelled,shipped,refunded,paid,placed} | {0.9352,0.0317,0.012,0.0098,0.0062,0.0051} |
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:
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';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.
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
EXPLAIN (ANALYZE, BUFFERS)the real query, with the real parameter values.- is the condition in an
Index Cond? if it's in aFilter, the index can't search on it as written: look for a function, a cast, a wildcard or an OR (1 to 4). - compare
rowsestimated with actual. off by 10x or more: statistics (6), or correlated columns (extended statistics). - estimates right and still a seq scan: it really is cheaper (5). check the plan with
SET enable_seqscan = offin 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.