Sub topic 6
Virtual Generated Columns
This chapter assumes basic CREATE TABLE, INSERT, UPDATE, and SELECT, plus the difference between a table and a view. It also assumes PostgreSQL 18 or later, where virtual generated columns are available.
A virtual generated column is a column whose value PostgreSQL calculates when a query reads the row. PostgreSQL does not store the calculated value in the table. The value comes from an expression based on other columns in the same row.
Defining a virtual column
Use GENERATED ALWAYS AS (...) VIRTUAL:
CREATE TABLE rectangles (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
width integer NOT NULL CHECK (width > 0),
height integer NOT NULL CHECK (height > 0),
area integer GENERATED ALWAYS AS (width * height) VIRTUAL
);
INSERT INTO rectangles (width, height)
VALUES (4, 7);
SELECT id, width, height, area
FROM rectangles;The query returns an area of 28. The table stores width and height, but it does not store area. PostgreSQL calculates width * height when it produces the result.
A virtual column still behaves like a column in a SELECT. You can use it in a filter or in another expression:
SELECT id
FROM rectangles
WHERE area > 20;
SELECT width, height, area * 2 AS doubled_area
FROM rectangles;If the base columns change, later reads use the new values:
UPDATE rectangles
SET width = 5
WHERE id = 1;
SELECT area
FROM rectangles
WHERE id = 1;The result is now 35. No separate application update is needed.
How virtual computation works
The following model shows when the expression is evaluated:
Virtual generation avoids storing duplicate data. That can reduce table size, especially when the derived value is large or many base columns are present. It also means PostgreSQL does the calculation each time a query needs the value.
| Property | Virtual generated column |
|---|---|
| Stored in the table | No |
| Computed | When read |
| Recomputed after a base-column update | On the next read |
| Directly supplied by `INSERT` | No, except `DEFAULT` |
| Main trade-off | Less storage, more read-time work |
Expression rules
A generated column must be derived from columns in the same row. Its expression cannot use a subquery, read another table, or depend on changing session state.
The expression must be immutable. An immutable expression always returns the same result for the same inputs. For example, multiplication of two integers is immutable. A function that depends on the current time or time zone is not reliably immutable.
Virtual generated columns have stricter rules than stored generated columns. In PostgreSQL, a virtual generation expression must use immutable built-in functions. It cannot use user-defined functions or user-defined types. These restrictions allow PostgreSQL to calculate the value safely whenever it reads the row.
Virtual columns can depend on ordinary columns, but they cannot depend on another generated column. Put the complete expression in one generated column or store an intermediate result in a normal column.
Virtual versus stored
A stored generated column calculates its value when the row is inserted or updated, then saves that value on disk. A virtual generated column calculates its value when a query reads it.
Choose a virtual column when:
- The expression is inexpensive to calculate.
- The value is not needed by most queries.
- Saving table space matters.
- The result should always reflect the current base columns.
Choose a stored column when:
- The expression is expensive.
- Many reads use the derived value.
- The value needs the broader expression support available to stored columns.
- You need a stored result for indexing or other database features.
Both forms keep the value consistent with the row's base columns. Neither form accepts an independently chosen value. The difference is when PostgreSQL performs the calculation and whether it keeps the result on disk.
Version and compatibility
Virtual generated columns require PostgreSQL 18 or later. A database that must run on PostgreSQL 17 or earlier cannot use the VIRTUAL syntax. For older versions, use a stored generated column where supported, a view, an expression index, or a trigger-backed ordinary column.
Exercise
A query reads one million rows and calculates a cheap price * quantity expression for each row. Should you choose a virtual or stored generated column?
Show answer
Either can be correct. Choose virtual to avoid storing the duplicate value when read-time multiplication is cheap. Choose stored when repeated queries need the value often and reducing read-time computation matters more than table space.