PostgreSQL DBA(53) - PG 12 Generated columns
本節簡單介紹了PostgreSQL 12中的generated columns.在其他資料庫中,generated columns又被成為計算列(calculated columns)/虛擬列(virtual columns)等.
簡介
在PG 11或以前的版本中,generated columns是不支援的:
testdb=# drop table if exists t_generated_col;
NOTICE: table "t_generated_col" does not exist, skipping
DROP TABLE
testdb=# create table t_generated_col
testdb-# (n1 int,
testdb(# n2 int,
testdb(# c1 varchar(10),
testdb(# c2 varchar(10),
testdb(# counter int generated always as (n1 + n2) stored,
testdb(# link varchar(20) generated always as (c1 || c2) stored);
ERROR: syntax error at or near "("
LINE 6: counter int generated always as (n1 + n2) stored,
^
counter列的值由n1和n2相加而得,而link列的值有c1和c2拼接而得.
在PG 12中,可以支援generated columns
testdb=# drop table if exists t_generated_col;
psql: NOTICE: table "t_generated_col" does not exist, skipping
DROP TABLE
testdb=# create table t_generated_col
testdb-# (n1 int,
testdb(# n2 int,
testdb(# c1 varchar(10),
testdb(# c2 varchar(10),
testdb(# counter int generated always as (n1 + n2) stored,
testdb(# link varchar(20) generated always as (c1 || c2) stored);
CREATE TABLE
testdb=# insert into t_generated_col(n1,n2,c1,c2) values(1,1,'c1','c2');
INSERT 0 1
testdb=#
testdb=# select * from t_generated_col;
n1 | n2 | c1 | c2 | counter | link
----+----+----+----+---------+------
1 | 1 | c1 | c2 | 2 | c1c2
(1 row)
生成列的值由表示式的值計算而得,如為null則為null:
testdb=# insert into t_generated_col(n1,n2,c1,c2) values(1,null,'c1',null);
INSERT 0 1
testdb=# select * from t_generated_col;
n1 | n2 | c1 | c2 | counter | link
----+----+----+----+---------+------
1 | 1 | c1 | c2 | 2 | c1c2
1 | | c1 | | |
(2 rows)
生成列不能被update:
testdb=# update t_generated_col set counter = 10;
psql: ERROR: column "counter" can only be updated to DEFAULT
DETAIL: Column "counter" is a generated column.
可以建立在其上建立index:
testdb=# create index idx_t_generated_col_counter on t_generated_col(counter);
CREATE INDEX
執行查詢時,跟普通列沒有什麼區別:
^
testdb=# insert into t_generated_col(n1,n2) select x,0 from generate_series(1,10000) x;
INSERT 0 10000
testdb=#
testdb=# explain select * from t_generated_col where counter = 1000;
QUERY PLAN
----------------------------------------------------------------------------------------------------
Index Scan using idx_t_generated_col_counter on t_generated_col (cost=0.29..8.30 rows=1 width=23)
Index Cond: (counter = 1000)
(2 rows)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-2650358/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PostgreSQL DBA(40) - PG 12 pg_promoteSQL
- PostgreSQL DBA(125) - PG 12(TPCC)SQL
- PostgreSQL DBA(82) - PG 12 Improving COPYSQL
- PostgreSQL DBA(92) - PG 12 Improving VACUUMSQL
- PostgreSQL DBA(67) - PG 12 SQLJSON pathSQLJSON
- PostgreSQL DBA(37) - PG 12 REINDEX CONCURRENTLYSQLIndex
- PostgreSQL DBA(39) - PG 12 Functions for partitionsSQLFunction
- PostgreSQL DBA(36) - PG 12 Inlined WITH queriesSQLinline
- PostgreSQL DBA(142) - PG 12(Monitoring PostgreSQL VACUUM processes)SQL
- PostgreSQL DBA(94) - PG 12 Improving Partition(Select)SQL
- PostgreSQL DBA(93) - PG 12 Improving Partition(Insert)SQL
- PostgreSQL DBA(62) - PG 12 More progress reportingSQL
- PostgreSQL DBA(70) - PG 12 Add SETTINGS option to EXPLAINSQLAI
- PostgreSQL DBA(98) - PG 12 Faster float conversion to textSQLAST
- PostgreSQL DBA(95) - PG 12 Partition(out of shared memory)SQL
- PostgreSQL DBA(126) - PG 12(搭建流複製)SQL
- PostgreSQL DBA(149) - PG 12(Add SETTINGS option to EXPLAIN)SQLAI
- PostgreSQL DBA(38) - PG 12 Connection slots and WAL sendersSQL
- PostgreSQL DBA(141) - PG 12(Discovering less-known PostgreSQL v12 features)SQL
- PostgreSQL DBA(53) - Index(BRIN)SQLIndex
- PostgreSQL DBA(91) - PG upgradeSQL
- PostgreSQL DBA(139) - PG 12(B-tree index improvement 1#)SQLIndex
- PostgreSQL DBA(41) - PG Index PropertiesSQLIndex
- PostgreSQL DBA(140) - PG 12(Don't log incomplete startup packet if it's empty)SQL
- PostgreSQL DBA(83) - Extension(pg_buffercache)SQL
- PostgreSQL DBA(84) - Extension(pg_prewarm)SQL
- PostgreSQL DBA(162) - Extension(pg_catcheck)SQL
- PostgreSQL DBA(63) - Extension(pg_qualstats)SQL
- PostgreSQL DBA(163) - Extension(pg_cron)SQL
- PostgreSQL DBA(46) - PG Operator classes and familiesSQL
- PostgreSQL DBA(189) - PG 14 Monitoring ImprovementsSQL
- PostgreSQL DBA(183) - PG 14(Better JSON)SQLJSON
- PostgreSQL DBA(146) - pgAdmin(pg_dumpall vs pg_dump)SQL
- PostgreSQL DBA(79) - Locks(pg_blocking_pids)SQLBloC
- PostgreSQL DBA(129) - Extension(pg_variables).mdSQL
- PostgreSQL DBA(138) - PG 13(Drop database force)SQLDatabase
- PostgreSQL DBA(63) - Extension(pg_stat_statements)SQL
- PostgreSQL DBA(172) - PG 13(WAL activity in EXPLAIN)SQLAI