Partial and Expression Indexes in PostgreSQL
Use PostgreSQL partial indexes (WHERE clauses) and expression indexes to index only what matters: soft deletes, queues, case-insensitive search and JSON fiel...
Published November 27, 2026
Two of PostgreSQL's most underused features are partial indexes, which index only rows matching a condition, and expression indexes, which index the result of a function rather than a raw column. Together they produce indexes that are smaller, faster, and enforce rules that ordinary unique constraints cannot express.
The concept
A partial index has a WHERE clause: CREATE INDEX ... WHERE status = 'pending'. Only matching rows are stored, so if 1% of a 50-million-row table is pending, the index is about 1% of the size. It stays hot in memory and is cheap to maintain because updates to non-matching rows do not touch it. The planner uses it when the query's WHERE clause implies the index predicate.
An expression index stores the result of an expression, for example lower(email) or (data->>'customer_id'). Queries must use the same expression to benefit. Combining the two gives powerful constraints, such as "email must be unique among non-deleted users", which is impossible with a plain unique constraint once soft deletes exist.
Smaller indexes
Patterns that work well
| Use case | Index |
|---|---|
| Job queue: find pending work fast | CREATE INDEX ON jobs (run_at) WHERE status = 'pending' |
| Soft deletes: unique among live rows | CREATE UNIQUE INDEX ON users (email) WHERE deleted_at IS NULL |
| Case-insensitive lookup | CREATE INDEX ON users (lower(email)) |
| JSON field lookup | CREATE INDEX ON orders ((data->>'customer_id')) |
| Only one active row per group | CREATE UNIQUE INDEX ON subscriptions (user_id) WHERE active |
| Sparse nullable column | CREATE INDEX ON users (referral_code) WHERE referral_code IS NOT NULL |
When the planner uses a partial index
Matching rules to remember
The planner proves implication with simple logic, so write predicates plainly. WHERE status = 'pending' matches an index on WHERE status = 'pending'. A parameterised WHERE status = $1 does not, because the planner cannot know $1 at plan time with a generic plan. For expression indexes, the query must contain the exact same expression, including casts.
Practical examples
Efficient job queue polling
Combine a partial index with SKIP LOCKED for concurrent workers.
CREATE INDEX idx_jobs_pending ON jobs (run_at) WHERE status = 'pending';
UPDATE jobs SET status = 'running', started_at = now()
WHERE id = (
SELECT id FROM jobs
WHERE status = 'pending' AND run_at <= now()
ORDER BY run_at
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING id, payload;Unique email with soft deletes
Deleted users no longer block re-registration with the same email.
CREATE UNIQUE INDEX uq_users_email_live
ON users (lower(email)) WHERE deleted_at IS NULL;Index a JSONB field with the right type
Cast in the index and in the query identically.
CREATE INDEX idx_orders_customer ON orders (((data->>'customer_id')::bigint));
SELECT * FROM orders WHERE (data->>'customer_id')::bigint = 42;Common mistakes
- Using a parameter for the column in the partial predicate (status = $1), which prevents the planner from matching.
- Writing the expression slightly differently in the query than in the index.
- Partial indexes on conditions that match most rows, gaining nothing.
- Forgetting that MySQL has no partial indexes; use functional indexes or generated columns there.
Key takeaways
- Partial indexes store only rows matching a WHERE clause: smaller and cheaper.
- Expression indexes index computed values; queries must use the same expression.
- Partial unique indexes enforce conditional uniqueness such as soft-delete-aware emails.
- Write simple, literal predicates so the planner can match them.
FAQ
Does MySQL support partial indexes?
No. MySQL 8 supports functional (expression) indexes and indexes on generated columns, which can emulate some partial-index patterns.
Can a partial index be used for queries without the WHERE condition?
No. If the query might need rows outside the predicate, the planner cannot use the partial index.
Do expression indexes slow down writes?
Slightly, because the expression is computed on every insert and update of the involved columns. Keep expressions immutable and cheap.