Sub topic 6
Partial indexes: indexing only the rows you query
Assume you know SELECT, WHERE, ORDER BY, and basic PostgreSQL indexes. This chapter focuses on how a partial index stores only rows that satisfy a condition, and how the planner decides whether a query can use it.
What a partial index stores
A partial index has a WHERE clause. PostgreSQL adds only rows that satisfy that clause to the index.
For example, an orders table may contain many completed orders but only a small number of pending orders. If your application often searches pending orders, index that subset:
CREATE INDEX orders_pending_created_idx
ON orders (created_at)
WHERE status = 'pending';This index contains the created_at value only for pending orders. It is smaller than an index on every row, so PostgreSQL has fewer index entries to search and less index data to maintain.
The table still contains every order. The WHERE clause limits the index, not the table.
Matching a query to the predicate
The index predicate is the condition after WHERE in CREATE INDEX. A query can use the index when its conditions guarantee that every row it needs satisfies that predicate.
EXPLAIN
SELECT id, created_at
FROM orders
WHERE status = 'pending'
AND created_at < now() - interval '1 hour';The query explicitly includes status = 'pending', so PostgreSQL can use orders_pending_created_idx to find matching timestamps.
The query below does not provide that guarantee:
EXPLAIN
SELECT id, created_at
FROM orders
WHERE created_at < now() - interval '1 hour';Old rows may have any status. Searching the pending-only index would miss valid rows, so PostgreSQL cannot use that index as the source for the complete result.
The planner performs logical implication, but it does not evaluate every possible relationship between expressions. Keep the query condition close to the index predicate.
| Index predicate | Query condition | Can use the partial index? |
|---|---|---|
| `status = 'pending'` | `status = 'pending' AND created_at < ...` | Usually yes |
| `status = 'pending'` | `created_at < ...` | No guarantee |
| `status = 'pending'` | `status IN ('pending', 'shipped')` | Usually no |
Conditions that can change
A row can enter or leave a partial index when an update changes the columns used by its predicate. For example, changing an order from pending to shipped removes it from the partial index. Changing it back adds it again.
The index therefore reduces read and storage costs only when the indexed subset is useful and reasonably selective. A predicate matching almost every row gives little benefit over a regular index.
Choosing a partial index
A partial index is a good candidate when:
- Queries repeatedly filter on a known condition.
- The condition selects a small or frequently accessed subset.
- The indexed columns support an additional lookup or sort.
- The subset changes less often than the whole table.
For example, this index supports searches for unprocessed jobs:
CREATE INDEX jobs_ready_idx
ON jobs (priority DESC, created_at)
WHERE processed_at IS NULL;A matching query should retain the predicate:
EXPLAIN
SELECT id, priority
FROM jobs
WHERE processed_at IS NULL
ORDER BY priority DESC, created_at
LIMIT 50;Exercise
Exercise
Why can a query with WHERE created_at < now() - interval '1 day' fail to use an index defined with WHERE status = 'pending'?
Show answer
The query does not guarantee that its rows have status = 'pending', so the pending-only index cannot provide every row the query might need.