xmax

The identity (transaction ID) of the deleting transaction, or zero for an undeleted row version. It is possible for this column to be nonzero in a visible row version. That usually indicates that the deleting transaction hasn't committed yet, or that an attempted deletion was rolled back.

http://www.postgresql.org/docs/9.1/static/ddl-system-columns.html

作一个实验:

我数据库关闭前,最后一条transaction的id是 1874。

我开一个终端A,此时终端A的当前transactionId为 1875。

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help. pgsql=# begin;
BEGIN pgsql=# select xmin,xmax,* from tab01;
xmin | xmax | id | cd
------+------+----+----
| | |
| | |
| | |
| | |
| | |
| | |
| | |
( rows) pgsql=#

我再开一个终端B,此时,终端B的transactionId为:1876。

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help. pgsql=# begin;
BEGIN
pgsql=# select xmin,xmax,* from tab01;
xmin | xmax | id | cd
------+------+----+----
| | |
| | |
| | |
| | |
| | |
| | |
| | |
( rows) pgsql=#

回到终端A,执行 delete 操作:

pgsql=# delete from tab01 where id=;
DELETE
pgsql=#

此时,在终端A中,已经看不到删除后的数据:

pgsql=# select xmin,xmax,* from tab01;
xmin | xmax | id | cd
------+------+----+----
| | |
| | |
| | |
| | |
| | |
| | |
( rows) pgsql=#

此时,由于终端A尚未提交,所以,可以在终端B中看到如下的情形:

pgsql=# select xmin,xmax,* from tab01;
xmin | xmax | id | cd
------+------+----+----
| | |
| | |
| | |
| | |
| | |
| | |
| | |
( rows) pgsql=#

也就是说,id为9的那条记录,其xmax为1875,表明其为 transactionid为 1875的事务所删除。

回到终端A,进行提交:

pgsql=# commit;
COMMIT
pgsql=# select xmin,xmax,* from tab01;
xmin | xmax | id | cd
------+------+----+----
| | |
| | |
| | |
| | |
| | |
| | |
( rows) pgsql=#

再回到终端B,查看:

pgsql=# select xmin,xmax,* from tab01;
xmin | xmax | id | cd
------+------+----+----
| | |
| | |
| | |
| | |
| | |
| | |
( rows) pgsql=#

让我来再进一步,看一看:

重新开两个终端:

终端A和终端B。

在终端A中:

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help. pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+----+----
| | | | |
| | | | |
( rows) pgsql=# \q

可以看到两条由不同的事务提交所形成的记录。

然后再次使用psql: 经过update 后,自己所看到的是 xmin的变化,这时尚未提交,别的终端看到就不一样了。

终端A:

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help. pgsql=# begin;
BEGIN
pgsql=# update tab01 set id= where cd='';
UPDATE
pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+----+----
| | | | |
| | | | |
( rows) pgsql=#

此时,进入终端B:

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help. pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+----+----
| | | | |
| | | | |
( rows) pgsql=#

我推测,update的时候,自身事务所看到的是内存中的影像。同时它也已经提交到了物理文件上。而别的事务是从物理文件来读取的。

在A终端提交以后:

pgsql=# commit;
COMMIT
pgsql=#

B终端所看到的:

pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+----+----
| | | | |
| | | | |
( rows) pgsql=#

继续从代码上进行分析:

