postgresql system columns
每张表都有这么一些隐含列,我们需要了解一下,但是不需要去深究,知道他们含义,以及如何使用即可。
oid:object ID的缩写,32bit,建表时候用with oids,或者是配置default_with_oids参数,这个值并不是唯一,除非自己采取别的措施保证唯一
举例: create table t1 (a int) with (oid=true);
insert into t1 values(1);
select oid, * from t1;
oid | a
-------+---
16469 | 1
tableoid:表对象表示符。该列对于从继承层次中进行查询非常有用,如果没有它,就很难说清行记录来自那个表。tableoid列与数据字典pg_class的oid进行关联可以获取表名。
举例:postgres=# select tableoid from t1;
tableoid
----------
16466
postgres=# select relname,relnamespace from pg_class where oid=16466;
relname | relnamespace
---------+--------------
t1 | 2200
xmin:行插入事务标识,行的插入会更新此xmin
举例:
postgres=# begin;
BEGIN
postgres=# select txid_current();
txid_current
--------------
1722
(1 row)
postgres=# insert into t(i) values(5);
INSERT 0 1
postgres=# select *,xmin,xmax from t where i=5 limit 10;
s | i | xmin | xmax
----------+---+------+------
7 | 5 | 1713 | 0
10000004 | 5 | 1719 | 0
10000005 | 5 | 1721 | 0
10000006 | 5 | 1722 | 0
(4 rows)
postgres=# end;
COMMIT
cmin:插入时的命令标识,从0开始
xmax:删除事务标识,对数据做删除和更新都会更改,该值为非0值时表示删除事务还没有提交,或者试图进行回滚,也就是说这里改值可能有三种状态。
举例:未提交(insert and update)
postgres=# select *,xmin,xmax from t where i=5 limit 10;
s | i | xmin | xmax
----------+---+------+------
7 | 5 | 1713 | 0
10000004 | 5 | 1719 | 0
10000005 | 5 | 1721 | 0
10000006 | 5 | 1722 | 0
(4 rows)
postgres=# begin;
BEGIN
postgres=# select txid_current();
txid_current
--------------
1723
(1 row)
postgres=# delete from t where s=7;
DELETE 1
未提交开启另一session:
postgres=# select *,xmin,xmax from t where i=5 limit 10;
s | i | xmin | xmax
----------+---+------+------
7 | 5 | 1713 | 1723
10000004 | 5 | 1719 | 0
10000005 | 5 | 1721 | 0
10000006 | 5 | 1722 | 0
delete rollback事务:
postgres=# select *,xmin,xmax from t where i=5 limit 10;
s | i | xmin | xmax
----------+---+------+------
10000004 | 5 | 1719 | 0
10000005 | 5 | 1721 | 0
10000006 | 5 | 1722 | 0
(3 rows)
postgres=# begin;
BEGIN
postgres=# select txid_current();
txid_current
--------------
1724
(1 row)
postgres=# delete from t where s=10000004;
DELETE 1
postgres=# rollback;
ROLLBACK
postgres=# select *,xmin,xmax from t where i=5 limit 10;
s | i | xmin | xmax
----------+---+------+------
10000004 | 5 | 1719 | 1724
10000005 | 5 | 1721 | 0
10000006 | 5 | 1722 | 0
(3 rows)
update rollback事务:
postgres=# select *,xmin,xmax from t where i=5 limit 10;
s | i | xmin | xmax
----------+---+------+------
10000004 | 5 | 1719 | 1724
10000005 | 5 | 1721 | 0
10000006 | 5 | 1722 | 0
(3 rows)
postgres=# begin;
BEGIN
postgres=# select txid_current();
txid_current
--------------
1725
(1 row)
postgres=# update t set i=10 where s=10000004;
UPDATE 1
postgres=# rollback;
ROLLBACK
postgres=# select *,xmin,xmax from t where s=10000004 limit 10;
s | i | xmin | xmax
----------+---+------+------
10000004 | 5 | 1719 | 1725
update 提交事务:
postgres=# select *,xmin,xmax from t where s=10000004 limit 10;
s | i | xmin | xmax
----------+---+------+------
10000004 | 5 | 1719 | 1725
(1 row)
postgres=# begin;
BEGIN
postgres=# select txid_current();
txid_current
--------------
1726
(1 row)
postgres=# update t set i=20 where s=10000004;
UPDATE 1
postgres=# commit;
COMMIT
postgres=# select *,xmin,xmax from t where s=10000004 limit 10;
s | i | xmin | xmax
----------+----+------+------
10000004 | 20 | 1726 | 0
总结一下,也就是说不管回滚还是未提交,都会获取新的事务号,但是在update提交的情况下,xmax归零,xmin获取当前事务号。
cmax:删除事务内部的命令标识
txid:有了以上的例子,很好理解了,就是事务ID,这个可不是表的隐含列哦