skip to content

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.

total is =
page.sql
  1. 1SELECT lp, lp_len, t_bits AS null_bitmap, t_data FROM heap_page_items(get_raw_page('line_s', 0)) ORDER BY lp;
the result for STORED
lpint2lp_lenint2null_bitmaptextt_databytea
146null\x01000000030000000f0081020088130f008107008813
(1 row)

the stored row on its page: four columns of data, total included, and no null bitmap.

real output, postgres 18.6 (PGlite 0.5.12) · generated-columns
the same row, (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 null bitmap 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:

sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · SELECT 3
attnamenameattgeneratedchar
qty''
unit_price''
totalv
(3 rows)
#3 · INSERT 0 1
totalnumeric
7.50
(1 row)

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):

sqlempty db
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);
output, from postgres 18.6
#1 · CREATE TABLE
#2 · ERROR 428C9
ERROR 428C9: cannot insert a non-DEFAULT value into column "total"
DETAIL: Column "total" is a generated column.

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:

events =
alter.sql
  1. 1SELECT pg_relation_filenode('events') AS file, pg_relation_size('events') / 8192 AS pages;
the result for before
fileoidpagesint8
16385335
(1 row)

events before: its file number and size.

real output, postgres 18.6 (PGlite 0.5.12) · generated-columns
the events table (25,000 rows) before and after adding each kind of column. pick one. VIRTUAL keeps the same file and size; STORED writes a new file, and it's bigger.

(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 EXPRESSION is 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:

uses =
refused.sql
  1. 1CREATE TABLE g1 (a int, b timestamptz GENERATED ALWAYS AS (now()));
ERROR: generation expression is not immutableSQLSTATE 42P17

now() changes every time it's called, so it's not immutable.

real output, postgres 18.6 (PGlite 0.5.12) · generated-columns
six things postgres refuses, each a real error. pick one. the first two aren't immutable: now() changes, and timestamptz to text depends on the session's TimeZone.
predict the output
sql
CREATE TABLE signups (
  at  timestamptz,
  day date GENERATED ALWAYS AS (at::date) STORED
);
sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 1
daydate
2026-03-29
(1 row)

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:

sqldb=events
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';
output, from postgres 18.6
#1 · ALTER TABLE
#2 · CREATE INDEX
#3 · ANALYZE
#4 · EXPLAIN
Aggregate -> Bitmap Heap Scan on events Recheck Cond: ((props ->> 'page'::text) = '/p/17'::text) -> Bitmap Index Scan on events_page Index Cond: ((props ->> 'page'::text) = '/p/17'::text)

the plan never mentions page. it shows the expression, and the index on it:

show =
actual rows
real output, postgres 18.6 (PGlite 0.5.12) · generated-columns
EXPLAIN ANALYZE of a count on the virtual column. the bitmap index scan uses the expression index on 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.