Questions tagged [deadlock]

A situation caused by two or more processes being unable to proceed (and thus release their locks) because they are blocked by locks on resources held by the other process.

A deadlock occurs when two or more processes acquire locks on resources needed by the other process and then block because they are waiting for that process to release locks on resources that they require. The processes will then wait indefinitely on the locks to be released by the other process; as the process holding the locks is blocked, the locks will never be released.

A deadlock situation can occur when two processes acquire locks on the same resources in a different order. For example, we have objects A and B, and processes 1 and 2.

  1. Process 1 (P1) acquires a lock on object A

  2. Process 2 (P2) acquires a lock on object B

  3. P1 tries to acquire a lock on object B but blocks because object B is locked by P2.

  4. P2 tries to acquire a lock on object A but blocks because object A is locked by P1.

In this situation, process 1 and process 2 will remain blocked as they are waiting on locks that will never be released because they are held by a process blocked on a lock request for an object they hold a lock on. As neither process can proceed the locks will never be released.

Deadlock Detection Database management systems maintain an internal record of which objects (database records etc.) are held by locks obtained by which transactions. This record forms a graph structure that can be traversed by standard graph searching algorithms. The DBMS detects deadlocks by periodically sweeping the graph structure searching for cycles (circular references).

A cycle in the graph implies two or more processes are blocked waiting on resources on which the other holds locks (the cycle can involve more than two processes). When this is detected the deadlock resolution system will select one process and kill it. In Oracle the statement is rolled back, but the process is not killed and previous statements are not committed or rolled back.

Deadlocks are frequently caused by un-indexed foreign keys, multi-table modification operations done in differing orders, or simply improper explicit user locking.

Relevant Questions: Oracle 11g hunting down the deadlock

617 questions
58
votes
6 answers

What are the main causes of deadlocks and can they be prevented?

Recently one of our ASP.NET applications displayed a database deadlock error and I was requested to check and fix the error. I managed to find the cause of the deadlock was a stored procedure that was rigorously updating a table within a…
Sandeep Kumar M
  • 4,682
  • 3
  • 33
  • 35
41
votes
1 answer

SQL Server: deadlocked on lock communication buffer resources

What could be possible reason for this deadlock type? (not deadlock in general) Lock communication buffer resources Is this indicated system is low in memory and buffers count ran out of limit? Detailed Error: Transaction (Process ID 59) was…
usman shaheen
34
votes
5 answers

Handling concurrent access to a key table without deadlocks in SQL Server

I have a table that is used by a legacy application as a substitute for IDENTITY fields in various other tables. Each row in the table stores the last used ID LastID for the field named in IDName. Occasionally the stored proc gets a deadlock - I…
Hannah Vernon
  • 70,928
  • 22
  • 177
  • 323
31
votes
5 answers

SQL Deadlock on the same exclusively locked clustered Key (with NHibernate) on delete / insert

I have been working on this deadlock issue for quite a few days now and no matter what I do, it persists in one way or another. First, the general premise: We have Visits with VisitItems in a one to many relationship. VisitItems relevant…
Ben
  • 411
  • 4
  • 7
30
votes
3 answers

Code to simulate deadlock

I am testing my application I need some code that stable simulates the deadlock on database site (sql script if possible). Thank you. ADDED: Reproducing deadlocks involving only one table
garik
  • 6,782
  • 10
  • 44
  • 56
24
votes
4 answers

Merge statement deadlocking itself

I have the following procedure (SQL Server 2008 R2): create procedure usp_SaveCompanyUserData @companyId bigint, @userId bigint, @dataTable tt_CoUserdata readonly as begin set nocount, xact_abort on; merge CompanyUser with…
Sako73
  • 435
  • 3
  • 5
  • 8
22
votes
1 answer

Meaning of 'locks rec but not gap waiting' in deadlock report

About the meaning of locks rec but not gap waitingin TRANSACTION(1), which one is correct? Already granted gap lock, waiting for clustered index X lock? Already granted clustered index X lock, waiting for gap lock? There are 31 rows in…
Ryan Lyu
  • 523
  • 2
  • 4
  • 14
21
votes
2 answers

Can foreign keys cause deadlocks and hinder READ COMMITTED SNAPSHOT?

This is a followup question from: https://stackoverflow.com/questions/7684477/is-it-possible-to-set-transaction-isolation-level-snapshot-automatically I'm still having deadlock/timeout situations in the ASP.NET application when running large reports…
Tim Schmelter
  • 729
  • 2
  • 9
  • 20
20
votes
2 answers

Deadlocks From Locks on Same Temporary Tables In Different Processes

I have found a deadlock that appears to show something I thought was impossible. There are two processes involved in the deadlock: 1. process8cf948 SPID 63 Performing an ALTER TABLE on temporary table #PB_Cost_Excp_Process_Invoices_Work. Owns IX…
Paul Williams
  • 1,434
  • 2
  • 14
  • 23
19
votes
4 answers

How can I convert a key in a SQL Server deadlock report to the value?

I have a deadlock report that tells me that there was a conflict involving waitresource="KEY: 9:72057632651542528 (543066506c7c)" and I can see this:
Mark Freeman
  • 2,293
  • 5
  • 32
  • 54
18
votes
1 answer

Resolving deadlock from 2 tables only related through indexed view

I have a situation where I'm getting deadlocks, and I think I've narrowed down the culprits, but I'm not quite sure what I can do to fix it. This is on a production environment running SQL Server 2008 R2. To give you a slightly simplified view of…
18
votes
1 answer

Is "deadlock detected" really an error? Should I be suppressing them after handling them?

A while ago, I spent a nightmarish number of weeks dealing with "deadlock detected" and trying to figure out how to handle it. I ended up finally handling it in such a manner that my code is able to detect when it happens and then retry the same…
user15080516
  • 745
  • 3
  • 8
  • 12
18
votes
1 answer

Deadlock with multi-row INSERTs despite ON CONFLICT DO NOTHING

Setup I have a bulk insert function set_interactions(arg_rows text) that looks like this: with inserts as ( insert into interaction ( thing_id, associate_id, created_time) select t->>'thing_id', t->>'associate_id', now()…
Kev
  • 474
  • 1
  • 4
  • 15
17
votes
2 answers

SQL Server Index Update Deadlock

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…
Dale K
  • 429
  • 1
  • 3
  • 17
17
votes
1 answer

View last several innodb deadlocks

I see that I can view the latest deadlock in mysql/innodb, but is there a way to view past deadlocks? We have two deadlock issues, one being important and the other not. The less important deadlock happens several times a day so it becomes the…
gwgeller
  • 175
  • 1
  • 2
  • 6
1
2 3
41 42