How to Use Partial Indexes for Hot Queries
Shrink and speed indexes with a WHERE clause: index only the rows a query needs, keep the index small, and verify the planner uses it.
On this page
Introduction
A full index covers every row, including rows the hot query never looks at. If a table holds millions of historical rows and the query only ever asks for the last week, most of the index is dead weight: it takes space, slows writes, and gives the planner a bigger structure to traverse. A partial index solves this by indexing only the rows a query needs, via a WHERE clause on the index itself.
This tutorial shows how to find a query suited to a partial index, how to define one, how to confirm the planner uses it, and how the technique pairs with soft deletes and status columns. The examples use PostgreSQL, which has long supported partial indexes and uses them automatically when the query’s predicate implies the index’s condition.
Find the right query and predicate
A partial index pays off when a hot query always filters on the same value and that value selects a small fraction of the rows. The classic cases are an active flag, a status such as pending, a recent time window, or a not-null column where most rows are null. If the query sometimes omits the filter, the index will not be used for those variants, so choose a predicate that appears in the query that actually needs the speed.
Measure the fraction before you build. If the predicate matches ninety percent of the rows, the partial index is nearly a full index and the saving is small; a full index may be simpler. The technique shines when the predicate matches a few percent, so the index is a fraction of the size and the writes it burdens are only for rows in the predicate.
-- a hot query: recent pending orders for one tenant
SELECT id, total FROM orders
WHERE tenant_id = $1
AND status = 'pending'
AND created_at >= now() - interval '1 day';
-- a partial index that only covers pending rows
CREATE INDEX CONCURRENTLY idx_orders_pending_recent
ON orders (tenant_id, created_at DESC)
WHERE status = 'pending';
Define the partial index
The index’s WHERE clause is evaluated as rows are inserted and updated, so only rows satisfying it are indexed. Choose the columns and order to match the query’s filter and sort. In the example, tenant_id first supports the equality filter and created_at DESC supports the recent window and the descending order, letting the index serve both the filter and the order. Keep the predicate immutable, because a volatile or non-deterministic expression is rejected.
Build the index CONCURRENTLY in production so writers are not blocked. A concurrent build takes longer and can fail, leaving an invalid index, so verify afterward that the index is valid and recreate it if not. Include the partial index in the migration that introduces the query pattern so the schema and the code stay in step, rather than adding it ad hoc during an incident.
-- verify the index is valid after a concurrent build
SELECT indexrelid::regclass AS index_name, indisvalid
FROM pg_index
WHERE indexrelid = 'idx_orders_pending_recent'::regclass;
Confirm the planner uses it
A partial index is used only when the planner can prove the query’s predicate implies the index’s condition. Run EXPLAIN on the exact query and look for the index name in the plan. If the plan shows a sequential scan or a different index, the predicate may not be implied, the statistics may be stale, or the planner judged the partial index not selective enough compared to another option.
Make the implication explicit when you can. If the index is built WHERE status = ‘pending’, the query must actually constrain status to that value; a query that filters status IN (‘pending’,‘processing’) does not imply the index’s condition and cannot use it. Align the query and the index so the planner has the proof it needs, and re-run ANALYZE after large data changes so the estimates the planner uses stay current.
Index Scan using idx_orders_pending_recent on orders
Index Cond: ((tenant_id = $1) AND (created_at >= ...))
Filter: (status = 'pending')
-- the index is chosen because the query's status filter implies the index predicate
Pair with soft deletes and status columns
A soft-delete flag is a natural predicate for a partial index. Index only the rows where deleted_at IS NULL, so the index excludes deleted rows and stays proportional to the active data, while the deleted rows accumulate outside it. The hot queries that filter deleted_at IS NULL can use the index; the rare queries that want deleted rows scan or use a different index.
The same idea applies to status columns. Index only the rows in the states that are hot, for example queued or running, and leave the terminal states such as done and failed out of the index. This keeps the index small even as historical rows pile up, and it makes the index a working set rather than an archive.
-- soft-delete aware partial index
CREATE INDEX idx_orders_live
ON orders (tenant_id, created_at DESC)
WHERE deleted_at IS NULL;
-- work-queue style index covering only live states
CREATE INDEX idx_jobs_runnable
ON jobs (priority DESC, created_at)
WHERE status IN ('queued', 'retrying');
Test that the index is used
Add a plan test that runs EXPLAIN on the hot query and asserts the partial index appears. Add a correctness test that inserts rows outside the predicate, such as a deleted or terminal row, and asserts the query does not return them, which proves the predicate and the query agree. Finally, compare the index size against a full index on the same columns to confirm the saving is real.
SELECT pg_size_pretty(pg_relation_size('idx_orders_pending_recent')) AS partial_size,
pg_size_pretty(pg_relation_size('idx_orders_full')) AS full_size;
Common pitfalls
Building a partial index whose predicate the query does not imply, choosing a predicate that matches most rows so the index is nearly full, using a volatile expression that the index rejects, forgetting CONCURRENTLY in production and blocking writes, and omitting the index from migrations so it drifts are the recurring mistakes. Aligning predicate and query, measuring selectivity, and testing the plan address them.
Conclusion
A partial index is a working set defined by a predicate. Find a hot query with a constant, selective filter, create an index with a matching WHERE clause built concurrently, confirm the planner uses it, and keep it in migrations. Self-check by asserting the index appears in EXPLAIN and that rows outside the predicate are excluded. When the index covers only what queries read, it is small, fast to scan, and cheap to maintain.
Sources: PostgreSQL: Partial indexes, PostgreSQL: CREATE INDEX, PostgreSQL: Index-only scans.
FAQ
- When will the planner use a partial index?
- Only when the query's WHERE clause provably implies the index's predicate. If the query filters status = 'active' and the index is built WHERE status = 'active', it can be used; if the query omits that filter, the index cannot cover all rows and is ignored.
- Does a partial index help deletes?
- Not directly for the delete itself, but for the follow-up query. A common pattern indexes only active rows and lets a soft-delete flag move a row out of the index, so the active-row queries scan a small index while the flagged rows are excluded.
Report a problem with this articleOpens a pre-filled issue on GitHub.