本节通过源码解释了snapshot中的xmax的具体含义.

一、xmax

上一节提到PostgreSQL通过txid_current_snapshot()函数获取快照,格式为xmin : xmax : xip_list,其中xmax应理解为最后已完结事务(COMMITTED/ABORTED)的txid + 1。
详见以下PG源码:

SnapshotGetSnapshotData(Snapshot snapshot){ /* xmax is always latestCompletedXid + 1 */ xmax = ShmemVariableCache->latestCompletedXid; Assert(TransactionIdIsNormal(xmax)); TransactionIdAdvance(xmax); /* initialize xmin calculation with xmax */ globalxmin = xmin = xmax; ... snapshot->xmax = xmax; ... return snapshot;}

xmax is always latestCompletedXid + 1,最后已完结事务(COMMITTED/ABORTED)的txid + 1(ShmemVariableCache->latestCompletedXid + 1)。

二、参考资料

PostgreSQL Source Code