Sub topic 3
Which PostgreSQL versions support what
This chapter assumes you know basic SQL (CREATE TABLE, ALTER TABLE, SELECT), common data types, what an index is, and the difference between a table and a view. The examples use this table:
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
);Which PostgreSQL versions support what
Generated columns arrived in stages. Check your server version before you rely on a feature.
| Feature | First version |
|---|---|
| `GENERATED ALWAYS AS (...) STORED` | 12 |
| `ALTER COLUMN ... DROP EXPRESSION` | 13 |
| `ALTER COLUMN ... SET EXPRESSION` | 17 |
| `GENERATED ALWAYS AS (...) VIRTUAL` | 18 |
Run SHOW server_version; to see your version. Always write STORED or VIRTUAL explicitly so the table definition does not depend on a default.
Indexing a generated column
A stored generated column holds a real value on disk, so you can index it like any other column:
CREATE INDEX people_full_name_idx ON people (full_name);
SELECT id FROM people WHERE full_name = 'Ada Lovelace';The index lets PostgreSQL find matching rows without scanning the whole table. In PostgreSQL 18, you cannot index a virtual column directly, so use a stored column or an expression index for that case.
Changing generated columns on an existing table
You can add a stored generated column to an existing table. PostgreSQL rewrites the table to compute the values, so schedule this for a quiet period on large tables.
ALTER TABLE people
ADD COLUMN initials text
GENERATED ALWAYS AS (left(first_name, 1) || left(last_name, 1)) STORED;
-- PostgreSQL 13+: keep the current values, but allow direct writes
ALTER TABLE people ALTER COLUMN full_name DROP EXPRESSION;
-- PostgreSQL 17+: change the expression of a stored generated column
ALTER TABLE people
ALTER COLUMN initials SET EXPRESSION AS (upper(left(first_name, 1) || left(last_name, 1)));You cannot turn an ordinary column into a generated column. Add a new generated column, copy the data, and drop the old column.
Limitations and replication notes
- A generation expression cannot refer to another generated column.
- A generated column cannot have a default value, and you cannot write to it directly.
- Logical replication does not send generated columns by default. PostgreSQL 18 added a publication option to include them.
- Physical (streaming) replication copies the stored values as part of the data files.
When to use something else
- View: Use a view when you need the derived value in queries but do not want to store it.
- Expression index: Use
CREATE INDEX ... ON people (lower(last_name))when you only need faster searches on a derived value. - Trigger: Use a trigger when the logic is too complex for a generation expression or needs to read other tables.
Exercise
On PostgreSQL 16, can you run ALTER TABLE people ALTER COLUMN initials SET EXPRESSION AS (...)?
Show answer
No. SET EXPRESSION was added in PostgreSQL 17. On PostgreSQL 16, drop the column and add a new generated column with the new expression.