Tehingute isolatsiooni tasemed PostgreSQL
sobes.tech AI
Vastus AI-lt
PostgreSQL supports four standard transaction isolation levels, defined in the SQL standard:
-
Read Uncommitted — the lowest isolation level. In PostgreSQL, it practically behaves like Read Committed, as it does not allow dirty reads.
-
Read Committed (default) — the transaction sees only data that has been committed at the start of each individual query. Non-repeatable reads and phantom reads can occur.
-
Repeatable Read — the transaction sees data as it was at the start of the transaction. Guarantees repeatable reads, but phantom reads are possible.
-
Serializable — the highest isolation level, ensures full transaction serialization, prevents phantom reads, and maintains data consistency as if transactions were executed sequentially.
Example of setting the isolation level in Go using database/sql and pq driver:
import (
"database/sql"
_ "github.com/lib/pq"
)
func setIsolationLevel(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
// Set the isolation level
_, err = tx.Exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")
if err != nil {
return err
}
// Additional operations within the transaction
return tx.Commit()
}