generated columns
columns computed from other columns. stored vs virtual, and why postgres 18 made virtual the default.
- address
- everything.types.generated_columns
- part
- part 6, types and values
- chapter
- chapter 11 of 11
- reading time
- 8 min
a generated column is computed from other columns of the same row, and you can never write to it. STORED computes it on INSERT and UPDATE and keeps it on disk. VIRTUAL computes it every time it's read and stores nothing. postgres 18 added virtual generated columns, and made them the default.
the expression must be immutable and see only its own row. you can index a stored one; for a virtual one, you index its expression.
the picture
you know how a spreadsheet has cells with formulas, =B2*C2, that you never type into? a generated column is that cell. the only question is when the formula runs: when the row is written, with the answer saved in the cell (stored), or every time someone looks at it (virtual).
think of a receipt. a stored total is printed at the till; reading it is free, and it takes ink. a virtual total is one you add up in your head each time you look, no ink, a little arithmetic.
- 1SELECT lp, lp_len, t_bits AS null_bitmap, t_data FROM heap_page_items(get_raw_page('line_s', 0)) ORDER BY lp;
| lpint2 | lp_lenint2 | null_bitmaptext | t_databytea |
|---|---|---|---|
| 1 | 46 | null | \x01000000030000000f0081020088130f008107008813 |
the stored row on its page: four columns of data, total included, and no null bitmap.
(1, 3, 2.50) with total = qty * price, as it sits on the 8 KB page. switch between STORED and VIRTUAL. the virtual tuple is shorter, and its marks total as NULL: on disk there's nothing there.t_data is the row's bytes after the header: 1, then 3, then the numeric 2.50, and for the stored one, the numeric 7.50 at the end. the virtual tuple stops after 2.50. (inside a tuple decodes the rest.)
STORED, VIRTUAL, and the 18 default
before postgres 18, STORED was required. since 18 pg 18+, VIRTUAL exists and is what you get if you don't say:
CREATE TABLE items (
qty int NOT NULL,
unit_price numeric(10,2) NOT NULL,
total numeric GENERATED ALWAYS AS (qty * unit_price)
);
SELECT attname, attgenerated FROM pg_attribute WHERE attrelid = 'items'::regclass AND attnum > 0 ORDER BY attnum;
INSERT INTO items (qty, unit_price) VALUES (3, 2.50) RETURNING total;| attnamename | attgeneratedchar |
|---|---|
| qty | '' |
| unit_price | '' |
| total | v |
| totalnumeric |
|---|
| 7.50 |
v in the catalog: virtual. either way the column is read-only. postgres computes it, and refuses a value from you (DEFAULT is the one thing you may write):
CREATE TABLE items (qty int, unit_price numeric(10,2), total numeric GENERATED ALWAYS AS (qty * unit_price));
INSERT INTO items (qty, unit_price, total) VALUES (3, 2.50, 99);the difference you'll feel first is adding one to a big table. a virtual column is a catalog change; a stored one rewrites every row:
- 1SELECT pg_relation_filenode('events') AS file, pg_relation_size('events') / 8192 AS pages;
| fileoid | pagesint8 |
|---|---|
| 16385 | 335 |
events before: its file number and size.
(a rewrite also takes a lock that blocks reads and writes for as long as it runs. on a big table that's the outage.) so which one?
- virtual when the expression is cheap and the column is read less than the table is written: no disk, no rewrite to add it, and
ALTER COLUMN ... SET EXPRESSIONis a catalog change too. - stored when the expression is expensive, or you need to index it, put a UNIQUE or foreign key on it, or build extended statistics on it. a virtual column can't have any of those.
what the expression may do
a generated column is a function of its row and nothing else, forever. so the expression must be immutable (same input, same answer, always) and can only see the row's own columns:
- 1CREATE TABLE g1 (a int, b timestamptz GENERATED ALWAYS AS (now()));
now() changes every time it's called, so it's not immutable.
CREATE TABLE signups (
at timestamptz,
day date GENERATED ALWAYS AS (at::date) STORED
);CREATE TABLE signups (
at timestamptz,
day date GENERATED ALWAYS AS ((at AT TIME ZONE 'UTC')::date) STORED
);
INSERT INTO signups (at) VALUES ('2026-03-29 01:30+00') RETURNING day;| daydate |
|---|
| 2026-03-29 |
the user-defined-function rule is specific to virtual columns in 18 (the error says "not yet supported"). a stored column may call an immutable function you wrote, and it's on you that it really is immutable: change the function, and the stored values don't change with it.
indexing a generated column
a stored generated column is an ordinary column on disk, so it takes ordinary indexes. a virtual one has nothing on disk to index, and postgres 18 says so. but a virtual column is expanded into its expression before planning, so an expression index on the same expression serves it:
ALTER TABLE events ADD COLUMN page text GENERATED ALWAYS AS (props ->> 'page');
CREATE INDEX events_page ON events ((props ->> 'page'));
ANALYZE events;
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE page = '/p/17';the plan never mentions page. it shows the expression, and the index on it:
props ->> 'page' and finds the 129 matching events.that's the jsonb pattern from json and jsonb: keep the document, name the key you query with a virtual column, and index its expression. promote it to STORED when you need a constraint on it.
keylines
- a generated column is computed from its own row, and you can't write to it.
- STORED keeps the value on disk; VIRTUAL computes it on read. since 18, virtual is the default.
- adding a virtual column is a catalog change; adding a stored one rewrites the table.
- the expression must be immutable and see only the row; virtual ones can't call your functions.
- index a stored one directly, or a virtual one's expression.