Expression Index (Index on an Expression)
Last updated: 2026-11-04
Expression Index (Index on an Expression)
An expression index stores the result of an expression over a column rather than the column value itself. The classic use is case-insensitive lookup: a plain index on name makes WHERE name = 'alice' case-sensitive, while an index on lower(name) lets WHERE lower(name) = 'alice' hit the index. The expression must be IMMUTABLE, otherwise the index cannot be created.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX <index_name> ON <table_name> ( <expression> )
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| expression | The indexed expression, e.g. lower(name), upper(email) or date_trunc('day', created_at). It must use IMMUTABLE functions, and function calls carry their own parentheses. |
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 Index Cond uses the expression lower(name) directly
Index Scan using idx_expr_lower_name on idx_demo_expr (cost=0.29..8.30 rows=1 width=25)
Index Cond: (lower(name) = 'user_42'::text)
Index Searches: 1
Buffers: shared hit=1 read=2
Planning Time: 0.251 ms
Execution Time: 0.177 ms
-- Query result
id | email
----+--------------------
42 | user42@example.com
(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
- The query expression must match the indexed expression exactly — WHERE name = 'user_42' will not use an index on lower(name).
- Expressions must be IMMUTABLE: lower(), upper() and date_trunc() qualify, while now() and random() do not.
- You can index several expressions, or mix expressions with plain columns: CREATE INDEX ON t (lower(name), status).
- Since PostgreSQL 11 an expression index can carry INCLUDE columns for partial covering scans, at the cost of a larger index.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy doesn't WHERE lower(name) = 'x' use my index on name?
AA plain B-tree index stores the raw column values; lower(name) is a different value and simply is not in the index. Either query the raw value (case-sensitively) or build the index on lower(name).
QI get "functions in index expression must be marked IMMUTABLE" — what now?
AThe expression contains a function whose result can change. An indexed value must always equal the expression evaluated at insert time, or the index would be wrong. Switch to an immutable function such as lower() or upper(), or write your own IMMUTABLE wrapper.