Summary of the transaction isolation in postgresql

1 Introduction

DBA has a basic knowledge knows, PostgreSQL consistency in the control data is by using a multi-version model maintenance is often said that the multi-version concurrency control MVCC. This means that every sql statement to see just a short time snapshot of the database, otherwise known version, but does not care about the current state of the underlying data files among. Such benefits is to protect each session transactions are not affected by other sessions, provides a very good transaction isolation mechanism. MVCC model of concurrency control advantages relative to the locking mechanism that locks query data requests and data write lock request does not conflict, so, do not block writers read and write will not block readers.

2 Transaction Isolation

In fact, talking about transaction isolation, may be biased in favor of some theory, but for the number of DBA staff, the need to firmly grasp, because a lot of time in troubleshooting problems when abnormal, you need to analyze every sql, each transaction are made to the database what actions, why would cause inconsistent data and so on.

sql standard defines four levels of isolation: Uncommitted Read, Read Committed, repeatable read, serializable. Four relationships progressive layers, more and more stringent.

  • Read Uncommitted Read uncommitted
    explanation is that a transaction read data from another uncommitted transaction, another transaction is rolled back once, then the read data is problematic. This phenomenon is dirty read.

  • Read Committed Read committed
    data read has been submitted to solve the problem of dirty read because the transaction needs to wait to read the data after another transaction commits. But this level there is a problem, repeatable read, they might produce different data because the transaction process twice read, the data is not locked, you can still be in the ability to modify (update and delete) a. This level is the default level postgresql.

  • Repeatable read Repeatable read
    This level solves the problem of non-repeatable read to read committed, when the transaction is opened, the operation can not be modified until the transaction commits. This level is mysql, Oracle, sql server default level. But at that level, or there will be problems phantom reads, and phantom reads and re-reads are very similar, the two are inconsistent query results, can be understood, at the time of a transaction to insert a record that does not exist checked and found data already existed, before checking the result is like an illusion, this is because new data has been inserted due.

From the difference between the required mechanism, the data is read a line, the line will add row exclusive lock, the transaction can not modify the data, inconsistent data repeatable read problem does not occur, but did not add table lock, or you can insert data. This leads to the back of the query data 变多了. mysql will appear Magic Reading, pg will not.

  • Serialization Serializable
    sequence of transaction isolation level is the highest, at the level of transactions are performed in serial order to avoid dirty reads, repeatable read, phantom read. But imagine, all transactions are executed in accordance with the serialization order, how to ensure that high concurrency it? This database is bound performance is very low, almost no database to reach this level.

The following table summarizes the description of the SQL standard transaction isolation level and implemented in PG

Isolation Levels Dirty read Non-repeatable read Magic Reading Sequence exception
Uncommitted Read Allowed, but not in PG may may may
Read Committed impossible may may may
Repeatable read impossible impossible Allowed, but not in PG may
Serializable impossible impossible impossible impossible

3 summary

Transaction isolation is very important for DBA, DBA can change the default isolation level of the database based on business needs, it is expected to reach maximum performance.

Guess you like

Origin www.cnblogs.com/easonbook/p/10990299.html