当前位置:网站首页>PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
2022-07-27 21:25:00 【mingjie73】
relevant :
《Postgresql Source code (18)PGPROC Related structure 》
《Postgresql Source code (65) New snapshot system Globalvis Working principle analysis 》
《Postgresql Snapshot optimization Globalvis New system analysis ( Great performance enhancements )》
《Improving Postgres Connection Scalability: Snapshots》
Some historical analysis :
《Postgresql Source code (28) Take a snapshot GetSnapshotData Process analysis and performance issues 》
1 The optimized allProcs Array
First introduce the new version PG Deleted PGXACT After structure , Business ID How did you save it :
- Previous PGPROC and PGXACT The structure is one-to-one , One for each backend PGPROC、 One PGXACT, Business ID Recorded in the PGXACT in , The question is like that mentioned in the previous article ,PGXACT Not compact , Traversal cannot be used efficiently cache.
- In the new version , stay PROC_HDR From the Central Plains allPgXact Be missing ( Point to PGXACT Big array ), In its place xids Compact array , Be careful ! This array and pgprocnos The index is corresponding .
- So find one PROC Of xid, Except directly from PGPROC Chinese Reading , Can also from PROC_HDR->xids Read , This array is compact , Traversal is more efficient .

debug Command memo ( hypothesis numProcs=3)
p *procArray
p allProcs[procArray->pgprocnos[0]]
p allProcs[procArray->pgprocnos[1]]
p allProcs[procArray->pgprocnos[2]]
p ProcGlobal->xids[0]
p ProcGlobal->xids[1]
p ProcGlobal->xids[2]
2 GetSnapshotData The first part —— Cycle through active transactions
summary :
- Traverse
ProcGlobal->xidsArray , Get all the xid( Optimize one : Compact array ). - Active xid Added to the snapshot xip Array .
- By the way xmin( Function stack variables ) Record the smallest active transaction id.
Read PGXACT->xmin( Optimization II )- xmax Just before entering the cycle , It must be latestCompletedXid+1.
- Finally, if MyProc Of xmin It's invalid , Updated once
PGPROC->xmin( Or update your own xmin, It's a must , No optimization ) - The whole process is wrapped in ProcArrayLock In the big lock .
Code flow :
GetSnapshotData
...
LWLockAcquire(ProcArrayLock, LW_SHARED)
...
for (int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
TransactionId xid = UINT32_ACCESS_ONCE(other_xids[pgxactoff]);
/* If xid>=xmax You can skip , Because of this xid It started after I took the snapshot , Definitely invisible to me */
if (!NormalTransactionIdPrecedes(xid, xmax))
continue;
/* If xid < xmin,xid Also include xmin in ,xmin Represents the smallest active transaction */
if (NormalTransactionIdPrecedes(xid, xmin))
xmin = xid;
/* Record to snapshot */
xip[count++] = xid;
/* Updated once xmin */
if (!TransactionIdIsValid(MyProc->xmin))
MyProc->xmin = TransactionXmin = xmin;
LWLockRelease(ProcArrayLock);
3 GetSnapshotData The second part —— maintain GlobalVis
This part should be compared with the historical code .
PG10
- 【0】 The previous snapshot has been calculated . below “ incidentally ” Calculate the global minimum cleanable site :RecentGlobalXmin.
- 【1】 Get the smallest transaction ID.
- 【2】 The smallest cleanable site is passed to RecentGlobalXmin, If it's with vacuum_defer_cleanup_age, Put the smallest cleanable site , Push forward , More transactions will be preserved during cleanup , For standby machines with high latency .
- 【3】 If the replication slot specifies a site ,vacuum It can't be cleaned up .
GetSnapshotData
//【0】 The previous snapshot has been calculated . below “ incidentally ” Calculate the global minimum cleanable site :RecentGlobalXmin
//【1】 Get the smallest transaction ID
if (TransactionIdPrecedes(xmin, globalxmin))
globalxmin = xmin;
//【2】 The smallest cleanable site is passed to RecentGlobalXmin, If it's with vacuum_defer_cleanup_age
// Put the smallest cleanable site , Push forward , More transactions will be preserved during cleanup , For standby machines with high latency
RecentGlobalXmin = globalxmin - vacuum_defer_cleanup_age;
if (!TransactionIdIsNormal(RecentGlobalXmin))
RecentGlobalXmin = FirstNormalTransactionId;
//【3】 If the replication slot specifies a site ,vacuum It can't be cleaned up ;
if (TransactionIdIsValid(replication_slot_xmin) &&
NormalTransactionIdPrecedes(replication_slot_xmin, RecentGlobalXmin))
RecentGlobalXmin = replication_slot_xmin;
...
...
PG14
Test scenarios :
Business one RC-----------4000440 start-up -------------------4000440 end ------------------------------->
Business two RR----------------------4000450 start-up -----------------------------4000450 end ---------->
Business three RC-------------------------------------------- Commissioning location ------------------------------->
Commissioning location :
GetSnapshotData
//【0】 The previous snapshot has been calculated . below “ incidentally ” Calculate the global minimum cleanable site , But because there is no traversal PGPROC Of xmin, Only by oneself xid,
/* maintain state for GlobalVis* */
{
TransactionId def_vis_xid;
TransactionId def_vis_xid_data;
FullTransactionId def_vis_fxid;
FullTransactionId def_vis_fxid_data;
FullTransactionId oldestfxid;
//【1】oldestfxid = 726
oldestfxid = FullXidRelativeTo(latest_completed, oldestxid);
//【2】def_vis_xid_data = 4000450
def_vis_xid_data =
TransactionIdRetreatedBy(xmin, vacuum_defer_cleanup_age);
//【3】def_vis_xid_data = 4000450
def_vis_xid_data =
TransactionIdOlder(def_vis_xid_data, replication_slot_xmin);
//【4】def_vis_xid = 4000450
def_vis_xid = def_vis_xid_data;
def_vis_xid = TransactionIdOlder(replication_slot_catalog_xmin, def_vis_xid);
def_vis_fxid = FullXidRelativeTo(latest_completed, def_vis_xid);
def_vis_fxid_data = FullXidRelativeTo(latest_completed, def_vis_xid_data);
//【5】4000440 ----> 4000450
GlobalVisSharedRels.definitely_needed =
FullTransactionIdNewer(def_vis_fxid,
GlobalVisSharedRels.definitely_needed);
...
//【6】4000440 ----> 4000440
GlobalVisSharedRels.maybe_needed =
FullTransactionIdNewer(GlobalVisSharedRels.maybe_needed,
oldestfxid);
...
}
summary :
- 【5】4000440 ----> 4000450:definitely_needed The calculation actually uses the smallest one swept out of the above cycle xid, After fine adjustment of parameters (vacuumdelay And copy slots ),definitely_needed The meaning of is easy to understand , All running transactions are >= The value of .
- 【6】4000440 ----> 4000440:maybe_needed The value is :newer( Your own value 、 The smallest frozen transaction ID), The meaning of this value is : Transactions less than this value should be inactive .
Inactive < maybe_needed < ... < definitely_needed <= active, that maybe_needed and definitely_needed How to know whether the intermediate value has been submitted ?
Look at the following function :
bool
GlobalVisTestIsRemovableFullXid(GlobalVisState *state,
FullTransactionId fxid)
{
// Than maybe_needed The small ones must have been inactive , Can be cleaned !
if (FullTransactionIdPrecedes(fxid, state->maybe_needed))
return true;
// Than definitely_needed Greater than or equal to , It must be active , Can't clean up !
if (FullTransactionIdFollowsOrEquals(fxid, state->definitely_needed))
return false;
// What about the middle ? call GlobalVisUpdate
if (GlobalVisTestShouldUpdate(state))
{
// call ComputeXidHorizons Calculate each PROC Of xmin, And then update to GlobalVis variable .
GlobalVisUpdate();
Assert(FullTransactionIdPrecedes(fxid, state->definitely_needed));
return FullTransactionIdPrecedes(fxid, state->maybe_needed);
}
else
return false;
}
- ( A rough judgment ) In judging a specific XID When it can be cleaned , If you compare definitely_needed Big ( Or wait ), Or ratio maybe_needed Small , Can return directly .
- ( Judge accurately when necessary ) In judging maybe_needed and definitely_needed In the middle of the xid when , Follow the old logic before optimization , Through each PGPROC Of xmin, Get the smallest xmin, And then call GlobalVisUpdateApply Update global variables GlobalVisSharedRels.maybe_needed、GlobalVisSharedRels.definitely_needed Value , Provided for later functions .
4 ( important ) After optimization, it is not read in the loop PGXACT->xmin What will be the impact ?
- loop PGPROC when , Can't see other processes xmin 了 , for example , One RR Business A When taking snapshots , Minimum business ID:10 It hasn't been submitted yet , So this RR Snapshot xmin Namely 10.
- Before optimization ,A New transaction after transaction B When constructing snapshots , Can pass A Of PGPROC notice xmin=10, And update your own xmin=10.
- After optimization ,A New transaction after transaction B When constructing snapshots , You can only see the smallest when you traverse xid, For example, at this time 10 Has submitted ,B To see is 15, Then the post constructed snapshot will be considered as the global minimum xmin=15, But because of A Of xmin yes 10, So the global cleanup site needs to be 10, It can't be 15.
边栏推荐
- Rust variable characteristics
- 论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles
- Dobot magician robot arm - Introduction
- Obtain website shell permission based on file upload vulnerability
- Lidar China's front loading curtain opens, millions of production capacity to be digested
- “地理-语言”大模型文心ERNIE-GeoL及应用
- How to check whether there is Tsinghua source / delete Tsinghua source and keep the default source
- 数字化工厂管理系统有哪些价值
- What is the value of digital factory management system
- Behavior level description and RTL level description
猜你喜欢

数字化工厂系统有什么现实优势

API gateway introduction

Knife4j dynamically refreshes global parameters through JS

PHP code audit 5 - XSS vulnerability

Zhongdi Digital: integrating innovative domestic GIS to boost the construction of real 3D China

Typoa spelling check: missing dictionary file for Chinese
![论文赏析[EMNLP18]用序列标注来进行成分句法分析](/img/99/98f3e5795407c764907e957f69df10.png)
论文赏析[EMNLP18]用序列标注来进行成分句法分析

MAPGIS 3D scene rendering technology and Application

The solution that the laptop can connect to WiFi but the browser cannot open the web page

中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
随机推荐
A new technical director asked me to do an IP territorial function~
Natapp intranet penetration tool Internet access personal projects
Leetcode daily practice 206. Reverse the linked list
多人协作开发规范
Chapter 7 Intermediate Shell Tool I
App test positioning method
Good driving, inexpensive, comfortable and safe! Experience BYD song Pro DM-I in depth
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
Can single mode and multi-mode of industrial switches replace each other?
中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
PHP代码审计5—XSS漏洞
R language uses t The test function performs a t-test to verify whether the population mean is a specific value (inferring the population mean from the sample set)
电脑微软账户登录一直转圈怎么解决问题
The new CTO strongly prohibits the use of calendar?
Simple use of express web server
LabVIEW learning note 9: capture the "value change" event generated by the program modifying the control value
报表设计丨如何让你的PowerBI看板出彩?
MAPGIS 3D scene rendering technology and Application
Tips for file upload to bypass WAF
图解 SQL,这也太形象了吧!