A generated column is a special column that is always computed from other columns. Thus, it is for columns what a view is for tables. There are two kinds of generated columns: stored and virtual. A stored generated column is computed when it is written (inserted or updated) and occupies storage as if it were a normal column. A virtual generated column occupies no storage and is computed when it is read. Thus, a virtual generated column is similar to a view and a stored generated column is similar to a materialized view (except that it is always updated automatically). PostgreSQL currently implements only stored generated columns.
To create a generated column, use the GENERATED ALWAYS AS
clause in CREATE TABLE
, for example:
CREATE TABLE people (
...,
height_cm numeric,
height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
);
The keyword STORED
must be specified to choose the stored kind of generated column. See CREATE TABLE for more details.
A generated column cannot be written to directly. In INSERT
or UPDATE
commands, a value cannot be specified for a generated column, but the keyword DEFAULT
may be specified.
Consider the differences between a column with a default and a generated column. The column default is evaluated once when the row is first inserted if no other value was provided; a generated column is updated whenever the row changes and cannot be overridden. A column default may not refer to other columns of the table; a generation expression would normally do so. A column default can use volatile functions, for example random()
or functions referring to the current time; this is not allowed for generated columns.
Several restrictions apply to the definition of generated columns and tables involving generated columns:
The generation expression can only use immutable functions and cannot use subqueries or reference anything other than the current row in any way.
A generation expression cannot reference another generated column.
A generation expression cannot reference a system column, except tableoid
.
A generated column cannot have a column default or an identity definition.
A generated column cannot be part of a partition key.
Foreign tables can have generated columns. See CREATE FOREIGN TABLE for details.
For inheritance:
If a parent column is a generated column, a child column must also be a generated column using the same expression. In the definition of the child column, leave off the GENERATED
clause, as it will be copied from the parent.
In case of multiple inheritance, if one parent column is a generated column, then all parent columns must be generated columns and with the same expression.
If a parent column is not a generated column, a child column may be defined to be a generated column or not.
Additional considerations apply to the use of generated columns.
Generated columns maintain access privileges separately from their underlying base columns. So, it is possible to arrange it so that a particular role can read from a generated column but not from the underlying base columns.
Generated columns are, conceptually, updated after BEFORE
triggers have run. Therefore, changes made to base columns in a BEFORE
trigger will be reflected in generated columns. But conversely, it is not allowed to access generated columns in BEFORE
triggers.
Generated columns are skipped for logical replication.
杜蕾斯是什么 | 独是什么生肖 | 肠道感染有什么症状 | 飞龙在天是什么生肖 | 张钧甯为什么读mi |
智商105是什么水平 | 低密度脂蛋白偏高什么意思 | pending是什么状态 | 怀孕了用排卵试纸测会显示什么 | 什么是心悸有什么症状 |
梦见很多蜜蜂是什么意思 | 暗物质是什么 | 口苦是什么病 | 什么叫有个性的人 | aug是什么意思 |
揽件是什么意思 | 劳烦是什么意思 | eva是什么材料 | 黄体期出血是什么原因 | ads是什么 |
做牛排需要什么调料hcv9jop1ns2r.cn | 眼睛干涩吃什么中成药hcv9jop4ns3r.cn | 可乐喝多了有什么危害hcv8jop5ns2r.cn | 负离子什么意思hcv7jop7ns4r.cn | 手脚发麻挂什么科hcv9jop1ns3r.cn |
慧眼识珠是什么意思hcv8jop4ns8r.cn | 降压药的原理是什么hcv9jop1ns5r.cn | 阿拉伯人是什么种人hcv7jop7ns2r.cn | 梦见抓龙虾是什么意思hcv9jop3ns2r.cn | darling是什么意思fenrenren.com |
结膜充血用什么眼药水bjcbxg.com | 貔貅什么人不能戴hcv9jop5ns4r.cn | 梦到和死人说话是什么意思luyiluode.com | 抽血抽不出来是什么原因hcv8jop7ns8r.cn | 什么水果可以降火hcv7jop6ns4r.cn |
月经期血块多是什么原因bysq.com | 九月一日什么节日hcv7jop6ns9r.cn | 吐鲁番为什么那么热shenchushe.com | 什么是黄油hcv9jop0ns8r.cn | 什么的天空飘着什么的白云hcv8jop3ns7r.cn |