---
source: https://iinventedlight.com/library/notes/postgres-generated-columns.md
title: "PostgreSQL generated columns"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PostgreSQL License; prose restates cited source lines"
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: PostgreSQL"
tags: ['postgresql', 'sql', 'ddl', 'generated-columns']
sources:
  - id: S1
    title: "PostgreSQL: Documentation: 18: 5.4. Generated Columns"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/ddl-generated-columns.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
  - id: S2
    title: "PostgreSQL: Documentation: 18: CREATE TABLE"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-createtable.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
---
# 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.

```sql
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 `DEFAULT` for 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).
