PostgreSQL generated columns
Answer: A generated column is a special column that is always computed from other columns. (S1) You cannot write to it directly, and the expression must be immutable and refer only to the current row.
CREATE TABLE people (
height_cm numeric,
height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
);
INSERT INTO people (height_cm) VALUES (180);
Rules
- In INSERT or UPDATE you may only give
DEFAULTfor it. - Only immutable functions; no subqueries.
- It cannot reference another generated column, or system columns other than
tableoid. - It cannot have a column default or identity definition.
Stored vs virtual
| | Stored | Virtual | |---|---|---| | Computed | on write | on read |
Use VIRTUAL or STORED explicitly to make the choice clear.
Claims
- id: C1 claim: A generated column is virtual by default. source: S1 evidence: "A generated column is by default of the virtual kind."
- id: C2 claim: A generated column cannot be written to directly. source: S1 evidence: "A generated column cannot be written to directly."
- id: C3 claim: A generation expression cannot reference another generated column. source: S1 evidence: "A generation expression cannot reference another generated column."
- id: C4 claim: CREATE TABLE syntax includes GENERATED ALWAYS AS with STORED or VIRTUAL. source: S2 evidence: "GENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] |"
Sources
- [S1] PostgreSQL: Documentation: 18: 5.4. Generated Columns. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/ddl-generated-columns.html . License: PostgreSQL License. Accessed 2026-11-04.
- [S2] PostgreSQL: Documentation: 18: CREATE TABLE. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-createtable.html . License: PostgreSQL License. Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 5.4. Generated Columns, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: CREATE TABLE, PostgreSQL License.
Cite this
iinventedlight. 2026. "PostgreSQL generated columns." iinventedlight Library, iil:note:j2e5vwks, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-generated-columns/. License: CC-BY-4.0.
@misc{iil_note_j2e5vwks,
author = {{iinventedlight}},
title = {{PostgreSQL generated columns}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/postgres-generated-columns/},
note = {Permanent ID iil:note:j2e5vwks. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-generated-columns.md.