title | summary | toc |
---|---|---|
Use Computed Columns |
A computed column exposes data generated by an expression included in the column definition. |
true |
A computed column exposes data generated from other columns by a scalar expression included in the column definition. A stored computed column (set with the 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 regular column. A virtual computed column (set with the VIRTUAL
SQL keyword) is not stored, and the value of the scalar expression is computed during queries as needed.
Computed columns are especially useful when used with JSONB
columns or secondary indexes.
-
JSONB columns are used for storing semi-structured
JSONB
data. When the table's primary information is stored inJSONB
, it's useful to index a particular field of theJSONB
document. In particular, computed columns allow for the following use case: a two-column table with aPRIMARY KEY
column and apayload
column, whose primary key is computed as some field from thepayload
column. This alleviates the need to manually separate your primary keys from your JSON blobs. For more information, see theJSONB
example below. -
Secondary indexes can be created on computed columns, which is especially useful when a table is frequently sorted. See the secondary indexes example below.
Computed columns:
- Cannot be used to generate other computed columns.
- Cannot be a foreign key reference.
- Behave like any other column, with the exception that they cannot be written to directly.
- Are mutually exclusive with
DEFAULT
.
Virtual computed columns:
- 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 primary key,
FAMILY
definition, inCHECK
constraints, orFOREIGN KEY
constraints. - Cannot be stored in indexes.
- Can be index columns.
To define a stored computed column, use the following syntax:
column_name <type> AS (<expr>) STORED
To define a virtual computed column, use the following syntax:
column_name <type> AS (<expr>) VIRTUAL
Parameter | Description |
---|---|
column_name |
The name/identifier of the computed column. |
<type> |
The data type of the computed column. |
<expr> |
The pure scalar expression used to compute column values. Any functions marked as impure , such as now() or nextval() cannot be used. |
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. |
For compatibility with PostgresSQL, CockroachDB also supports creating computed columns with the syntax column_name <type> GENERATED ALWAYS AS (<expr>) STORED
.
{% include {{ page.version.version }}/computed-columns/simple.md %}
{% include {{ page.version.version }}/computed-columns/jsonb.md %}
{% include {{ page.version.version }}/computed-columns/virtual.md %}
{% include {{ page.version.version }}/computed-columns/secondary-index.md %}
{% include {{ page.version.version }}/computed-columns/add-computed-column.md %}
For more information, see ADD COLUMN
.
{% include {{ page.version.version }}/computed-columns/convert-computed-column.md %}