PostgreSQL系统列 System Columns

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:

每个表都有隐藏的系统列,创建表列的时候不能和系统列名相同,下面讲解一下PostgreSQL有哪些系统列.


(1)oid(4 bytes)

object identifier(即object ID)主要用于系统表如pg_class(记录table的一个表),pg_namespace(记录schema的一个表),

创建表时,如果指定with oids,则存在oid列。还可以由参数default_with_oids控制,默认是off,表示不加with oids建表时,没有oid列。


eg:


#查看pg_class这个条记录对应的oid

postgres=# select oid,relname from pg_class where oid='pg_class'::regclass;

oid  | relname  

------+----------

1259 | pg_class


#创建一个新的表


postgres=# create table t1(c1 integer,c2 varchar(20)) with oids;

CREATE TABLE


postgres=# insert into t1 select 1,'aaa';

INSERT 16456 1


postgres=# insert into t1 values(2,'bbb');

INSERT 16457 1


postgres=# \d+ t1;

                                 Table "public.t1"

Column |         Type          | Modifiers | Storage  | Stats target | Description

--------+-----------------------+-----------+----------+--------------+-------------

c1     | integer               |           | plain    |              |

c2     | character varying(20) |           | extended |              |

Has OIDs: yes


postgres=# select oid,c1,c2 from t1;

  oid  | c1 | c2  

-------+----+-----

16456 |  1 | aaa

16457 |  2 | bbb


(2)tableid(4 bytes)

表对象的一个唯一标识符,一个表只对应一个tableoid,可以将tableoid与pgclass的oid列连接起来,以获得表名


postgres=# select oid,tableoid from t1;

  oid  | tableoid

-------+----------

16456 |    16453

16457 |    16453

16458 |    16453


postgres=# select tableoid from t2;

tableoid

----------

    16464


postgres=# select oid,relname from pg_class ;

  oid  |                 relname                 

-------+-----------------------------------------

16453 | t1

16464 | t2


postgres=# select relname from pg_class where oid in (16453,16464);

relname

---------

t1

t2


(3)ctid(6 bytes)

在表中的一个物理位置标识符,和oracle的rowid类似,但有一点不同,当表被vacuum full或该行值被update时该值可能会改变。所以定义表值的唯一性最好还是自己创建一个序列值的主键列来标识比较合适


(4)xmin

是插入的事务标识符transaction ID,是用来标识不同事务下的一个版本控制。每一次更新该行都会改变这个值。可以和mvcc版本结合起来看


(5)xmax

是删除更新的事务标识符transaction ID,如果该值不为0,则说明该行数据当前还未提交或回滚。比如设置begin...commit事务时可以明显看到该值的变化


(6)cmin

插入事务的命令标识符command identifier,从0开始


(7)cmax

删除事务的命令标识符command identifier,或者为0


eg:

postgres=# create table t1(c1 integer,c2 varchar(20));

postgres=# insert into t1 select generate_series(1,3),repeat('hello',2);


#三行记录的xmin一样,表示是同一个事物

postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;

cmin | cmax | xmin | xmax | ctid  | c1 |     c2     

------+------+------+------+-------+----+------------

    0 |    0 | 1806 |    0 | (0,1) |  1 | hellohello

    0 |    0 | 1806 |    0 | (0,2) |  2 | hellohello

    0 |    0 | 1806 |    0 | (0,3) |  3 | hellohello


begin;

insert into t1 values(4,'aaa');

insert into t1 values(5,'bbb');

insert into t1 values(6,'ccc');

commit;


#第四行,第五行,第六行的xmin不同,表示是不同的事物,cmin和cmax也都发生变化了

postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;

cmin | cmax | xmin | xmax | ctid  | c1 |     c2     

------+------+------+------+-------+----+------------

    0 |    0 | 1806 |    0 | (0,1) |  1 | hellohello

    0 |    0 | 1806 |    0 | (0,2) |  2 | hellohello

    0 |    0 | 1806 |    0 | (0,3) |  3 | hellohello

    0 |    0 | 1807 |    0 | (0,4) |  4 | aaa

    1 |    1 | 1807 |    0 | (0,5) |  5 | bbb

    2 |    2 | 1807 |    0 | (0,6) |  6 | ccc


