4

I have a question. Transaction isolation level is set to serializable. When the one user opens a transaction and INSERTs or UPDATEs data in "table1" and then another user opens a transaction and tries to INSERT data to the same table, does the second user need to wait 'til the first user commits the transaction?

sth
  • 222,467
  • 53
  • 283
  • 367
Alexander
  • 1,287
  • 1
  • 15
  • 34

2 Answers2

3

Generally, no. The second transaction is inserting only, so unless there is a unique index check or other trigger that needs to take place, the data can be inserted unconditionally. In the case of a unique index (including primary key), it will block if both transactions are updating rows with the same value, e.g.:

-- Session 1                           -- Session 2
CREATE TABLE t (x INT PRIMARY KEY);
BEGIN;
INSERT INTO t VALUES (1);
                                       BEGIN;
                                       INSERT INTO t VALUES (1);  -- blocks here
COMMIT;
                                       -- finally completes with duplicate key error

Things are less obvious in the case of updates that may affect insertions by the other transaction. I understand PostgreSQL does not yet support "true" serialisability in this case. I do not know how commonly supported it is by other SQL systems.

See http://www.postgresql.org/docs/current/interactive/mvcc.html

Edmund
  • 10,533
  • 3
  • 39
  • 57
-1

The second user will be blocked until the first user commits or rolls back his/her changes.

Martin Doms
  • 8,598
  • 11
  • 43
  • 60