Consider two transactions running concurrently under the Read Committed isolation level in PostgreSQL.
Transaction 1 updates a row but has not committed yet. Transaction 2 tries to read the same row.
What will Transaction 2 see?
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; -- Transaction 1 has not committed yet -- Transaction 2 starts BEGIN; SELECT balance FROM accounts WHERE id = 1; COMMIT;
Read Committed isolation level only shows committed data to other transactions.
Under Read Committed, a transaction sees only data committed before the query began. Since Transaction 1 has not committed, Transaction 2 reads the original data.
Transaction 1 updates a row and commits immediately. Transaction 2 reads the same row twice under Read Committed isolation.
What can Transaction 2 observe?
BEGIN; UPDATE products SET price = price + 10 WHERE id = 5; COMMIT; -- Transaction 2 BEGIN; SELECT price FROM products WHERE id = 5; -- some time passes SELECT price FROM products WHERE id = 5; COMMIT;
Each query in Read Committed sees the latest committed data at its start.
Since Transaction 1 committed before Transaction 2's queries, both SELECTs see the updated price.
Which SQL statement correctly sets the transaction isolation level to Read Committed in PostgreSQL?
Check the exact syntax for setting isolation level per transaction.
The correct syntax is SET TRANSACTION ISOLATION LEVEL READ COMMITTED; to set it for the current transaction.
Why does the Read Committed isolation level prevent dirty reads in PostgreSQL?
Think about what data is visible to a query under Read Committed.
Read Committed reads only committed data at query start, so uncommitted changes (dirty data) are never visible.
Transaction 1 updates a row and commits. Transaction 2 reads the same row twice under Read Committed isolation level.
Transaction 2 notices the two reads return different values. What is this phenomenon called?
It happens when a row changes between two reads in the same transaction.
Non-repeatable read occurs when a transaction reads the same row twice and sees different data due to another committed update.