Caveats
This only works if you're not changing the type of the generated column. Also it does not work if you're changing what columns you depend on. (So, to be clear, it won't help you in the specific example given)
Depend on a function instead
Create a function, then generate the column using that function. That way, you can update the generated column definition without any ALTER
statements. This can be very handy if you've got views depending on the table, as those otherwise would need to be dropped and re-created.
Example
CREATE OR REPLACE FUNCTION do_math(i integer)
RETURNS integer LANGUAGE sql IMMUTABLE STRICT
AS $function$
SELECT i + 10;
$function$;
CREATE TABLE example_table (
number integer PRIMARY KEY,
gen integer GENERATED ALWAYS AS (do_math(number)) STORED
);
INSERT INTO example_table VALUES (1),(4);
SELECT * FROM example_table;
-- =>
-- number | gen
-- --------+-----
-- 1 | 11
-- 4 | 14
CREATE OR REPLACE FUNCTION do_math(i integer)
RETURNS integer LANGUAGE sql IMMUTABLE STRICT
AS $function$
SELECT i * 100;
$function$;
-- Reguired to re-caluclate the genrated columns
UPDATE example_table SET number = number;
SELECT * FROM example_table;
-- =>
-- number | gen
-- --------+-----
-- 1 | 100
-- 4 | 400