When I query sys.dm_tran_locks
, a couple of questions arise:
-
I can see there are some locks that are placed at KEY, PAGE, EXTENT, RID level (
resource_type
column). How to find out to which table or object these particular keys, pages, extents, rids belong to? -
For the OBJECT level locks (
resource_type = 'OBJECT'
) – what is the proper way to define object name? Is theresource_associated_entity_id
column theobject_id
of the object? Or is there some caveat? -
Some OBJECTS have
resource_associated_entity_id = -1261057897
(negative number) – how to find out the object name for these?
Best Answer
sys.dm_db_database_page_allocations()
was introduced specifically to avoid having to do any of this spelunking yourself, using unsupported and undocumented commands likeDBCC IND
andDBCC PAGE
. I talk about an even better replacement, coming in SQL Server 2019, in this tip. With the caveat that this is a pretty expensive query, and you may want to filter it down further to limit the output to just the specific rows insys.dm_tran_locks
you are investigating:Yes,
resource_associated_entity_id
is theobject_id
, when the resource_type isOBJECT
. For other resource types, it is going to reference other types of identifiers, likerowset_id
orallocation_unit_id
. This is why the column isn't simply namedobject_id
.SELECT OBJECT_NAME(object_id);
will return the object name, regardless of whether theobject_id
is negative or positive. #temp tables have negativeobject_id
values, for example, in SQL Server 2012 and up, and really there is no guarantee that anyobject_id
will be positive.