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.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE INDEX <index_name> ON <table_name> ( <expression> )

⚙️ Parameters

index_nameName of the new index.
table_nameThe table to index.
expressionThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