← Back to Glossary

Generated column

A generated column is a table column whose value is automatically computed from an expression involving other columns, rather than being stored directly by INSERT or UPDATE statements.

Overview

A generated (or computed) column is defined by an expression instead of a plain data type, and its value is derived automatically from other columns in the same row. Applications cannot insert or update a generated column's value directly — the database computes it.

Copy code

CREATE TABLE orders ( quantity INTEGER, unit_price DECIMAL(10,2), total_price DECIMAL(10,2) GENERATED ALWAYS AS (quantity * unit_price) );

Generated columns are useful for keeping derived values (totals, normalized text, extracted JSON fields) consistent and in sync with their source columns automatically, without relying on application code or triggers to recompute them.

Virtual vs stored

SQL databases that support generated columns typically offer two flavors: virtual (computed on the fly whenever the column is read, not stored on disk) and stored (computed once and persisted to disk, refreshed whenever the source columns change, trading storage for read speed).

DuckDB specifics

DuckDB supports generated columns with the syntax column_name [type] GENERATED ALWAYS AS (expression) [VIRTUAL]:

Copy code

CREATE TABLE t1 (x FLOAT, two_x AS (2 * x)); -- type inferred, virtual CREATE TABLE t2 (x FLOAT, two_x FLOAT GENERATED ALWAYS AS (2 * x) VIRTUAL); -- fully explicit

As of the current DuckDB version, only the VIRTUAL variant is implemented — generated column values are computed from the expression each time the column is referenced, rather than being persisted to disk; STORED generated columns are not yet supported. A generated column's expression can reference other regular columns or even other generated columns defined earlier in the same table.

Related terms

FAQS

No. Generated columns derive their value from an expression over other columns, so INSERT and UPDATE statements cannot set them directly — the database computes the value automatically.

Currently only VIRTUAL generated columns are supported in DuckDB, meaning the value is recomputed from the expression each time it's read rather than persisted to disk. STORED generated columns are not yet implemented.