postgresql system columns

php中文网
发布: 2016-06-07 14:58:43
原创
1420人浏览过

postgresql system columns 每张表都有这么一些隐含列,我们需要了解一下,但是不需要去深究,知道他们含义,以及如何使用即可。 oid:object ID的缩写,32bit,建表时候用with oids,或者是配置default_with_oids参数,这个值并不是唯一,除非自己采取别的

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,这个可不是表的隐含列哦

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号