Does MVCC database isolation mode allow in-progress transactions to see rows inserted (and committed) by other transactions?
For example, given:
- Table
names[id BIGINT NOT NULL, name VARCHAR(30), PRIMARY KEY(id), UNIQUE(name)] - Transactions T1 and T2,
T1: open transaction
T2: open transaction
T1: select * from names;
insert into names(name) values("John");
// do something
commit;
T2: select * from names;
insert into names values("John");
// do something
commit;
When does T2 first become aware of the new row? At select time? At insert time? Or at commit time?
Answer really depends on server implementation and whether unique constraint is marked deferrable or not.
I have not tested it for other databases, but in PostgreSQL (as one of most prominent open-source MVCC databases) in my test replicating your setup T2 fails on
INSERT. However, T2 cannot see any changes made by T1 by usingSELECT.I have executed following statements almost at the same time in 2 separate SQL connections:
One succeeded, but another failed after 10 seconds with:
This makes sense, because documentation says:
If, however, unique constraint was marked deferrable, uniqueness will be checked at COMMIT time: