From: | Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> |
---|---|
To: | Celia McInnis <celia(dot)mcinnis(at)gmail(dot)com>, "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org> |
Subject: | Re: adding a generated column to a table? |
Date: | 2024-04-22 16:16:26 |
Message-ID: | 6731dc51-3752-45d0-b1e1-00c3246532ea@aklaver.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 4/22/24 09:05, Celia McInnis wrote:
> If I have a table containing a date field, say:
> create temporary table tmp1 as select now()::date as evtdate;
> SELECT 1
>
> select DATE_PART('year', evtdate)::integer as year from tmp1;
> year
> ------
> 2024
> (1 row)
>
> Is there some way of doing something like the following?:
>
> alter table tmp1 add column year integer generated always as
> DATE_PART('year', evtdate)::integer STORED;
> ERROR: syntax error at or near "DATE_PART"
> LINE 1: ... tmp1 add column year integer generated always as DATE_PART(...
>
https://www.postgresql.org/docs/current/sql-createtable.html
GENERATED ALWAYS AS ( generation_expr ) STORED
So:
generated always as (DATE_PART('year', evtdate)::integer) STORED
--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2024-04-22 16:29:21 | Re: CLUSTER vs. VACUUM FULL |
Previous Message | Celia McInnis | 2024-04-22 16:05:39 | adding a generated column to a table? |