日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

对PostgreSQL cmin和cmax的理解

發布時間:2024/7/19 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对PostgreSQL cmin和cmax的理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看例子:

開兩個終端來對比:

在終端A:

[pgsql@localhost bin]$ ./psql psql (9.1.2) Type "help" for help.pgsql=# begin; BEGIN pgsql=# select xmin,xmax,cmin,cmax,* from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+-----------+----1878 | 0 | 0 | 0 | 1 | 11884 | 0 | 0 | 0 | 999888777 | 2 (2 rows)pgsql=# insert into tab01 values(3,'3'); INSERT 0 1 pgsql=# insert into tab01 values(4,'4'); INSERT 0 1 pgsql=# select xmin,xmax,cmin,cmax,* from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+-----------+----1878 | 0 | 0 | 0 | 1 | 11884 | 0 | 0 | 0 | 999888777 | 21885 | 0 | 0 | 0 | 3 | 31885 | 0 | 1 | 1 | 4 | 4 (4 rows)pgsql=#

此時的終端B:

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

然后再在終端A進行提交:

pgsql=# commit; COMMIT pgsql=# select xmin,xmax,cmin,cmax,* from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+-----------+----1878 | 0 | 0 | 0 | 1 | 11884 | 0 | 0 | 0 | 999888777 | 21885 | 0 | 0 | 0 | 3 | 31885 | 0 | 1 | 1 | 4 | 4 (4 rows)pgsql=#

此時,再在終端B進行觀察:

pgsql=# select xmin,xmax,cmin,cmax, * from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+-----------+----1878 | 0 | 0 | 0 | 1 | 11884 | 0 | 0 | 0 | 999888777 | 21885 | 0 | 0 | 0 | 3 | 31885 | 0 | 1 | 1 | 4 | 4 (4 rows)pgsql=#

?繼續研究cmin是咋回事:

pgsql=# begin; BEGIN pgsql=# insert into tab01(id,cd) values(generate_series(5,6),'xx'); INSERT 0 2 pgsql=# select xmin,xmax,cmin,cmax,* from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+-----------+----1878 | 0 | 0 | 0 | 1 | 11884 | 0 | 0 | 0 | 999888777 | 21885 | 0 | 0 | 0 | 3 | 31885 | 0 | 1 | 1 | 4 | 41886 | 0 | 0 | 0 | 5 | xx1886 | 0 | 0 | 0 | 6 | xx (6 rows)pgsql=#

可以說cmin可理解為一個事務里,執行了幾次sql命令的順序。

那么cmax呢?在前面的基礎上繼續執行,居然沒有看到區別:

pgsql=# update tab01 set id=2 where cd = '2'; UPDATE 1 pgsql=# select xmin,xmax,cmin,cmax,* from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+----+----1878 | 0 | 0 | 0 | 1 | 11885 | 0 | 0 | 0 | 3 | 31885 | 0 | 1 | 1 | 4 | 41886 | 0 | 0 | 0 | 5 | xx1886 | 0 | 0 | 0 | 6 | xx1886 | 0 | 1 | 1 | 2 | 2 (6 rows)pgsql=# commit; COMMIT pgsql=# select xmin,xmax,cmin,cmax,* from tab01;xmin | xmax | cmin | cmax | id | cd ------+------+------+------+----+----1878 | 0 | 0 | 0 | 1 | 11885 | 0 | 0 | 0 | 3 | 31885 | 0 | 1 | 1 | 4 | 41886 | 0 | 0 | 0 | 5 | xx1886 | 0 | 0 | 0 | 6 | xx1886 | 0 | 1 | 1 | 2 | 2 (6 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 = 0; /* keep compiler quiet */break;}return result; }

?

轉載于:https://www.cnblogs.com/gaojian/p/3165178.html

總結

以上是生活随笔為你收集整理的对PostgreSQL cmin和cmax的理解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。