PostgreSQL 版本识别

Insert\delete\update通过ctid定位,并查看该记录xmin\xmax的变化。

    Xid:数据库的事务ID

    Xmin:行头部的xid信息,xmin表示插入该记录的事务ID

    Xmax:表示删除或lock该记录的事务ID

    xid_snapshot:当前集群中为结束的事务

    Clog:事务提交状态日志

 记录格式的定义:htup_details.hPOSTGRES heap tuple header definitions.

1)查看所有xid相关的函数有哪些,这里需要的是txid_current函数

2)可以看到当前的事务ID

postgres=# select * from txid_current();

 txid_current

--------------

         1676

(1 row)

3)进行一次insert后,看事务ID已经+1

postgres=# insert into tt values(1);

INSERT 0 1

postgres=# select ctid,xmin,xmax,cmin,cmax,id from tt;

 ctid  | xmin | xmax | cmin | cmax | id

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

 (0,1) | 1677 |    0 |    0 |    0 |  1

(1 row)

4)开启一个事务后,进行update

postgres=# begin;

BEGIN

postgres=# update tt set id=3;

UPDATE 1

postgres=# select ctid,xmin,xmax,cmin,cmax,id from tt;

 ctid  | xmin | xmax | cmin | cmax | id

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

 (0,2) | 1678 |    0 |    0 |    0 |  3

(1 row)

5)在另外一个会话查看

postgres=#  select ctid,xmin,xmax,cmin,cmax,id from tt;

 ctid  | xmin | xmax | cmin | cmax | id

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

 (0,1) | 1677 | 1678 |    0 |    0 |  1

(1 row)


看当前未结束的事务,或未开启的事务

postgres=# select *from txid_current_snapshot();

 txid_current_snapshot

-----------------------

 1684:1684:

(1 row)

postgres=# select * from txid_current();

 txid_current

--------------

         1684

(1 row)

记录事务是否提交,在这个文件里面,bit

-rw-------. 1 pg pg 8192 Jun 10 04:19 0000

[pg@localhost pg_clog]$ pwd

/home/pg/data/pg_clog


猜你喜欢

转载自blog.csdn.net/yanzongshuai/article/details/80644175