0
0
PostgreSQLquery~5 mins

Serializable isolation in PostgreSQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Serializable isolation
O(n * m)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As more transactions run and access more rows, the system must do more conflict checks.

Input Size (n)Approx. Operations
10 transactionsFew conflict checks, fast commit.
100 transactionsMore conflict checks, some delays possible.
1000 transactionsMany 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.

Final Time Complexity

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).

Common Mistake

[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.

Interview Connect

Understanding how serializable isolation scales helps you explain database behavior clearly and shows you grasp how systems keep data safe under load.

Self-Check

"What if we changed from serializable to read committed isolation? How would the time complexity of conflict checking change?"