Database Isolation Levels: How to Choose One
An isolation level defines when concurrent transactions can see one another’s changes and how the database handles conflicting work. A lower level can handle many common operations simply, but some query results may change during a transaction. A stronger level offers stricter guarantees and may require transaction retries. In PostgreSQL 18, the default `READ COMMITTED` is sufficient for many tasks, while work that reads several rows and checks a business rule may need additional protection.
What does an isolation level protect against?
When two transactions read and change the same data at once, each can be correct on its own yet violate a business rule when they run together. Isolation levels limit these concurrency anomalies; they do not invent missing business rules or repair a flawed data model.
**Dirty read:** a transaction reads another transaction’s uncommitted change, which might later be rolled back.
**Nonrepeatable read:** a transaction reads the same row again and sees a different value because another transaction committed an update in between.
**Phantom read:** repeating a query with the same condition returns a different set of rows after another transaction commits.
**Serialization anomaly:** the outcome of successfully committed concurrent transactions cannot be explained by any order in which they ran one at a time. For example, two on-call workers each see that the other is on duty and independently turn off their own status, violating a rule that at least one must remain on call.
How do PostgreSQL 18’s levels differ?
| Requested level | Snapshot seen by reads | Important anomalies in PostgreSQL | Practical note |
|---|---|---|---|
READ UNCOMMITTED | A new snapshot for each statement, just like READ COMMITTED | Dirty reads do not occur; the others are possible | PostgreSQL has no behavior difference between these two requested levels. |
READ COMMITTED (default) | Each SQL statement sees data committed before that statement began | Nonrepeatable reads, phantoms, and serialization anomalies are possible | Two SELECTs in one transaction can see different committed states. |
REPEATABLE READ | A stable snapshot from the first query or data-changing statement in the transaction | Prevents dirty and nonrepeatable reads and, in PostgreSQL, phantoms; serialization anomalies remain possible | Conflicting updates can raise a serialization failure, so the whole transaction may need to be retried. |
SERIALIZABLE | A stable snapshot plus monitoring of concurrent read/write dependencies | Ensures successful transactions have an outcome equivalent to some serial order | The database may abort a transaction; handle SQLSTATE 40001 by retrying the entire transaction. |
The SQL standard defines four levels, but PostgreSQL implements three distinct behaviors. Its READ UNCOMMITTED acts like READ COMMITTED, so it does not allow dirty reads. PostgreSQL REPEATABLE READ also prevents phantom reads, a stronger guarantee than the standard minimum, but it does not prevent every serialization anomaly as SERIALIZABLE does. Other database products may implement the same level names differently, so do not assume identical guarantees when switching systems.
How can the default level lead to an oversold seat?
Assume a show has one seat left. Two purchase requests run at the same time; each first checks the remaining count and then inserts a reservation. If both read 1 before either records its reservation, both can proceed, even though only one seat exists. With READ COMMITTED, each statement gets its own snapshot, so splitting a check and an action across statements does not make them one protected business operation.
One option is to make the inventory change a single conditional update and check whether one row was actually changed. Both requests then contend on the same inventory row, using row locking and condition re-evaluation:
UPDATE event_inventory
SET remaining = remaining - 1
WHERE event_id = 42 AND remaining > 0
RETURNING remaining;One returned row means a seat was reserved; no row means there are no seats left or that the event does not exist. A real application should place this update and reservation record in one transaction and roll back both if either operation fails. Add constraints such as a unique reservation key where appropriate. This SQL is illustrative and was not run.
If a rule spans several rows or their combined values and cannot be represented by one update or a constraint, consider SERIALIZABLE or a deliberate locking design. A serializable transaction may be canceled with a 40001 serialization failure. Retry the whole transaction, including the reads used to make the business decision, rather than only the failed statement. Define retry limits and backoff, and keep difficult-to-reverse external effects—such as email or payment calls—safe from duplicate execution.
How should you choose?
**Simple, independent reads and updates:** Start by evaluating the default `READ COMMITTED`. It is often suitable for ordinary CRUD, but a check-then-act sequence across statements needs separate protection.
**Several reads that need one stable view:** `REPEATABLE READ` can be useful. It keeps a transaction’s view stable, but conflicting updates may require retries, and it does not guarantee that every business invariant is serializable.
**A combination of reads and writes must behave as if transactions ran one at a time:** Consider `SERIALIZABLE`. It offers the strongest guarantee, with the cost of detecting conflicts and retrying aborted transactions.
**A simple invariant on one inventory or balance row:** A conditional atomic update, a `CHECK` or `UNIQUE` constraint, or a targeted row lock may be more direct than raising the entire isolation level. Verify that this approach covers rules involving multiple rows.
Write down the invariant first, then identify whether it involves one statement, one row, or multiple rows. Decide whether the default level plus constraints and conditional updates are enough. Use a stronger level or locks only where needed, and validate concurrent requests, failures, and full retry paths in a representative test environment. No database setting or concurrency experiment was performed for this article.
Setting an isolation level in PostgreSQL
Choose the level before the first query or data-changing statement in a transaction. For example:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- Read and write data needed for the operation
COMMIT;With SERIALIZABLE or REPEATABLE READ, an error means the current transaction must be abandoned and the full logic rerun in a new transaction. Do not reuse reads from an aborted attempt or retry only one update. Avoid performing external side effects inside the retry block; use idempotency or an outbox pattern when needed. Keep transactions short because long-running transactions retain old snapshots and resources.
Scope and sources
[PostgreSQL 18: Transaction Isolation](https://www.postgresql.org/docs/18/transaction-iso.html)
[PostgreSQL 18: `SET TRANSACTION`](https://www.postgresql.org/docs/18/sql-set-transaction.html)
[PostgreSQL 18: Explicit Locking](https://www.postgresql.org/docs/18/explicit-locking.html)
[PostgreSQL 18: Error Codes and SQLSTATE](https://www.postgresql.org/docs/18/errcodes-appendix.html)
The PostgreSQL 18 documentation was checked on 2026-11-22. The anomaly terms are general SQL concepts; defaults, snapshots, guarantees, and retry behavior described here are scoped to PostgreSQL 18. Seat counts and simultaneous requests are hypothetical, not observed or tested.