What Is a Generated Column? Keep Repeated Calculations Consistent
A generated column is a database column whose value is computed from other columns in the same row. For example, a database can recompute `line_total` whenever an order line's quantity or unit price changes. This keeps one calculation rule in the database instead of duplicating it across queries and applications.
What Is a Generated Column? Keep Repeated Calculations Consistent
A generated column is a database column whose value is computed from other columns in the same row. For example, a database can recompute line_total whenever an order line's quantity or unit price changes. This keeps one calculation rule in the database instead of duplicating it across queries and applications.
How is it different from a default?
A default is calculated once when a row is inserted without an input value. It is not recalculated when other columns later change. A generated column gets its value from a defined expression, so its result reflects changes to its base columns. You cannot overwrite it with an arbitrary value; when inserting or updating, a DBMS may allow only DEFAULT for that column.
A generated column also differs from a view. A view adds a calculation to query results without adding a column to a table. A generated column is defined as a table column and can be read like an ordinary column; depending on the DBMS, it may also work with constraints and indexes. Storage and feature support vary by product and version.
Example: calculate an order-line total
This example calculates a subtotal from quantity and the unit price stored on the same order-line row.
CREATE TABLE order_lines (
order_line_id bigint PRIMARY KEY,
quantity integer NOT NULL,
unit_price numeric(10, 2) NOT NULL,
line_total numeric(12, 2)
GENERATED ALWAYS AS (quantity * unit_price) STORED
);The application writes quantity and unit_price, not line_total. When either input changes, the database computes the subtotal again. This SQL is illustrative and was not executed. A real design should separately specify rules such as whether zero quantity or negative prices are allowed, how rounding works, and which currency unit applies.
Here, unit_price is assumed to be the price captured on the order line at purchase time. A generated expression cannot query another table, so it cannot look up the current price in products to calculate an old order amount. Store the purchase-time price on the order line and calculate the subtotal within that row; otherwise a later product-price change could alter the apparent historical amount.
What is the difference between VIRTUAL and STORED?
| Mode | When calculated | Storage | When it may fit |
|---|---|---|---|
VIRTUAL | When the value is read | The generated value itself is not stored | The expression is inexpensive or saving storage matters |
STORED | When a row is inserted or updated | The result is stored with the row | Repeated read-time computation should be reduced and storing the result is useful |
A stored column can reduce calculation work during reads, but uses storage and performs the calculation when base values are written. A virtual column uses no separate storage for its value, but evaluates the expression on reads. Index support and performance depend on the DBMS, so check actual queries and execution plans.
PostgreSQL 18 and MySQL 8.4 support both virtual and stored generated columns and default to VIRTUAL. PostgreSQL 18 introduced virtual generated columns; earlier PostgreSQL versions support only the stored form. Check the target version's documentation before a migration. MySQL 8.4 InnoDB supports secondary indexes on virtual columns, and stored columns can also be indexed.
What limits can apply to the expression?
Generated columns generally need to be computed from values in the same row. PostgreSQL permits only immutable functions in the expression and does not allow references to other rows, tables, or subqueries. MySQL 8.4 likewise permits only allowed expressions, such as deterministic built-in functions and operators; subqueries and nondeterministic functions such as the current-time function are not allowed. The result must be reproducible from the row's inputs.
That is why a generated column is not a way to automatically track the current time, the latest price in another table, or an aggregate over other rows. Use a query, view, summary table, or another data model suited to the business rule. Consult the documentation for the DBMS in use for the exact expression rules and restrictions involving foreign keys or triggers.
When should you choose a generated column?
Consider one when the same calculation using columns from one row is repeated in multiple queries and should follow one database rule.
A query expression or view may be simpler when the calculation is needed in just one read path.
A default or application-supplied value is more suitable when a value should be set once at insertion and remain independent of later changes to its inputs.
Do not use a generated column for values that depend on the current time or another table's state.
Choose `VIRTUAL` or `STORED` by comparing storage, read and write costs, index support, and the DBMS version.
Sources and scope
[PostgreSQL 18: Generated Columns](https://www.postgresql.org/docs/18/ddl-generated-columns.html)
[PostgreSQL 18 release notes: virtual generated columns](https://www.postgresql.org/docs/18/release-18.html)
[MySQL 8.4: CREATE TABLE and Generated Columns](https://dev.mysql.com/doc/refman/8.4/en/create-table-generated-columns.html)
[MySQL 8.4: Indexes on Generated Columns and Optimizer Use](https://dev.mysql.com/doc/refman/8.4/en/generated-column-index-optimizations.html)
Documentation was checked on 2026-11-23. The SQL is an illustrative example and was not executed. Storage behavior, expression restrictions, indexes, and support for other features vary by DBMS and version.