r/SQL • u/ClickOk5811 • Oct 19 '26
PostgreSQL Case file: an index on email exists, the query on lower(email) still does a sequential scan
A small case file, written as a hypothetical so nobody mistakes it for a war story.
Symptom
Looking up a user by email is slow on a large users table, even though email has an index.
SELECT * FROM users WHERE lower(email) = 'ana@example.com';Suspects, and why I’d rule them out first
- Missing index: it exists (
CREATE INDEX users_email_idx ON users (email)). - Stale statistics: worth an
ANALYZE, but the plan below doesn’t suggest this is the main issue. - Table too small to bother with an index: not here, the table is large.
Evidence
EXPLAIN SELECT * FROM users WHERE lower(email) = 'ana@example.com';
-- Seq Scan on users
-- Filter: (lower((email)::text) = 'ana@example.com'::text)The planner is evaluating lower(email) for every row. An index on email stores the raw values, so it can’t answer a question about lower(email).
Verdict
The query applies a function to the indexed column, so the plain index can’t be used.
Fix
Index the expression itself:
CREATE INDEX users_email_lower_idx ON users (lower(email));Now the same query can use that index. If you also want case-insensitive uniqueness, you can make it unique:
CREATE UNIQUE INDEX users_email_lower_uniq ON users (lower(email));Another option is the citext extension, which makes a column compare case-insensitively without wrapping it in a function, at the cost of an extension and a different column type.
Caveats
- the query has to use exactly the same expression as the index (
lower(email), notLOWER(TRIM(email))) - on small tables, or when most rows match, the planner may still choose a sequential scan, and that can be the right choice
- check with
EXPLAINafter creating the index, because what you get depends on your data and statistics - every extra index costs something on writes
Open question
For emails specifically, do you normalize to lowercase on the way in and keep a plain index, or leave the data as typed and index the expression? I can see arguments for both, mostly about where you’d rather put the rule.

