当前位置:网站首页>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->xmaxSnapshot->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 :

  •   All committed transactions at the time of snapshot creation
  • Commands executed before this transaction  

Invisibility :

  •   Transactions that were active at the time the snapshot was created
  • Transactions started after the snapshot was created
  • Changes caused by the current command (changes made by the current command) 

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 :

  •   All committed transactions at this time
  • Commands executed before this transaction
  • Changes caused by the current command  

Invisibility :

  • Active business at the moment  

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 :

  •   All submitted and Active Business
  • Commands executed before this transaction
  • Changes caused by the current command  

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

原网站

版权声明
本文为[Hehuyi_ In]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110248364882.html