mirror of
https://github.com/postgres/postgres.git
synced 2025-12-21 05:21:08 +03:00
Virtual generated columns
This adds a new variant of generated columns that are computed on read
(like a view, unlike the existing stored generated columns, which are
computed on write, like a materialized view).
The syntax for the column definition is
... GENERATED ALWAYS AS (...) VIRTUAL
and VIRTUAL is also optional. VIRTUAL is the default rather than
STORED to match various other SQL products. (The SQL standard makes
no specification about this, but it also doesn't know about VIRTUAL or
STORED.) (Also, virtual views are the default, rather than
materialized views.)
Virtual generated columns are stored in tuples as null values. (A
very early version of this patch had the ambition to not store them at
all. But so much stuff breaks or gets confused if you have tuples
where a column in the middle is completely missing. This is a
compromise, and it still saves space over being forced to use stored
generated columns. If we ever find a way to improve this, a bit of
pg_upgrade cleverness could allow for upgrades to a newer scheme.)
The capabilities and restrictions of virtual generated columns are
mostly the same as for stored generated columns. In some cases, this
patch keeps virtual generated columns more restricted than they might
technically need to be, to keep the two kinds consistent. Some of
that could maybe be relaxed later after separate careful
considerations.
Some functionality that is currently not supported, but could possibly
be added as incremental features, some easier than others:
- index on or using a virtual column
- hence also no unique constraints on virtual columns
- extended statistics on virtual columns
- foreign-key constraints on virtual columns
- not-null constraints on virtual columns (check constraints are supported)
- ALTER TABLE / DROP EXPRESSION
- virtual column cannot have domain type
- virtual columns are not supported in logical replication
The tests in generated_virtual.sql have been copied over from
generated_stored.sql with the keyword replaced. This way we can make
sure the behavior is mostly aligned, and the differences can be
visible. Some tests for currently not supported features are
currently commented out.
Reviewed-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Tested-by: Shlok Kyal <shlok.kyal.oss@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/a368248e-69e4-40be-9c07-6c3b5880b0a6@eisentraut.org
This commit is contained in:
@@ -65,7 +65,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 AS ( <replaceable>generation_expr</replaceable> ) [ STORED | VIRTUAL ] |
|
||||
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ] |
|
||||
UNIQUE [ NULLS [ NOT ] DISTINCT ] <replaceable class="parameter">index_parameters</replaceable> |
|
||||
PRIMARY KEY <replaceable class="parameter">index_parameters</replaceable> |
|
||||
@@ -725,8 +725,9 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
|
||||
<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.
|
||||
Any generation expressions as well as the stored/virtual choice of
|
||||
copied column definitions will be copied. By default, new columns
|
||||
will be regular base columns.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
@@ -907,7 +908,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry id="sql-createtable-parms-generated-stored">
|
||||
<term><literal>GENERATED ALWAYS AS ( <replaceable>generation_expr</replaceable> ) STORED</literal><indexterm><primary>generated column</primary></indexterm></term>
|
||||
<term><literal>GENERATED ALWAYS AS ( <replaceable>generation_expr</replaceable> ) [ STORED | VIRTUAL ]</literal><indexterm><primary>generated column</primary></indexterm></term>
|
||||
<listitem>
|
||||
<para>
|
||||
This clause creates the column as a <firstterm>generated
|
||||
@@ -916,8 +917,11 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
|
||||
</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.
|
||||
When <literal>VIRTUAL</literal> is specified, the column will be
|
||||
computed when it is read, and it will not occupy any storage. When
|
||||
<literal>STORED</literal> is specified, the column will be computed on
|
||||
write and will be stored on disk. <literal>VIRTUAL</literal> is the
|
||||
default.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
@@ -2504,9 +2508,9 @@ CREATE TABLE cities_partdef
|
||||
<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.
|
||||
The options <literal>STORED</literal> and <literal>VIRTUAL</literal> are
|
||||
not standard but are also used by other SQL implementations. The SQL
|
||||
standard does not specify the storage of generated columns.
|
||||
</para>
|
||||
</refsect2>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user