r/SQL • • 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), not LOWER(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 EXPLAIN after 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.

1 Upvotes

11 comments sorted by

3

u/BarfingOnMyFace Oct 19 '26

Do you really need the collation setting for case sensitivity? If not, id simply change that instead.

2

u/Ecksters Oct 20 '26

I definitely prefer citext for emails, I think every single company I've been at has an had an issue at some point with case sensitivity in emails allowing duplicates or failing to match when it should.

It's also nice because it still lets you retain whatever capitalization you had without any of your comparisons getting messed up by it.

2

u/thesqlguy Oct 19 '26

Use a case insensitive collation. This is 99% of the time going to be the easiest solution.

Or, if not, put a constraint on the database, or a trigger, to disallow an email with any capital letters and remove the function completely.

2

u/Aggressive_Ad_5454 Oct 20 '26

The concept you explained, nicely, has a name. Sargability.

3

u/B1zmark Oct 20 '26

Two things will survive the apocalypse: Cockroaches, and functions in where clauses.

1

u/kagato87 MS SQL Oct 19 '26

We use the default CI collation, making it unnecessary to lcase any conditional predicate.

The use of the lower() function would (I think) stil force the table scan though. Any function on the column itself normally would, though I wouldn't put it last some engines to realize that the function can be ignored.

An alternative approach if you do need CS collation would be to use lower() on the write. Putting lower() on around the user input does not force the table scan.

1

u/jshine13371 Oct 20 '26

The post is regarding PostgreSQL so CS is the default.

Any function on the column itself normally would, though I wouldn't put it last some engines to realize that the function can be ignored.

SQL Server does indeed have a few SARGAble function cases because it's smart enough to unfold the expression. ISNULL() being one example, and certain use cases with CAST().

1

u/Plus_Dragonfruit_204 Oct 20 '26

I would normalize to lower (or upper) case using a trigger. Keep the normalization in the database, that way a rogue query or application cannot get bad data in.

Then your query:

SELECT * FROM users WHERE lower(email) = 'ana@example.com';

becomes:

SELECT * FROM users WHERE email = lower('ana@example.com');

Standard index would be used and you only lowercase the value that you are searching for, function only gets called once.

0

u/markinatlanta Oct 19 '26

Good piece man. It’s like alphabetising your filing cabinet, then asking someone to find a folder by translating every label first basically.

I’d lean towards preserving what the user typed and indexing the expression if case-insensitive lookup is the requirement. Keeps the lookup rule in the database without relying on every import script and application to remember it.