Describe blocking and locking
Locking is a key feature of relational databases, essential for maintaining the atomicity, consistency, and isolation properties of the ACID model. All RDBMSs block actions that would violate the consistency and isolation of database writes. SQL programmers must start and end transactions at the right points to ensure data consistency. The database engine provides locking mechanisms to protect the logical consistency of the affected tables, which is foundational to the relational model.
In SQL Server, blocking occurs when one process holds a lock on a specific resource (row, page, table, database), and a second process attempts to acquire a lock with an incompatible lock type on the same resource. Typically, locks are held for a short period, and once the process holding the lock releases it, the blocked process can acquire the lock and complete its transaction.
SQL Server can lock rows, pages, or tables, depending on the operation and locking configuration. Finer-grained locks generally allow more concurrency, but each lock requires memory. To balance concurrency with memory use, SQL Server uses a technique called lock escalation. A statement acquiring at least 5,000 locks on a single table reference can trigger an escalation attempt, as can lock-memory pressure. The threshold counts locks, not rows, and escalation can be prevented by an incompatible lock held by another session. Partitioned tables can also support partition-level escalation when configured appropriately.
Locking is a normal behavior and occurs frequently throughout the day. It only becomes problematic when it causes blocking that isn't quickly resolved. There are two types of performance issues caused by blocking:
- A process holds locks on a set of resources for an extended period before releasing them, causing other processes to block and degrading query performance and concurrency.
- A process acquires locks on a set of resources and never releases them, requiring administrator intervention to resolve.
Deadlocking is another blocking scenario that occurs when one transaction holds a lock on a resource, and another transaction holds a lock on a different resource. Each transaction then attempts to acquire a lock on the resource currently locked by the other transaction, creating a cycle of dependencies. The SQL Server engine detects these scenarios and resolves the deadlock by rolling back a transaction. It selects the session with the lower deadlock priority or, when priorities are equal, the transaction that is least expensive to roll back. The selected transaction is known as the deadlock victim. In SQL Server, deadlocks are recorded in the system_health extended event session, which is enabled by default.
It's important to understand the concept of a transaction. Autocommit is the default mode of SQL Server and Azure SQL Database, which means the changes made by the following statement would automatically be recorded in the database's transaction log.
INSERT INTO DemoTable (A) VALUES (1);
In order to allow developers to have more granular control over their application code, SQL Server also allows you to explicitly control your transactions. With traditional locking, without optimized locking, the following query holds locks on the inserted data until the transaction is committed or rolled back.
BEGIN TRANSACTION
INSERT INTO DemoTable (A) VALUES (1);
The proper way to write the following query is as follows:
BEGIN TRANSACTION
INSERT INTO DemoTable (A) VALUES (1);
COMMIT TRANSACTION
The COMMIT TRANSACTION command explicitly commits a record of the changes to the transaction log. The changed data will eventually make its way into the data file asynchronously. These transactions represent a unit of work to the database engine. If the developer forgets to issue the COMMIT TRANSACTION command, the transaction stays open and the locks won't be released. This is one of the main reasons for long running transactions.
The other mechanism the database engine uses to help the concurrency of the database is row versioning. When row versioning is used, the engine maintains versions of modified rows in tempdb, or in the user database's persistent version store when accelerated database recovery is enabled. This is typically used in mixed use workloads, in order to prevent reading queries from blocking queries that are writing to the database.
To monitor open transactions awaiting commit or rollback run the following query:
SELECT tst.session_id, [database_name] = db_name(s.database_id)
, tat.transaction_begin_time
, transaction_duration_s = datediff(s, tat.transaction_begin_time, sysdatetime())
, transaction_type = CASE tat.transaction_type WHEN 1 THEN 'Read/write transaction'
WHEN 2 THEN 'Read-only transaction'
WHEN 3 THEN 'System transaction'
WHEN 4 THEN 'Distributed transaction' END
, input_buffer = ib.event_info, tat.transaction_uow
, transaction_state = CASE tat.transaction_state
WHEN 0 THEN 'The transaction has not been completely initialized yet.'
WHEN 1 THEN 'The transaction has been initialized but has not started.'
WHEN 2 THEN 'The transaction is active - has not been committed or rolled back.'
WHEN 3 THEN 'The transaction has ended. This is used for read-only transactions.'
WHEN 4 THEN 'The commit process has been initiated on the distributed transaction.'
WHEN 5 THEN 'The transaction is in a prepared state and waiting resolution.'
WHEN 6 THEN 'The transaction has been committed.'
WHEN 7 THEN 'The transaction is being rolled back.'
WHEN 8 THEN 'The transaction has been rolled back.' END
, transaction_name = tat.name, request_status = r.status
, tst.is_user_transaction, tst.is_local
, session_open_transaction_count = tst.open_transaction_count
, s.host_name, s.program_name, s.client_interface_name, s.login_name, s.is_user_process
FROM sys.dm_tran_active_transactions tat
INNER JOIN sys.dm_tran_session_transactions tst on tat.transaction_id = tst.transaction_id
INNER JOIN Sys.dm_exec_sessions s on s.session_id = tst.session_id
LEFT OUTER JOIN sys.dm_exec_requests r on r.session_id = s.session_id
CROSS APPLY sys.dm_exec_input_buffer(s.session_id, null) AS ib
ORDER BY tat.transaction_begin_time DESC;
Isolation levels
SQL Server offers several isolation levels to allow you to define the level of consistency and correctness you need guaranteed for your data. Isolation levels let you find a balance between concurrency and consistency. With traditional locking, exclusive locks on modified data are held until the transaction ends, regardless of isolation level. Isolation levels affect how reads use locks or row versions. Lower isolation levels can increase concurrency, but allow more read consistency anomalies. The isolation levels in SQL Server are as follows:
Read uncommitted – Lowest isolation level available. Dirty reads are allowed, which means one transaction may see changes made by another transaction that haven't yet been committed.
Read committed – prevents dirty reads. When
READ_COMMITTED_SNAPSHOTis OFF, reads use shared locks and wait for incompatible write locks to be released. Row and page read locks are released as the read progresses, rather than held until the transaction ends. This is the default isolation level in SQL Server.Repeatable Read – This level keeps read and write locks that are acquired on selected data until the end of the transaction.
Serializable – This is the highest level of isolation where transactions are isolated. Read and write locks are acquired on selected data and not released until the end of the transaction.
SQL Server also includes two isolation levels that include row-versioning.
Read Committed Snapshot – This is an implementation of read committed enabled by the database option
READ_COMMITTED_SNAPSHOT, not a separate level selected withSET TRANSACTION ISOLATION LEVEL. Read operations use row versions instead of row or page shared locks, and the engine presents each statement with a consistent snapshot of the data as it existed at the start of that statement. This option is ON by default in Azure SQL Database.Snapshot – This level provides transaction-level read consistency through row versioning. An update conflict occurs if a snapshot transaction attempts to modify a row changed by another transaction that committed after the snapshot transaction began. The snapshot transaction is then terminated and rolled back. Reading a versioned row alone doesn't cause an update conflict.
Isolation levels are set for each session with the T-SQL SET command, as shown:
SET TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SNAPSHOT
| SERIALIZABLE
}
There's no way to set a global isolation level all queries running in a database, or for all queries run by a particular user. It's a session level setting.
Monitoring for blocking problems
Identifying blocking problems can be challenging due to their sporadic nature. The DMV sys.dm_tran_locks, when joined with sys.dm_exec_requests, provides information on the locks held by each session. A more effective way to monitor blocking problems is to use the Extended Events engine on an ongoing basis.
Blocking problems typically fall into two categories:
- Poor transactional design: For example, a transaction without a
COMMIT TRANSACTIONwill never end. Trying to do too much work in a single transaction or having a distributed transaction using a linked server connection can lead to unpredictable performance. - Long-running transactions caused by schema design: This often involves an update on a column with a missing index or a poorly designed update query.
Monitoring for locking-related performance problems allows you to quickly identify performance degradation related to locking.
For more information about how to monitor blocking, see Understand and resolve SQL Server blocking problems.