Why WHERE LOWER(email) Ignores a Regular Index and How PostgreSQL Expression Indexes Fix ItNote · digest for November 23
Comparison diagram of PostgreSQL B-tree index traversal with a functional expression
Note3 sections≈ 5 min

Why WHERE LOWER(email) Ignores a Regular Index and How PostgreSQL Expression Indexes Fix It

Putting a standard index on an email column feels like solid insurance for fast logins. Then your code runs WHERE LOWER(email) = 'alex@example.com' so Alex@Example.com and alex@example.com match equally. The index is right there, so it should be instant—until you run it on a million-row table and the database does a full Seq Scan.

Why a standard B-tree goes blind on functions

A regular B-tree stores a sorted tree of raw column values. If the table holds 'Alex@Example.com', the tree records that exact string with capital letters. Picture a paper directory sorted strictly by letter case: ask it to find entries after lowercasing every letter, or by just the year from a date, and the alphabetical order breaks down. The engine has to scan the whole table row by row, running the function on each.

PostgreSQL expression indexes (Indexes on Expressions) solve this. Such an index stores the precomputed result of an expression inside the B-tree. At read time, the database skips recalculating the formula and walks the tree directly (Index Scan).

Single-function calls like LOWER(email) only need one pair of parentheses, while any expression with operators or EXTRACT(...) in CREATE INDEX must be wrapped in double parentheses ((...)), or the parser throws a syntax error.

-- 1. Case-insensitive email lookup (single parentheses)
CREATE INDEX idx_users_email_lower
  ON users (LOWER(email));

SELECT id, email FROM users
WHERE LOWER(email) = 'alex@example.com';

-- 2. Computed order total (double parentheses)
CREATE INDEX idx_orders_total
  ON orders ((price * quantity));

The planner matches the expression in WHERE literally: if the index is built on LOWER(email) and your query says LOWER(TRIM(email)), PostgreSQL ignores it and falls back to Seq Scan.

Common use cases and checking with EXPLAIN

Teams mostly reach for expression indexes in four cases: case-insensitive search (LOWER(email)), prefix lookups (SUBSTRING(code, 1, 4)), computed totals (price * quantity or (price * qty) - discount), and filters on date parts or JSON fields. For text, alternatives include citext or ICU collations, and for frequently read totals you can use GENERATED ALWAYS AS (...) STORED, though LOWER with an expression index remains the simplest option.

Dates have a subtle trick. If you need every January signup across multiple years (EXTRACT(MONTH FROM signup_date) = 1), an expression index is essential. For a single year like 2023, it is often unnecessary: rewrite the filter as a date range, and a plain B-tree on order_date works fine.

-- Year index on a date or timestamp column
CREATE INDEX idx_order_year
  ON orders ((EXTRACT(YEAR FROM order_date)));

-- Check the query plan: look for Index Scan / Bitmap Index Scan
EXPLAIN
SELECT * FROM orders
WHERE EXTRACT(YEAR FROM order_date) = 2023;

-- For a specific year, a range with a regular B-tree is simpler
SELECT * FROM orders
WHERE order_date >= '2023-01-01'
  AND order_date < '2024-01-01';

After creating the index, verify the plan with EXPLAIN: Seq Scan should become Index Scan or Bitmap Index Scan. EXPLAIN ANALYZE shows actual execution time, though use it carefully in production since it runs the query for real.

Limits: IMMUTABLE, timestamptz, and write overhead

PostgreSQL does not allow just any function in an index. VOLATILE functions can change output on every call (random(), now()), STABLE functions depend on locale or time zone (to_char), and only IMMUTABLE functions always return the exact same output for the same arguments. Indexes strictly require IMMUTABLE functions.

That rule often trips people up on timestamp with time zone (timestamptz). Indexing EXTRACT(YEAR FROM order_date) on such a column fails with ERROR: functions in index expression must be marked IMMUTABLE: 23:30 UTC on December 31 is already January 1 in Moscow, meaning the year depends on the session time zone (STABLE). Fix it by locking the time zone explicitly.

-- For timestamptz, pin UTC and build without blocking writes
CREATE INDEX CONCURRENTLY idx_order_year_utc
  ON orders ((EXTRACT(YEAR FROM order_date AT TIME ZONE 'UTC')));

-- Repeat the exact same expression in WHERE
SELECT * FROM orders
WHERE EXTRACT(YEAR FROM order_date AT TIME ZONE 'UTC') = 2023;

With AT TIME ZONE 'UTC', the expression becomes deterministic and the index builds cleanly. Never mark a time- or locale-dependent custom function as IMMUTABLE just to bypass this check: the index will silently drift out of sync with your data and miss rows.

CONCURRENTLY keeps CREATE INDEX from blocking writes on a live table. Remember that every expression index takes disk space and is recomputed on every INSERT and UPDATE, so it pays off on read-heavy tables while slowing down high-throughput write streams.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2