Diagnostic Queries
Symptoms
A DEFAULT expression referenced another column of the same table. Column defaults are evaluated without a row context, so they cannot read other columns. PostgreSQL raises SQLSTATE 42P10 (invalid_column_reference).
- Triggered at
CREATE TABLE/ALTER TABLEdefining the default. - People often hit this trying to default one column from another.
- Generated columns or triggers are the correct alternative.
What the server log shows
ERROR: cannot use column reference in DEFAULT expression
Why PostgreSQL raises this — what the manual says
Section 5.2 Default Values:
“The default value can be an expression, which will be evaluated whenever the default value is inserted (not when the table is created).”
A column default is computed in isolation when no explicit value is supplied; at that moment the rest of the row is not available. Referencing another column is therefore disallowed and reported as 42P10.
Common causes
- Trying to default a column to a value derived from a sibling column.
- Porting a schema where another engine allowed inter-column defaults.
How to fix it
- Use a GENERATED column:
total numeric GENERATED ALWAYS AS (qty * price) STORED. - Compute the value in a BEFORE INSERT/UPDATE trigger.
- Set the value explicitly in the INSERT statement.
Related & next steps
Reference: PostgreSQL 18 Section 5.4 “Default Values”.
Thanks — noted. This helps keep the database accurate.