Sobes.tech
Senior

What levels of transaction isolation exist in PostgreSQL?

sobes.tech AI

Answer from AI

In PostgreSQL, there are four standard transaction isolation levels defined by the SQL standard:

  1. Read Uncommitted — the lowest isolation level. Allows seeing uncommitted changes from other transactions (dirty reads). In PostgreSQL, it effectively behaves like Read Committed.

  2. Read Committed — the default level in PostgreSQL. A transaction sees only data that was committed at the start of each individual SQL query. Non-repeatable reads are possible.

  3. Repeatable Read — a transaction sees data as it was at the start of the transaction. It guarantees that repeated reads of the same data within a transaction will be identical. Phantom reads can occur in some DBMSs, but in PostgreSQL, they are prevented by MVCC.

  4. Serializable — the strictest isolation level. Transactions execute as if they were sequential, one after another. It prevents phantom reads and all anomalies but may lead to transaction rollbacks in case of conflicts.

Example of setting the isolation level in Java (JDBC):

connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

Thus, PostgreSQL supports all standard isolation levels, but the behavior of Read Uncommitted is equivalent to Read Committed.