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 1rzkk5-006Ru0-GZ for pgsql-general@arkaria.postgresql.org; Wed, 24 Apr 2024 22:05:29 +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 1rzkk3-000a2O-N4 for pgsql-general@arkaria.postgresql.org; Wed, 24 Apr 2024 22:05:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rzkk3-000a2G-A2 for pgsql-general@lists.postgresql.org; Wed, 24 Apr 2024 22:05:27 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rzkk1-002mPg-1H for pgsql-general@lists.postgresql.org; Wed, 24 Apr 2024 22:05:26 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2ddc2ea2091so3193431fa.1 for ; Wed, 24 Apr 2024 15:05:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713996324; x=1714601124; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=GnbkXEj7aRL47nDjMjXOw2Ut0MNAkdj7JW5Tb67FBz0=; b=ED0c/16T3A1Qdm9BtILSsgsLOjJxOTzb3+b300ZJL0Zb7YMpDEle121Df/IkxDO1ab wSXU9c2xjSrsXf4kkdsEF4XuUefLKT0WCV/wQSs+ln3J0QAROBAmgv+ONtuJNtTs/CBE sBkaC08hEEbZ4bUx7qaZwBqG8KSJzpytleEKNmlMJtQtiAj/XJxtNEZ7EdZcVq99U9vp 7/7P/MkYPmHfNdUz5SO2Bby0+QzqlxPbKQd0IZ5aOSLvZApa8ntz7wbCUGt6AsQ12hWL ZTAG7NskeQwjIvEq1Ahr3F/jp7vRO7p5T2/HLJ9SMV+vQn5bpRi8ewx6+Jo6NExL7zI6 z7GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713996324; x=1714601124; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GnbkXEj7aRL47nDjMjXOw2Ut0MNAkdj7JW5Tb67FBz0=; b=jlmsCVaV7iexesec+o88Hp//jEmjucu211Wf1adBVcxn1N9PvFX4cypi+Nvr4QfCvG XI5m3aiK+gVAcqawsVqBg3poLDGKwquENI8Hiz4aUxuiBdKk5Gq633fv/BHLnmLKp1eH rUQBYrv8mHmUeQrfzmm80/eT+v+ZO5a4yFcvQRqTydQCcmkNLKansA3Gr1/fYB/zGsak lYbEPa0FOjVdckjjAekdPuEj1x4KTL/YqpxAJ6GN6Hdeg8BSGxyO4OEfRh6Ruf8CJb5z 8PJ93R0exNWm1JenPuXLJBHMbHxhqhccUUGYGEK1nf07U53zqbpIt0wJFN1Q5T9Ct7NK dQMQ== X-Gm-Message-State: AOJu0Yysj63gjziLifjlIvtMBWWFZ9Ifw4js9OrY9RNTIF2Lw3JbRe5z U32cxPDXAb6BXDV+b3qIp0rjg4vxWwz1p44RA9q/w+5HZsPV7WcbysSfXyZ2rj/7X7563EefHTu Qrn7wBAJ4WaoW9hNrhZWc79eITps= X-Google-Smtp-Source: AGHT+IEPpYdTuHxH4FvNQldtwXJoIq2Sx4EvjOK9cJi7MfTL5fhGyKkzY80JFnThFzhtTBFT/HSWIV4LXcDUkgXB8UI= X-Received: by 2002:a2e:9855:0:b0:2d8:144e:c464 with SMTP id e21-20020a2e9855000000b002d8144ec464mr2318947ljj.36.1713996323493; Wed, 24 Apr 2024 15:05:23 -0700 (PDT) MIME-Version: 1.0 References: <3768124.1713995696@sss.pgh.pa.us> In-Reply-To: <3768124.1713995696@sss.pgh.pa.us> From: Celia McInnis Date: Wed, 24 Apr 2024 18:05:12 -0400 Message-ID: Subject: Re: is there an immutable function to switch from date to character? To: Tom Lane Cc: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000021d5b0616dede1d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000021d5b0616dede1d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sorry I pasted in the wrong code, I had wanted a column with the character version of the date (ie., YYYY-Mon-DD). Steve Baldwin's hack pointed me in the right direction. Here is the example: create temporary table junk as select now()::date as evtdate; SELECT 1 alter table junk add column chardate text GENERATED ALWAYS AS (cmm_date_to_char(evtdate)) STORED; select * from junk; evtdate | chardate ------------+------------- 2024-04-24 | 2024-Apr-24 (1 row) where cmm_date_to_char is defined as: create or replace function cmm_date_to_char(i_date in date) returns text immutable language sql as $$ select to _char(i_date, 'YYYY-Mon-DD') $$; Thanks! On Wed, Apr 24, 2024 at 5:54=E2=80=AFPM Tom Lane wrote: > 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: > > =3D# 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 > =3D# 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 > --000000000000021d5b0616dede1d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Sorry I pasted in the wrong code, I = had wanted a column with the character version of the date (ie., YYYY-Mon-D= D). Steve Baldwin's hack pointed me in the right direction. Here is the= example:

create temporary table junk as select no= w()::date as evtdate;
SELECT 1

alter table junk add column charda= te text GENERATED ALWAYS AS (cmm_date_to_char(evtdate)) STORED;

sele= ct * from junk;
=C2=A0 evtdate =C2=A0 | =C2=A0chardate =C2=A0
------= ------+-------------
=C2=A02024-04-24 | 2024-Apr-24
(1 row)

where cmm_date_to_char is defined as:

<= div>create or replace function cmm_date_to_char(i_date in date) returns tex= t immutable language sql as $$ select to
_char(i_date, 'YYYY-Mon-DD&= #39;) $$;

Thanks!

On Wed, Apr 24, 2024 at= 5:54=E2=80=AFPM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Celia McInnis <celia.mcinnis@gmail.com> 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:=C2=A0 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.=C2=A0 (Type date doesn't depend on timezone, but that keeps y= ou
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.=C2=A0 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:

=3D# 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
=3D# 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.=C2=A0 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.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--000000000000021d5b0616dede1d--