加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL事务初始化过程概要

发布时间:2020-12-13 17:16:56 所属栏目:百科 来源:网络整理
导读:事务状态结构定义如下: typedefstructTransactionStateData{TransactionIdtransactionId;/*myXID,orInvalidifnone*/SubTransactionIdsubTransactionId;/*mysubxactID*/char*name;/*savepointname,ifany*/intsavepointLevel;/*savepointlevel*/TransStatesta

事务状态结构定义如下:

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。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读