Why does PostgreSQL serializable transaction think this as conflict? Why does PostgreSQL serializable transaction think this as conflict? postgresql postgresql

Why does PostgreSQL serializable transaction think this as conflict?


You can fix this problem with the following index:

CREATE INDEX accounts_user_idx ON accounts(user_id);

Since there are so few data in your example table, you will have to tell PostgreSQL to use an index scan:

SET enable_seqscan=off;

Now your example will work!

If that seems like black magic, take a look at the query execution plans of your SELECT and UPDATE statements.

Without the index both will use a sequential scan on the table, thereby reading all rows in the table. So both transactions will end up with a SIReadLock on the whole table.

This triggers the serialization failure.


To my knowledge serializable has the highest level of isolation, therefore lowest level of concurrency. The transactions occur one after the other with zero concurrency.