PostgreSQL uses Multi Version Concurrency Control (MVCC) to manage concurrent data access. It minimizes lock contention and improves performance compared to traditional locking. A key benefit is that reading data never blocks writing, and vice-versa.
https://www.postgresql.org/docs/current/transaction-iso.html
All isolation mode avoids Dirty Read: Reading data written by a concurrent uncommitted transaction.
Set at the Session Level (DB connection)
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Set at the Transaction Level
BEGIN;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- or
-- SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- Other transaction queries here...
COMMIT;
A new snapshot is taken for each SQL statement.
If new rows are added/deleted by other transactions, subsequent queries can see those.
This anomaly falls under Serialization Anomalies.
Example
<aside> 💡
Solutions
When querying the same data multiple times, you might get different results due to other transactions modifying the data in between your reads.
This anomaly falls under Serialization Anomalies.
Example