/*
* heap_update - replace a tuple
*
* NB: do not call this directly unless you are prepared to deal with
* concurrent-update conditions. Use simple_heap_update instead.
*
* relation - table to be modified (caller must hold suitable lock)
* otid - TID of old tuple to be replaced
* newtup - newly constructed tuple data to store
* ctid - output parameter, used only for failure case (see below)
* update_xmax - output parameter, used only for failure case (see below)
* cid - update command ID (used for visibility test, and stored into
* cmax/cmin if successful)
* crosscheck - if not InvalidSnapshot, also check old tuple against this
* wait - true if should wait for any conflicting update to commit/abort
*
* Normal, successful return value is HeapTupleMayBeUpdated, which
* actually means we *did* update it. Failure return codes are
* HeapTupleSelfUpdated, HeapTupleUpdated, or HeapTupleBeingUpdated
* (the last only possible if wait == false).
*
* On success, the header fields of *newtup are updated to match the new
* stored tuple; in particular, newtup->t_self is set to the TID where the
* new tuple was inserted, and its HEAP_ONLY_TUPLE flag is set iff a HOT
* update was done. However, any TOAST changes in the new tuple's
* data are not reflected into *newtup.
*
* In the failure cases, the routine returns the tuple's t_ctid and t_xmax.
* If t_ctid is the same as otid, the tuple was deleted; if different, the
* tuple was updated, and t_ctid is the location of the replacement tuple.
* (t_xmax is needed to verify that the replacement tuple matches.)
*/
HTSU_Result
heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
ItemPointer ctid, TransactionId *update_xmax,
CommandId cid, Snapshot crosscheck, bool wait)
{
HTSU_Result result;
TransactionId xid = GetCurrentTransactionId();
Bitmapset *hot_attrs;
ItemId lp;
HeapTupleData oldtup;
HeapTuple heaptup;
Page page;
Buffer buffer,
newbuf;
bool need_toast,
already_marked;
Size newtupsize,
pagefree;
bool have_tuple_lock = false;
bool iscombo;
bool use_hot_update = false;
bool all_visible_cleared = false;
bool all_visible_cleared_new = false; ... ////////////////First Phase marked by gaojian newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
newtup->t_data->t_infomask |= (HEAP_XMAX_INVALID | HEAP_UPDATED);
HeapTupleHeaderSetXmin(newtup->t_data, xid);
HeapTupleHeaderSetCmin(newtup->t_data, cid);
HeapTupleHeaderSetXmax(newtup->t_data, ); /* for cleanliness */
newtup->t_tableOid = RelationGetRelid(relation); ... if (!already_marked)
{
/* Clear obsolete visibility flags ... */
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
HEAP_XMAX_INVALID |
HEAP_XMAX_IS_MULTI |
HEAP_IS_LOCKED |
HEAP_MOVED);
/* ... and store info about transaction updating this tuple */ ///HeapTupleHeaderSetXmax(oldtup.t_data, xid);
/////>>>>>added by gaojian for testing.
////xid = (TransactionId)8888; fprintf(stderr,"x-----1,xid is :%d \n",(int)xid); HeapTupleHeaderSetXmax(oldtup.t_data, xid);
HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo); sleep();
} ... return HeapTupleMayBeUpdated;
}

可以看到,第一段的 :

    newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
newtup->t_data->t_infomask |= (HEAP_XMAX_INVALID | HEAP_UPDATED);
HeapTupleHeaderSetXmin(newtup->t_data, xid);
HeapTupleHeaderSetCmin(newtup->t_data, cid);
HeapTupleHeaderSetXmax(newtup->t_data, ); /* for cleanliness */
newtup->t_tableOid = RelationGetRelid(relation);

写的很明白, HeapTupleHeaderSetXmin(newtup->t_data, xid);

而第二段的 :

    if (!already_marked)
{
/* Clear obsolete visibility flags ... */
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
HEAP_XMAX_INVALID |
HEAP_XMAX_IS_MULTI |
HEAP_IS_LOCKED |
HEAP_MOVED);
/* ... and store info about transaction updating this tuple */ HeapTupleHeaderSetXmax(oldtup.t_data, xid);
HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
}

然后,我再来验证一下,加点调试代码,这样,我执行sql时会出错:

    if (!already_marked)
{
/* Clear obsolete visibility flags ... */
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_COMMITTED |
HEAP_XMAX_INVALID |
HEAP_XMAX_IS_MULTI |
HEAP_IS_LOCKED |
HEAP_MOVED);
/* ... and store info about transaction updating this tuple */ ///HeapTupleHeaderSetXmax(oldtup.t_data, xid);
/////>>>>>added by gaojian for testing.
////xid = (TransactionId)8888; fprintf(stderr,"x-----1,xid is :%d \n",(int)xid); HeapTupleHeaderSetXmax(oldtup.t_data, xid);
HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo); }

执行结果:

