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.

What lands in your log

ERROR: functions in index expression must be marked IMMUTABLE

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

In 10 seconds

What triggers it
Create an index whose expression calls a STABLE or VOLATILE function (for example a timezone- or now()-dependent expression).
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.

Fix

What to do right now

Application-level steps for 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
CREATE INDEX idx_ref_demo_lower
ON ref_demo ((lower(label)));
SELECT indexdef FROM pg_indexes WHERE indexname = 'idx_ref_demo_lower';

Runbook to fix this

The runbook for this incident

Full step-by-step fixes for the condition behind this error: the diagnosis, the exact SQL, and output captured in the lab.

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops