当前位置:网站首页>PostgreSQL source code (60) transaction system summary
PostgreSQL source code (60) transaction system summary
2022-07-07 07:12:00 【mingjie73】
relevant
《Postgresql Source code (23)Clog The use of Slru Page elimination mechanism 》
《Postgresql Source code (27) Why does the transaction commit pass delayChkpt Blocking checkpoint》
《Postgresql Source code (59) Business ID Summary of value taking and judgment rules 》
To recapitulate PG Transaction management system :
PG The transaction processing in can be divided into two parts according to the functions provided : Basic transaction state management 、 Sub transaction state management .
PG The transaction system of can be summed up in one sentence : User commands trigger state machine functions, resulting in transaction state flow , Call the underlying transaction processing function to work according to the corresponding state during flow .
1 State machine flow system
User commands trigger state machine functions, resulting in transaction state flow , Call the underlying transaction processing function to work according to the corresponding state during flow .
State machine flow function
12 A state machine flow function , It can be divided into three categories .
- Package all single lines SQL Two functions of ( Get into SQL front StartTransactionCommand、SQL After execution CommitTransactionCommand), No matter what you do begin、 still select 1, I will walk through these two functions . It is equivalent to the passive flow of transaction state .
b StartTransactionCommand
b CommitTransactionCommand
- Transaction block processing function , Corresponding to user transaction commands , stay PortalRun It calls , Active circulation transaction status .
// System internal call rollback
b AbortCurrentTransaction
// User execution begin
b BeginTransactionBlock
// User execution commit
b EndTransactionBlock
// User execution rollback、abort
b UserAbortTransactionBlock
- Sub transaction status flow .
b DefineSavepoint
b ReleaseSavepoint
b RollbackToSavepoint
b BeginInternalSubTransaction
b RollbackAndReleaseCurrentSubTransaction
b AbortOutOfAnyTransaction
The underlying transaction processing function
When the state machine flows, it will call the underlying function to work , There are two types of functions
- Basic transaction function
// Called when the transaction is started , Configure transaction status , Apply for resources
StartTransaction
// The normal commit of a transaction is to call
CommitTransaction
// Called when the transaction is rolled back , First tune AbortTransaction, In tune CleanupTransaction
CleanupTransaction
// Called when the transaction is rolled back
AbortTransaction
- Sub transaction function
StartSubTransaction
CommitSubTransaction
AbortSubTransaction
CleanupSubTransaction
2 What does the underlying transaction function do ?
StartTransaction
- Get vxid( from backendid and localid Combination value ) Represents a virtual transaction id( Writing hasn't happened yet , Cannot assign real xid)
- use vxid register MyProc, After registration, you can find vxid lock , Indicates that the transaction started
- The transaction status flows to TRANS_INPROGRESS
StartTransaction
// vxid = {backendId = 3, localTransactionId = 76407}
GetNextLocalTransactionId
VirtualXactLockTableInsert
...
s->state = TRANS_INPROGRESS;
...
CommitTransaction( There are write operations in the transaction )
Assigned transactions ID Scene
- Transaction status flow TRANS_COMMIT
- Open for checkpoint The critical area of :MyProc->delayChkpt = true(《Postgresql Source code (27) Why does the transaction commit pass delayChkpt Blocking checkpoint》)
- Write commit Transaction log for 、 Brush the transaction log
- Write clog( Don't brush )TransactionIdCommitTree
- clear ProcArray Medium xid Information
- Clean up other
- Transaction status flow TRANS_DEFAULT
CommitTransaction
s->state = TRANS_COMMIT
RecordTransactionCommit
[START_CRIT_SECTION]
[[MyProc->delayChkpt = true]]
XactLogCommitRecord
XLogFlush
TransactionIdCommitTree
[[MyProc->delayChkpt = false]]
[END_CRIT_SECTION]
ProcArrayEndTransaction
// Can get the lock and clean it normally , Can't get the lock and add list It's waiting to be cleaned up later
LWLockConditionalAcquire(ProcArrayLock, LW_EXCLUSIVE)
// Normal cleaning : clear MyProc and ProcGlobal It records xid Information
ProcArrayEndTransactionInternal
...
// clear
...
s->state = TRANS_DEFAULT
CommitTransaction( There is no write operation in the transaction )
Unassigned transaction ID
- Transaction status flow TRANS_COMMIT
- clear
- Transaction status flow TRANS_DEFAULT
CommitTransaction
s->state = TRANS_COMMIT
RecordTransactionCommit // do nothing
// clear
s->state = TRANS_DEFAULT
3 Business ID Distribute
Before you really want to write data , Would call GetCurrentTransactionId, such as heap_insert.
- Take a new one xid
- Configuration to MyProc->xid
- Configuration to ProcGlobal->xids[MyProc->pgxactoff]
- xid Lock XactLockTableInsert
TransactionId
GetCurrentTransactionId(void)
{
TransactionState s = CurrentTransactionState;
if (!FullTransactionIdIsValid(s->fullTransactionId))
AssignTransactionId(s);
return XidFromFullTransactionId(s->fullTransactionId);
}
If not assigned , perform AssignTransactionId Take a new one xid Assigned to TransactionState. Refer to this article (《Postgresql Source code (59) Business ID Summary of value taking and judgment rules 》)
AssignTransactionId
...
GetNewTransactionId
...
MyProc->xid = xid;
ProcGlobal->xids[MyProc->pgxactoff] = xid;
...
XactLockTableInsert
...
4 Sub transaction system
Example :
drop table t1;
create table t1 (c1 int, c2 int);
begin;
insert into t1 values (1,1);
savepoint a;
insert into t1 values (2,1);
savepoint b;
insert into t1 values (3,1);
Here are some differences with ordinary affairs :
State of affairs : Sub transactions will make CurrentTransactionState It has a multi-layer structure , Used between parent Connect .
p *CurrentTransactionState
$39 = {fullTransactionId = {value = 4000071}, subTransactionId = 3, name = 0x199ca60 "b", savepointLevel = 0,
state = TRANS_INPROGRESS, blockState = TBLOCK_SUBINPROGRESS, nestingLevel = 3, gucNestLevel = 3,
curTransactionContext = 0x19de090, curTransactionOwner = 0x192a458, childXids = 0x0, nChildXids = 0, maxChildXids = 0,
prevUser = 10, prevSecContext = 0, prevXactReadOnly = false, startedInRecovery = false, didLogXid = false,
parallelModeLevel = 0, chain = false, assigned = false, parent = 0x199c558}
p *CurrentTransactionState->parent
$40 = {fullTransactionId = {value = 4000070}, subTransactionId = 2, name = 0x199c6c0 "a", savepointLevel = 0,
state = TRANS_INPROGRESS, blockState = TBLOCK_SUBINPROGRESS, nestingLevel = 2, gucNestLevel = 2,
curTransactionContext = 0x19d7200, curTransactionOwner = 0x19164c8, childXids = 0x0, nChildXids = 0, maxChildXids = 0,
prevUser = 10, prevSecContext = 0, prevXactReadOnly = false, startedInRecovery = false, didLogXid = true, parallelModeLevel = 0,
chain = false, assigned = false, parent = 0xe6a940 <TopTransactionStateData>}
p *CurrentTransactionState->parent->parent
$41 = {fullTransactionId = {value = 4000069}, subTransactionId = 1, name = 0x0, savepointLevel = 0, state = TRANS_INPROGRESS,
blockState = TBLOCK_INPROGRESS, nestingLevel = 1, gucNestLevel = 1, curTransactionContext = 0x199c400,
curTransactionOwner = 0x191e3e8, childXids = 0x0, nChildXids = 0, maxChildXids = 0, prevUser = 10, prevSecContext = 0,
prevXactReadOnly = false, startedInRecovery = false, didLogXid = true, parallelModeLevel = 0, chain = false, assigned = false,
parent = 0x0}
Allocating transactions ID when , Each sub transaction will get its own XID.
AssignTransactionId
...
SubTransSetParent(XidFromFullTransactionId(s->fullTransactionId),
XidFromFullTransactionId(s->parent->fullTransactionId));
And put yourself on a higher level xid It was recorded that subtrans in (SLRU Page and CLOG The same mechanism is used , This article is part of 《Postgresql Source code (23)Clog The use of Slru Page elimination mechanism 》).
Sub transaction commit
- In addition to the above (CommitTransaction( There are write operations in the transaction )) The steps mentioned
- Writing CLOG when , It is different from the case without sub transactions
- If there are no sub transactions , direct writing CLOG that will do
- When there are sub transactions
- Writing CLOG First of all, the sub business XID To configure SUB_COMMITTED State to CLOG in
- And then the father XID Configure the submission status of
- And then we can deal with the sub business XID The state of the from SUB_COMMITTED Become submitted ( Similar to a two-stage submission )
CommitTransaction
RecordTransactionCommit
TransactionIdCommitTree
TransactionIdSetTreeStatus
// One CLOG The page is all done
TransactionIdSetPageStatus
TransactionIdSetPageStatusInternal
// every last subxid All configured TRANSACTION_STATUS_SUB_COMMITTED
for
TransactionIdSetStatusBit
// Configure
TransactionIdSetStatusBit
// Then configure the submission of sub transactions
for
TransactionIdSetStatusBit
边栏推荐
- Fast quantitative, abbkine protein quantitative kit BCA method is coming!
- 2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
- Brand · consultation standardization
- Advantages of using net core / why
- Jetpack compose is much more than a UI framework~
- $parent (get parent component) and $root (get root component)
- 工具类:对象转map 驼峰转下划线 下划线转驼峰
- Please ask a question, flick Oracle CDC, read a table without update operation, and repeatedly read the full amount of data every ten seconds
- Networkx drawing and common library function coordinate drawing
- MySQL SQL的完整处理流程
猜你喜欢
Matlab tips (30) nonlinear fitting lsqcurefit
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
Prime partner of Huawei machine test questions
How can gyms improve their competitiveness?
MOS tube parameters μ A method of Cox
组件的嵌套和拆分
Bus消息总线
数据资产管理与数据安全国内外最新趋势
Non empty verification of collection in SQL
虚拟机的作用
随机推荐
Torefs API and toref API
2022年全国所有A级景区数据(13604条)
libcurl返回curlcode说明
Anr principle and Practice
main函数在import语句中的特殊行为
Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
Implementation of AVL tree
Learning records on July 4, 2022
Tumor immunotherapy research prosci Lag3 antibody solution
虚拟机的作用
Databinding exception of kotlin
$refs:组件中获取元素对象或者子组件实例:
Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
Asynchronous components and suspend (in real development)
MySQL SQL的完整处理流程
Lvs+kept (DR mode) learning notes
Common function detect_ image/predict
.net core 访问不常见的静态文件类型(MIME 类型)
Data of all class a scenic spots in China in 2022 (13604)