GENERATED Column

Last updated: 2026-11-04

GENERATED Column

A generated column hands the computation to the database: the value follows from an expression, so the application need not calculate it and the stored value can never drift from its inputs. Generated columns come as STORED (materialized) or VIRTUAL (computed on demand), with virtual columns newly supported in PostgreSQL 18. The column must be omitted when writing.

Kindconstraint
Example LanguageSQL

📝 Syntax

`column_name type GENERATED ALWAYS AS (expression) STORED`
Virtual columns (PostgreSQL 18+): `GENERATED ALWAYS AS (expression) VIRTUAL`

⚙️ Parameters

expressionThe expression producing the column's value. It may reference other columns of the same row and must be immutable.
STORED / VIRTUALSTORED materializes the computed value on disk (PostgreSQL 12+); VIRTUAL stores only the expression and computes on demand (introduced in PostgreSQL 18).

Returns:A generated column returns nothing by itself — the database computes its value. Writing to it explicitly raises `cannot insert a non-DEFAULT value into column "..."`.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

SELECT 'stored total = quantity * unit_price' AS step,
       (SELECT total::TEXT FROM invoices WHERE invoice_id = 1) AS result
UNION ALL SELECT 'write to generated column',
       (SELECT pg_try('INSERT INTO invoices VALUES (2, 2, 5.00, 10.00)'))
UNION ALL SELECT 'rows stored',
       (SELECT count(*)::TEXT FROM invoices)
 step                                 | result
--------------------------------------+-------------------------------------------------------
 stored total = quantity * unit_price | 59.97
 write to generated column            | cannot insert a non-DEFAULT value into column "total"
 rows stored                          | 1
(3 rows)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QHow is a generated column different from a view?
AA generated column belongs to the table: it has its own storage and type, can be indexed, referenced by foreign keys and queried directly. A view is a virtual query re-executed on every access and cannot be indexed directly without materializing it.
QAre PostgreSQL 18's virtual columns worth using?
AThey suit expensive derivations that are read rarely, such as a complex expression used only in occasional reports. For frequently read columns STORED is usually better, since it moves the cost to write time.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