Sub topic 2
Defining a Generated Column
A generated column is a table column whose value PostgreSQL computes from an expression. The expression can use other columns in the same row. This keeps derived data consistent without requiring every application to calculate and update it.
This chapter assumes that you know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. You should also understand that a table stores rows, while a view computes query results when you read it.
The basic syntax
For a stored generated column, use this form:
column_name data_type
GENERATED ALWAYS AS (expression) STOREDThe data type is the type of the computed result. The expression describes how PostgreSQL produces that result. STORED tells PostgreSQL to calculate the value when the row is written and save the result with the row.
The following table shows the role of each part:
| Part | Meaning | Column | Column | Column | Column |
|---|---|---|---|---|---|
| `full_name` | The generated column's name | ||||
| `text` | The result type | ||||
| `GENERATED ALWAYS AS` | PostgreSQL calculates the value | ||||
| `(first_name | ' ' | last_name)` | The calculation | ||
| `STORED` | Save the calculated value in the table |
Here is a complete example:
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 row whose full_name is Ada Lovelace. The INSERT statement supplies only the two base columns. PostgreSQL supplies full_name.
A base column is a column whose value is stored directly from an insert or update. A generated column depends on base columns. In this example, first_name and last_name are base columns, while full_name is derived from them.
How the value is produced
The calculation happens as part of the table write. PostgreSQL evaluates the expression after it has the row's input values. It then stores the result in the generated column.
If a source column changes, PostgreSQL recalculates the generated column:
UPDATE people
SET last_name = 'King'
WHERE first_name = 'Ada';
SELECT full_name
FROM people;The result is now Ada King. You do not write a separate update for full_name.
GENERATED ALWAYS communicates that the generated value belongs to PostgreSQL. When inserting or updating a row, use the base columns and let PostgreSQL calculate the generated column. An explicit non-DEFAULT value for a generated column is rejected.
Rules for the expression
A generated column expression can refer to other columns in the same row, but it cannot read arbitrary external data. In particular, the expression cannot use:
- Another generated column.
- A subquery.
- A different table.
- A function that can return different results for the same input.
PostgreSQL requires the functions used by a generated expression to be immutable. An immutable function always returns the same result for the same arguments. This requirement makes the stored value dependable: PostgreSQL can calculate it during a write and know that the result is determined by the row's values.
For example, string concatenation and arithmetic are suitable for common generated columns:
CREATE TABLE line_items (
quantity integer NOT NULL,
unit_price numeric(10, 2) NOT NULL,
line_total numeric(12, 2) GENERATED ALWAYS AS
(quantity * unit_price) STORED
);The expression uses only values from the current row. It does not look up a price in another table or depend on the current time.
Choosing a generated column
Use a generated column when all of these conditions are true:
- The value is derived from columns in the same row.
- The calculation should be consistent for every application.
- The expression meets PostgreSQL's immutability and dependency rules.
- Storing the result is useful for later reads, constraints, or indexes.
Exercise
In the line_items table, which statement should you use to create a stored generated column named subtotal from quantity and unit_price?
Show answer
subtotal numeric(12, 2) GENERATED ALWAYS AS (quantity * unit_price) STORED