PostgreSQL系统列 System Columns

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
24天前
|
关系型数据库 MySQL 数据库
【Mac os系统】安装MySQL数据库
本文详细介绍了在Mac OS系统上安装MySQL数据库的步骤,包括下载、安装、配置环境变量、启动服务、授权设置以及解决常见问题,并提供了一些常用的MySQL命令。
49 0
【Mac os系统】安装MySQL数据库
|
3月前
|
存储 安全 Java
基于Java+MySQL停车场车位管理系统详细设计和实现(源码+LW+调试文档+讲解等)
基于Java+MySQL停车场车位管理系统详细设计和实现(源码+LW+调试文档+讲解等)
|
9天前
|
关系型数据库 MySQL 应用服务中间件
win7系统搭建PHP+Mysql+Apache环境+部署ecshop项目
这篇文章介绍了如何在Windows 7系统上搭建PHP、MySQL和Apache环境,并部署ECShop项目,包括安装配置步骤、解决常见问题以及使用XAMPP集成环境的替代方案。
23 1
win7系统搭建PHP+Mysql+Apache环境+部署ecshop项目
|
10天前
|
数据可视化 关系型数据库 MySQL
Mysql8 如何在 Window11系统下完成跳过密钥校验、完成数据库密码的修改?
这篇文章介绍了如何在Windows 11系统下跳过MySQL 8的密钥校验,并通过命令行修改root用户的密码。
Mysql8 如何在 Window11系统下完成跳过密钥校验、完成数据库密码的修改?
|
25天前
|
存储 关系型数据库 MySQL
基于python django 医院管理系统,多用户功能,包括管理员、用户、医生,数据库MySQL
本文介绍了一个基于Python Django框架开发的医院管理系统,该系统设计了管理员、用户和医生三个角色,具备多用户功能,并使用MySQL数据库进行数据存储和管理。
基于python django 医院管理系统,多用户功能,包括管理员、用户、医生,数据库MySQL
|
7天前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
10天前
|
关系型数据库 MySQL 数据库连接
成功解决「MySQL问题1」启动mysql时:发生系统错误5拒绝访问
这篇文章介绍了如何解决启动MySQL服务时遇到的系统错误5(拒绝访问),通过管理员权限启动命令窗口并使用"net start mysql"和"net stop mysql"命令来控制服务。
|
11天前
|
SQL 数据可视化 关系型数据库
MySQL 备份可视化巡检系统
MySQL 备份可视化巡检系统
|
12天前
|
关系型数据库 MySQL Linux
一文教会你如何在Linux系统中使用Docker安装Mysql 5.7版本 【详细过程+图解】
这篇文章提供了在Linux系统中使用Docker安装Mysql 5.7版本的详细过程和图解,包括安装指定版本、创建实例、启动、使用Navicat连接测试、文件挂载与端口映射、进入容器、配置文件修改以及重新启动容器等步骤。
一文教会你如何在Linux系统中使用Docker安装Mysql 5.7版本 【详细过程+图解】
|
23天前
|
数据采集 数据可视化 关系型数据库
【优秀python web设计】基于Python flask的猫眼电影可视化系统,可视化用echart,前端Layui,数据库用MySQL,包括爬虫
本文介绍了一个基于Python Flask框架、MySQL数据库和Layui前端框架的猫眼电影数据采集分析与可视化系统,该系统通过爬虫技术采集电影数据,利用数据分析库进行处理,并使用Echart进行数据的可视化展示,以提供全面、准确的电影市场分析结果。
下一篇
云函数