In PostgreSQL's MVCC system, what happens when one transaction reads a row that another transaction is currently updating but not yet committed?
Think about how MVCC provides a consistent snapshot for readers.
MVCC allows readers to see the last committed version of data, so they do not block or get blocked by writers. Uncommitted changes are invisible to other transactions.
Consider two transactions in PostgreSQL:
-- Transaction 1 BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; -- Transaction 2 BEGIN; SELECT balance FROM accounts WHERE id = 1;
What balance does Transaction 2 see if Transaction 1 has not committed yet?
Remember MVCC shows a consistent snapshot to readers.
Transaction 2 sees the last committed balance because Transaction 1's update is not committed yet.
Which option shows a transaction that will cause a serialization failure error due to MVCC in PostgreSQL?
-- Transaction 1 BEGIN; SELECT balance FROM accounts WHERE id = 1; UPDATE accounts SET balance = balance + 50 WHERE id = 1; COMMIT;
Think about what causes serialization failures in MVCC.
When two transactions read and update the same row concurrently without proper locking, PostgreSQL may raise a serialization failure to maintain consistency.
Which approach best helps reduce bloat caused by MVCC dead tuples in PostgreSQL?
Think about how PostgreSQL cleans up old row versions.
Autovacuum automatically removes dead tuples created by MVCC. Increasing its frequency and tuning cost parameters helps keep tables lean.
In PostgreSQL, a transaction running at REPEATABLE READ isolation sees phantom rows after another transaction inserts new rows and commits. Why does this happen?
Consider the difference between REPEATABLE READ and SERIALIZABLE isolation levels.
PostgreSQL's REPEATABLE READ prevents non-repeatable reads but allows phantom reads. Only SERIALIZABLE isolation prevents phantoms by stricter locking and serialization.