Sub topic 10
Changing Generated Columns on an Existing Table
This chapter assumes you know basic ALTER TABLE, understand stored generated columns, and know that changing a table can lock or rewrite data. The examples use PostgreSQL syntax. Test schema changes on a copy of production data before applying them during a busy period.
A generated column is part of the table definition, so changing it is a schema migration. PostgreSQL may need to calculate the expression for every existing row. That can take time and require a strong table lock.
Adding a generated column
Use ADD COLUMN with GENERATED ALWAYS AS and STORED:
ALTER TABLE people
ADD COLUMN full_name text
GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;For existing rows, PostgreSQL calculates full_name from first_name and last_name. New rows and later updates use the same expression automatically.
The expression must satisfy the same rules as an expression used when creating a table. It can reference columns in the same row, but it cannot read another table or call a non-immutable function.
If you need to add the column only when it does not already exist, use IF NOT EXISTS:
ALTER TABLE people
ADD COLUMN IF NOT EXISTS full_name text
GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;This avoids an error when the column exists, but it does not verify that the existing column has the expression or data type you expected. Check the schema separately when correctness matters.
Changing the generation expression
PostgreSQL provides SET EXPRESSION AS for changing the expression of an existing generated column:
ALTER TABLE people
ALTER COLUMN full_name
SET EXPRESSION AS (concat_ws(' ', first_name, last_name));PostgreSQL recalculates the column for all existing rows. Future inserts and updates use the new expression.
Changing an expression can affect indexes and constraints that depend on the generated column. The index definition still refers to the column, but its entries must reflect the newly calculated values. Plan and test this operation as a data-changing migration, not as a metadata-only rename.
If the new expression does not meet generated-column rules, PostgreSQL rejects the command. For example, an expression that uses a volatile function such as random() cannot define a generated column.
Converting a normal column to generated
An ordinary column can be changed into a generated column with ADD GENERATED:
ALTER TABLE people
ALTER COLUMN full_name
ADD GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;PostgreSQL computes the expression for existing rows. Before running this command, inspect the current values. Any values that do not match the expression will be replaced by generated values.
After conversion, applications must stop supplying values for full_name. Inserts may omit the column or use DEFAULT:
INSERT INTO people (first_name, last_name, full_name)
VALUES ('Grace', 'Hopper', DEFAULT);An explicit non-DEFAULT value is rejected because generated columns are controlled by PostgreSQL.
Removing generation
DROP EXPRESSION converts a stored generated column into an ordinary column:
ALTER TABLE people
ALTER COLUMN full_name DROP EXPRESSION;The current values remain in the column. PostgreSQL no longer recomputes them, so later inserts and updates can write values directly:
UPDATE people
SET full_name = 'Grace Hopper'
WHERE first_name = 'Grace' AND last_name = 'Hopper';This is useful when you need to preserve the computed data but take control of future writes. It also creates a maintenance responsibility: application code or another database process must now keep the value correct.
Replacing a column safely
If the desired change is too complex for SET EXPRESSION, create a replacement column, validate it, and then migrate dependents:
ALTER TABLE people
ADD COLUMN display_name text
GENERATED ALWAYS AS (
last_name || ', ' || first_name
) STORED;
CREATE INDEX people_display_name_idx
ON people (display_name);Do not drop the old column until queries, indexes, views, and application code no longer depend on it. Dropping a column can cascade to dependent objects if you explicitly use CASCADE, so avoid that option unless you have reviewed every dependency.
Exercise
What happens to the existing values when a normal column is converted with ADD GENERATED, and what happens after DROP EXPRESSION?
Show answer
Conversion recalculates the values from the generation expression. DROP EXPRESSION keeps the current values but turns the column into an ordinary writable column.