session1:

postgres=# begin;

postgres=# update t1 set c2='cdhu' where c1=5;

postgres=# update t1 set c2='cdhucdhu' where c1=6;


#此时的ctid变化了:

postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;

cmin | cmax | xmin | xmax | ctid  | c1 |     c2     

------+------+------+------+-------+----+------------

    0 |    0 | 1806 |    0 | (0,1) |  1 | hellohello

    0 |    0 | 1806 |    0 | (0,2) |  2 | hellohello

    0 |    0 | 1806 |    0 | (0,3) |  3 | hellohello

    0 |    0 | 1807 |    0 | (0,4) |  4 | aaa

    0 |    0 | 1808 |    0 | (0,7) |  5 | cdhu

    1 |    1 | 1808 |    0 | (0,8) |  6 | cdhucdhu

(6 rows)


再开一个会话


session2:

#上面update事物还没有结束,所以xmax现在不为0:

postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;

cmin | cmax | xmin | xmax | ctid  | c1 |     c2     

------+------+------+------+-------+----+------------

    0 |    0 | 1806 |    0 | (0,1) |  1 | hellohello

    0 |    0 | 1806 |    0 | (0,2) |  2 | hellohello

    0 |    0 | 1806 |    0 | (0,3) |  3 | hellohello

    0 |    0 | 1807 |    0 | (0,4) |  4 | aaa

    0 |    0 | 1807 | 1808 | (0,5) |  5 | bbb

    1 |    1 | 1807 | 1808 | (0,6) |  6 | ccc


session1:

postgres=# commit;


session2:

postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;

cmin | cmax | xmin | xmax | ctid  | c1 |     c2     

------+------+------+------+-------+----+------------

    0 |    0 | 1806 |    0 | (0,1) |  1 | hellohello

    0 |    0 | 1806 |    0 | (0,2) |  2 | hellohello

    0 |    0 | 1806 |    0 | (0,3) |  3 | hellohello

    0 |    0 | 1807 |    0 | (0,4) |  4 | aaa

    0 |    0 | 1808 |    0 | (0,7) |  5 | cdhu

    1 |    1 | 1808 |    0 | (0,8) |  6 | cdhucdhu




本文转自 Darren_Chen 51CTO博客,原文链接:http://blog.51cto.com/darrenmemos/1972372,如需转载请自行联系原作者
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
11天前
|
SQL 自然语言处理 关系型数据库
PolarDB上实现一个自然语言查询系统
PolarDB上实现一个自然语言查询系统
|
1月前
|
Oracle 关系型数据库 分布式数据库
PolarDB助力欧派家居核心系统去O上云,每秒处理万次事务
欧派家居选择阿里云PolarDB-PG数据库,因其顺应云趋势,提供稳定服务,提升扩容和运维效率。欧派运维负责人表示,PolarDB-PG云上运行优于自建Oracle,云运维响应更快,解决问题效率更高。
|
27天前
|
存储 关系型数据库 MySQL
MySQL数据库进阶第一篇(存储引擎与Linux系统上安装MySQL数据库)
MySQL数据库进阶第一篇(存储引擎与Linux系统上安装MySQL数据库)
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列14、博客后台管理系统
MySQL数据库基础练习系列14、博客后台管理系统
20 1
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列13、用户注册与登录系统
MySQL数据库基础练习系列13、用户注册与登录系统
16 1
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列11、新闻发布系统
MySQL数据库基础练习系列11、新闻发布系统
19 1
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列10、访客登记系统
MySQL数据库基础练习系列10、访客登记系统
19 1
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列8、成绩录入与分析系统
MySQL数据库基础练习系列8、成绩录入与分析系统
14 1
|
1月前
|
SQL 监控 关系型数据库
MySQL数据库基础练习系列7、日志记录系统
MySQL数据库基础练习系列7、日志记录系统
12 1
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础练习系列5、会员管理系统
MySQL数据库基础练习系列5、会员管理系统
13 1