13

I have 2 queries which when run at the same time are causing a deadlock.

Query 1 - update a column which is included in an index (index1):

update table1 set column1 = value1 where id = @Id

Takes X-Lock on table1 then attempts an X-Lock on index1.

Query 2:

select columnx, columny, etc from table1 where {some condition}

Takes an S-Lock on index1 then attempts an S-Lock on table1.

Is there a way to prevent the deadlock while maintaining the same queries? For example can I somehow take an X-Lock on the index in the update transaction before the update to ensure the table and index access are in the same order - which should prevent the deadlock?

Isolation level is Read Committed. Row and page locks are enabled for the indexes. It's possible that the same record is participating in both queries - I can't tell from the deadlock graph as it doesn't show the parameters.

Deadlock graph

11

Is there a way to prevent the deadlock while maintaining the same queries?

The deadlock graph shows that this particular deadlock was a conversion deadlock associated with a bookmark lookup (an RID lookup in this case):

Deadlock graph

As the question notes, the general deadlock risk arises because the queries may obtain incompatible locks on the same resources in different orders. The SELECT query needs to access the index before the table due to the RID lookup, whereas the UPDATE query modifies the table first, then the index.

Eliminating the deadlock requires removing one of the deadlock ingredients. The following are the main options:

  1. Avoid the RID lookup by making the nonclustered index covering. This is probably not practical in your case because the SELECT query returns 26 columns.
  2. Avoid the RID lookup by creating a clustered index. This would involve creating a clustered index on the column Proposal. This is worth considering, though it appears this column is of type uniqueidentifier, which may or may not be a good choice for a clustered index, depending on broader issues.
  3. Avoid taking shared locks when reading by enabling the READ_COMMITTED_SNAPSHOT or SNAPSHOT database options. This would require careful testing, especially with respect to any designed-in blocking behaviours. Trigger code would also require testing to ensure the logic performs correctly.
  4. Avoid taking shared locks when reading by using the READ UNCOMMITTED isolation level for the SELECT query. All the usual caveats apply.
  5. Avoid concurrent execution of the two queries in question by using an exclusive application lock (see sp_getapplock).
  6. Use table lock hints to avoid concurrency. This is a bigger hammer than option 5, as it may affect other queries, not just the two identified in the question.

Can I somehow take an X-Lock on the index in the update transaction before the update to ensure the table and index access are in the same order

You can try this, by wrapping the update in an explicit transaction and performing a SELECT with an XLOCK hint on the nonclustered index value before the update. This relies on you knowing for certain what the current value in the nonclustered index is, getting the execution plan right, and correctly anticipating all the side-effects of taking this extra lock. It also relies on the locking engine not being smart enough to avoid taking the lock if it is judged to be redundant.

In short, while this is feasible in principle, I do not recommend it. It is too easy to miss something, or to outsmart oneself in creative ways. If you really must avoid these deadlocks (rather than just detecting them and retrying), I would encourage you to look instead at the more general solutions listed above.

  • From looking further into the issue I think leaving it unchanged is probably best. Its a more common problem that I originally realised. – Dale Burrell Jan 9 '15 at 1:16
1

I have a similar issue that occurs occasionally and here is the approach I take.

  1. Add set deadlock priority low; to the select. This will cause this query to be the deadlock victim when a deadlock occurs.
  2. Setup retry logic within your application to automatically retry the select if it fails because of deadlock (or timeout), after waiting / sleeping for a brief period of time to allow the blocking queries to complete.

Note : if your select is part of an explicit multi-statement transaction, then you need to be sure to retry the entire transaction, and not just the statement that failed, or else you can get some unexpected results. If this is a single select then you are fine, but if it is statement x of n within a transaction, then just make sure you retry all n statements during the retry.

  • Thanks - queries are automatically deadlock victims by default. And yes we already have a robust retry mechanism in place. – Dale Burrell Jan 7 '15 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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