snippetsqlModerate
How to change definition of a Postgres GENERATED (stored) column
Viewed 0 times
definitionstoredgeneratedpostgrescolumnhowchange
Problem
How to change definition of a Postgres 12 GENERATED column?
I tried
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.
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.
Solution
I'm not sure this is the correct answer, but reading the docs about ALTER TABLE I would say: No, you can't.
where action is one of:
ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE
collation ] [ column_constraint [ ... ] ]
It seems that the only command that allows column_constraint is ADD COLUMN
In fact you can add a constraint name to the computed column:
where action is one of:
ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE
collation ] [ column_constraint [ ... ] ]
and column_constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL |
NULL |
CHECK ( expression ) [ NO INHERIT ] |
DEFAULT default_expr |
GENERATED ALWAYS AS ( generation_expr ) STORED |
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |
UNIQUE index_parameters |
PRIMARY KEY index_parameters |
REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]It seems that the only command that allows column_constraint is ADD COLUMN
In fact you can add a constraint name to the computed column:
val_sum int4 CONSTRAINT GA_test generated always AS (val_a + val_b) storedCode Snippets
and column_constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL |
NULL |
CHECK ( expression ) [ NO INHERIT ] |
DEFAULT default_expr |
GENERATED ALWAYS AS ( generation_expr ) STORED |
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |
UNIQUE index_parameters |
PRIMARY KEY index_parameters |
REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]val_sum int4 CONSTRAINT GA_test generated always AS (val_a + val_b) storedContext
StackExchange Database Administrators Q#250868, answer score: 13
Revisions (0)
No revisions yet.