Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Don't do a write lock, just do a FOR UPDATE: BEGIN; SELECT ... FOR UPDATE; ... UPDATE...; COMMIT; Commented May 11, 2015 at 21:51
  • SELECT FOR UPDATE acquires a write lock. That's precisely the cause of the problem. Commented May 12, 2015 at 1:06
  • No that is just part of the problem. Usually (always?) a deadlock involves two rows locked in a different order. You need the write lock to prevent other problems. Commented May 12, 2015 at 15:08
  • The essence of the problem is that the write lock obtained by SELECT FOR UPDATE does not block other write locks from being obtained on the gap. The two write locks block each other from being upgraded to an insert intent lock, leading to, consecutively, a block, then a deadlock. Commented May 12, 2015 at 15:46
  • Speaking of the gap... I am concerned about having a prefix inside a UNIQUE index. It makes my brain hurt to try to figure out what the "gap" would be when dealing with (8). Possibly the engine punts and locks a much bigger gap than "necessary". Can you remove the (8)? Commented May 12, 2015 at 15:56