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
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
Modified Files
--------------
contrib/pageinspect/expected/page.out | 37 +
contrib/pageinspect/sql/page.sql | 19 +
contrib/postgres_fdw/expected/postgres_fdw.out | 81 +-
contrib/postgres_fdw/sql/postgres_fdw.sql | 9 +-
doc/src/sgml/catalogs.sgml | 6 +-
doc/src/sgml/ddl.sgml | 46 +-
doc/src/sgml/ref/alter_table.sgml | 15 +-
doc/src/sgml/ref/create_foreign_table.sgml | 11 +-
doc/src/sgml/ref/create_table.sgml | 22 +-
doc/src/sgml/trigger.sgml | 4 +
src/backend/access/common/tupdesc.c | 3 +
src/backend/access/heap/heapam_handler.c | 2 +
src/backend/catalog/heap.c | 23 +-
src/backend/catalog/pg_publication.c | 10 +-
src/backend/commands/analyze.c | 4 +
src/backend/commands/indexcmds.c | 33 +-
src/backend/commands/publicationcmds.c | 23 +-
src/backend/commands/statscmds.c | 20 +-
src/backend/commands/tablecmds.c | 196 +++-
src/backend/commands/trigger.c | 45 +-
src/backend/executor/execExprInterp.c | 4 +
src/backend/executor/execMain.c | 5 +-
src/backend/executor/execUtils.c | 4 +-
src/backend/executor/nodeModifyTable.c | 41 +-
src/backend/parser/gram.y | 15 +-
src/backend/parser/parse_relation.c | 6 +-
src/backend/parser/parse_utilcmd.c | 16 +-
src/backend/replication/pgoutput/pgoutput.c | 3 +-
src/backend/rewrite/rewriteHandler.c | 133 ++-
src/backend/utils/cache/relcache.c | 3 +
src/bin/pg_dump/pg_dump.c | 3 +
src/bin/pg_dump/t/002_pg_dump.pl | 6 +-
src/bin/psql/describe.c | 6 +
src/include/access/tupdesc.h | 1 +
src/include/catalog/catversion.h | 2 +-
src/include/catalog/heap.h | 1 +
src/include/catalog/pg_attribute.h | 1 +
src/include/executor/nodeModifyTable.h | 6 +-
src/include/nodes/execnodes.h | 2 +
src/include/nodes/parsenodes.h | 1 +
src/include/parser/kwlist.h | 1 +
src/include/rewrite/rewriteHandler.h | 2 +
src/pl/plperl/expected/plperl_trigger.out | 7 +-
src/pl/plperl/plperl.c | 3 +
src/pl/plperl/sql/plperl_trigger.sql | 3 +-
src/pl/plpython/expected/plpython_trigger.out | 7 +-
src/pl/plpython/plpy_typeio.c | 3 +
src/pl/plpython/sql/plpython_trigger.sql | 3 +-
src/pl/tcl/expected/pltcl_trigger.out | 19 +-
src/pl/tcl/pltcl.c | 3 +
src/pl/tcl/sql/pltcl_trigger.sql | 3 +-
src/test/regress/expected/collate.icu.utf8.out | 20 +
src/test/regress/expected/create_table_like.out | 23 +-
src/test/regress/expected/fast_default.out | 12 +
src/test/regress/expected/generated_stored.out | 21 +-
src/test/regress/expected/generated_virtual.out | 1400 +++++++++++++++++++++++
src/test/regress/expected/publication.out | 45 +-
src/test/regress/expected/rowsecurity.out | 29 +
src/test/regress/expected/stats_ext.out | 23 +
src/test/regress/parallel_schedule | 2 +-
src/test/regress/sql/collate.icu.utf8.sql | 15 +
src/test/regress/sql/create_table_like.sql | 2 +-
src/test/regress/sql/fast_default.sql | 11 +
src/test/regress/sql/generated_stored.sql | 12 +-
src/test/regress/sql/generated_virtual.sql | 734 ++++++++++++
src/test/regress/sql/publication.sql | 43 +-
src/test/regress/sql/rowsecurity.sql | 27 +
src/test/regress/sql/stats_ext.sql | 14 +
src/test/subscription/t/011_generated.pl | 39 +-
src/test/subscription/t/028_row_filter.pl | 38 +-
src/test/subscription/t/031_column_list.pl | 2 +-
71 files changed, 3225 insertions(+), 209 deletions(-)