The idea · Lab 3.2.2A regular index has to include an entry for every row in the table, whether or not that row will ever be found through it. When a query pattern only ever cares about a small, identifiable slice of a table — active rows, pending orders, unprocessed events — indexing the other 99% is pure waste: it makes the index bigger, slower to maintain on every write, and slower to scan through even for the rows that do matter. A partial index fixes this directly, by attaching a WHERE clause to CREATE INDEX itself, so only rows matching that condition are ever included in the index structure at all.
A related but different problem: a query that needs to match 'user@example.com' case-insensitively against stored emails of any capitalization cannot use a plain index on the email column at all, since the index is sorted by the literal stored value, not by any transformation of it. An expression index solves this by indexing the output of a function — lower(email) — rather than the raw column, letting a query written the same way use the index directly.
A Partial Index, Measured Against a Full One
CREATE INDEX idx_partial_pending ON idx_orders2(status) WHERE status = 'pending';
SELECT pg_size_pretty(pg_relation_size('idx_partial_pending'));
-- 16 kB
CREATE INDEX idx_full_status ON idx_orders2(status);
SELECT pg_size_pretty(pg_relation_size('idx_full_status'));
-- 784 kB
With 1,000 pending rows out of 101,000 total, the partial index is roughly 49 times smaller than a full index on the same column — because it only ever contains entries for the 1,000 rows matching its WHERE clause, not all 101,000.
The Partial Index, Used and Correctly Bypassed
EXPLAIN SELECT * FROM idx_orders2 WHERE status = 'pending';
Index Scan using idx_partial_pending on idx_orders2 (cost=0.15..31.14 rows=933 width=18)
No Index Cond line at all — the WHERE clause on the query exactly matches the WHERE clause baked into the index, so PostgreSQL does not even need to recheck the condition; every row the index contains already qualifies.
EXPLAIN SELECT * FROM idx_orders2 WHERE status = 'completed';
Seq Scan on idx_orders2 (cost=0.00..1857.50 rows=100067 width=18)
Filter: (status = 'completed'::text)
Correctly falls back to a sequential scan — the partial index structurally does not contain any completed rows at all, so it cannot answer this query, and there is no full index present to fall back on either.
An Expression Index for Case-Insensitive Lookups
CREATE INDEX idx_lower_email ON idx_users(lower(email));
EXPLAIN SELECT * FROM idx_users WHERE lower(email) = 'user500@example.com';
-- Index Scan using idx_lower_email, Index Cond: (lower(email) = ...)
EXPLAIN SELECT * FROM idx_users WHERE email = 'User500@Example.com';
-- Seq Scan, Filter: (email = 'User500@Example.com'::text)
The index is built on the result of lower(email), not on email itself — so a query wrapping the column in lower() the same way finds a direct match, while a query comparing the raw column, even to an equivalent value, cannot use it at all.
Key concepts
Partial index
An index built with a WHERE clause in its own CREATE INDEX statement, restricting which rows are ever included in the index structure. Only queries whose own WHERE clause is provably implied by the index's WHERE clause can use it — a query for a status the partial index excludes falls back to whatever other index or scan method is available, exactly as if the excluded rows were never indexed at all, because they genuinely are not.
Expression index
An index built on the result of a function or expression applied to one or more columns, rather than on a raw column value directly — CREATE INDEX ON table(lower(col)) indexes the lowercased value of col, not col itself. A query can only use it when written to apply the identical expression, since the index has no entries for the raw, untransformed column value at all.
What to remember
- A partial index's WHERE clause restricts which rows are ever stored in it — dramatically smaller than a full index when only a small slice of the table is ever queried
- A query whose WHERE clause exactly matches a partial index's predicate needs no recheck at all, since every indexed row already qualifies
- A partial index correctly and automatically falls back to another plan for rows it structurally excludes — this is correct behavior, not a limitation to work around
- An expression index is built on a function's output, not the raw column — only a query applying the identical expression can use it
- Case-insensitive lookup performance depends on every query consistently wrapping the column the same way the index does, not just on the index existing