Search code examples
postgresqlpostgresql-12generated-columns

Auto updated column and generated always values


Usually if I need to have some auto updated column as updated_at I used function and trigger. For example as it is described here.

In Postgres 12 we got generated columns. It does not give ability to use now() function directly, however I could create my own function wrapping it:

CREATE FUNCTION
    now_time()
    RETURNS timestamptz
AS $CODE$
BEGIN
    RETURN now();
END
$CODE$
    LANGUAGE plpgsql IMMUTABLE;

create table user(
    id serial primary key,
    name varchar,
    updated_at timestamptz not null generated always as (now_time()) stored
);

And this works.

Which unwanted side effects could I get? Is such way better then old good trigger?


Solution

  • The one-side effect would be,

    The value of the column would change if the database is restored from a pg_dump.