mirror of
https://github.com/postgres/postgres.git
synced 2025-11-06 07:49:08 +03:00
Generated columns
This is an SQL-standard feature that allows creating columns that are computed from expressions rather than assigned, similar to a view or materialized view but on a column basis. This implements one kind of generated column: stored (computed on write). Another kind, virtual (computed on read), is planned for the future, and some room is left for it. Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/b151f851-4019-bdb1-699e-ebab07d2f40a@2ndquadrant.com
This commit is contained in:
@@ -62,6 +62,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
|
||||
NULL |
|
||||
CHECK ( <replaceable class="parameter">expression</replaceable> ) [ NO INHERIT ] |
|
||||
DEFAULT <replaceable>default_expr</replaceable> |
|
||||
GENERATED ALWAYS AS ( <replaceable>generation_expr</replaceable> ) STORED |
|
||||
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ] |
|
||||
UNIQUE <replaceable class="parameter">index_parameters</replaceable> |
|
||||
PRIMARY KEY <replaceable class="parameter">index_parameters</replaceable> |
|
||||
@@ -83,7 +84,7 @@ class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable cl
|
||||
|
||||
<phrase>and <replaceable class="parameter">like_option</replaceable> is:</phrase>
|
||||
|
||||
{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }
|
||||
{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }
|
||||
|
||||
<phrase>and <replaceable class="parameter">partition_bound_spec</replaceable> is:</phrase>
|
||||
|
||||
@@ -627,6 +628,16 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>INCLUDING GENERATED</literal></term>
|
||||
<listitem>
|
||||
<para>
|
||||
Any generation expressions of copied column definitions will be
|
||||
copied. By default, new columns will be regular base columns.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>INCLUDING IDENTITY</literal></term>
|
||||
<listitem>
|
||||
@@ -797,6 +808,28 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>GENERATED ALWAYS AS ( <replaceable>generation_expr</replaceable> ) STORED</literal><indexterm><primary>generated column</primary></indexterm></term>
|
||||
<listitem>
|
||||
<para>
|
||||
This clause creates the column as a <firstterm>generated
|
||||
column</firstterm>. The column cannot be written to, and when read it
|
||||
will be computed from the specified expression.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The keyword <literal>STORED</literal> is required to signify that the
|
||||
column will be computed on write and will be stored on disk. default.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The generation expression can refer to other columns in the table, but
|
||||
not other generated columns. Any functions and operators used must be
|
||||
immutable. References to other tables are not allowed.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ]</literal></term>
|
||||
<listitem>
|
||||
@@ -2028,6 +2061,16 @@ CREATE TABLE cities_partdef
|
||||
</para>
|
||||
</refsect2>
|
||||
|
||||
<refsect2>
|
||||
<title>Generated Columns</title>
|
||||
|
||||
<para>
|
||||
The option <literal>STORED</literal> is not standard but is also used by
|
||||
other SQL implementations. The SQL standard does not specify the storage
|
||||
of generated columns.
|
||||
</para>
|
||||
</refsect2>
|
||||
|
||||
<refsect2>
|
||||
<title><literal>LIKE</literal> Clause</title>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user