Consider two transactions running concurrently under serializable isolation in PostgreSQL. Both try to update the same row in a table accounts with columns id and balance. What will happen if both transactions commit?
BEGIN; UPDATE accounts SET balance = balance + 100 WHERE id = 1; -- Transaction 1 waits COMMIT;
Serializable isolation prevents concurrent transactions from producing anomalies by aborting one if conflicts occur.
Under serializable isolation, concurrent conflicting updates cause one transaction to abort with a serialization failure to maintain consistency.
Which of the following best describes the guarantee provided by serializable isolation in PostgreSQL?
Serializable isolation is the strictest standard isolation level.
Serializable isolation ensures transactions behave as if executed one after another, preventing dirty reads, non-repeatable reads, and phantom reads.
Which SQL command correctly sets the transaction isolation level to serializable for the current session in PostgreSQL?
Consider the scope of the setting: session vs transaction.
To set the isolation level for the entire session, use SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;. The command SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; applies only to the next transaction.
You have a high-traffic PostgreSQL application using serializable isolation. Serialization failures are frequent, causing many transaction retries. Which approach can reduce serialization failures without sacrificing serializability?
Explicit locking can help control concurrency conflicts.
Using SELECT FOR UPDATE locks rows explicitly, reducing conflicts and serialization failures while maintaining serializability.
Given the following two transactions running concurrently under serializable isolation, which statement best explains why Transaction 2 fails with a serialization error?
-- Transaction 1 BEGIN; UPDATE inventory SET quantity = quantity - 10 WHERE product_id = 5; COMMIT; -- Transaction 2 BEGIN; SELECT quantity FROM inventory WHERE product_id = 5; UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 5; COMMIT;
Think about how concurrent reads and writes interact under serializable isolation.
Transaction 2 reads the quantity before Transaction 1 commits its update. When Transaction 2 tries to update based on the old read, PostgreSQL detects a serialization anomaly and aborts Transaction 2.