PostgreSQL DBA(26) - MVCC#6(Tuple可见性判断-简化版)

本节介绍了Tuple可见性判断的基本逻辑,以下判断逻辑来自于Bruce Momjian写的 Mvcc Unmasked - Bruce Momjian ,推荐阅读.

判断逻辑
((Xmin == my-transaction &&       inserted by the current transaction
 Cmin < my-command &&          before this command, and
 (Xmax is null ||            the row has not been deleted, or
  (Xmax == my-transaction &&      it was deleted by the current transaction
   Cmax >= my-command)))       but not before this command,
||                     or
 (Xmin is committed &&          the row was inserted by a committed transaction, and
  (Xmax is null ||            the row has not been deleted, or
   (Xmax == my-transaction &&     the row is being deleted by this transaction
    Cmax >= my-command) ||      but it’s not deleted “yet”, or
    (Xmax != my-transaction &&    the row was deleted by another transaction
     Xmax is not committed))))    that has not been committed

其中,my-command在PG中通过cmin/cmax体现.
cmin/cmax
如下例所示,游标在删除命令前定义,因此Tuple可见.


10:51:21 (xdb@[local]:5432)testdb=# DELETE FROM mvcc_demo;
DELETE 3
10:51:22 (xdb@[local]:5432)testdb=# 
10:51:22 (xdb@[local]:5432)testdb=# BEGIN WORK;
BEGIN
10:51:22 (xdb@[local]:5432)testdb=#* INSERT INTO mvcc_demo VALUES (1);
INSERT 0 1
10:51:22 (xdb@[local]:5432)testdb=#* INSERT INTO mvcc_demo VALUES (2);
INSERT 0 1
10:51:22 (xdb@[local]:5432)testdb=#* INSERT INTO mvcc_demo VALUES (3);
INSERT 0 1
10:51:22 (xdb@[local]:5432)testdb=#* SELECT xmin, cmin, xmax, cmax, * FROM mvcc_demo;
 xmin | cmin | xmax | cmax | val 
------+------+------+------+-----
 2400 |    0 |    0 |    0 |   1
 2400 |    1 |    0 |    1 |   2
 2400 |    2 |    0 |    2 |   3
(3 rows)
10:51:23 (xdb@[local]:5432)testdb=#* DECLARE c_mvcc_demo CURSOR FOR
[local] xdb@testdb-# SELECT xmin, cmin, xmax, cmax, * FROM mvcc_demo; --> 定义游标
DECLARE CURSOR
10:51:34 (xdb@[local]:5432)testdb=#* DELETE FROM mvcc_demo; --> 删除数据
DELETE 3
10:51:34 (xdb@[local]:5432)testdb=#* SELECT xmin, cmin, xmax, cmax, * FROM mvcc_demo;
 xmin | cmin | xmax | cmax | val 
------+------+------+------+-----
(0 rows)
10:51:35 (xdb@[local]:5432)testdb=#* FETCH ALL FROM c_mvcc_demo; --> 游标在删除命令前定义,因此可见
 xmin | cmin | xmax | cmax | val 
------+------+------+------+-----
 2400 |    0 | 2400 |    0 |   1
 2400 |    1 | 2400 |    1 |   2
 2400 |    2 | 2400 |    2 |   3
(3 rows)

参考资料
Mvcc Unmasked - Bruce Momjian
HeapTupleSatisfiesSelf
PostgreSQL Source Code

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2561482/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6906/viewspace-2561482/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值