Sub topic 5
How INSERT and UPDATE Treat Generated Columns
This chapter assumes you know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. It also assumes you understand that a generated column calculates its value from other columns in the same row.
A generated column is controlled by PostgreSQL. Your statement supplies the source columns, and PostgreSQL evaluates the generation expression. This prevents application code from writing a derived value that disagrees with its source data.
INSERT: omit the column or use `DEFAULT`
Consider this table:
CREATE TABLE people (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL,
full_name text GENERATED ALWAYS AS
(first_name || ' ' || last_name) STORED
);The normal insert omits full_name:
INSERT INTO people (first_name, last_name)
VALUES ('Ada', 'Lovelace')
RETURNING id, first_name, last_name, full_name;The result includes the computed value:
id | first_name | last_name | full_name
----+------------+-----------+---------------
1 | Ada | Lovelace | Ada LovelaceYou may also include the generated column if its value is DEFAULT:
INSERT INTO people (first_name, last_name, full_name)
VALUES ('Grace', 'Hopper', DEFAULT);Here, DEFAULT does not mean that PostgreSQL uses a fixed default such as NULL. It tells PostgreSQL to calculate the generated value from first_name and last_name.
An explicit value is rejected:
INSERT INTO people (first_name, last_name, full_name)
VALUES ('Grace', 'Hopper', 'Someone Else');PostgreSQL reports an error similar to:
ERROR: cannot insert a non-DEFAULT value into column "full_name"This rule applies even when the supplied value happens to be correct. The database must remain the owner of the calculation.
UPDATE: change source columns, not the result
When an UPDATE changes a column used by the generation expression, PostgreSQL recomputes the generated column:
UPDATE people
SET last_name = 'King'
WHERE first_name = 'Ada';
SELECT first_name, last_name, full_name
FROM people
WHERE first_name = 'Ada';The row now contains:
first_name | last_name | full_name
------------+-----------+----------
Ada | King | Ada KingThe recomputation happens as part of the same row update. An observer does not see a new last_name paired with an old stored full_name after the statement completes.
You can explicitly write DEFAULT to the generated column:
UPDATE people
SET full_name = DEFAULT
WHERE first_name = 'Grace';For a generated column, this means “recalculate the expression.” A direct value or another expression is not allowed:
UPDATE people
SET full_name = 'Incorrect Name'
WHERE first_name = 'Grace';The statement fails because full_name is not an ordinary writable column.
Loading rows with `COPY`
The same ownership rule matters when bulk-loading data. With COPY FROM, provide the base columns and leave the generated column out of the input:
COPY people (first_name, last_name)
FROM STDIN
WITH (FORMAT csv);
Marie,Curie
Alan,Turing
\.PostgreSQL computes full_name for each loaded row. Do not add a third input field for full_name and expect PostgreSQL to accept it as authoritative data.
Generated columns also participate in normal constraints and indexes after their values are calculated. For example, a NOT NULL constraint on a generated column checks the computed result, not an input value.
A practical write pattern
A common pattern is to update source data and immediately return the derived result:
UPDATE people
SET last_name = 'Byron'
WHERE first_name = 'Ada'
RETURNING id, first_name, last_name, full_name;This avoids duplicating the generation expression in application code and gives the application the value PostgreSQL actually stored.
Exercise
What happens when this statement runs: INSERT INTO people (first_name, last_name, full_name) VALUES ('Katherine', 'Johnson', DEFAULT);?
Show answer
PostgreSQL accepts the row and computes full_name as Katherine Johnson. The DEFAULT keyword requests the generated value; it does not provide an independent value.