Sub topic 3
Stored Generated Columns in Practice
Assume you know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. A stored generated column is a table column that PostgreSQL calculates from other columns in the same row and saves the result on disk.
Stored columns are useful when a derived value is read often, costs something to calculate, or needs to behave like a normal column for later indexing and constraints.
Define a stored generated column
Use GENERATED ALWAYS AS followed by an expression and the keyword STORED:
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
);
INSERT INTO people (first_name, last_name)
VALUES ('Ada', 'Lovelace');
SELECT id, first_name, last_name, full_name
FROM people;The query returns a full_name value of Ada Lovelace. The application does not need to build that string, and it cannot accidentally store a different value for the same first and last names.
The expression can refer to other columns in the same row. It cannot use a subquery or read another table. PostgreSQL evaluates the expression when it writes the row.
See the value change with an update
PostgreSQL recalculates the generated column whenever an update changes a column used by its expression:
UPDATE people
SET last_name = 'King'
WHERE first_name = 'Ada';
SELECT full_name
FROM people
WHERE first_name = 'Ada';The result is now Ada King. You update last_name; PostgreSQL updates full_name as part of the same row operation.
This keeps the derived value consistent with its source values. It also means every insert or relevant update pays the cost of evaluating the expression. For a simple string concatenation, that cost is small. For a larger expression, the write cost can matter.
The dependency works in both directions conceptually: PostgreSQL knows that full_name depends on first_name and last_name, but applications should still treat the generated column as read-only.
Insert and copy data safely
Do not provide an ordinary value for a generated column. PostgreSQL rejects an explicit value because the database owns that value.
INSERT INTO people (first_name, last_name, full_name)
VALUES ('Grace', 'Hopper', 'Someone Else');This fails with an error similar to:
ERROR: cannot insert a non-DEFAULT value into column "full_name"You may omit the generated column, which is the usual approach. You may also write DEFAULT explicitly:
INSERT INTO people (first_name, last_name, full_name)
VALUES ('Grace', 'Hopper', DEFAULT);The generated value is Grace Hopper.
The same rule matters when loading data with COPY. The input should omit the generated column or use the database's supported default behavior. A data file containing a manually supplied value for the generated column cannot override PostgreSQL's calculation.
Follow the row's data flow
The following diagram shows when the stored value is computed:
A stored generated column is therefore part of the row's write process. It is not recalculated for every ordinary read. A SELECT reads the saved value like it reads any other stored column.
Check the result
You can compare the generated value with the source columns while testing:
SELECT
first_name || ' ' || last_name AS calculated_again,
full_name AS stored_value
FROM people;The two values should match. If the expression uses text operations, remember that NULL values can affect the result. In this example, NOT NULL prevents either name from being missing.
Exercise
After running the table definition, what happens when this statement runs: UPDATE people SET firstname = 'Augusta' WHERE lastname = 'King';?
Show answer
PostgreSQL changes the generated value to Augusta King because it recalculates full_name after first_name changes.