The common misconception that a serializable transaction will single thread access to an entire table or even an entire database is pretty hard to eradicate. And the sad thing is that everybody justkeeps repeating other supposedly informed sources instead of doing a little experimentation. So I have gathered some scripts that will show you how 2 serializable transactions are writing to the same database table simultaneously. When you run these scripts, run them from top to bottom, the commands on the left through one connection, the commands on the right through another connection. html
There is no need to set up any configuration in PostgreSQL as it will be fully concurrent out of the box. less
CREATE TABLE test (ID INT PRIMARY KEY); SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; INSERT INTO test (id) VALUES (1); SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; INSERT INTO test (id) VALUES (2); COMMIT; SELECT * FROM test WHERE id = 2; SELECT * FROM test; COMMIT; SELECT * FROM test;
As you can see, despite having 2 serializable transactions, the transaction that was started last commits first. And none of the queries block at any point. ide
By default MS SQL Server 2005 is somewhat less concurrent then PostgreSQL because it does predicate locking. As long as you stay out of the way of the predicates of other transactions it is still concurrent, but when you cross other running transactions you might need to wait for those other transactions to commit or rollback. So here is the first example. this
CREATE TABLE test (ID INT PRIMARY KEY); SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; INSERT INTO test (id) VALUES (1); SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; INSERT INTO test (id) VALUES (2); COMMIT; SELECT * FROM test WHERE id = 2; SELECT * FROM test; COMMIT; SELECT * FROM test;
Again you can see the transaction that was started last commits first. But while the query for record 2 from the test table didn’t block, the query for the entire table blocked until the first transaction was committed. This is caused by predicate locking. spa
Mathematically speaking serializable means that it is possible to replay all the queries that were executed simultaneously one after another in a single threaded fashion and still get the same results. In terms of the SQL standard it doesn’t mean that. In terms of the SQL standard it just means that 3 specific events are not possible (and I quote this directly from SQL/IEC 9075-2:2003): .net
The above is what serializable means in a database. It does not mean mathematical serializability. It does not mean full table locks. It just means that these three specific events can not occur. code