In PostgreSQL (PG), when using the REPEATABLE_READ isolation level, transactions are supposed to behave as if they were executing in a serialized manner, even if they are actually executed concurrently. However, there are certain scenarios where PostgreSQL cannot guarantee serialization between two REPEATABLE_READ transactions. This typically occurs due to the phenomenon known as "serialization anomalies."
One of the primary reasons REPEATABLE_READ transactions might not be able to serialize is due to the detection of serialization anomalies that could lead to non-serializable behavior. PostgreSQL uses a technique called "Serializable Snapshot Isolation" (SSI) to detect and prevent such anomalies when the isolation level is set to SERIALIZABLE. However, when the isolation level is REPEATABLE_READ, it does not fully enforce serializability by default, as it relies on snapshot isolation, which can allow certain anomalies that a fully serializable execution would prevent.
For REPEATABLE_READ transactions specifically, PostgreSQL does not actively prevent all serialization anomalies by default, as it does with SERIALIZABLE isolation level. However, certain patterns of dependencies between transactions can still lead to situations where the outcome is not equivalent to any serial execution of the transactions.
A classic example where serialization can fail (or where anomalies can occur) under REPEATABLE_READ involves a "read-write dependency cycle" or "serialization anomaly." For instance, consider two transactions, T1 and T2, both running under REPEATABLE_READ:
- T1 reads data.
- T2 reads the same data.
- T1 writes data based on its read, but to a different row or table than T2 is reading.
- T2 writes data based on its read, but this write conflicts with T1's write in some way (e.g., T2's write depends on the state read before T1's write committed).
In this simplified example, if T1 and T2 are under REPEATABLE_READ, and they read and then write data in such a way that their concurrent execution could result in an outcome that wouldn't be possible if they were executed serially (one after the other), PostgreSQL might not prevent this by aborting one of the transactions due to a serialization failure, as it would under SERIALIZABLE isolation.
However, it's worth noting that PostgreSQL's REPEATABLE_READ does prevent certain anomalies compared to READ COMMITTED, such as non-repeatable reads. But for full protection against serialization anomalies, you would typically need to use the SERIALIZABLE isolation level.
To directly answer your question: PostgreSQL cannot serialize two REPEATABLE_READ transactions when their execution pattern results in a serialization anomaly that isn't detected or prevented under REPEATABLE_READ isolation. To avoid such issues, you can use the SERIALIZABLE isolation level, which enforces more stringent checks to prevent serialization anomalies.
Here's a simple example that might lead to a serialization anomaly under REPEATABLE_READ:
-- Transaction 1 (T1)
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT * FROM mytable WHERE id = 1; -- Assume it exists
UPDATE mytable SET value = 'T1' WHERE id = 2; -- Different row
-- Transaction 2 (T2), running concurrently
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT * FROM mytable WHERE id = 2; -- Assume it exists
UPDATE mytable SET value = 'T2' WHERE id = 1; -- Different row, but conflicting with T1's logic
In this contrived example, if T1 and T2 run concurrently, their execution could potentially result in a non-serializable outcome. To prevent such outcomes, you would need to use SERIALIZABLE isolation level instead of REPEATABLE READ.