Sub topic 7
Choosing Between Stored and Virtual Generated Columns
This chapter assumes that you know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. You should also understand common PostgreSQL types, the purpose of an index, and the difference between a table and a view.
A generated column derives its value from other columns in the same row. PostgreSQL supports two calculation strategies:
- A stored generated column is calculated when the row is inserted or updated. PostgreSQL saves the result on disk.
- A virtual generated column is calculated when a query reads it. PostgreSQL does not save the result as part of the table row.
The same expression, different costs
The following table demonstrates both strategies. The VIRTUAL form requires PostgreSQL 18 or later.
CREATE TABLE rectangles (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
width numeric NOT NULL,
height numeric NOT NULL,
stored_area numeric GENERATED ALWAYS AS (width * height) STORED,
virtual_area numeric GENERATED ALWAYS AS (width * height) VIRTUAL
);
INSERT INTO rectangles (width, height)
VALUES (10, 4), (3, 7);
SELECT id, stored_area, virtual_area
FROM rectangles;Both generated columns return the same values. The difference is when PostgreSQL performs the multiplication.
| Property | Stored | Virtual |
|---|---|---|
| Calculation time | Insert or update | Read |
| Table storage | Uses space for the result | Does not store the result |
| Read cost | Usually low | Repeats the expression calculation |
| Write cost | Includes calculation and storage | Does not store the result |
| Direct indexing | Can be indexed like a column | Prefer an expression index when indexing is required |
A stored column is useful when many queries read the derived value. The calculation happens once per write, and later reads can use the saved value. The value can also have an index, which helps PostgreSQL find matching rows without calculating the expression for every row.
CREATE INDEX rectangles_stored_area_idx
ON rectangles (stored_area);
SELECT id
FROM rectangles
WHERE stored_area = 40;A virtual column is useful when saving the derived value would waste space or when the table is written often but the value is read rarely. PostgreSQL calculates the value only when a query needs it. This reduces table storage, but repeated reads consume CPU time.
Changing an input column affects the trade-off. With a stored column, PostgreSQL recalculates the result during the update:
UPDATE rectangles
SET width = 12
WHERE id = 1;The stored stored_area value is now 48. The virtual virtual_area also returns 48, but PostgreSQL computes it when the row is read.
Choosing deliberately
Choose a stored generated column when:
- Queries read the value frequently.
- Queries filter or sort by the value.
- You need an index on the derived value.
- The expression is moderately or highly expensive.
- The extra table and index storage is acceptable.
- You want read performance to be more predictable.
Choose a virtual generated column when:
- The derived value is wide and would consume substantial storage.
- The value is read infrequently.
- The table receives many writes.
- The expression is inexpensive.
- You do not need to index the generated value directly.
- The latest calculation should be produced at read time without storing another copy.
The choice is a cost decision. Stored columns move work toward writes and storage. Virtual columns move work toward reads and CPU usage.
A decision path
Use this decision path when both definitions appear suitable:
Expression restrictions can also affect the decision. Both forms must use expressions that PostgreSQL allows for generated columns. In particular, the expression must be stable for the same input row and must follow PostgreSQL's immutability rules. Virtual columns have additional restrictions in PostgreSQL because their expressions are evaluated during reads.
A stored column does not automatically make every query fast. An index helps only when its definition matches the query and PostgreSQL chooses to use it. Likewise, a virtual column may be perfectly suitable for a simple value that is selected occasionally.
Exercise
A table receives millions of updates, but a derived text label is selected only in an occasional administrative report. Should the label usually be stored or virtual, assuming the expression is allowed for both forms?
Show answer
It should usually be virtual. The choice avoids storing the label and avoids recalculating it during every write. The report pays the calculation cost when it reads the label.