温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PostgreSQL系统列 System Columns

发布时间:2020-05-16 13:47:08 来源:网络 阅读:646 作者:Darren_Chen 栏目:数据库

每个表都有隐藏的系统列,创建表列的时候不能和系统列名相同,下面讲解一下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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI