当前位置:网站首页>PostgreSQL source code learning (17) -- mvcc ② - Introduction to snapshot and isolation level
PostgreSQL source code learning (17) -- mvcc ② - Introduction to snapshot and isolation level
2022-06-11 03:19:00 【Hehuyi_ In】
One 、 Snapshot introduction
Snapshot is a data structure that records the current transient state of a database .pg The snapshot of is mainly saved : Minimum of all currently active transactions 、 The biggest business ID、 List of currently active transactions 、commandID etc. .
Snapshots can be divided into many types , Each snapshot type corresponds to a method for judging tuple visibility .
Two 、 Snapshot structure
1. Structure definition
typedef struct SnapshotData *Snapshot;
typedef struct SnapshotData
{
SnapshotType snapshot_type; /* type of snapshot, Snapshot type */
/* be used for MVCC control */
TransactionId xmin; /* all XID < xmin are visible to me, If business ID Less than xmin, Is visible to the current transaction */
TransactionId xmax; /* all XID >= xmax are invisible to me, If business ID Greater than xmax, Is not visible to the current transaction */
/* List of active transactions corresponding to snapshot generation */
TransactionId *xip;
uint32 xcnt; /* # of xact ids in xip[],xip Array size */
/* List of active sub transactions */
TransactionId *subxip;
int32 subxcnt; /* # of xact ids in subxip[],sub xip Array size */
bool suboverflowed; /* has the subxip array overflowed? When there are too many sub transactions , Arrays are possible overflow */
bool takenDuringRecovery; /* recovery-shaped snapshot? */
bool copied; /* false if it's a static snapshot, If it is a static snapshot; otherwise false */
CommandId curcid; /* in my xact, CID < curcid are visible */
/*
* An extra return value for HeapTupleSatisfiesDirty, not used in MVCC
* snapshots.,HeapTupleSatisfiesDirty A variable used , While judging visibility , With the help of this variable, the speculativeToken Back to the top
*/
uint32 speculativeToken;
/*
* For SNAPSHOT_NON_VACUUMABLE (and hopefully more in the future) this is
* used to determine whether row could be vacuumed. Whether tuples can be vacuum( be used for SNAPSHOT_NON_VACUUMABLE state )
*/
struct GlobalVisState *vistest;
/*
* Book-keeping information, used by the snapshot manager
*/
uint32 active_count; /* refcount on ActiveSnapshot stack */
uint32 regd_count; /* refcount on RegisteredSnapshots */
pairingheap_node ph_node; /* link in the RegisteredSnapshots heap */
TimestampTz whenTaken; /* timestamp when snapshot was taken, Snapshot generation time */
XLogRecPtr lsn; /* position in the WAL stream when taken, Transactions when snapshots are generated REDO The log LSN*/
/*
* The transaction completion count at the time GetSnapshotData() built
* this snapshot. Allows to avoid re-computing static snapshots when no
* transactions completed since the last GetSnapshotData().
*/
uint64 snapXactCompletionCount;
} SnapshotData;
#endif /* SNAPSHOT_H */2. Meaning of main fields
The most important thing in snapshots is xmin,xmax,xip( contain subxip), They are used to represent the status of all transactions in the current system .
testdb=# SELECT txid_current_snapshot();
txid_current_snapshot
-----------------------
100:104:100,102
(1 row)this 3 The first value is xmin:xmax:xip[] Array , Be careful not to associate with tuples t_xmin and t_xmax Muddle up
- Snapshot->xmin: The smallest of all currently active transactions txid, All earlier transactions (xid<xmin), Either committed or rolled back , So we can see .
- Snapshot->xmax:Snapshot->xmax=latestCompletedXid+1,latestCompletedXid The largest committed transactions are recorded id, therefore Snapshot->xmax Compared with all committed transactions id All big . If a transaction xid>= Snapshot->xmax, It indicates that this transaction is still active when the snapshot is created ( Or has not been started ) Therefore, the result is not visible to the current transaction .
- xip[] Array : On snapshot creation , Transactions of all active transactions id list , This list contains only [xmin,xmax) Active transactions in scope xid.
Let's take a little example

100:100: The meaning is as follows
- xmin=100, therefore txid<100 Are not active
- xmax=100, therefore txid>=100 All transactions of are active or not started
- xip_list It's empty , Express [xmin,xmax) No active transactions in scope
100:104:100,102 The meaning is as follows
- xmin=100, therefore txid<100 Are not active
- xmax=104, therefore txid>=104 All transactions of are active or not started
- xip_list by 100,102, Express [xmin,xmax) Within the scope of 100,102 For active business
3. Snapshot and isolation levels
pg It will be set according to different isolation levels , Take snapshots at different times :
- Read committed : In the... Of this transaction Every one of them SQL Perform before Will take a snapshot again
- Repeatable and serializable : This transaction only occurs in Article 1 with a SQL Perform before Take a snapshot