pgsql=# begin;
BEGIN
pgsql=# update tab01 set id= where cd = '';
ERROR: could not access status of transaction
DETAIL: Could not read from file "pg_subtrans/0000" at offset : Success.
pgsql=# \q
[pgsql@loca

再次登陆进来看看,哈,这证明我所看到的代码部分,就是实现写入xmax的部分。

总结来说,就是,在update(可能delete也是),用向oldtup写入transaction id的方式,来记录哪个transaction改动了记录。

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help. pgsql=# select xmin,xmax,cmin,cmax, * from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+-----------+----
| | | | |
| | | | |
| | | | |
( rows) pgsql=#

对PostgreSQL xmax的理解的更多相关文章

  1. 腾讯云数据库团队:PostgreSQL TOAST技术理解

    作者介绍:胡彬 腾讯云高级工程师 TOAST是"The Oversized-Attribute Storage Technique"的缩写,主要用于存储一个大字段的值.要理解TOA ...

  2. 关于linux上postgresql的一些理解

    刚开始接触postgresql,安装后就有一个默认用户postgres,而且在启动postgresql后只能通过切换到linux的postgres用户才能登录数据库进行操作,和Mysql的登录认证居然 ...

  3. PostgreSQL相关的软件,库,工具和资源集合

    PostgreSQL相关的软件,库,工具和资源集合. 备份 wal-e - Simple Continuous Archiving for Postgres to S3, Azure, or Swif ...

  4. PostgreSQL安装详细步骤(windows)

    原文地址:http://blog.chinaunix.net/uid-354915-id-3498734.html PostgreSQL安装:一.windows下安装过程安装介质:postgresql ...

  5. PostgreSQL安装详细步骤windows

    PostgreSQL安装:一.windows下安装过程安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,过程如下:1.开始安装: 2.选择程序安装目录 ...

  6. PostgreSQL安装详细步骤(windows)[转]

    PostgreSQL安装: 一.windows下安装过程 安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,过程如下: 1.开始安装: 2.选择程序安 ...

  7. 2.PostgreSQL安装详细步骤(windows)【转】

    感谢 Junn9527 PostgreSQL安装:一.windows下安装过程安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,过程如下:1.开始安装 ...

  8. PostgreSQL Replication之第五章 设置同步复制(1)

    到目前为止,我们已经处理了基于文件的复制(或日志传送)和简单的基于流复制的设置.在两种情况中,在master上事务被提交之后,数据被提交,由slave接收.在master提交和slave实际上完全地接 ...

  9. PostgreSQL在win7上安装详细步骤

    原文:PostgreSQL在win7上安装详细步骤 PostgreSQL安装: 一.windows下安装过程 安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程 ...

随机推荐

  1. 安装win7 32位系统出现的问题解决办法

    计算机意外地重新启动或遇到错误.Windows 安装无法继续.若要安装Windows,请单击“确定”重新启动计算机,然后重新启动安装”. http://www.baidusoso.net/      ...

  2. CruiseControl.net

    CruiseControl.net 使用CruiseControl.NET进行自动化构建总结 http://blog.csdn.net/chenbin520/article/details/10112 ...

  3. OneNote快捷键

    转载自:http://onenoter.com/2013/04/5792 记录笔记和设置笔记格式 键入和编辑笔记 若要执行此操作 按 打开一个新的 OneNote 窗口. Ctrl+M 打开一个小的 ...

  4. POJ3921

    搜索 每次找出最短路 如果小于等于k 那么必定这里有一点是要被删掉的 枚举这个最短路径上的每一个点 (一般不会超过20) 将其相邻边删除 用dijskra求最短路径并且保存即可 深度搜索 #inclu ...

  5. 大四实习准备5_android广播机制

    2015-5-1 android 广播机制 5.1简介 分为标准广播(Normal broadcasts)(无先后顺序,几乎同时接收,不可截断)和有序广播(Ordered broadcasts)(有先 ...

  6. 【JSP】让HTML和JSP页面不缓存从Web服务器上重新获取页面

    用户退出后,如果点击浏览器上的后退按钮,Web应用将不能正确保护受保护的页面——在Session销毁后(用户退出)受保护的JSP页重新在浏览器中显示出来. 然而,如果用户点击返回页面上的任何链接,We ...

  7. 【转】Android项目中编译 C的模块

    原文网址:http://blog.csdn.net/Harrison_zhu/article/details/4057738 Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才 ...

  8. [面试题] for() while() 条件判断 赋值问题

    http://group.jobbole.com/7963/#comm-11311 [题目]:下列for循环的循环体执行次数为 for(int i=10, j=1; i=j=0; i++, j--)( ...

  9. qtp不识别树结构中的点击事件

    qtp不识别树结构中的点击事件,未生成该点击事件的脚本,解决办法: 1.未生成点击"auto分类c1"的脚本 2.点击1.对象库-2.添加对象库-3.选中对象-点击OK,即将该对象 ...

  10. Jquery+asp.net后台数据传到前台js进行解析的方法

    所以在解析后台数据的时候,我们需要根据后台的数据情况,特殊处理和对待. 我这里后台用的是asp.net提供的wcf服务,也有ashx一般处理程序.大致原理差不多. C#中我们经常用的对象,有实体对象比 ...