In this post I’m sharing a SQL script that will show exclusive locks for all tables in a database, with added info to help you understand what we’re looking at.
Exclusive locks are a natural thing in SQL Server which ensures data integrity during updates or deletions by preventing other transactions from accessing the locked resource. However, prolonged exclusive locks can cause performance issues, especially when planning maintenance tasks or deploying schema changes.
Script to Show Exclusive Locks
The script below lists the exclusive locks held on tables in one database: who holds them, on which table, and from which machine and program. Set @DatabaseName and run it. It only reads DMVs. To see every lock mode, not just the exclusive ones, set @ExclusiveOnly = 0.
-- Show exclusive locks on tables in one database (who holds them, on what, and from where)
-- Read only: queries DMVs, changes nothing. Set @DatabaseName, then run.
DECLARE @DatabaseName sysname = N'database01';
DECLARE @ExclusiveOnly bit = 1; -- 1 = exclusive modes only (X, IX, SIX, UIX, Sch-M), 0 = every lock
DECLARE @SQL nvarchar(max) = N'
USE ' + QUOTENAME(@DatabaseName) + N';
SELECT
ses.login_name AS UserName,
ses.session_id AS SessionID,
ses.host_name AS HostName,
OBJECT_SCHEMA_NAME(obj.object_id) + N''.'' + obj.name AS ObjectName,
obj.type_desc AS ObjectType,
locks.resource_type AS ResourceType,
locks.request_status AS LockStatus,
CASE locks.request_mode
WHEN ''X'' THEN ''X (Exclusive)''
WHEN ''IX'' THEN ''IX (Intent Exclusive)''
WHEN ''SIX'' THEN ''SIX (Shared with Intent Exclusive)''
WHEN ''UIX'' THEN ''UIX (Update with Intent Exclusive)''
WHEN ''Sch-M'' THEN ''Sch-M (Schema modification)''
WHEN ''U'' THEN ''U (Update)''
WHEN ''IU'' THEN ''IU (Intent Update)''
WHEN ''S'' THEN ''S (Shared)''
WHEN ''IS'' THEN ''IS (Intent Shared)''
WHEN ''Sch-S'' THEN ''Sch-S (Schema stability)''
ELSE locks.request_mode
END AS LockType,
ses.program_name AS ProgramName,
ses.client_interface_name AS ClientInterface
FROM sys.dm_tran_locks AS locks
JOIN sys.dm_exec_sessions AS ses
ON ses.session_id = locks.request_session_id
LEFT JOIN sys.partitions AS part
ON locks.resource_type IN (''PAGE'', ''KEY'', ''RID'', ''HOBT'')
AND part.hobt_id = locks.resource_associated_entity_id
JOIN sys.objects AS obj
ON obj.object_id = CASE WHEN locks.resource_type = ''OBJECT''
THEN locks.resource_associated_entity_id
ELSE part.object_id END
WHERE locks.resource_database_id = DB_ID()
AND (@ExclusiveOnly = 0 OR locks.request_mode IN (''X'', ''IX'', ''SIX'', ''UIX'', ''Sch-M''))
ORDER BY SessionID, ObjectName, ResourceType;';
EXEC sys.sp_executesql @SQL, N'@ExclusiveOnly bit', @ExclusiveOnly = @ExclusiveOnly;
Output:
UserName:The login name of the user holding the lock.SessionID:The unique session ID for the locking session.HostName:The machine where the session originated.ObjectName:The name of the locked object, usually a table.ObjectType:The type of the object (e.g., USER_TABLE, SYSTEM_TABLE).ResourceType:The resource type being locked (e.g., OBJECT, PAGE, KEY).LockStatus:The status of the lock (e.g., GRANTED or WAIT).LockType:A descriptive name for the lock mode (e.g., Exclusive, Shared).ProgramName:The application responsible for the session (e.g., SSMS, SQLAgent).ClientInterface:The interface used to connect (e.g., ODBC Driver, ADO.NET).
One row change shows up as three locks: an X lock on the KEY (the row itself), and IX locks on the PAGE and on the table (OBJECT) above it. The sys.dm_tran_locks docs page explains every column. If sessions are waiting on these locks, the lock waits guide on sqldba.blog covers what to do next.
Operations Blocked and Allowed with Exclusive Locks
When a table has an exclusive lock, certain operations are blocked, while others may still be possible depending on the type and level of the lock applied.
SQL Server uses different lock types, like exclusive (X), shared (S), and intent (I), and lock levels (row, page, or table) to manage concurrency and ensure data integrity. For example, an exclusive lock (X) prevents other transactions from accessing or modifying the locked resource, but the lock’s granularity (row, page, or table) determines if other parts of the table can still be used.
What an Exclusive Lock Blocks
- Reading or writing the locked data: other sessions cannot modify it or take the shared locks they need to read it. The
NOLOCKhint reads it anyway, at the cost of dirty reads (uncommitted data) and inconsistent results. - Schema changes: ALTER, DROP and adding the table to replication wait, because they need a schema modification lock.
- Truncating the table: TRUNCATE needs a schema modification lock too, so it waits as well.
What You Can Still Do
- Find the blocker: run the script above, or
sp_who2, to see which session holds the lock. - Use the unlocked parts: if the lock is on rows or pages (KEY or PAGE), the rest of the table can still be read and written.
Managing Exclusive Locks During Changes
To minimize the impact of exclusive locks, it’s important to plan changes during off-peak hours and schedule maintenance windows when database activity is low. If you’re struggling to perform a DDL change on a table, consider arranging application downtime to allow the operation to complete without interference. For tasks like adding a table to replication, you can populate the subscriber first and then use the replication support-only option to avoid conflicts, ensuring smoother changes with minimal disruption.
These strategies should help you maintain performance and reduce the risk of lock contention during critical operations. As a follow-up, you could also be checking the currently running SPIDs on your SQL Server for more information on busy periods.
Leave a Reply