You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CREATE OR REPLACEFUNCTIONgenerate_foo_immutable ()
RETURNS TRIGGER
AS $$
BEGINNEW.foo=NEW.bar*2;
RETURN new;
END;
$$
LANGUAGE plpgsql IMMUTABLE;
CREATETRIGGERgenerate_foo_immutable_trigger
BEFORE INSERT ORUPDATE [OF other_column1, other_column2, ...] ON foo_table
FOR EACH ROW
EXECUTE PROCEDURE public.generate_foo_immutable();
The text was updated successfully, but these errors were encountered:
reference: https://stackoverflow.com/questions/77852268/how-to-add-a-stored-generated-column-to-a-very-large-table
tldr, when adding a generated stored column to an existing large table, it will lock the table so it can calculate the value for each row.
There seems to be a workaround:
BEFORE INSERT OR UPDATE
with a function that is equivalent to what you would put as the as generated expression.However, there seems to be a trade-off in that INSERT times are slower with a triggered function. source (4yrs old, for postgres v12): https://www.ongres.com/blog/generate_columns_vs_triggers/
The text was updated successfully, but these errors were encountered: