Serializable isolation in PostgreSQL - Time & Space Complexity
When using serializable isolation in databases, we want to understand how the cost of managing transactions grows as more data and transactions happen.
We ask: How does the system handle conflicts and checks as the workload increases?
Analyze the time complexity of this transaction under serializable isolation:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- Read some rows
SELECT balance FROM accounts WHERE account_id = 123;
-- Update a row
UPDATE accounts SET balance = balance - 100 WHERE account_id = 123;
COMMIT;
This code reads and updates data in a transaction that uses serializable isolation to avoid conflicts.
In serializable isolation, the database tracks conflicts between transactions.
- Primary operation: Checking for conflicts between transactions.
- How many times: For each transaction, the system compares its read and write sets against other concurrent transactions, which can grow with the number of transactions and rows accessed.
As more transactions run and access more rows, the system must do more conflict checks.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 transactions | Few conflict checks, fast commit. |
| 100 transactions | More conflict checks, some delays possible. |
| 1000 transactions | Many conflict checks, higher chance of retries. |
Pattern observation: The work grows roughly with the number of transactions and the rows they touch, making conflict checking more expensive as load increases.
Time Complexity: O(n * m)
This means the time to check conflicts grows with the number of transactions (n) times the number of rows accessed per transaction (m).
[X] Wrong: "Serializable isolation always runs as fast as lower isolation levels."
[OK] Correct: Serializable isolation does extra work to prevent conflicts, so as transactions and data grow, it can slow down due to more conflict checks and possible retries.
Understanding how serializable isolation scales helps you explain database behavior clearly and shows you grasp how systems keep data safe under load.
"What if we changed from serializable to read committed isolation? How would the time complexity of conflict checking change?"