Sub topic 9
Expression Indexes
Assume you know SELECT, WHERE, CREATE INDEX, and the basics of reading EXPLAIN. This chapter uses the users and orders tables from the running schema.
An expression index stores the result of a calculation instead of storing only a table column. It is useful when queries repeatedly filter or sort by a computed value.
For example, email addresses are often compared without regard to letter case. This query computes lower(email) for every row:
SELECT id
FROM users
WHERE lower(email) = '[email protected]';Without an expression index, PostgreSQL may need to scan many rows and calculate lower(email) repeatedly. Create an index on the same expression:
CREATE INDEX users_lower_email_idx
ON users (lower(email));
EXPLAIN
SELECT id
FROM users
WHERE lower(email) = '[email protected]';The plan can use an index scan or bitmap index scan on users_lower_email_idx. The index contains the lowercased values, so PostgreSQL can search those values directly.
The query expression must match
The planner looks for an index whose stored expression can support the query expression. Write the query using the same expression as the index definition.
| Index expression | Matching query expression |
|---|---|
| `lower(email)` | `WHERE lower(email) = '[email protected]'` |
| `lower(email)` | `WHERE email = '[email protected]'` |
The second query does not ask for a lowercased comparison. A normal equality test may therefore need a different index, such as an index on email.
Do not assume that moving the calculation to the other side produces the same plan. Check the actual query with EXPLAIN. Also remember that an expression index does not change the stored table value. It only adds an indexed representation of that value.
Immutable expressions
PostgreSQL must be able to trust that an indexed expression always returns the same result for the same row values. Functions used in an index expression must be immutable. Immutable means the result cannot change because of the current time, session settings, database contents, or another changing state.
lower(text) is immutable, so this index is valid:
CREATE INDEX users_lower_email_idx
ON users (lower(email));A cast from timestamptz to date depends on the session time zone. PostgreSQL therefore rejects this expression index:
-- Rejected because the result depends on the session time zone.
-- CREATE INDEX orders_created_date_bad
-- ON orders ((created_at::date));Fix the time zone inside the expression so the calculation has a stable meaning:
CREATE INDEX orders_created_date_utc_idx
ON orders (((created_at AT TIME ZONE 'UTC')::date));
EXPLAIN
SELECT count(*)
FROM orders
WHERE (created_at AT TIME ZONE 'UTC')::date = DATE '2026-01-15';The query must use the same fixed-zone expression. If a query uses a different time zone, this index does not represent that query's calculation.
Choosing an expression index
Expression indexes help when a computed value appears often in filters, joins, or ordering. They also have costs:
- PostgreSQL calculates the expression whenever an indexed row is inserted or updated.
- The index consumes storage.
- Changes to the expression may require dropping and rebuilding the index.
- A low-selectivity expression, such as a boolean result, may not improve a query.
Use EXPLAIN with representative data. Do not judge the index only by whether it exists.
Exercise
What index supports WHERE lower(email) = '[email protected]', and why will an index on email alone not provide the same case-insensitive lookup?
Show answer
Create CREATE INDEX users_lower_email_idx ON users (lower(email));. The expression index stores lowercased values, while an index on email stores the original values and does not directly represent the case-insensitive comparison.