事务状态结构定义如下:
typedefstructTransactionStateData { TransactionIdtransactionId; /*myXID,orInvalidifnone*/ SubTransactionIdsubTransactionId; /*mysubxactID*/ char *name; /*savepointname,ifany*/ int savepointLevel;/*savepointlevel*/ TransState state; /*low-levelstate*/ TBlockStateblockState; /*high-levelstate*/ int nestingLevel; /*transactionnestingdepth*/ int gucNestLevel; /*GUCcontextnestingdepth*/ MemoryContextcurTransactionContext; /*myxact-lifetimecontext*/ ResourceOwnercurTransactionOwner; /*myqueryresources*/ TransactionId*childXids; /*subcommittedchildXIDs,inXIDorder*/ int nChildXids; /*#ofsubcommittedchildXIDs*/ int maxChildXids; /*allocatedsizeofchildXids[]*/ Oid prevUser; /*prevIoUsCurrentUserIdsetting*/ int prevSecContext;/*prevIoUsSecurityRestrictionContext*/ bool prevXactReadOnly; /*entry-timexactr/ostate*/ bool startedInRecovery; /*didwestartinrecovery?*/ bool didLogXid; /*hasxidbeenincludedinWALrecord?*/ structTransactionStateData*parent; /*backlinktoparent*/ }TransactionStateData; typedefTransactionStateData*TransactionState;
顶层事务状态初始定义,当前事务状态经常在运行过程中指向它:
staticTransactionStateDataTopTransactionStateData={ 0,/*transactionid*/ 0,/*subtransactionid*/ NULL,/*savepointname*/ 0,/*savepointlevel*/ TRANS_DEFAULT,/*transactionstate*/ TBLOCK_DEFAULT,/*transactionblockstatefromtheclient *perspective*/ 0,/*transactionnestingdepth*/ 0,/*GUCcontextnestingdepth*/ NULL,/*curtransactioncontext*/ NULL,/*curtransactionresourceowner*/ NULL,/*subcommittedchildXids*/ 0,/*#ofsubcommittedchildXids*/ 0,/*allocatedsizeofchildXids[]*/ InvalidOid,/*prevIoUsCurrentUserIdsetting*/ 0,/*prevIoUsSecurityRestrictionContext*/ false,/*entry-timexactr/ostate*/ false,/*startedInRecovery*/ false,/*didLogXid*/ NULL /*linktoparentstateblock*/ };
当任何语句(或语句块)执行前,都会检查当前事务状态,如果未开始事务,则新开始一个。可以在 src/backend/tcop/postgres.c 函数 exec_simple_query() 中看到相关代码循环体:
... start_xact_command(); ... for(parsetree_item,parsetree_list) ... start_xact_command(); ...
start_xact_command() 函数内:
此处判断事务状态并开始,会用到上述结构定义,并且可以看到使用的状态是 blockState 此时为 TBLOCK_DEFAULT。随后的 StartTransaction() 函数中 state(low-level state) 由 TRANS_DEFAULT 变为 TRANS_START,事务ID 为无效(InvalidTransactionId,也就是0)。
StartTransaction()中也可以看到 PG此时开始了一个虚事务(virtual transaction),并分配了一个本地事务ID。最终经过一系列初始化之后,state 变为 TRANS_INPROGRESS。
成功调用 StartTransaction() 之后 blockState 变为 TBLOCK_STARTED。
原文链接:https://www.f2er.com/postgresql/195092.html