3、 ... and 、 Snapshot type
As mentioned earlier , Snapshots can be divided into many types , Each snapshot type corresponds to a method for judging tuple visibility , The snapshot type structure is in SnapshotType In the definition of .
typedef enum SnapshotType
{
SNAPSHOT_MVCC = 0,
SNAPSHOT_SELF,
SNAPSHOT_ANY,
SNAPSHOT_TOAST,
SNAPSHOT_DIRTY,
SNAPSHOT_HISTORIC_MVCC,
SNAPSHOT_NON_VACUUMABLE
} SnapshotType;Judgment conditions for visibility of various types of snapshots :
Snapshot type | explain |
SNAPSHOT_MVCC | Used to implement MVCC Snapshot type of , The most important type of snapshot . Visible condition :
Invisibility :
|
SNAPSHOT_SELF | Its visibility has little to do with the time the snapshot was created , It mainly depends on the transaction version information recorded on the tuple . Visible condition :
Invisibility :
|
SNAPSHOT_ANY | All tuples scanned are visible , It has nothing to do with tuple version and snapshot information |
SNAPSHOT_TOAST | be used for toast Tuple checking ,toast The visibility of tuples depends on the visibility of tuples in the main table , Therefore, the judgment logic can be simplified |
SNAPSHOT_DIRTY | Visible condition :
Follow SNAPSHOT_SELF The difference is that it also sees active transactions ( This is why it is called dirty snapshot , Equivalent to dirty reading ) |
SNAPSHOT_HISTORIC_MVCC | Used to judge the visibility of logical decoding in logical replication |
SNAPSHOT_NON_VACUUMABLE | Determine whether tuples are visible to some transactions , For example, dead tuples (VACUUMABLE state ) It's invisible |
3、 ... and 、 Isolation level viewing and setting
View the database transaction isolation level
SELECT name, setting FROM pg_settings WHERE name ='default_transaction_isolation';
-- or
SELECT current_setting('default_transaction_isolation');Set the global transaction isolation level
Law 1 :
modify postgresql.conf file Medium default_transaction_isolation
Law two :
alter system set default_transaction_isolation to 'REPEATABLE READ';View the current session transaction isolation level
show transaction_isolation
or
SELECT current_setting('transaction_isolation');Set the transaction isolation level of the current session
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;Set the transaction isolation level of the current transaction
START TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
or
BEGIN ISOLATION LEVEL READ UNCOMMITTED READ WRITE;Reference resources
《postgresql actual combat 》
The Internals of PostgreSQL : Chapter 5 Concurrency Control
《PostgreSQL Technology insider : Deep exploration of transaction processing 》 The first 3 Chapter
边栏推荐
- 数据库唯一索引和普通索引的区别?
- postgresql源码学习(十七)—— MVCC②-快照与隔离级别简介
- Flink开发环境搭建及WordCount
- Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique
- Explication du cours de graphacademy: neo4j Graph Data Science Foundation
- How to ensure reliable delivery of messages?
- Operations on annotation and reflection
- 数组全全排列
- 2022年6月中国数据库排行榜:TiDB卷土重来摘桂冠,达梦蛰伏五月夺探花
- org. apache. solr. common. SolrException:Could not load core configuration for core hotel
猜你喜欢

單片機通信數據延遲問題排查

ThoughtWorks. QRcode full-featured generator

WinDbg virtual machine dual machine debugging driver file debugging

Operations on annotation and reflection

Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?

Troubleshooting of single chip microcomputer communication data delay

GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》

Arduino uno connected to jq8900-16p voice broadcast module

三维GIS行业需求及展望

计算机视觉(AI)面试大全
随机推荐
Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique
C语言指针
postgresql源码学习(二十)—— 故障恢复①-事务日志格式
Multivalued mapping: arraylistmultimap and hashmultimap
023 MySQL索引优化口诀-索引失效的常见情况
Operations on annotation and reflection
Unity之数据持久化——Json
VMware virtual machine IP, gateway settings. The virtual machine cannot be pinged to the Internet
postgresql源码学习(十八)—— MVCC③-创建(获取)快照
ORA-00392 ORA-00312 错误处理
MySQL learning notes: JSON nested array query
Error in Solr access: error initializing queryelevationcomponent
pip 安装 qt5 。
File compositor
Go quick start of go language (I): the first go program
Whether the outbound sequence is the inbound sequence
Vocabulary Construction -- code completion fast food tutorial (3) - word segmentation
Determine whether a string of numbers is the result of a quick sort
com. mchange. v2.c3p0. Combopooleddatasource red
Correct posture of cocoscreator's native secondary development