public inbox for [email protected]  
help / color / mirror / Atom feed
From: Holger Jakobs <[email protected]>
To: [email protected]
Subject: Re: Adding timestamp column
Date: Wed, 3 Sep 2025 19:46:16 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAMdfv4XEHpa8XSYf1C5EG5CE_VbcpptxiijUNOzj3HEvwLOaxQ@mail.gmail.com>
References: <CAMdfv4XEHpa8XSYf1C5EG5CE_VbcpptxiijUNOzj3HEvwLOaxQ@mail.gmail.com>

Am 03.09.25 um 17:50 schrieb Mauricio Fernandez:
> Hi admins, greetings.
>
> I'm trying to add a timestamp column in an existent table.
>
> This query works fine:
>
> select timecreated, timezone('America/Santiago'::text, 
> to_timestamp(timecreated::double precision)) datecreated
> from MDL_LOGSTORE_STANDARD_LOG;
>
> But, when I want to alter the table I get  a syntax error
>
> alter table MDL_LOGSTORE_STANDARD_LOG
>   add column datecreated timestamp
>   generated always as ( timezone ('America/Santiago'::text, 
> to_timestamp(timecreated::double precision)));
>
> SQL Error [42601]: ERROR: syntax error at end of input
>   Position: 185
>
> I would appreciate some tips
>
> Thanks in advanced
>
> kind regards
>
> Mauricio Fernández

Instead of

alter table MDL_LOGSTORE_STANDARD_LOG
   add column datecreated timestamp
   generated always as ( timezone ('America/Santiago'::text, 
to_timestamp(timecreated::double precision)));

I would suggest

ALTER TABLE MDL_LOGSTORE_STANDARD_LOG
ADD COLUMN datecreated TIMESTAMP
   GENERATED ALWAYS AS (timecreated AT TIME ZONE 'America/Santiago') STORED;

You didn't tell us the data type of the column timecreated. It should be 
'TIMESTAMPTZ DEFAULT current_timestamp' for it to work properly.

As of Version 18 you can leave out STORED, creating a virtual generated 
column.

Kind Regards,

Holger

-- 

Holger Jakobs, Bergisch Gladbach






reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Adding timestamp column
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox