Sub topic 8
Which PostgreSQL versions support what
This chapter assumes you know how to define a stored or virtual generated column and why the expression must be immutable. Its subject is the server version. Generated columns arrived in stages across several major releases, so the same DDL can work, fail, or mean something different depending on the server. Before you write migrations or rely on a feature, check these differences.
The short history
Five releases matter for generated columns:
| Version | What changed |
|---|---|
| 11 and earlier | No generated columns. You needed triggers, views, or application code. |
| 12 | `GENERATED ALWAYS AS (expr) STORED` is introduced. Only stored columns exist. |
| 13 | `ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION` turns a stored generated column into an ordinary column. |
| 17 | `ALTER TABLE ... ALTER COLUMN ... SET EXPRESSION AS (expr)` changes the expression of an existing generated column. |
| 18 | `VIRTUAL` columns are added and become the default. Built-in logical replication can publish stored generated columns. |
The sections below cover each step and what it means in practice.
PostgreSQL 12 to 17: stored only
In these versions the STORED keyword is mandatory. A generated column without it is a syntax error, and VIRTUAL is not recognized at all.
-- Works on PostgreSQL 12 and later
CREATE TABLE people (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL,
full_name text GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED
);Everything you learned about stored columns in the earlier chapters applies from version 12 onward:
- The value is computed when the row is written and takes space on disk.
- The expression must be immutable.
- You cannot write a non-
DEFAULTvalue into the column. - You can index the column.
Version 12 also fixed some limits that stay in place in later versions. A generation expression can only reference ordinary columns of the same table, not other generated columns, and it cannot use subqueries. A generated column cannot have a DEFAULT, and it cannot be part of a partition key.
Version 13: DROP EXPRESSION
In version 12, the only way to make a stored generated column into a normal column was to drop it and add a new one, which lost the data unless you copied it first. Version 13 added a metadata-only operation:
ALTER TABLE people ALTER COLUMN full_name DROP EXPRESSION;The column keeps its current values and becomes writable. There is no reverse command. To turn an ordinary column back into a generated one, you add a new generated column, which rewrites the table, and then retire the old column.
Version 17: SET EXPRESSION
Until version 17, changing the formula meant dropping the column and re-adding it, along with any indexes or dependent objects. Version 17 added:
ALTER TABLE people
ALTER COLUMN full_name SET EXPRESSION AS (last_name || ', ' || first_name);For a stored column this recomputes every row, so it rewrites the table and takes an ACCESS EXCLUSIVE lock for the duration. Plan it like any other table rewrite. Indexes on the column are rebuilt as part of the operation.
PostgreSQL 18: virtual columns and a new default
Version 18 adds the VIRTUAL kind, which stores nothing and computes the value on every read.
-- PostgreSQL 18 and later only
CREATE TABLE rectangles (
width integer NOT NULL,
height integer NOT NULL,
area integer GENERATED ALWAYS AS (width * height) VIRTUAL
);On version 17 or earlier, this statement fails with a syntax error at VIRTUAL.
The default changed
In version 18, if you write a generated column without either keyword, you get a virtual column:
-- PostgreSQL 12 to 17: syntax error
-- PostgreSQL 18 and later: creates a VIRTUAL column
ALTER TABLE rectangles
ADD COLUMN perimeter integer GENERATED ALWAYS AS (2 * (width + height));Versions 12 to 17 reject this, so no old script can contain it. The risk runs the other way. If you write new DDL without a keyword on version 18 and later run it against a stored-only environment, it fails. If you copy a pattern from a version 18 tutorial and expect stored behavior, you get a virtual column instead. Always write STORED or VIRTUAL explicitly so the DDL means the same thing on every server that accepts it.
What virtual columns cannot do in version 18
The virtual kind has more restrictions than stored. Check these before you choose it:
- You cannot create an index on a virtual column, and you cannot use one in an index expression or predicate.
- You cannot create a unique constraint, primary key, or foreign key on a virtual column.
- The expression can only use built-in functions and types. User-defined functions and types are rejected.
- Virtual columns are not published through built-in logical replication.
Stored columns do not have these limits. The trade-off is covered in the chapter on choosing between stored and virtual. The point here is that a feature can exist in version 18 and still be unavailable for a given use case.
Cheap adds and changes
Adding a virtual column does not rewrite the table, because there is nothing to compute or store. Adding a stored column with ADD COLUMN ... STORED rewrites the table, because every existing row needs a value. The same logic applies to SET EXPRESSION in version 18. On a virtual column it only changes metadata, while on a stored column it still rewrites.
Replication of stored columns
Before version 18, built-in logical replication did not send stored generated column values. The subscriber had to compute them itself or have them as a plain column. Version 18 adds a publication option that includes stored generated columns:
CREATE PUBLICATION people_pub FOR TABLE people
WITH (publish_generated_columns = stored);The default is to not publish them. Virtual columns are never published, and the subscriber still computes or stores what it needs. On versions before 18 this option does not exist and the statement fails.
No direct conversion between kinds
Neither kind can be switched to the other in place. There is no SET STORED or SET VIRTUAL. To change a column from virtual to stored, add a new stored column with the same expression, switch your queries to it, then drop the virtual one. To go the other way, add the virtual column, drop the stored one, and rebuild anything that depended on it.
If a stored column is no longer needed as generated, DROP EXPRESSION still works on it from version 13 onward. It gives you an ordinary column with the values preserved.
Checking what a server supports
Do not guess the version from memory of a deployment. Ask the server.
SELECT current_setting('server_version_num')::int AS version_num,
current_setting('server_version_num')::int >= 180000 AS has_virtual;The value of server_version_num is a single integer. For version 18.0 it is 180000, and for 17.4 it is 170004. Comparing integers is more reliable than parsing the text from version().
To see what an existing table actually uses, look at pg_attribute:
SELECT attname,
CASE attgenerated
WHEN 's' THEN 'stored'
WHEN 'v' THEN 'virtual'
END AS kind
FROM pg_attribute
WHERE attrelid = 'people'::regclass
AND attnum > 0
AND NOT attisdropped
AND attgenerated <> '';The attgenerated column is an empty string for ordinary columns, s for stored, and v for virtual. The 'v' value only occurs on version 18 and later.
Version gotchas
A few failure modes come up in practice.
- Dump and restore across versions. A dump taken from version 18 that contains
VIRTUALcolumns cannot be restored on 17 or earlier. A dump from version 12 to 17 restores fine on 18.pg_upgradekeeps stored columns as stored. - Mixed fleets. If your development database is on 18 and production is on 16, a migration that uses
VIRTUAL, a bareGENERATED ALWAYS AS, orSET EXPRESSIONpasses locally and fails in production. Run migration tests on the oldest version you deploy to. - Managed services. Hosted PostgreSQL services often lag behind the latest major release. Check the version your provider runs, not the version in the documentation you are reading.
- Replication topology. Replicating a table with generated columns between servers on different major versions needs care. A subscriber on version 17 cannot use the version 18 publication option on the publisher side to expect anything useful from it, and the subscriber's table definition has to match what is sent.
- Version 11 and earlier. There are no generated columns at all, so anything that must run there still needs a trigger. The alternatives chapter covers that.
A decision table
| You need | Minimum version |
|---|---|
| A stored generated column | 12 |
| Turning a stored column into an ordinary one without losing data | 13 |
| Changing a generated column's expression in place | 17 |
| A virtual generated column | 18 |
| Publishing stored generated columns with built-in logical replication | 18 |
Exercise
Your team develops on PostgreSQL 18 and production runs PostgreSQL 16. A developer writes ALTER TABLE orders ADD COLUMN total_with_tax numeric GENERATED ALWAYS AS (total * 1.2); and the migration succeeds locally. What happens in production, what is the problem, and how should the statement be written so it behaves the same everywhere?
Show answer
On PostgreSQL 16 the statement fails with a syntax error, because a generated column must end with the STORED keyword there. On 18 it succeeded only because virtual is now the default, so the local column is virtual while the intended behavior may have been stored. Write the kind explicitly. Use STORED to run on 12 and later. Use VIRTUAL only if every environment is on 18 or later. Test migrations on the oldest server version you deploy to. Note also that total * 1.2 is immutable only if total is a plain numeric column, so the expression itself is fine.
Exercise
A stored generated column full_name exists on PostgreSQL 15. You want to change its expression without dropping the column. Which options do you have, and what changes if you upgrade to 17?
Show answer
On 15 there is no SET EXPRESSION. You can run ALTER COLUMN full_name DROP EXPRESSION (available since 13) to make it an ordinary column, but that removes the automatic computation. To keep a generated column you must add a new stored column with the new expression, move queries and indexes to it, and drop the old one. On 17 you can run ALTER TABLE ... ALTER COLUMN full_name SET EXPRESSION AS (...). For a stored column this rewrites the table under an ACCESS EXCLUSIVE lock and rebuilds its indexes, so schedule it accordingly.
Exercise
Write a query that reports, for the table rectangles, which columns are generated and whether each is stored or virtual.
Show answer
SELECT attname, CASE attgenerated WHEN 's' THEN 'stored' WHEN 'v' THEN 'virtual' END AS kind FROM pg_attribute WHERE attrelid = 'rectangles'::regclass AND attnum > 0 AND NOT attisdropped AND attgenerated <> '';