Virtual generated columns are the PostgreSQL 18 default. Measured on 18.4: indexing, statistics, function and ALTER rules that differ from STORED.
The default kind of generated column changed
PostgreSQL has supported generated columns since version 12, but only the stored kind: the value is computed on insert or update and written to disk like any other column. PostgreSQL 18 adds virtual generated columns, which occupy no storage and are computed when read, and makes virtual the default. The documentation describes the relationship neatly: a virtual generated column is to a table what a view is, and a stored generated column is like a materialized view that is always kept up to date.
Existing stored columns are not affected by an upgrade: their definition already says STORED, and a dump of them carries the keyword. The risk lies in hand-written DDL, migration files and ORM-generated schemas created after the upgrade.
That default matters for anyone who copies an existing table definition into a PostgreSQL 18 database. A column declared as GENERATED ALWAYS AS (expression) without the STORED keyword used to be rejected on older versions, because stored was the only kind and the keyword was required. On 18 the same declaration is accepted and creates a virtual column, with different indexing, statistics, function and migration rules.
This guide runs each of those rules against a disposable PostgreSQL 18.4 server and records what happened. The companion SQL example executes 25 statements with an expected outcome for each, and exits non-zero if any outcome differs, so the claims below are reproducible rather than paraphrased.
A worked table
The example models order lines for Belgavi's Book Store with one virtual and one stored generated column.
-- Author: Sandeep Belgavi. Synthetic order lines, PostgreSQL 18.
CREATE TABLE book_order_lines (
id bigint PRIMARY KEY,
quantity int NOT NULL,
unit_price_cents int NOT NULL,
title text NOT NULL,
line_total_cents int GENERATED ALWAYS AS (quantity * unit_price_cents),
title_key text GENERATED ALWAYS AS (lower(title)) STORED
);
SELECT attname, attgenerated FROM pg_attribute
WHERE attrelid = 'book_order_lines'::regclass AND attgenerated <> '';
-- line_total_cents | v
-- title_key | sThe catalog shows the difference directly: attgenerated is v for virtual and s for stored. That query is also the quickest way to audit an existing database after an upgrade, because a schema migration tool that omitted STORED will have produced v columns without any warning.
Inserting rows without values for the generated columns works as expected, and reading them returns 2500 and 3999 for the two synthetic lines. Supplying a value for line_total_cents fails with the GeneratedAlways error class: generated columns of either kind cannot be written directly, although the keyword DEFAULT is permitted.
Indexes: not on the column, but on its expression
The first practical difference appears when you try to index.
CREATE INDEX ON book_order_lines (line_total_cents);
-- ERROR: indexes on virtual generated columns are not supported
CREATE INDEX line_total_expr ON book_order_lines ((quantity * unit_price_cents));
-- CREATE INDEXAn index directly on the virtual column is rejected. An expression index on the same expression is accepted. The interesting part is what the planner does with a query that filters on the virtual column. With sequential scans disabled to force the planner to show whether an index is usable, EXPLAIN for WHERE line_total_cents > 3000 reports an index scan using line_total_expr with the condition written as (quantity * unit_price_cents) > 3000. The virtual column is expanded into its expression before planning, so the expression index matches.
The stored column title_key behaves like an ordinary column: a plain index and a unique index both succeed.
That gives a clear decision rule. If you need a unique constraint, a primary key or a foreign key on a computed value, use STORED, because those require an index on the column itself. If you only need lookups by the computed value, a virtual column plus an expression index on the identical expression works, but the two definitions must be kept identical by hand. A later change to the column's expression will not update the index.
Statistics, functions and constraints
Extended statistics follow the same pattern as indexes. CREATE STATISTICS on the virtual column fails with "statistics creation on virtual generated columns is not supported", while CREATE STATISTICS on the expression (quantity * unit_price_cents) succeeds.
Functions are more restricted for virtual columns. The documentation states that the expression of a virtual generated column may use only built-in functions and types. The test confirms it: an IMMUTABLE SQL function add_tax works in a STORED column and is rejected in a VIRTUAL one with "generation expression uses user-defined function". If your computed values depend on domain logic in your own functions, they must be stored.
Check constraints on a virtual column work and are evaluated on write. Adding CHECK (line_total_cents > 0) succeeded, and an insert with quantity zero then failed with a check violation. That behaviour is useful, but it interacts with the next topic.
Changing the expression later
Schema evolution is where the two kinds diverge most, and where a team that chose virtual by default may be surprised.
On the stored column, ALTER TABLE ... ALTER COLUMN title_key SET EXPRESSION AS (upper(title)) succeeded and rewrote existing values; reading back returned the upper-case titles.
On the virtual column, two operations failed on PostgreSQL 18.4 in this table. SET EXPRESSION was rejected with a message stating that it is not supported for virtual generated columns in tables with check constraints. Because this table had a check constraint referencing the column, the expression could not be changed in place. DROP EXPRESSION, which converts a generated column into an ordinary column holding its current values, was rejected outright for the virtual column.
Those results describe this server version and this table shape; read the release notes of later minor and major versions before relying on either limitation persisting. The practical consequence today is that converting a virtual column to an ordinary one is not a one-statement operation. The safe path is to add a new ordinary column, backfill it from the expression in batches, switch readers, and drop the virtual column.
Costs that move from write to read
A stored generated column costs storage and write time and saves read time. A virtual one does the reverse. For cheap arithmetic like quantity times unit price the read cost is negligible and the storage saving is real. To measure it, the same two rows were inserted into twin tables that differ only in whether line_total_cents is virtual or stored, and the heap pages were read with the pageinspect extension: each tuple was 6 bytes shorter in the virtual table, 66 and 58 bytes against 72 and 64. Note that pg_column_size(t.*) reported 72 and 64 bytes for both tables, because it measures the row value as a query sees it, with the virtual column already computed; it is not a measure of what is on disk.
For expensive expressions the trade-off reverses. A virtual column computed by a complex expression is evaluated for every row a query reads, including rows later filtered out, unless an expression index lets the planner avoid reading them. Measure with EXPLAIN ANALYZE on representative data before choosing virtual for anything more than simple arithmetic or string functions.
Replication and privileges
Logical replication currently supports publishing stored generated columns only, according to the PostgreSQL 18 documentation on generated columns. If a downstream subscriber needs the computed value, either store it or compute it on the subscriber.
Privileges are checked separately for generated columns, so a role can be allowed to read a generated column without reading its base columns. For virtual columns the documentation adds a caution: this is only fully secure when the expression uses leakproof functions, and that is not enforced by the system. Do not rely on a virtual column to hide base data from a role unless you have checked the functions involved.
Migration checklist
After upgrading to PostgreSQL 18, list generated columns with the pg_attribute query above and confirm each column's kind is intentional. For new definitions, write VIRTUAL or STORED explicitly so the intent survives copy and paste between versions. Choose STORED when you need uniqueness, keys, user-defined functions, logical replication of the value, or in-place expression changes on tables with check constraints. Choose VIRTUAL for cheap derived values where storage and write amplification matter, and pair it with an expression index if you filter on it. Keep the companion example's statement list in your test suite so that an upgrade that changes any of these behaviours fails loudly rather than silently.