当前位置:网站首页>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
边栏推荐
- Pyqt5: button control
- Pyqt5: basic usage of label control
- 关于玩家身上有个普通Set并发安全的讨论
- Vocabulary Construction -- code completion fast food tutorial (3) - word segmentation
- postgresql源码学习(二十)—— 故障恢复①-事务日志格式
- Pyqt5:slider slider control
- 【安全科普】今天你被社工了吗?
- 出栈序列是否是入栈序列
- File file = new file ("test.txt") file path
- In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
猜你喜欢

WinDbg-虚拟机-双机调试-驱动文件的调试

【安全科普】挖矿技术,从一个理工男的爱情故事讲起

Basic use of sonarqube platform

cv. Matchtemplate image model matching opencv

Disk quota exceeded

How to ensure reliable delivery of messages?

Pyqt5:qlineedit control code

Correct posture of cocoscreator's native secondary development

Graphacademy course explanation: Fundamentals of neo4j graph data science

intXX_ T and int_ fastXX_ T what is the difference- What is the difference between intXX_ t and int_ fastXX_ t?
随机推荐
HQChart实战教程55-欧易网K线面积图
B_QuRT_User_Guide(16)
Construction of Flink development environment and wordcount
[safety science popularization] mining technology starts from the love story of a man of science and Engineering
File file = new File(“test.txt“)文件路径
TCP/IP
DNS Optimization Practice of APP network optimization
[long time series prediction] aotoformer code detailed [3] model overall architecture analysis
A simple understanding of C language array
Stringutils string tool class used by FreeMarker to create templates
第七章 常用的协议简介(1)
ORA-00392 ORA-00312 错误处理
pip 安装 qt5 。
cv. Matchtemplate image model matching opencv
UBIFS FAQ and HOWTO
Demand and Prospect of 3D GIS Industry
Array full permutation
B_ QuRT_ User_ Guide(17)
配置用命令行编译的环境-MSVC
The two departments jointly issued the nine provisions on fire safety management of off campus training institutions