Sub topic 7
Writing Queries the Planner Can Match to the Predicate
Assume you know basic SELECT, WHERE, and CREATE INDEX, and that you can read the main parts of an EXPLAIN plan. This chapter focuses on partial indexes: indexes that contain only rows matching a fixed condition.
Define the partial index
A partial index has a predicate, which is the WHERE condition attached to the index:
CREATE INDEX orders_pending_created_idx
ON orders (created_at)
WHERE status = 'pending';This index contains created_at values only for pending orders. It is smaller than an index on every order, and changes to non-pending orders do not add entries to it.
The planner can use this index only when it can prove that the query returns rows covered by the predicate. In this example, the query must imply status = 'pending'.
EXPLAIN
SELECT id, created_at
FROM orders
WHERE status = 'pending'
AND created_at < now() - interval '1 hour';The query has the required status condition and adds a time condition. Therefore, every possible result belongs to the partial index.
Match the predicate logically
The planner recognizes common logical relationships, but it does not solve every possible equivalent expression. Keep the query condition close to the index predicate.
| Index predicate | Query condition that can match |
|---|---|
| `status = 'pending'` | `status = 'pending' AND created_at < now()` |
| `total_cents > 10000` | `total_cents > 20000` |
| `created_at >= DATE '2026-01-01'` | `created_at >= DATE '2026-02-01'` |
For inequalities, a stronger condition implies a weaker one. If the index contains rows where total_cents > 10000, a query for total_cents > 20000 can use it. The reverse is unsafe because rows from 10001 through 20000 might not be represented by another predicate.
CREATE INDEX large_orders_idx
ON orders (customer_id)
WHERE total_cents > 10000;
EXPLAIN
SELECT id, customer_id
FROM orders
WHERE total_cents > 20000
AND customer_id = 42;Equivalent-looking expressions are not always matched reliably. For example, write status = 'pending' in the query when that is the index predicate, rather than hiding it inside a more complicated expression.
Avoid unknown parameters
A prepared statement can prevent predicate matching when its parameter value is unknown during planning:
PREPARE pending_orders(text) AS
SELECT id, created_at
FROM orders
WHERE status = $1
AND created_at < now();
EXPLAIN EXECUTE pending_orders('pending');The planner may create a generic plan that must work for any value of $1. It cannot assume that $1 will always be 'pending', so it may not use the partial index. A literal condition is easier to prove:
EXPLAIN
SELECT id, created_at
FROM orders
WHERE status = 'pending'
AND created_at < now();Some PostgreSQL configurations eventually choose a custom plan when that is cheaper. Do not depend on that behavior without measuring the real workload.
Remember NULL behavior
SQL uses three-valued logic: a comparison can be true, false, or unknown. A row with status IS NULL does not satisfy status = 'pending', so it is not in the partial index. If your query needs null rows, its predicate must account for them, and it will not match the pending-only index.
Verify the actual plan
Use EXPLAIN to check the chosen access path. Look for an Index Scan or Index Only Scan using orders_pending_created_idx. If the planner chooses a sequential scan, that does not prove the predicate failed to match. The table might be small, or the query might return too many rows. Test with realistic data and EXPLAIN (ANALYZE, BUFFERS) when measuring execution.
Exercise
Why can a query with WHERE created_at < now() fail to use an index defined with WHERE status = 'pending'?
Show answer
The query does not prove that its result rows have status = 'pending', so the planner cannot safely use an index containing only pending rows.