STORED SQL keyword) is calculated when a row is inserted or updated, and stores the resulting value of the scalar expression in the primary index similar to a non-computed column.
A virtual computed column (set with the VIRTUAL SQL keyword) is not stored, and the value of the scalar expression is computed at query-time as needed.
Why use computed columns?
Computed columns are especially useful when used with columns or .-
JSONB columns are used for storing semi-structured
JSONBdata. When the table’s primary information is stored inJSONB, it’s useful to index a particular field of theJSONBdocument. In particular, computed columns allow for the following use case: a two-column table with aPRIMARY KEYcolumn and apayloadJSONB column, whose primary key is computed from a field of thepayloadcolumn. This alleviates the need to manually separate your primary keys from your JSON blobs. For more information, see Create a table with aJSONBcolumn and a stored computed column. - Secondary indexes can be created on computed columns, which is especially useful when a table is frequently sorted. See Create a table with a secondary index on a computed column.
Considerations
Computed columns:- Cannot be used to generate other computed columns.
- Behave like any other column, with the exception that they cannot be written to directly.
- Are mutually exclusive with and expressions.
- Are not stored in the table’s primary index.
- Are recomputed as the column data in the expression changes.
- Cannot be used as part of a
FAMILYdefinition, inCHECKconstraints, or inFOREIGN KEYconstraints. - Cannot be a reference.
- Cannot be stored in indexes.
- Can be index columns.
Define a computed column
To define a stored computed column, use the following syntax:| Parameter | Description |
|---|---|
column_name | The of the computed column. |
<type | The of the computed column. |
<expr | The used to compute column values. You cannot use functions such as now() or nextval() that are not immutable. |
STORED | (Required for stored computed columns) The computed column is stored alongside other columns. |
VIRTUAL | (Required for virtual columns) The computed column is virtual, meaning the column data is not stored in the table’s primary index. |
column_name <type> GENERATED ALWAYS AS (<expr) STORED.
Examples
Create a table with a stored computed column
In this example, let’s create a simple table with a computed column:full_name column is computed from the first_name and last_name columns without the need to define a .
Create a table with a JSONB column and a stored computed column
In this example, create a table with a JSONB column and a stored computed column:
id is computed as a field from the profile column. Additionally the age column is computed from the profile column data as well.
This example shows how add a stored computed column with a :
Create a virtual computed column using JSONB data
In this example, create a table with a JSONB column and virtual computed columns:
full_name is computed as a field from the profile column’s data. The first name and last name are concatenated and separated by a single whitespace character using the .
The virtual column birthday is parsed as a TIMESTAMP value from the profile column’s birthdate string value. The is used to parse strings in TIMESTAMP format.
Create a table with a secondary index on a computed column
In this example, create a table with a virtual computed column and an index on that column:Add a computed column to an existing table
In this example, create a table:d column is added to the table and computed from the a column divided by 2.
Convert a computed column into a regular column
You can convert a stored, computed column into a regular column by usingALTER TABLE.
In this example, create a simple table with a computed column:
full_name column is computed from the first_name and last_name columns without the need to define a . You can view the column details with the statement:
full_name) to a regular column:
Alter the formula for a computed column
To alter the formula for a computed column, you must and the column back with the new definition. Take the following table for instance:d:

