Sub topic 4
What the Expression May Contain
This chapter assumes you know basic CREATE TABLE, INSERT, UPDATE, and SELECT, common PostgreSQL data types, and the difference between a table and a view. You should also know that an index can speed up searches by storing an ordered or structured lookup for column values.
A generated column calculates its value from other columns in the same row. PostgreSQL does not accept every SQL expression for this calculation. The expression must be predictable and independent of changing outside conditions.
The immutability rule
For a generated column, the expression must use immutable functions. An immutable function always returns the same result when given the same inputs. For example, adding two integers is immutable:
CREATE TABLE measurements (
width integer NOT NULL,
height integer NOT NULL,
area integer GENERATED ALWAYS AS (width * height) STORED
);If width is 4 and height is 6, area is always 24. The result does not depend on the current time, the database session, or another table.
PostgreSQL records whether a function is immutable, stable, or volatile:
| Function category | Meaning | Suitable for a generated expression? |
|---|---|---|
| Immutable | Same inputs always produce the same result | Yes |
| Stable | Result can change between statements or sessions | No |
| Volatile | Result can change even within one statement | No |
A function such as random() is volatile. A function such as current_date depends on the current date. Neither can be used in a generated column.
Values from the same row
A generated expression can read ordinary columns from its own row. It cannot read a value from another row or run a subquery.
CREATE TABLE products (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
unit_price numeric(10, 2) NOT NULL,
quantity integer NOT NULL,
total_price numeric(12, 2)
GENERATED ALWAYS AS (unit_price * quantity) STORED
);The expression can combine columns, constants, operators, casts, and approved immutable functions:
CREATE TABLE contacts (
first_name text NOT NULL,
last_name text NOT NULL,
full_name text
GENERATED ALWAYS AS (trim(first_name || ' ' || last_name)) STORED
);A generated column cannot use another generated column. If several derived values are needed, repeat the base expression or redesign the table so that each generated column depends directly on ordinary columns.
Expressions affected by session settings
Some functions look deterministic but depend on settings outside the row. PostgreSQL rejects those functions when they are not immutable.
For example, formatting a timestamptz value can depend on the session time zone:
CREATE TABLE events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
occurred timestamptz NOT NULL,
year_text text
GENERATED ALWAYS AS (to_char(occurred, 'YYYY')) STORED
);This definition fails because to_char for a timestamp with time zone can produce a different result in different time zones. Store a value whose meaning is independent of the session, or calculate the display text in a query or application instead.
A dependency model
The dependency must point inward, from ordinary columns in one row to the generated value.
For a virtual generated column, PostgreSQL evaluates the expression when the column is read instead of storing the result. The same basic dependency restrictions apply, with additional restrictions depending on the PostgreSQL version and expression features.
Checking a rejected expression
When PostgreSQL rejects a generated column, inspect each function in the expression. Ask:
- Does it depend only on its arguments?
- Can a session setting change its result?
- Does it read the clock, random state, or database contents?
- Does it invoke a subquery or refer to another row?
- Does it refer to another generated column?
If the answer to any of these questions is yes, use a simpler immutable expression or choose another mechanism, such as a view or trigger.
Exercise
Why does PostgreSQL reject a generated column expression that uses random()?
Show answer
random() is volatile. It can return a different value for the same row and inputs, so PostgreSQL cannot treat the generated value as a dependable result of the row's data.