Deadlock error in INSERT statement

前端 未结 4 1239
别跟我提以往
别跟我提以往 2021-02-12 22:29

We\'ve got a web-based application. There are time-bound database operations (INSERTs and UPDATEs) in the application which take more time to complete, hence this particular fl

4条回答
  •  暖寄归人
    2021-02-12 22:36

    One way to cope with deadlocks is to have a retry mechanism that waits for a random interval and tries to run the transaction again. The random interval is necessary so that the colliding transactions don't continuously keep bumping into each other, causing what is called a live lock - something even nastier to debug. Actually most complex applications will need such a retry mechanism sooner or later when they need to handle transaction serialization failures.

    Of course if you are able to determine the cause of the deadlock it's usually much better to eliminate it or it will come back to bite you. For almost all cases, even when the deadlock condition is rare, the little bit of throughput and coding overhead to get the locks in deterministic order or get more coarse-grained locks is worth it to avoid the occasional large latency hit and the sudden performance cliff when scaling concurrency.

    When you are consistently getting two INSERT statements deadlocking it's most likely an unique index insert order issue. Try for example the following in two psql command windows:

    Thread A           | Thread B
    BEGIN;             | BEGIN;
                       | INSERT uniq=1;
    INSERT uniq=2;     | 
                       | INSERT uniq=2; 
                       |   block waiting for thread A to commit or rollback, to
                       |   see if this is an unique key error.
    INSERT uniq=1;     |
       blocks waiting  |
       for thread B,   |
         DEADLOCK      | 
                       V    
    

    Usually the best course of action to resolve this is to figure out the parent objects that guard all such transactions. Most applications have one or two of primary entities, such as users or accounts, that are good candidates for this. Then all you need is for every transaction to get the locks on the primary entity it touches via SELECT ... FOR UPDATE. Or if touches several, get locks on all of them but in the same order every time (order by primary key is a good choice).

提交回复
热议问题