Sub topic 8
Which PostgreSQL Versions Support What
This chapter assumes you know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. It also assumes you understand that an index stores extra data to make searches faster.
PostgreSQL has not supported every kind of generated column in every release. The important difference is between stored and virtual generated columns:
- A stored generated column computes its value when a row is inserted or updated, then saves the value in the table.
- A virtual generated column computes its value when a query reads the row. It does not save the generated value as table storage.
Version timeline
Stored generated columns became available in PostgreSQL 12. Virtual generated columns became available in PostgreSQL 18.
| PostgreSQL version | Stored generated columns | Virtual generated columns |
|---|---|---|
| 11 and earlier | Not supported | Not supported |
| 12 through 17 | Supported | Not supported |
| 18 and later | Supported | Supported |
Check the server version before using virtual columns:
SELECT current_setting('server_version');
SHOW server_version;A client version is not enough. Your psql program might be newer than the PostgreSQL server to which it connects. The server decides which SQL features are available.
PostgreSQL 12 through 17: use stored columns
On PostgreSQL 12 through 17, use STORED explicitly:
CREATE TABLE orders (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
quantity integer NOT NULL,
unit_price numeric(10, 2) NOT NULL,
total numeric(12, 2)
GENERATED ALWAYS AS (quantity * unit_price) STORED
);The expression uses values from the same row. PostgreSQL calculates total during an insert or an update that changes quantity or unit_price.
Applications cannot provide an ordinary value for total:
INSERT INTO orders (quantity, unit_price, total)
VALUES (2, 9.99, 100.00);This fails because total is generated. Use DEFAULT if you need to include the column in a general-purpose insert statement:
INSERT INTO orders (quantity, unit_price, total)
VALUES (2, 9.99, DEFAULT);PostgreSQL 18 and later: virtual columns
PostgreSQL 18 adds the VIRTUAL option:
CREATE TABLE rectangles (
width integer NOT NULL,
height integer NOT NULL,
area integer
GENERATED ALWAYS AS (width * height) VIRTUAL
);area is computed when PostgreSQL reads it. This can reduce table storage, but the expression work happens during reads. A virtual column is useful when the derived value is inexpensive to calculate and does not need to be stored or indexed like a normal stored value.
Virtual columns have stricter expression rules than stored columns. In particular, a virtual generation expression must use immutable built-in functions. An immutable function always returns the same result for the same inputs. Expressions involving the current time, session settings, or changing database state do not meet this rule.
Writing portable schema changes
If your application supports several PostgreSQL versions, do not deploy VIRTUAL unless every target server is PostgreSQL 18 or later. A migration that works on a development server can fail on an older production server.
You can inspect the version in a migration tool and choose a compatible definition:
DO $$
BEGIN
IF current_setting('server_version_num')::integer < 120000 THEN
RAISE EXCEPTION
'This schema requires PostgreSQL 12 or later';
END IF;
END
$$;This check allows stored generated columns on PostgreSQL 12 and later. A migration that creates a virtual column should require version number 180000 or higher.
Practical compatibility choices
Choose the newest feature that all supported servers understand:
- Use a stored generated column when you need PostgreSQL 12–17 compatibility.
- Use a virtual generated column only when PostgreSQL 18 or later is guaranteed.
- Use a view or an expression in a query when the schema must work on older PostgreSQL releases.
- Use an expression index when you need fast searches but do not need the derived value exposed as a table column.
The version boundary affects more than table creation. Check your migration framework, schema-diff tool, backup tools, and replicas for PostgreSQL 18 support before deploying virtual columns.
Exercise
A project supports PostgreSQL 14 and PostgreSQL 18. Which generated-column kind can its shared schema safely use?
Show answer
A stored generated column. A virtual generated column would fail on the PostgreSQL 14 servers unless the project uses separate, version-aware migrations.