Deadlock Overview SQL Server



The information on deadlock in this blog is get from my final year of engineering book, Database System Concepts - 7th edition by Henry F. Korth
The deadlock topic is fixed 7 marks question in our university. 

Deadlock:

System is deadlocked if there is a set of transactions such that every transaction in the set is waiting for another transaction in the set.

Before starting first learn what is lock:

LOCK:

A lock is a mechanism to control concurrent access to a data item
Data items can be locked in two modes :

1. Exclusive (X) mode. 
Data item can be both read as well as written. X-lock is requested using  lock-X instruction.

2. Shared (S) mode. Data item can only be read. S-lock is requested using  lock-S instruction.


Deadlock Situation:

Consider the partial schedule



Neither T3 nor T4 can make progress — executing  lock-S(B) causes T4 to wait for T3 to release its lock on B, while executing  lock-X(A) causes T3 to wait for T4 to release its lock on A.
Such a situation is called a deadlock. 

To handle a deadlock one of T3 or T4 must be rolled back and its locks released.

SQL Server selects deadlock victim following these rules:

1. The process with the lowest deadlock priority is set as deadlock victim.

2. If the deadlock priority of all the processes involved in deadlock is same, then the process that is least expensive to rollback is selected as deadlock victim.

3. If both the deadlock priority and cost of processes involved in deadlock is same, then the process a process is selected randomly as deadlock victim.

Handling Deadlock:

After a deadlock is detected, the Database Engine ends a deadlock by choosing one of the threads as a deadlock victim.

After choosing it rolls back the transaction of the deadlock victim, and returns a 1205 error to the application.

The 1205 deadlock victim error records information about the threads and resources involved in a deadlock in the error log.

Deadlock Error Detail:


Msg 1205, Level 13, State 45, Line 20
Transaction (Process ID 57) was deadlocked on lock resources with another process and has been
chosen as the deadlock victim. Rerun the transaction.

DEADLOCK PRIORITY:

Alternatively, a user can specify the priority of sessions in a deadlock situation using the SET DEADLOCK_PRIORITY statement. DEADLOCK_PRIORITY can be set to LOW, NORMAL, or HIGH, or alternatively can be set to any integer value in the range (-10 to 10). The deadlock priority defaults to NORMAL. 

Deadlock priority low : -5
Deadlock priority high: 5
Normal: 0

After setting low and high priority deadlock is resolved automatically by eliminating low priority deadlock session.

If both sessions have the same deadlock priority, the session with the transaction that is least expensive to roll back is chosen. If sessions involved in the deadlock cycle have the same deadlock priority and the same cost, a victim is chosen randomly.

Setting deadlock priority:


SET DEADLOCK_PRIORITY NORMAL;

GO

or

SET DEADLOCK_PRIORITY -10

GO


DMV’s to identify deadlock priority: 


SELECT session_id, deadlock_priority FROM sys.dm_exec_sessions;
GO

Changing deadlock priority:

SET DEADLOCK_PRIORITY 5
GO

SELECT session_id, deadlock_priority FROM sys.dm_exec_sessions;







Name

Azure Backup Database Clustering Crash Dumps DBCC Deadlock Link Server Log Shipping Maintenance Migration Mirroring Monitoring Performance Tuning Permissions Post Installations Prerequisites Replication Restore Database SQL Installations SQL on Linux SQL Uninstallations SSIS T-SQL Windows Server
false
ltr
item
hybriddba.blogspot.com: Deadlock Overview SQL Server
Deadlock Overview SQL Server
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgv9maZjDlSGcQFW-QIIpj_C3tgMbiR3WUZzK1IO_0Di4uzdgaPcPOCpB0CzalZHvp2m6Bpcm5EYBh5rqGPwgxHNigWZPsgZ023KAp4q0eId-mNkBomwr0GS7fgzWTA7HGnh-2_H_7YzD6/s1600/1579233310402929-0.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgv9maZjDlSGcQFW-QIIpj_C3tgMbiR3WUZzK1IO_0Di4uzdgaPcPOCpB0CzalZHvp2m6Bpcm5EYBh5rqGPwgxHNigWZPsgZ023KAp4q0eId-mNkBomwr0GS7fgzWTA7HGnh-2_H_7YzD6/s72-c/1579233310402929-0.png
hybriddba.blogspot.com
https://hybriddba.blogspot.com/2019/07/deadlock-overview-sql-server.html
https://hybriddba.blogspot.com/
https://hybriddba.blogspot.com/
https://hybriddba.blogspot.com/2019/07/deadlock-overview-sql-server.html
true
7679493960263860249
UTF-8
Not found any posts Not found any related posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU Tag ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Contents See also related Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy