PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 42P17: functions in index expression must be marked IMMUTABLE

An index or generated column uses a function whose result can change, so PostgreSQL refuses it.

PostgreSQL 17.12
42P17: functions in index expression must be marked IMMUTABLE

Jump to a cause

  1. 01to_char(created_at, 'YYYY-MM') in an expression index
  2. 02Generated column created_at::date on a timestamptz column
  3. 03Your own function used in an index without IMMUTABLE
  4. 04now() in a partial index WHERE clause

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

Index expressions, partial index predicates and generated columns must always give the same result for the same input. PostgreSQL only accepts functions marked IMMUTABLE there. Functions marked STABLE or VOLATILE, such as to_char, now(), a timestamptz-to-date cast (it depends on the TimeZone setting) or your own function created without a volatility label, raise 42P17.

The message tells you where: functions in index expression must be marked IMMUTABLE, functions in index predicate must be marked IMMUTABLE, or generation expression is not immutable. The four runs below cover each.

01to_char(created_at, 'YYYY-MM') in an expression index

Creating an index on (to_char(created_at, 'YYYY-MM')) failed with functions in index expression must be marked IMMUTABLE. to_char is not immutable.

created_at was timestamp (without time zone). Indexing date_trunc('month', created_at) instead was accepted, and WHERE date_trunc('month', created_at) = '2024-02-01' returned the February order. The query must use the same expression as the index.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, created_at timestamp, amount int);
INSERT INTO orders VALUES (1, '2024-01-05 10:00', 100), (2, '2024-02-20 15:30', 70);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
CREATE INDEX orders_month_idx ON orders ((to_char(created_at, 'YYYY-MM')));
✕ ERROR
ERROR:  42P17: functions in index expression must be marked IMMUTABLE
Fixrun as lab_a
CREATE INDEX orders_month_idx ON orders ((date_trunc('month', created_at)));
OUTPUT
CREATE INDEX
After the fixrun as lab_a
SELECT id, amount FROM orders WHERE date_trunc('month', created_at) = '2024-02-01';
SELECT indexdef FROM pg_indexes WHERE indexname = 'orders_month_idx';
✓ SUCCESS
 id | amount
----+--------
  2 |     70
(1 row)

                                             indexdef
---------------------------------------------------------------------------------------------------
 CREATE INDEX orders_month_idx ON lab_a.orders USING btree (date_trunc('month'::text, created_at))
(1 row)

02Generated column created_at::date on a timestamptz column

events.created_at was timestamptz. GENERATED ALWAYS AS (created_at::date) STORED failed with generation expression is not immutable, because the date of a timestamptz depends on the session time zone.

Naming the time zone fixed it: (created_at AT TIME ZONE 'Asia/Seoul')::date. A row with 2024-01-05 23:30 UTC got created_date 2024-01-06, the date in Seoul.

Setup
Setuprun as lab_a
CREATE TABLE events (id int PRIMARY KEY, created_at timestamptz);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
ALTER TABLE events ADD COLUMN created_date date GENERATED ALWAYS AS (created_at::date) STORED;
✕ ERROR
ERROR:  42P17: generation expression is not immutable
Fixrun as lab_a
ALTER TABLE events ADD COLUMN created_date date GENERATED ALWAYS AS ((created_at AT TIME ZONE 'Asia/Seoul')::date) STORED;
OUTPUT
ALTER TABLE
After the fixrun as lab_a
INSERT INTO events (id, created_at) VALUES (1, '2024-01-05 23:30+00');
SELECT id, created_at AT TIME ZONE 'UTC' AS created_utc, created_date FROM events;
✓ SUCCESS
INSERT 0 1
 id |     created_utc     | created_date
----+---------------------+--------------
  1 | 2024-01-05 23:30:00 | 2024-01-06
(1 row)

03Your own function used in an index without IMMUTABLE

norm_phone(text) removed non-digits from a phone number and was created without a volatility label. Indexing norm_phone(phone) failed with 42P17.

pg_proc showed provolatile = v (VOLATILE, the default). The function only uses its argument, so ALTER FUNCTION norm_phone(text) IMMUTABLE was correct. The index was then created and WHERE norm_phone(phone) = '01098765432' found the row stored as '010 9876 5432'. Mark a function IMMUTABLE only when its result really depends on nothing but its arguments.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, phone text);
INSERT INTO customers VALUES (1, '010-1234-5678'), (2, '010 9876 5432');
CREATE FUNCTION norm_phone(p text) RETURNS text LANGUAGE plpgsql AS $$
BEGIN
  RETURN regexp_replace(p, '[^0-9]', '', 'g');
END;
$$;
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE FUNCTION
Reproducerun as lab_a
CREATE INDEX customers_phone_idx ON customers (norm_phone(phone));
✕ ERROR
ERROR:  42P17: functions in index expression must be marked IMMUTABLE
Checkrun as lab_a
SELECT proname, provolatile FROM pg_proc WHERE proname = 'norm_phone';
OUTPUT
  proname   | provolatile
------------+-------------
 norm_phone | v
(1 row)
Fixrun as lab_a
ALTER FUNCTION norm_phone(text) IMMUTABLE;
OUTPUT
ALTER FUNCTION
After the fixrun as lab_a
CREATE INDEX customers_phone_idx ON customers (norm_phone(phone));
SELECT id, phone FROM customers WHERE norm_phone(phone) = '01098765432';
✓ SUCCESS
CREATE INDEX
 id |     phone
----+---------------
  2 | 010 9876 5432
(1 row)

04now() in a partial index WHERE clause

CREATE INDEX ... ON sessions (user_id) WHERE expires_at > now() failed with functions in index predicate must be marked IMMUTABLE. now() changes every moment, so the index could not stay correct.

An index on (user_id, expires_at) was created instead, and the query put expires_at > now() in its own WHERE. It returned only the session that had not expired.

Setup
Setuprun as lab_a
CREATE TABLE sessions (id int PRIMARY KEY, user_id int, expires_at timestamptz);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
CREATE INDEX sessions_active_idx ON sessions (user_id) WHERE expires_at > now();
✕ ERROR
ERROR:  42P17: functions in index predicate must be marked IMMUTABLE
Fixrun as lab_a
CREATE INDEX sessions_user_exp_idx ON sessions (user_id, expires_at);
OUTPUT
CREATE INDEX
After the fixrun as lab_a
INSERT INTO sessions VALUES (1, 10, now() + interval '1 hour'), (2, 10, now() - interval '1 hour');
SELECT id FROM sessions WHERE user_id = 10 AND expires_at > now();
✓ SUCCESS
INSERT 0 2
 id
----
  1
(1 row)

FAQ

How do I index a timestamp by month in PostgreSQL?

Use date_trunc('month', col) on a timestamp column, and write the same expression in the query. to_char cannot be used in an index.

Why is created_at::date not immutable?

For timestamptz the date depends on the session TimeZone. Use (created_at AT TIME ZONE 'Asia/Seoul')::date with a fixed zone.

Can I just mark my function IMMUTABLE?

Only if the result depends on nothing but its arguments (no table reads, no now(), no settings). In the run above norm_phone met that condition.

How do I check a function's volatility?

SELECT proname, provolatile FROM pg_proc WHERE proname = 'name'; returns i (IMMUTABLE), s (STABLE) or v (VOLATILE).

Can a partial index use now()?

No, it raised 42P17. Index the column and put the time condition in the query.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12