BUG #18097: Immutable expression not allowed in generated at

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: jim(at)jimkeener(dot)com
Subject: BUG #18097: Immutable expression not allowed in generated at
Date: 2023-09-08 03:47:49
Message-ID: 18097-ebb179674f22932f@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

The following bug has been logged on the website:

Bug reference: 18097
Logged by: Jim Keener
Email address: jim(at)jimkeener(dot)com
PostgreSQL version: 15.0
Operating system: Linux
Description:

Given this table:

CREATE TABLE test_table (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
created_at timestamptz NOT NULL DEFAULT now()
);

The following work:

* alter table test_table add created_local_y text GENERATED ALWAYS AS
(EXTRACT(isoyear FROM created_at AT TIME ZONE 'America/New_York')) STORED;

* alter table test_table add created_local_w text GENERATED ALWAYS AS
(EXTRACT(week FROM created_at AT TIME ZONE 'America/New_York')) STORED;

* alter table test_table add created_local text GENERATED ALWAYS AS
(EXTRACT(isoyear FROM created_at AT TIME ZONE 'America/New_York')::text ||
'|' || EXTRACT(week FROM created_at AT TIME ZONE 'America/New_York')::text)
STORED;

* CREATE INDEX ON test_table ((EXTRACT(isoyear FROM created_at AT TIME ZONE
'America/New_York') || '|' || EXTRACT(week FROM created_at AT TIME ZONE
'America/New_York')));

However, the following DOES NOT work with an error of (ERROR: generation
expression is not immutable):

* alter table test_table add created_local text GENERATED ALWAYS AS
(EXTRACT(isoyear FROM created_at AT TIME ZONE 'America/New_York') || '|' ||
EXTRACT(week FROM created_at AT TIME ZONE 'America/New_York')) STORED;

Given that casting shouldn't "increase" the immutability of an expression,
and expression indexes need also be immutable afaik, I think that there is a
bug somewhere here?

Thank you,
Jim

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Nikolay Samokhvalov 2023-09-08 04:25:20 Re: BUG #18094: max max_connections cannot be set
Previous Message Michael Paquier 2023-09-08 02:48:28 Re: FW: query pg_stat_ssl hang 100%cpu

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2023-09-08 03:48:43 Re: old_snapshot_threshold bottleneck on replica
Previous Message Amit Kapila 2023-09-08 03:44:59 Re: Impact of checkpointer during pg_upgrade