Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

PostgreSQL rejects an expression index because a function in it is not immutable

Your error

ERROR: functions in index expression must be marked IMMUTABLE1 more line it can show

Fix it fast

Most common cause, 1 of 3

A timestamptz value is turned into a date or truncated in the session's time zone

Name the zone inside the expression.

Fix
CREATE INDEX orders_created_day_idx
    ON orders (((created_at AT TIME ZONE 'UTC')::date));

-- or keep a timestamptz result
CREATE INDEX orders_created_trunc_idx
    ON orders ((date_trunc('day', created_at, 'UTC')));

-- the query has to use the same expression
SELECT count(*) FROM orders
WHERE (created_at AT TIME ZONE 'UTC')::date = DATE '2026-11-20';
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ERROR: functions in index expression must be marked IMMUTABLE means something in the CREATE INDEX expression or WHERE clause can return a different result for the same input, usually by reading TimeZone or locale settings. Casting timestamptz to date, concat() and to_char() are typical. Give the zone explicitly, use ||, or label your own function correctly. PostgreSQL 15 to 19.

  • Applies to PostgreSQL 15 to 19 (19.0, 18.7, 17.12, 16.16, 15.20)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Look up the volatility of every function the index expression calls. In pg_proc, provolatile is i for immutable, s for stable and v for volatile, and only i is allowed in an index. Put the function names from your CREATE INDEX into the list.

Run this first
SELECT p.oid::regprocedure AS function, p.provolatile
FROM pg_proc p
WHERE p.proname IN ('date', 'date_trunc', 'to_char', 'concat')
ORDER BY p.proname;

Then find the line you see in this list and open its fix:

  1. 1A timestamptz value is turned into a date or truncated in the session's time zoneThe expression is created_at::date, date(created_at) or date_trunc('day', created_at) on a timestamp with time zone column
  2. 2Your own function was created without a volatility labelThe expression calls a function you wrote, and its provolatile is v
  3. 3A built-in text function in the expression is STABLE: concat(), concat_ws() or to_char()The expression builds a string, for example concat(first_name, ' ', last_name) or to_char(created_at, 'YYYY-MM')

What does the error look like?

  • ERROR: functions in index expression must be marked IMMUTABLE
  • ERROR: functions in index predicate must be marked IMMUTABLE

Cause 1 of 3

A timestamptz value is turned into a date or truncated in the session's time zone

You seeThe expression is created_at::date, date(created_at) or date_trunc('day', created_at) on a timestamp with time zone column

Check

This IMMUTABLE error on a timestamptz column comes from the conversion depending on the TimeZone setting: the same instant falls on different calendar days in different zones, so date(timestamptz) and the two-argument date_trunc on timestamptz are STABLE. The same calls on a plain timestamp column are immutable, which is why the index works on one table and fails on another.

Check
SELECT p.oid::regprocedure, p.provolatile
FROM pg_proc p
WHERE p.oid IN ('date(timestamptz)'::regprocedure,
                'date(timestamp)'::regprocedure,
                'date_trunc(text,timestamptz)'::regprocedure,
                'date_trunc(text,timestamptz,text)'::regprocedure);

Fix

Name the zone inside the expression. Converting with AT TIME ZONE gives a timestamp without time zone, whose cast to date is immutable; date_trunc with a third time zone argument is immutable too. A query can use the index when its WHERE clause contains that same expression, as in the lower(col1) example of the PostgreSQL index documentation.

Fix
CREATE INDEX orders_created_day_idx
    ON orders (((created_at AT TIME ZONE 'UTC')::date));

-- or keep a timestamptz result
CREATE INDEX orders_created_trunc_idx
    ON orders ((date_trunc('day', created_at, 'UTC')));

-- the query has to use the same expression
SELECT count(*) FROM orders
WHERE (created_at AT TIME ZONE 'UTC')::date = DATE '2026-11-20';

Verify

The index builds, and EXPLAIN shows it in the plan for the matching query.

Verify
EXPLAIN SELECT count(*) FROM orders
WHERE (created_at AT TIME ZONE 'UTC')::date = DATE '2026-11-20';

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 3

Your own function was created without a volatility label

You seeThe expression calls a function you wrote, and its provolatile is v

Check

When the IMMUTABLE error names an index on a user-defined function, the function most likely kept the default category. CREATE FUNCTION makes every function VOLATILE unless it says otherwise, and the CREATE INDEX documentation asks you to mark a function immutable before using it in an index expression or WHERE clause.

Check
SELECT p.oid::regprocedure, p.provolatile
FROM pg_proc p
WHERE p.proname = 'normalize_phone';

Fix

Only if the result truly depends on nothing but the arguments (no table reads, no settings, no current time), change the label. If it reads a setting such as TimeZone, it is STABLE and cannot back an index; rewrite it to take that value as an argument instead.

Fix
ALTER FUNCTION normalize_phone(text) IMMUTABLE;

CREATE INDEX customers_phone_norm_idx
    ON customers ((normalize_phone(phone)));

Verify

provolatile now shows i and the index is created.

Verify
SELECT provolatile FROM pg_proc WHERE oid = 'normalize_phone(text)'::regprocedure;

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

A built-in text function in the expression is STABLE: concat(), concat_ws() or to_char()

You seeThe expression builds a string, for example concat(first_name, ' ', last_name) or to_char(created_at, 'YYYY-MM')

Check

This version of the error appears even on plain text or timestamp columns, because concat(), concat_ws(), array_to_string() and to_char() for timestamps are marked STABLE in the catalog. The || operator between two text values is the immutable textcat function, but text || a non-text value goes through a STABLE function as well.

Check
SELECT p.oid::regprocedure, p.provolatile
FROM pg_proc p
WHERE p.proname IN ('concat', 'concat_ws', 'to_char', 'textcat', 'textanycat');

Fix

Build strings with || between text columns, wrapping nullable ones in coalesce(), and replace to_char() date formatting with date_trunc() or extract() on a timestamp column, both of which are immutable for that type.

Fix
CREATE INDEX people_full_name_idx
    ON people ((coalesce(first_name, '') || ' ' || coalesce(last_name, '')));

-- instead of to_char(created_on, 'YYYY-MM') on a timestamp column
CREATE INDEX events_month_idx
    ON events ((date_trunc('month', created_on)));

Verify

Both indexes build; run the queries with the same expressions and check EXPLAIN.

Verify
EXPLAIN SELECT * FROM people WHERE coalesce(first_name, '') || ' ' || coalesce(last_name, '') = 'Ada Lovelace';

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Store event times as timestamptz but index calendar days with an explicit zone, and keep that expression in one shared query helper.
  • Give every CREATE FUNCTION an explicit IMMUTABLE, STABLE or VOLATILE label in code review, instead of relying on the VOLATILE default.
  • Before writing an expression index, run the pg_proc volatility query on each function it calls.
  • For a value the application needs often, consider a stored generated column with an immutable expression and a plain index on it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.