論PostgreSQL中的各種“xid”

論PostgreSQL中的各種“xid”

​ 在分析PostgreSQL 9.6.9(下文稱PG)的多版本(MVCC)問題時,經常打交道的就是Snapshot了,而Snapshot的構造過程中,我們會看到多種“xid”,而這些“xid”究竟如何賦值?作用又是什麼?說實話,我並沒有完全搞明白。今天,把這些“xid”做一些歸納總結,以便於以後分析定位問題。

/*
 * These are updated by GetSnapshotData.  We initialize them this way
 * for the convenience of TransactionIdIsInProgress: even in bootstrap
 * mode, we don't want it to say that BootstrapTransactionId is in progress.
 *
 * RecentGlobalXmin and RecentGlobalDataXmin are initialized to
 * InvalidTransactionId, to ensure that no one tries to use a stale
 * value. Readers should ensure that it has been set to something else
 * before using it.
 */
TransactionId TransactionXmin = FirstNormalTransactionId;
TransactionId RecentXmin = FirstNormalTransactionId;
TransactionId RecentGlobalXmin = InvalidTransactionId;
TransactionId RecentGlobalDataXmin = InvalidTransactionId;
1. 各種xid的生成

爲了說明這些“xid”是如何產生的,我們必須從GetSnapshotData着手。
這裏寫圖片描述
通過上圖,可看出:

  • xmax=ShmemVariableCache>latestCompletedXid+1 (考慮事務號輪轉問題)
  • globalxmin=xmin=max (初值賦定)

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

通過上圖,可看出:

  • globalxminallPgXact中所有PGXACT中最小的xmin
  • xminallPgXact中所有PGXACT中最小的xid
  • globalxminallPgXact中所有xidxmin中最小的。

這裏寫圖片描述
通過上圖,可看出:

  • allPgXact中的每個PGXACTxmin是如此賦值的。
  • TransactionXminallPgXact中所有PGXACT中最小的xid

這裏寫圖片描述

這裏寫圖片描述
通過上圖,可看出:

  • RecentGlobaXmin是所有allPgXact中最小的xminvacuum_defer_cleanup_age .

這裏寫圖片描述
通過上圖,可看出:

  • RecentGlobalXminRecentGlobalDataXmin 的,按照註釋來說應該是用於非系統表的vacuum操作的。

這裏寫圖片描述
通過上圖,可看出:

  • RecentXmin=xmin

總結一下:

  • snapshotxmax=xmax 是最近一次已完成事務+1。
  • snapshotxmin=xminallPgXact中最小的xid
  • globalxminallPgXact中所有xidxmin中最小的。
  • RecentGlobalXmin=globalxminvacuum_defer_cleanup_age
  • RecentGlobalDataXminRecentGlobalXmin
  • RecentXmin=xmin

如上所述,即有:

RecentGlobalXminRecentGlobalDataXminglobalxminMyPgXactxmin=TransactionXmin=RecnetXmin=snapshotxminsnapshotxmax

2. RecentGlobalDataXmin

the global xmin for non-catalog tables >= RecentGlobalXmin

這裏寫圖片描述

3. RecentGlobalXmin

the global xmin (oldest TransactionXmin across all running transactions, except those running LAZY VACUUM). This is the same computation done by GetOldestXmin(true, true).

這裏寫圖片描述

4. TransactionXmin

the oldest xmin of any snapshot in use in the current transaction (this is the same as MyPgXact->xmin).

這裏寫圖片描述

5. RecentXmin

the xmin computed for the most recent snapshot. XIDs older than this are known not running any more.

這裏寫圖片描述

還是不太明白,這些“xid”的具體作用,一頭霧水… …
未完待續… …

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章