SQLSTATE 42P17Severity mediumLab verified

Incident brief

Index expression must be immutable

An expression index may only use IMMUTABLE functions, because a changing result would silently corrupt the index; a non-immutable function in the expression raises this error.

Reproduced on PostgreSQL 18.4Verified 2026-07-24 (isolated lab, PostgreSQL 18.4)Reviewed by Verified against PostgreSQL 18.4 in an isolated lab environment

In 10 seconds

What
Index expression must be immutable
What triggers it
Create an index whose expression calls a STABLE or VOLATILE function (for example a timezone- or now()-dependent expression).
The fix
Index only IMMUTABLE expressions (or a plain column).
Proof
Reproduced on PostgreSQL 18.4 → A CREATE INDEX reproduces SQLSTATE 42P17; the non-immutable expression is rejected before the index is built.

The fix

What to do right now

The immediate, application-level response to this error.

  • Index only IMMUTABLE expressions (or a plain column).
  • If a function is genuinely immutable but not marked so, mark it (or a wrapper) IMMUTABLE — only when the output truly never changes for the same input.
Fix — SQL
-- Index an immutable expression (here, a plain column).
CREATE INDEX idx_ok ON ref_demo (label);

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 18.4
Last verified
2026-07-24 (isolated lab, PostgreSQL 18.4)
Reviewed by
Verified against PostgreSQL 18.4 in an isolated lab environment
Audit status
reviewed