PostgreSQL 11 preview - 虚拟列(自动根据表达式产生值)

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , 虚拟列 , GENERATED column


背景

通过增加虚拟字段,可以让数据库根据虚拟列的定义,自动填充值。

与自增,DEFAULT不同的是,虚拟列中可以从其他列的内容产生。

例如

  CREATE TABLE t1 (  
    ...,  
    height_cm numeric,  
    height_in numeric GENERATED ALWAYS AS (height_cm * 2.54)  
  );  

PostgreSQL 11中,有望提交这个PATCH,使用前请详细参考文档说明。

Here is another attempt to implement generated columns.  This is a  
well-known SQL-standard feature, also available for instance in DB2,  
MySQL, Oracle.  A quick example:  
  
  CREATE TABLE t1 (  
    ...,  
    height_cm numeric,  
    height_in numeric GENERATED ALWAYS AS (height_cm * 2.54)  
  );  
  
(This is not related to the recent identity columns feature, other than  
the similar syntax and some overlap internally.)  
  
In previous discussions, it has often been a source of confusion whether  
these generated columns are supposed to be computed on insert/update and  
stored, or computed when read.  The SQL standard is not explicit, but  
appears to lean toward stored.  DB2 stores.  Oracle computes on read.  
MySQL supports both.  So I target implementing both.  This makes sense:  
Both regular views and materialized views have their uses, too.  For the  
syntax, I use the MySQL/Oracle syntax of appending [VIRTUAL|STORED].  In  
this patch, only VIRTUAL is fully implemented.  I also have STORED kind  
of working, but it wasn't fully baked, so I haven't included it here.  
  
Known bugs:  
  
- pg_dump produces a warning about a dependency loop when dumping these.  
 Will need to be fixed at some point, but it doesn't prevent anything  
from working right now.  
  
Open design issues:  
  
- COPY behavior: Currently, generated columns are automatically omitted  
if there is no column list, and prohibited if specified explicitly.  
When stored generated columns are implemented, they could be copied out.  
 Some user options might be possible here.  
  
- Catalog storage: I store the generation expression in pg_attrdef, like  
a default.  For the most part, this works well.  It is not clear,  
however, what pg_attribute.atthasdef should say.  Half the code thinks  
that atthasdef means "there is something in pg_attrdef", the other half  
thinks "column has a DEFAULT expression".  Currently, I'm going with the  
former interpretation, because that is wired in quite deeply and things  
start to crash if you violate it, but then code that wants to know  
whether a column has a traditional DEFAULT expression needs to check  
atthasdef && !attgenerated or something like that.  
  
Missing/future functionality:  
  
- STORED variant  
  
- various ALTER TABLE variants  
  
- index support (and related constraint support)  
  
These can be added later once the basics are nailed down.  
  
--   
Peter Eisentraut              http://www.2ndQuadrant.com/  
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services  

参考

https://commitfest.postgresql.org/17/1443/

https://www.postgresql.org/message-id/flat/b151f851-4019-bdb1-699e-ebab07d2f40a@2ndquadrant.com#b151f851-4019-bdb1-699e-ebab07d2f40a@2ndquadrant.com

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3月前
|
SQL 关系型数据库 MySQL
mysql使用default给列设置默认值的问题
mysql使用default给列设置默认值的问题
34 0
|
4月前
|
SQL 关系型数据库 MySQL
6:列属性完整性-MySQL
6:列属性完整性-MySQL
126 0
|
4月前
|
存储 关系型数据库 MySQL
MySQL中的列类型之字符串类型总结
MySQL中的列类型之字符串类型总结
75 1
|
2月前
|
存储 关系型数据库 MySQL
如何修改MySQL列允许Null?
如何修改MySQL列允许Null?
47 1
如何修改MySQL列允许Null?
|
2月前
|
关系型数据库 MySQL 数据库
如何检查 MySQL 中的列是否为空或 Null?
如何检查 MySQL 中的列是否为空或 Null?
46 1
如何检查 MySQL 中的列是否为空或 Null?
|
4月前
|
存储 关系型数据库 MySQL
为什么建议MySQL列属性尽量NOT NULL
为什么建议MySQL列属性尽量NOT NULL
153 0
|
7月前
|
存储 算法 关系型数据库
4.3.3.1 【MySQL】CHAR(M)列的存储格式
4.3.3.1 【MySQL】CHAR(M)列的存储格式
45 0
|
7月前
|
存储 关系型数据库 MySQL
mysql中修改某个数据列的名字或数据类型
mysql中修改某个数据列的名字或数据类型
161 1
|
7月前
|
存储 关系型数据库 MySQL
mysql添加或删除表以及给数据表添加或删除列
mysql添加或删除表以及给数据表添加或删除列
85 1
|
8月前
|
SQL 关系型数据库 MySQL
MySQL变量使用与介绍之为查询结果增加序号列和自定义序号列
SQL三:用户表(包含字段有:用户ID[自增]、姓名、性别、民族、出生日期、身份证号),无论查询条件如何,要求查询的结果显示一个行号列(即:第一行显示1,第二行显示2,第三行显示3,依此类推)
192 0

相关产品

  • 云原生数据库 PolarDB