What levels of transaction isolation have you encountered in PostgreSQL?
sobes.tech AI
Answer from AI
PostgreSQL supports four standard transaction isolation levels:
-
Read Uncommitted — the lowest isolation level, where a transaction can see uncommitted changes made by other transactions. In PostgreSQL, it effectively behaves like Read Committed.
-
Read Committed — the default level. A transaction only sees changes that were committed at the start of each individual query within the transaction. Data can change between queries within the same transaction.
-
Repeatable Read — the transaction sees a snapshot of the data at the start of the transaction. All queries within the transaction see the same data, even if other transactions modify and commit changes.
-
Serializable — the strictest isolation level. Transactions are executed as if they were sequential, one after another, preventing any anomalies, including phantom reads.
Example of setting the isolation level in Go using database/sql:
db.Exec("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ")
// or at the start of a transaction
trx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})
Each isolation level balances between performance and data consistency.