Sample Header Ad - 728x90

How to change definition of a Postgres GENERATED (stored) column

19 votes
4 answers
25146 views
How to change definition of a Postgres 12 GENERATED column?
CREATE TABLE test (
    id serial PRIMARY KEY,
    val_a int4 NOT NULL DEFAULT 0,
    val_b int4 NOT NULL DEFAULT 0,
    val_sum int4 generated always AS (val_a + val_b) stored
);
I tried
ALTER TABLE test ALTER COLUMN val_sum TYPE int4 generated always AS (val_a + val_b + 1) stored;
ALTER TABLE test ALTER COLUMN val_sum SET generated always AS (val_a + val_b + 1) stored;
but both give syntax errors. The alternative is to drop & add column, which works, but I wonder how to simply change it like any other column.
Asked by Marius (293 rep)
Oct 11, 2019, 04:16 PM
Last activity: Jan 22, 2024, 10:30 AM