当前位置:网站首页>PG basics -- Logical Structure Management (transaction)

PG basics -- Logical Structure Management (transaction)

2022-07-06 20:49:00 51CTO

Transaction submission

pg The default is to automatically commit transactions , You can turn off the automatic submission function

Turn off auto submit

      
      
postgres =# \set autocommit off
postgres =# \echo :autocommit
off
  • 1.
  • 2.
  • 3.

savepoint

pg Support savepoint function , In a big business , The operation process can be divided into several parts , After the first part is successfully executed, a savepoint can be created , If the later part fails , Rollback to this savepoint , Instead of rolling back the entire transaction .

      
      
postgres =# \d test01 ;
Table "public.test01"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
id | integer | | not null |
note | character varying ( 20 ) | | |
Indexes :
"test01_pkey" PRIMARY KEY , btree (id )

postgres =# insert into test01 values ( 1 , '11' ) ;
INSERT 0 1
postgres =# select * from test01 ;
id | note
----+------
1 | 11
( 1 row )

postgres =# savepoint my_savepoint01 ;
SAVEPOINT
postgres =# insert into test01 values ( 2 , '22' ) ;
INSERT 0 1
postgres =# rollback to savepoint my_savepoint01 ;
ROLLBACK
postgres =# select * from test01 ;
id | note
----+------
1 | 11
( 1 row )
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

The second data inserted , Rollback to the first savepoint .


Transaction isolation level

and mysql equally ,pg The transaction isolation level of is also divided into 4 Kind of ,

read uncommited Read uncommitted

read commit Read submitted

repeatable read Repeatable

serializable Serialization

Dirty reading : One transaction reads uncommitted data from another transaction .

It can't be read repeatedly : When a transaction re reads the previously read data , It is found that the data has been modified by another transaction (update ,delete).

Fantasy reading : After a transaction has started , After querying the qualified data , Query again , More data found (insert).

pg Repeatable reading can avoid non repeatable reading , But unreal reading cannot be avoided , This and mysql equally . The difference is pg The default isolation level is read committed , and mysql The default is repeatable reading .


Two-phase commit

In distributed systems , Transactions often involve the operation of multiple databases , Although the operation of a single database can ensure atomicity , However, atomicity between multiple databases requires two-stage submission .

step

1) The application program first calls each database to do some operations , But don't commit the transaction , Then the application calls the commit method in the transaction coordinator

2) The transaction coordinator will contact each database involved in the transaction , And inform them that they are ready to commit the transaction , This is the beginning of the first phase , It's called PREPARE TRANSACTION.

3) Each database receives PREPARE TRANSACTION after , If the return is successful , Then the database must put itself in the following state : Ensure that subsequent transactions can be raised when they are submitted , Or the transaction can be rolled back later when required . therefore PG The information ready for submission will be written to the persistent storage , If the database cannot complete this transaction , It will directly return the failure to the transaction coordinator .

4) The transaction coordinator receives responses from all databases .

5) The second stage , If any database fails in the first stage , Then the transaction coordinator will send a rollback command to each database , If all database responses are successful , Send to each database commit prepared command . Notify each database that the transaction is submitted successfully .





原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061233054541.html