Partial Index (Index with a WHERE Predicate)
Last updated: 2026-11-04
Partial Index (Index with a WHERE Predicate)
A partial index stores only the rows that satisfy its WHERE predicate, which makes it far smaller than a full index. It pays off when queries only ever touch a subset of the table — say unarchived orders or passing grades. The catch: the query predicate must imply the index predicate, otherwise the planner will not use it.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX <index_name> ON <table_name> ( <column> ) WHERE <index_predicate>
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column; entries are created only for rows satisfying the WHERE predicate. |
| index_condition | The predicate deciding which rows enter the index. Query predicates must imply it for the index to be chosen. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
INSERT 0 20000
CREATE INDEX
ANALYZE
-- EXPLAIN: the query predicate score >= 80 implies the index predicate, so the index is used
Aggregate (cost=84.33..84.34 rows=1 width=8)
-> Bitmap Heap Scan on idx_demo_partial (cost=4.60..84.23 rows=40 width=0)
Recheck Cond: ((customer_id = 7) AND (score >= 80))
Heap Blocks: exact=39
Buffers: shared hit=39 read=2
-> Bitmap Index Scan on idx_partial_high (cost=0.00..4.59 rows=40 width=0)
Index Cond: (customer_id = 7)
Index Searches: 1
Buffers: shared hit=2
Planning Time: 0.389 ms
Execution Time: 0.210 ms
-- Query result
count
-------
39
(1 row)
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)
📌 Key Points
- A very common use is soft deletes: CREATE INDEX ON t (created_at) WHERE deleted_at IS NULL.
- The query must imply the index predicate: with score >= 80 in the index, queries on score >= 80 or score > 100 qualify, while score IS NOT NULL does not.
- Partial indexes shrink the index and speed up writes, since no entries are maintained for rows that fail the predicate.
- Expressions inside the index predicate must be IMMUTABLE — now() and friends are not allowed.
- For prepared statements the planner must prove the implication for a generic plan; use EXPLAIN to confirm the index is chosen.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy does the planner ignore my partial index?
AUsually the query predicate does not imply the index predicate. The index holds only rows with score >= 80; a query without that restriction could match rows the index does not contain, so the planner gives up. Add the same condition to the query.
QHow does a unique partial index differ from a plain partial index?
AA unique partial index enforces uniqueness only on the rows it covers. The classic case is requiring unique e-mail addresses only for live rows: CREATE UNIQUE INDEX ON users (email) WHERE deleted_at IS NULL, which lets historical rows keep duplicate addresses.