Why does PostgreSQL serializable transaction think this as conflict?

后端 未结 2 1293

In my understanding PostgreSQL use some kind of monitors to guess if there\'s a conflict in serializable isolation level. Many examples are about modifying same resource in

相关标签:
2条回答
  • 2020-12-29 09:00

    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.

    0 讨论(0)
  • 2020-12-29 09:21

    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.

    0 讨论(0)
提交回复
热议问题