首页 技术 正文
技术 2022年11月12日
0 收藏 791 点赞 2,388 浏览 2980 个字

看例子:

开两个终端来对比:

在终端A:

[pgsql@localhost bin]$ ./psql
psql (9.1.)
Type "help" for help.pgsql=# begin;
BEGIN
pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+-----------+----
| | | | |
| | | | |
( rows)pgsql=# insert into tab01 values(,'');
INSERT
pgsql=# insert into tab01 values(,'');
INSERT
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=#

然后再在终端A进行提交:

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

此时,再在终端B进行观察:

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

继续研究cmin是咋回事:

pgsql=# begin;
BEGIN
pgsql=# insert into tab01(id,cd) values(generate_series(,),'xx');
INSERT
pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+-----------+----
| | | | |
| | | | |
| | | | |
| | | | |
| | | | | xx
| | | | | xx
( rows)pgsql=#

可以说cmin可理解为一个事务里,执行了几次sql命令的顺序。

那么cmax呢?在前面的基础上继续执行,居然没有看到区别:

pgsql=# update tab01 set id= where cd = '';
UPDATE
pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+----+----
| | | | |
| | | | |
| | | | |
| | | | | xx
| | | | | xx
| | | | |
( rows)pgsql=# commit;
COMMIT
pgsql=# select xmin,xmax,cmin,cmax,* from tab01;
xmin | xmax | cmin | cmax | id | cd
------+------+------+------+----+----
| | | | |
| | | | |
| | | | |
| | | | | xx
| | | | | xx
| | | | |
( rows)pgsql=#

经过反复折腾,终于发现,其实 cmin和 cmax就是一个东西:

看源代码:

/* ----------------
* heap_getsysattr
*
* Fetch the value of a system attribute for a tuple.
*
* This is a support routine for the heap_getattr macro. The macro
* has already determined that the attnum refers to a system attribute.
* ----------------
*/
Datum
heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
{
Datum result; Assert(tup); /* Currently, no sys attribute ever reads as NULL. */
*isnull = false; switch (attnum)
{
case SelfItemPointerAttributeNumber:
/* pass-by-reference datatype */
result = PointerGetDatum(&(tup->t_self));
break;
case ObjectIdAttributeNumber:
result = ObjectIdGetDatum(HeapTupleGetOid(tup));
break;
case MinTransactionIdAttributeNumber:
result = TransactionIdGetDatum(HeapTupleHeaderGetXmin(tup->t_data));
break;
case MaxTransactionIdAttributeNumber:
result = TransactionIdGetDatum(HeapTupleHeaderGetXmax(tup->t_data));
break;
case MinCommandIdAttributeNumber:
case MaxCommandIdAttributeNumber: /*
* cmin and cmax are now both aliases for the same field, which
* can in fact also be a combo command id. XXX perhaps we should
* return the "real" cmin or cmax if possible, that is if we are
* inside the originating transaction?
*/

result = CommandIdGetDatum(HeapTupleHeaderGetRawCommandId(tup->t_data));
break;
case TableOidAttributeNumber:
result = ObjectIdGetDatum(tup->t_tableOid);
break;
default:
elog(ERROR, "invalid attnum: %d", attnum);
result = ; /* keep compiler quiet */
break;
}
return result;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848