14

Documentation says, serializable transactions execute one after one.

But in practic it seems not to be truth. Here's two almost equal transactions, the difference is delay for 15 seconds only.

#1:

set transaction isolation level serializable
go
begin transaction
if not exists (select * from articles where title like 'qwe')
begin
waitfor delay '00:00:15'
insert into articles (title) values ('qwe')
end
commit transaction go

#2:

set transaction isolation level serializable
go
begin transaction
if not exists (select * from articles where title like 'qwe')
begin
insert into articles (title) values ('asd')
end
commit transaction go

The second transaction has been run after couple of seconds since the start of first one.

The result is deadlock. The first transaction dies with

Transaction (Process ID 58) was deadlocked on 
lock resources with another process and has been chosen as the deadlock victim. 
Rerun the transaction.

reason.

The conclusion, serializable transactions are not serial?

2
  • 1
    Where does it say that serializable means one transaction at a time? As far as I am aware serializable transaction isolation level will obtain exclusive/Range locks on the resources even if it is only reading data (in less strict isolation levels it obtains a shared lock when only reading data), and prevent other users to even read data.
    – M.Ali
    Commented Dec 7, 2014 at 21:11
  • 1
    The whole point of serializable transactions is to run them in parallel if at all possible. The end result of the transactions will be identical to running them one after the another so the promise you get is that there cannot be any effects caused by parallel execution. If you want to run stuff one after another even when both could run at the same time without collision, use explicit locking instead. The serializable isolation level you take the risk of wasting CPU and RAM for trying to execute queries in parallel to reduce typical latency. Commented Sep 26, 2022 at 16:28

3 Answers 3

13

serializable transactions don't necessarily execute serially.

The promise is just that transactions can only commit if the result would be as if they had executed serially (in any order).

The locking requirements to meet this guarantee can frequently lead to deadlock where one of the transactions needs to be rolled back. You would need to code your own retry logic to resubmit the failed query.

See The Serializable Isolation Level for more about the differences between the logical description and implementation.

3
  • 1
    This could happen thousands of time under high load, especially if there are many copies of the query all requesting serialized access. This makes the mode virtually useless, because it deadlocks all the time. It's probably better to just take out an exclusive table lock up front and hold it. Then the queries at least all queue up and complete without deadlock errors.
    – Triynko
    Commented Apr 28, 2021 at 19:30
  • @Triynko taking U lock (with UPDLOCK hint) is less blocking than X lock if you want that approach. Commented Apr 28, 2021 at 20:32
  • 1
    @Triynko You might need to increase locks available for row locking to avoid collisions between transactions with isolation level serializable. PostgreSQL will upgrade row level locks to table level locks if it's running short of locks and when you have multiple transactions trying to use table level locks instead of row level locks, those will obviously collide. Commented Sep 26, 2022 at 16:25
12

What happens here: Because transactions 1 runs in serializable isolation level, it keeps a share lock it obtains on table articles while it wait. This way, it is guaranteed that the non exists condition remains true until the transaction terminates. Transaction 2 gets a share lock as well that allows it to do the exist check condition. Then, with the insert statement, Transaction 2 requires to convert the share lock to a exclusive lock but has to wait as Transaction 1 holds a shared lock. When Transaction 1 finishes to wait, it also requests a conversion to exclusive mode => deadlock situation, 1 of the transaction has to be terminated.

2
  • This was exactly my problem. What I did to solve it was SELECT with an explicit exclusive lock and the issue went away. Commented Nov 8, 2016 at 20:43
  • nice explanation for a good question, and also gives an example how processing only one table can also generate a deadlock situation.
    – Vikash
    Commented Jun 19, 2020 at 8:14
6

I got into a similar problem and i found that:

From MSDN:

SERIALIZABLE Specifies the following:

  • Statements cannot read data that has been modified but not yet committed by other transactions.
  • No other transactions can modify data that has been read by the current transaction until the current transaction completes.
  • Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.

The second point does not state that both sessions can't take the shared lock that will result in deadlock. We solved it with a hint on SELECT.

select * from articles WITH (UPDLOCK, ROWLOCK) where title like 'qwe'

Have not tried if it would work in this case but i think you would have to lock on the table part since the row is not yet created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.