A manager opens a report that says a product has ten units available. While the report is running, another transaction completes a sale. A second query in the same report now sees nine. Did the database make a mistake? Not necessarily. It may be giving each query a fresh view of committed data, exactly as the transaction’s isolation setting permits.
Transaction isolation governs which effects of concurrent transactions a transaction can observe and which combinations of operations may successfully commit. The choices and exact behavior depend on the database. This article uses PostgreSQL’s documented behavior as a concrete example; do not assume another product’s identically named setting has the same implementation. The PostgreSQL transaction-isolation guide explains its snapshots and possible anomalies.
Isolation is one part of application correctness. It does not replace a sensible data model, constraints, careful transaction boundaries or testing. Selecting a stronger level can change conflicts and retries; it does not turn an incomplete business rule into a complete one.
What a transaction is trying to protect
A transaction groups database operations into a unit that can commit or roll back. Consider a transfer between two internal account records: the application reduces one balance and increases another. The intended outcome is both changes together, not a permanently recorded debit with no corresponding credit. Actual financial systems need additional controls and ledgers; this simple example illustrates why the boundary matters.
Concurrency creates a separate question. While the transfer is underway, another transaction may read or update related records. Isolation determines what each operation can see and which concurrent outcomes the database permits. Atomicity and isolation are related but different: an all-or-nothing transaction can still participate in a surprising interaction with another all-or-nothing transaction if the isolation level allows it.
A database view is not necessarily a live camera showing every uncommitted change as it occurs. PostgreSQL uses multiversion concurrency control, so a query observes an appropriate snapshot under the configured rules. The application’s own previous writes within the transaction also matter. Identify the database, isolation level and order of events before calling a result stale or incorrect.
Read committed: a new view for each statement
PostgreSQL’s default is read committed. A simple SELECT sees data committed before that statement begins, plus its own transaction’s earlier changes. Two SELECT statements in the same transaction can therefore see different committed values if another transaction commits between them. They do not see another transaction’s uncommitted change.
Imagine transaction A reads an available quantity of ten. Transaction B then changes the quantity to nine and commits. If A issues a second SELECT after B’s commit, that later statement can see nine under read committed. The first answer was accurate to the first statement’s snapshot; the second uses a later one. The report may still be unsuitable for its purpose if the business needed a single consistent view across all sections.
This is a simplified read example. Updates and locking have additional behavior: a statement that tries to modify a row changed by another transaction can wait and then reconsider the relevant row under PostgreSQL’s rules. The read-committed documentation describes those details. Do not infer the outcome of a competing UPDATE solely from a pair of SELECT examples.
Repeatable read: one transaction snapshot
In PostgreSQL repeatable read, a transaction’s queries use a snapshot established at the start of its first non-transaction-control statement, rather than taking a new snapshot for each query. Subsequent reads in that transaction do not see changes another transaction commits after that snapshot. The transaction still sees its own changes. This makes a report’s related reads more consistent with one another, but it can mean that a later query does not reflect a newer committed change.
Return to the ten-unit example. A starts a repeatable-read transaction and performs its first relevant query, seeing ten. B sells one unit and commits. A’s later ordinary read in the same transaction still sees the earlier snapshot, along with any changes A itself made. A new transaction started after B’s commit can see nine. The time at which the snapshot is established matters; merely opening an application window is not the same as establishing a database snapshot.
PostgreSQL’s implementation is stronger than the SQL standard’s minimum repeatable-read guarantee in some respects, including phantom reads. It can still permit serialization anomalies. If a repeatable-read transaction attempts a conflicting update, PostgreSQL may require the application to abort and retry the whole transaction. Other databases can implement these names differently, so verify the actual product and version.
Serializable and the retry question
PostgreSQL serializable isolation aims to ensure that successfully committed concurrent transactions have an effect consistent with some serial order. It can detect dangerous patterns and abort a transaction with a serialization failure. The application must be prepared to retry the entire transaction, including rereading the data on which its decisions depended. A retry is part of the design, not an unexpected administrative footnote.
For a fictional scheduling example, two staff members are marked on call. Two transactions each check that another person is on call and then remove a different person. Each decision can look reasonable from its own snapshot, but the combined result may leave nobody on call. A serializable design can reject a conflicting execution so the application retries against a newer state. The exact outcome depends on the complete transactions and database behavior; test the real rule and handle failure rather than relying on this illustration as a substitute for a design.
Retries have consequences outside the database. If a transaction sends an email, charges a payment or calls another service before it commits, repeating the code may repeat an external action even when the database rolled back. Design the boundary, idempotency and delivery process intentionally. Our guide to idempotency keys explains why a repeated request is not automatically safe.
What a retry needs to repeat
Suppose a reservation transaction reads the remaining capacity, chooses a seat and writes a reservation. If the database rejects that transaction for a serialization conflict, rerunning only the final INSERT can preserve a decision based on the old snapshot. The application generally needs to repeat the decision-making reads and writes as a unit, using its documented retry policy. The second attempt may discover that the seat is no longer available, in which case a different business outcome may be correct.
Set a bounded retry policy, record conflicts and surface an understandable failure when the operation cannot complete. Blind, unlimited retries can amplify pressure when the database is already contended. Distinguish a serialization failure from unrelated errors; not every exception is safe or useful to retry. Verify the actual error codes and driver behavior for the database in use.
Locks, constraints and the business rule
Isolation is not the only way to protect an invariant. A unique constraint can prevent duplicate values, while an appropriate lock can coordinate specific competing operations. An atomic update with a condition can sometimes express the real rule more directly than reading a value, making a decision in application code and writing later. Each technique has limitations and can interact with transaction isolation.
Suppose an inventory operation must not sell more units than are available. The relevant rule includes the quantity requested, competing orders, cancellation and what a failed operation should tell the customer. A developer should test simultaneous requests and verify the final state under the chosen database and isolation settings. An example in an article cannot specify a safe implementation for every inventory system.
Locks can introduce waiting and deadlocks. Stronger isolation can increase retry frequency under contention. Weaker isolation can be entirely appropriate for an operation that does not need a transaction-wide stable snapshot. The tradeoff depends on the invariant, load, latency and willingness to retry, not on a label that is “best” in every situation.
Make the setting visible in a diagnosis
When two reports disagree, record the database product and version, transaction boundaries, isolation level, query times and the identity of the data source. A read replica can introduce a different issue: replication delay. A stable snapshot on a replica may still be behind the primary. Our read-replica guide separates replica lag from backup and failover questions.
Check whether the application framework starts transactions automatically and whether a connection pool carries settings between borrowers. Ask how long transactions remain open and whether errors are retried, surfaced or silently ignored. Avoid recording sensitive customer values in a general debugging channel. A small reproducible test with synthetic records is often more useful than a screenshot of two unexplained totals.
End with the decision the business needs to make. A dashboard that may refresh with newer committed data has different requirements from a financial close or a stock-allocation operation. Write down what must remain consistent, what can change during the work, what happens on a conflict and how a retry avoids duplicate effects. The isolation setting becomes meaningful when it is connected to that actual promise.
Explore Infinite Upside
For Vijay Sheth’s personal accounts, read Infinite Upside: Building Wealth With Room to Live, published by Sheth Capital, Inc.
Explore a related guide
What Does a Database Read Replica Actually Do?