// dialect comparison

Generated / computed columns

Columns the engine computes from other columns, stored on write or computed on read.

Filed under Tables & schema

The verdict

The feature exists everywhere now. The portability trap is storage: Oracle has no stored generated columns, PostgreSQL had no virtual ones before 18, and only SQL Server, MySQL, SQLite and PostgreSQL 18 let you pick per column.

Where they differ

Generated columns remove a class of stale-data bugs: the engine derives the value, so nobody can forget to update it. The real choice is storage. VIRTUAL computes the value on every read; STORED (PERSISTED in SQL Server) computes it on write and keeps it on disk. Oracle offers only virtual, and PostgreSQL only stored before version 18.

How each engine writes it

SQL Server

CREATE TABLE products (
  price DECIMAL(10,2), qty INT,
  total AS price * qty PERSISTED
);

Computed on read unless PERSISTED. Either kind can be indexed if the expression is deterministic; a non-persisted one must also be precise (no float).

PostgreSQL

CREATE TABLE products (
  price NUMERIC(10,2), qty INT,
  total NUMERIC(10,2) GENERATED ALWAYS AS (price * qty) STORED
);

12+. PostgreSQL 18 adds VIRTUAL and makes it the default when neither keyword is given. Virtual columns can't be indexed yet.

MySQL

CREATE TABLE products (
  price DECIMAL(10,2), qty INT,
  total DECIMAL(10,2) AS (price * qty) VIRTUAL
);

5.7+. VIRTUAL (the default) computes on read, STORED on write. InnoDB can index either; an index on a virtual column stores the computed values.

SQLite

CREATE TABLE products (
  price REAL, qty INT,
  total REAL GENERATED ALWAYS AS (price * qty) VIRTUAL
);

3.31+. VIRTUAL (the default) or STORED. Expressions must be deterministic, and ALTER TABLE ADD COLUMN can only add VIRTUAL ones.

Oracle

CREATE TABLE products (
  price NUMBER(10,2), qty INT,
  total AS (price * qty) VIRTUAL
);

Virtual columns since 11g: computed on read, with no stored option. They can be indexed, constrained and used as partition keys.

Questions that come up

VIRTUAL or STORED?

VIRTUAL costs nothing on write and no disk space, but recomputes on every read; STORED costs write time and space and reads like a normal column. Pick STORED for expensive expressions read often, or when the engine can only index stored values (PostgreSQL).

Can a generated column reference other generated columns?

In MySQL and SQLite, yes, as long as there is no cycle (MySQL also requires the referenced column to be defined earlier). PostgreSQL, SQL Server and Oracle refuse; repeat the expression instead.

Are generated columns good for indexing expressions?

Yes. Index the generated column and queries that filter on it use the index. MySQL and SQL Server go further and can match a query that spells out the same expression to an indexed generated column. In PostgreSQL and Oracle a plain expression index does the same job without the extra column.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.