HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Looking up which record(s) correspond to a specific resource description in dm_tran_locks

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
resourcedescriptiondm_tran_lockslookingcorrespondrecordspecificwhich

Problem

I've spent the better part of two days trying to figure out what exactly is being locked in one of my tables and why I'm getting deadlocks that seem to make no sense at face value.

I've found numerous blogs explaining how to use the undocumented %%lockres%% function to get the hash for a specific row in a table. But each of these guides only give the example where the lock in question is on the primary key for the table. I have a strange situation where there is a lock on the primary key and a unique key as well.

For context: My primary key is a clustered index on a UUID string. The only other index in this table is a composite unique key on two columns (not including the pk). When I do an INSERT into this table, I can see in sys.dm_tran_locks that there are two X KEY locks on this table: one for the pk and one for the unique constraint.

My deadlock report seems to imply (unless I'm reading it wrong -- you can see my other question here) that the deadlock is caused by a second query also locking on the unique index.

I've been experimenting on a different database using the same schema to try and figure out how I can determine if the cause of the deadlock is avoidable or not. I did an INSERT in an open transaction and compared the resource description to the %%lockres%% of all records in the locked table, only to find out that the lock on the primary key maps to the row that I added, but the lock on the unique index does not match anything in the table at all.

Does anybody here know what %%lockres%% is for this unique index? It clearly isn't a specific record in my table.

For context, here is the queries I ran to see this information:

This query lists the locks on my current database. Output below.

```
SELECT dm_tran_locks.request_session_id,
dm_tran_locks.resource_database_id,
CASE
WHEN resource_type = 'object'
THEN OBJECT_NAME(dm_tran_locks.resource_associated_entity_id)
ELSE OBJECT_NAME(partitions.OBJ

Solution

To find lockers on a particular index you have to use seek on that index explicitly:

select *
from entities WITH (index(unique_entities))
where %%lockres%% like '%7e24236fccb8%'

Code Snippets

select *
from entities WITH (index(unique_entities))
where %%lockres%% like '%7e24236fccb8%'

Context

StackExchange Database Administrators Q#257575, answer score: 7

Revisions (0)

No revisions yet.