Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rzka3-006QVr-6U for pgsql-general@arkaria.postgresql.org; Wed, 24 Apr 2024 21:55:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rzka1-000Tyu-9R for pgsql-general@arkaria.postgresql.org; Wed, 24 Apr 2024 21:55:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rzka1-000Tyl-0A for pgsql-general@lists.postgresql.org; Wed, 24 Apr 2024 21:55:05 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rzkZu-004U6Y-Fp for pgsql-general@lists.postgresql.org; Wed, 24 Apr 2024 21:55:03 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 43OLsuCS3768125; Wed, 24 Apr 2024 17:54:56 -0400 From: Tom Lane To: Celia McInnis cc: "pgsql-generallists.postgresql.org" Subject: Re: is there an immutable function to switch from date to character? In-reply-to: References: Comments: In-reply-to Celia McInnis message dated "Wed, 24 Apr 2024 17:31:00 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3768123.1713995696.1@sss.pgh.pa.us> Date: Wed, 24 Apr 2024 17:54:56 -0400 Message-ID: <3768124.1713995696@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Celia McInnis writes: > create temporary table junk as select now()::date as evtdate; > alter table junk add column chardate text GENERATED ALWAYS AS > (to_char(evtdate,'YYYY-Mon-DD')) STORED; > ERROR: generation expression is not immutable Probably not; I think all the available conversion functions respond to some combination of datestyle, lc_time, and timezone settings. (Type date doesn't depend on timezone, but that keeps you from using anything that shares functionality with timestamptz ... and your to_char call promotes the date to timestamptz.) I find your example not terribly compelling. Why expend storage space on such a column? If you're bound and determined to do it, writing a wrapper function that's labeled immutable should work: =# create function mytochar(date) returns text strict immutable parallel safe as $$ begin return to_char($1::timestamp, 'YYYY-Mon-DD'); end $$ language plpgsql; CREATE FUNCTION =# alter table junk add column chardate text GENERATED ALWAYS AS (mytochar(evtdate)) STORED; ALTER TABLE It's on you to be sure that the function actually is immutable, or at least immutable enough for your use-case. I believe my example is pretty safe: neither datestyle nor timezone should affect the timestamp-without-timezone variant of to_char(), and this particular format string doesn't depend on lc_time. regards, tom lane