Lock Types for User Data

Now we'll examine four aspects of locking user data. First, we'll look at the mode of locking (the type of lock). We already mentioned shared, exclusive, and update locks, and we'll go into more detail about these modes as well as others. Next , we'll look at the granularity of the lock, which specifies how much data is covered by a single lock. This can be a row, a page, an index key, a range of index keys, an extent, or an entire table. The third aspect of locking is the duration of the lock. As mentioned earlier, some locks are released as soon as the data has been accessed and some locks are held until the transaction commits or rolls back. For example, cursor scroll locks are held until a new FETCH operation is executed. The fourth aspect of locking concerns the ownership of the lock (the scope of the lock). Locks can be owned by a session, a transaction, or a cursor.

Lock Modes

SQL Server uses several locking modes, including shared locks, exclusive locks, update locks, and intent locks.

Shared Locks

Shared locks are acquired automatically by SQL Server when data is read. Shared locks can be held on a table, a page, an index key, or an individual row. Many processes can hold shared locks on the same data, but no process can acquire an exclusive lock on data that has a shared lock on it (unless the process requesting the exclusive lock is the same process as the one holding the shared lock). Normally, shared locks are released as soon as the data has been read, but you can change this by using query hints or a different transaction isolation level.

Exclusive Locks

SQL Server automatically acquires exclusive locks on data when it is modified by an insert, update, or delete operation. Only one process at a time can hold an exclusive lock on a particular data resource; in fact, as we'll see when we discuss lock compatibility, no locks of any kind can be acquired by a process if another process has the requested data resource exclusively locked. Exclusive locks are held until the end of the transaction. This means that the changed data is normally not available to any other process until the current transaction commits or rolls back. Other processes can decide to read exclusively locked data by using query hints.

Update Locks

Update locks are really not a separate kind of lock; they are a hybrid between shared and exclusive locks. They are acquired when SQL Server executes a data modification operation but first needs to search the table to find the resource that will be modified. Using query hints, a process can specifically request update locks, and in that case they prevent the conversion deadlock situation presented in Figure 13-2.

Update locks provide compatibility with other current readers of data, allowing the process to later modify data with the assurance that the data hasn't been changed since it was last read. An update lock is not sufficient to allow you to change the data ”all modifications require that the data resource being modified has an exclusive lock. An update lock acts as a serialization gate to queue future requests for the exclusive lock. (Many processes can hold shared locks for a resource, but only one process can hold an update lock.) As long as a process holds an update lock on a resource, no other process can acquire an update lock or an exclusive lock for that resource; instead, another process requesting an update or exclusive lock for the same resource must wait. The process holding the update lock can acquire an exclusive lock on that resource because the update lock prevents lock incompatibility with any other processes. You can think of update locks as "intent-to-update" locks, which is essentially the role they perform. Used alone, update locks are insufficient for updating data ”an exclusive lock is still required for actual data modification. Serializing access for the exclusive lock lets you avoid conversion deadlocks.

Don't let the name fool you: update locks are not just for update operations. SQL Server uses update locks for any data modification operation that requires a search for the data prior to the actual modification. Such operations include qualified updates and deletes, as well as inserts into a table with a clustered index. In the latter case, SQL Server must first search the data (using the clustered index) to find the correct position at which to insert the new row. While SQL Server is only searching, it uses update locks to protect the data; only after it has found the correct location and begins inserting does it escalate the update lock to an exclusive lock.

Intent Locks

Intent locks are not really a separate mode of locking; they are a qualifier to the modes previously discussed. In other words, you can have intent shared locks, intent exclusive locks, and even intent update locks . Because SQL Server can acquire locks at different levels of granularity, a mechanism is needed to indicate that a component of a resource is already locked. For example, if one process tries to lock a table, SQL Server needs a way to determine whether a row (or a page) of that table is already locked. Intent locks serve this purpose. We'll discuss them in more detail when we look at lock granularity.

Special Lock Modes

SQL Server offers three additional lock modes: schema stability locks, schema modification locks, and bulk update locks . When queries are compiled, schema stability locks prevent other processes from acquiring schema modification locks, which are taken when a table's structure is being modified. A bulk update lock is acquired when the BULK INSERT command is executed or when the bcp utility is run to load data into a table. In addition, the copy operation must request this special lock by using the TABLOCK hint. Alternatively, the table can set the table option called table lock on bulk load to true, and then any bulk copy IN or BULK INSERT operation automatically requests a bulk update lock.

Another lock mode that you might notice is the SIX lock. This mode is never requested directly by the lock manager but is the result of a conversion. If a transaction is holding a shared (S) lock on a resource and later an IX lock is needed, the lock mode will be indicated as SIX. For example, suppose you are operating at the Repeatable Read transaction isolation level and you issue the following batch:

 BEGIN TRAN SELECT * FROM bigtable  UPDATE bigtable     SET col = 0     WHERE keycolumn = 100 

Assuming the table is large, the SELECT statement will acquire a shared table lock. (If there are only a few rows in bigtable , SQL Server will acquire individual row or key locks.) The UPDATE statement will then acquire a single exclusive key lock to do the update of a single row, and the X lock at the key level will mean an IX lock at the page and table level. The table will then show SIX when viewed through sp_lock .

Table 13-1 shows most of the lock modes, as well as the abbreviations used in the output of sp_lock .

Table 13-1. SQL Server lock modes.

Abbreviation Lock Mode Description
S Shared Allows other processes to read but not change the locked resource.
X Exclusive Prevents another process from modifying or reading data in the locked resource (unless the process is set to the Read Uncommitted isolation level).
IS Intent shared Indicates that a component of this resource is locked with a shared lock. This lock can be acquired only at the table or page level.
IX Intent exclusive Indicates that a component of this resource is locked with an exclusive lock. This lock can be acquired only at the table or page level.
SIX Shared with shared intent exclusive Indicates that a resource holding a lock also has a component (a page or row) locked with an exclusive lock.
Sch-S Schema stability Indicates that a query using this table is being compiled.
Sch-M Schema modification Indicates that the structure of the table is being changed.
BU Bulk update Used when a bulk copy operation is copying data into a table and the TABLOCK hint is being applied (either manually or automatically).

Lock Granularity

SQL Server can lock user data resources (not system resources, which are protected with latches) at the table, page, or row level. SQL Server also locks index keys and ranges of index keys. Figure 13-3 shows the possible lock levels in a table. Note that if the table has a clustered index, the data rows are at the leaf level of the clustered index, and they are locked with key locks instead of row locks.

click to view at full size.

Figure 13-3. Levels of granularity for SQL Server locks.

The syslockinfo table keeps track of each lock by storing the type of resource locked (such as a row, key, or page), the mode of the lock, and an identifier for the specific resource. When a process requests a lock, SQL Server compares the lock requested to the resources already listed in the syslockinfo table and looks for an exact match on the resource type and identifier. (The lock modes don't have to be the same to yield an exact match.) However, if one process has a row exclusively locked in the authors table, another process might try to get a lock on the entire authors table. Since these are two different resources, SQL Server does not find an exact match unless additional information is already stored in syslockinfo . This is what intent locks are for. The process that has the exclusive lock on a row of the authors table also has an intent exclusive lock on the page containing the row and another intent exclusive lock on the table containing the row. When the second process attempts to acquire the exclusive lock on the table, it finds a conflicting row already in the syslockinfo table on the same lock resource (the authors table).

Key Locks

SQL Server 7 supports two kinds of key locks, whose use depends on the isolation level of the current transaction. If the isolation level is Read Committed or Repeatable Read, SQL Server tries to lock the actual index keys accessed while processing the query. With a table that has a clustered index, the data rows are the leaf level of the index, and you will see key locks acquired. If the table is a heap, you might see key locks for the nonclustered indexes and row locks for the actual data.

If the isolation level is Serializable, the situation is special. We want to prevent phantoms, which means that if we have scanned a range of data within a transaction, we need to lock enough of the table to make sure that no one can insert a value into the range that was scanned. For example, we can issue the following query within an explicit transaction:

 SELECT * FROM employees WHERE salary BETWEEN 30000 AND 50000 

Locks must be acquired to make sure that no new rows with salary values between 30000 and 50000 are inserted before the end of the transaction. Earlier versions of SQL Server guaranteed this by locking whole pages or even the entire table. In many cases, however, this was too restrictive ”more data was locked than the actual WHERE clause indicated, resulting in unnecessary contention . SQL Server 7 uses key range locks, which are associated with a particular key value in an index and indicate that all values between that key and the previous one in the index are locked.

Suppose we have an index on the lastname field in the employee table. We are in TRANSACTION ISOLATION LEVEL SERIALIZABLE and we issue this SELECT statement:

 SELECT *  FROM members WHERE last_name BETWEEN 'Delaney' AND 'DuLaney' 

If Dallas , Donovan , and Duluth are sequential leaf-level index keys in the table, the second two of these ( Donovan and Duluth ) acquire key range locks (although only one row, for Donovan , is returned in the result set). The key range locks prevent any inserts into the ranges ending with the two key range locks. No values greater than Dallas and less than or equal to Donovan can be inserted, and no values greater than Donovan and less than or equal to Duluth can be inserted. Note that the key range locks imply an open interval starting at the previous sequential key and a closed interval ending at the key on which the lock is placed. These two key range locks prevent anyone from inserting either Delany or Delanie , which are in the range specified in the WHERE clause. However, the key range locks would also prevent anyone from inserting DeLancey (which is greater than Dallas and less than Donovan ) even though DeLancey is not in the query's specified range. Key range locks are not perfect, but they do give much greater concurrency than locking whole pages or tables, which was the only possibility in previous SQL Server versions.

Additional Lock Resources

Locking is also done on extents ”units of disk space that are 64 KB in size (eight pages of 8 KB each). This kind of locking occurs automatically when a table or an index needs to grow and a new extent must be allocated. You can think of an extent lock as another type of special purpose latch, but it does show up in the output of the sp_lock procedure. Extents can have both shared extent and exclusive extent locks.

When you examine the output of sp_lock , notice that each process holds a lock on at least one database. These are always shared locks and are used by SQL Server for determining when a database is in use. Generally, you don't need to be concerned with extent or database locks, but you might see them if you are running sp_lock or perusing syslockinfo.

Identifying Lock Resources

When the lock manager tries to determine whether a requested lock can be granted, it checks the syslockinfo table to determine whether a matching lock with a conflicting lock mode already exists. It compares locks by looking at the database id (dbid), the object id (objid), the type of resource locked, and the description of the specific resource referenced by the lock. The lock manager knows nothing about the meaning of the resource description. It simply compares the strings identifying the lock resources to look for a match. If it finds a match, it knows that resource is already locked; it then uses the lock compatibility matrix to determine whether the current lock is compatible with the one being requested. Table 13-2 shows all the lock resources, the abbreviations used in the output of sp_lock , and the information used to define the actual resource locked.

Table 13-2. Lockable resources in SQL Server.

Resource Abbreviation Resource Description Example
Database DB None; the database is always indicated in the dbid column for every locked resource.
Table TAB The table ID. 261575970 (Note that sp_lock shows the table ID in its own column rather than in the resource description column.)
Extent EXT File number:page number of the first page of the extent. 1:96
Page PAG File number:page number of the actual table or index page. 1:104
Index Key KEY A hashed value derived from all the key components and the locator. For a non-clustered index on a heap, where columns c1 and c2 are indexed, the hash would contain contributions from c1 , c2 , and the RID. ac0001a10a00
Index Key Range KEY Same as Index Key. ac0001a10a00
Row RID File number:page number: slot number of the actual row. 1:161:3

Note that key locks and key range locks have identical resource descriptions. When we look at some examples of output from the sp_lock procedure, you'll see that you can distinguish between these types of locks by the value in the lock mode column.

Lock Duration

The length of time that a lock is held depends primarily on the mode of the lock and the transaction isolation level in effect. The default isolation level for SQL Server is Read Committed. At this level, shared locks are released as soon as SQL Server has read and processed the locked data. An exclusive lock is held until the end of the transaction, whether it is committed or rolled back. An update lock is held until the end of the transaction unless it has been promoted to an exclusive lock, in which case the exclusive lock remains for the duration of the transaction. If your transaction isolation level is Repeatable Read or Serializable, shared locks have the same duration as exclusive locks. That is, they are not released until the transaction is over.

In addition to changing your transaction isolation level, you can control the lock duration by using query hints. We'll discuss query hints for locking and for other purposes in Chapter 14.

Lock Ownership

Lock duration can also be affected by the lock ownership. There are three types of lock owners : transactions, cursors , and sessions. These are available through the req_ownertype column in the syslockinfo table. (This information is not visible through the sp_lock stored procedure.) A req_ownertype value of 1 indicates that the lock is owned by transaction, and its duration is as discussed above. Most of our locking discussion, in fact, deals with locks owned by a transaction. A cursor lock has a req_ownertype value of 2. If a cursor is opened using a locking mode of scroll_locks, a cursor lock is held on every row fetched until the next row is fetched or the cursor is closed. Even if the transaction commits before the next fetch, the cursor lock is not released. Locks owned by a session have a req_ownertype value of 3. A session lock is one taken on behalf of a process that is outside the scope of a transaction. The most common example is a database lock, as discussed earlier. A process acquires a session lock on the database when it issues the USE database command, and that lock isn't released until another USE command is issued or until the process is disconnected.

Viewing Locks

To see the locks currently outstanding in the system as well as those that are being waited for, examine the syslockinfo system table or execute the system stored procedure sp_lock . (The syslockinfo table is not really a system table. It is not maintained on disk because locks are not maintained on disk. Rather, it is materialized in table format based on the lock manager's current accounting of locks each time syslockinfo is queried.) Another way to watch locking activity is with the excellent graphical representation of locking status provided by SQL Server Enterprise Manager. (Even those of you who think GUIs are for wimps can appreciate SQL Server Enterprise Manager's view of locking.)

The following examples show what each of the lock types and modes discussed earlier look like when reported by the sp_lock procedure. Note that the call to the sp_lock procedure is preceded by the keyword EXECUTE, which is required when the call to a stored procedure is not the first thing in a batch. Note also that the sp_lock procedure is given an argument of @@spid, which refers to the process ID of the current process (the server process ID). We don't want to see all the locks in the system, only those held by our process.

Example 1: SELECT with Default Isolation Level

SQL BATCH

 USE PUBS SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRAN SELECT * FROM authors WHERE au_lname = 'Ringer' EXEC sp_lock @@spid COMMIT TRAN 

OUTPUT OF sp_lock

 spid   dbid   ObjId       IndId  Type Resource         Mode     Status  ------ ------ ----------- ------ ---- ---------------- -------- -----  11     1      0           0      DB                    S        GRANT 11     2      0           0      DB                    S        GRANT 11     5      0           0      DB                    S        GRANT 11     1      117575457   0      TAB                   IS       GRANT 

Every time we run the sp_lock procedure, we acquire locks in the master database to generate the output to be displayed. If you look at the dbid column in the output, you'll see locks in database 1 ( master ) and database 2 ( tempdb ). We won't show those locks in the output for the rest of the examples. If we ignore the locks in master and tempdb , we'll see that the only lock in the pubs database is the session-level database lock. No locks on the authors table are held at this point because the batch was doing only select operations that acquired shared locks. By default, the shared locks are released as soon as the data has been read, so by the time sp_lock is executed, the locks are no longer held.

Example 2: SELECT with Repeatable Read Isolation Level

SQL BATCH

 USE PUBS SET TRANSACTION ISOLATION LEVEL REPEATABLE READ BEGIN TRAN SELECT * FROM authors WHERE au_lname = 'Ringer' EXEC sp_lock @@spid COMMIT TRAN 

OUTPUT OF sp_lock

 spid   dbid   ObjId       IndId  Type Resource         Mode     Status  ------ ------ ----------- ------ ---- ---------------- -------- ------ 11     5      117575457   2      PAG  1:123            IS       GRANT 11     5      117575457   1      PAG  1:96             IS       GRANT 11     5      0           0      DB                    S        GRANT 11     5      117575457   2      KEY  (d5f329a7dcdc)   S        GRANT 11     5      117575457   2      KEY  (4c62318cf11f)   S        GRANT 11     5      117575457   1      KEY  (3dc1b1ecb5be)   S        GRANT 11     5      117575457   1      KEY  (37fdb5efbcbe)   S        GRANT 11     5      117575457   0      TAB                   IS       GRANT 

Because the authors table has a clustered index, the rows of data are all index rows in the leaf level. The locks on the individual rows are marked as key locks instead of row locks. There are also key locks at the leaf level of the nonclustered index on the table. You can tell the two indexes apart by the value in the IndId field: the data rows have an IndId value of 1, and the nonclustered index rows have an IndId value of 2. Because the transaction isolation level is Repeatable Read, the shared locks are held until the transaction is finished. Note that the two rows and two index rows have shared (S) locks, and the data and index pages, as well as the table itself, have intent shared (IS) locks.

Example 3: SELECT with Serializable Isolation Level

SQL BATCH

 USE PUBS SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRAN SELECT * FROM authors WHERE au_lname = 'Ringer' EXEC sp_lock @@spid COMMIT TRAN 

OUTPUT OF sp_lock

 spid   dbid   ObjId       IndId  Type Resource         Mode     Status  ------ ------ ----------- ------ ---- ---------------- -------- ------ 11     5      117575457   2      PAG  1:123            IS       GRANT 11     5      117575457   1      PAG  1:96             IS       GRANT 11     5      0           0      DB                    S        GRANT 11     5      117575457   2      KEY  (d5f329a7dcdc)   IS-S     GRANT 11     5      117575457   2      KEY  (4c62318cf11f)   IS-S     GRANT 11     5      117575457   1      KEY  (3dc1b1ecb5be)   IS-S     GRANT 11     5      117575457   1      KEY  (37fdb5efbcbe)   IS-S     GRANT 11     5      117575457   2      KEY  (d5968ed3b619)   IS-S     GRANT 11     5      117575457   0      TAB                   IS       GRANT 

The locks held with the Serializable isolation level are almost identical to those held with the Repeatable Read isolation level. The main difference is in the mode of the lock. The two-part mode IS-S indicates a key range lock in addition to the lock on the key itself. The first part (IS) is the lock on the range of keys between (and including) the key holding the lock and the previous key in the index. The key range locks prevent other transactions from inserting new rows into the table that meet the condition of this query; that is, no new rows with a last name of Ringer can be inserted. There are three key locks in the nonclustered index on au_lname ( IndId = 2) because three different ranges need to be locked. SQL Server must lock the range from the key preceding the first Ringer in the table up to the first Ringer , it must lock the range between the two instances of Ringer , and it must lock the range from the second Ringer to the next key in the index. (So actually nothing between Ringer and the previous key, Panteley , and nothing between Ringer and the next key, Smith , could be inserted into the table. For example, we could not insert an author with the last name Pattin or Singh .)

Example 4: Update Operations

SQL BATCH

 USE PUBS SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRAN UPDATE authors SET contract = 0 WHERE au_lname = 'Ringer' EXEC sp_lock @@spid COMMIT TRAN 

OUTPUT OF sp_lock

 spid   dbid   ObjId       IndId  Type Resource         Mode     Status  ------ ------ ----------- ------ ---- ---------------- -------- ------ 11     5      117575457   1      PAG  1:96             IX       GRANT 11     5      0           0      DB                    S        GRANT 11     5      117575457   1      KEY  (3dc1b1ecb5be)   X        GRANT 11     5      117575457   1      KEY  (37fdb5efbcbe)   X        GRANT 11     5      117575457   0      TAB                   IX       GRANT 

The two rows in the leaf level of the clustered index are locked with X locks. The page and the table are then locked with IX locks. We discussed earlier that SQL Server actually acquires update locks while it looks for the rows to update. However, these are escalated to X locks when the actual update is done, and by the time the sp_lock procedure is run, the update locks are gone. Unless you actually force update locks with a query hint, you might never see them in the output of sp_lock .

Example 5: Creating a Table

SQL BATCH

 USE PUBS SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRAN SELECT *  INTO newTitles FROM titles WHERE price < 5 EXEC sp_lock @@spid COMMIT TRAN 

OUTPUT OF sp_lock

 spid   dbid   ObjId       IndId  Type Resource         Mode     Status  ------ ------ ----------- ------ ---- ---------------- -------- ------ 7      5      0           0      DB   [BULK-OP-LOG]    S        GRANT 7      5      0           0      DB                    S        GRANT 7      5      0           0      DB   [BULK-OP-DB]     S        GRANT 7      5      1           0      TAB                   IX       GRANT 7      5      3           0      TAB                   IX       GRANT 7      5      2           0      TAB                   IX       GRANT 7      5      0           0      PAG  1:199            X        GRANT 7      5      0           0      PAG  1:198            X        GRANT 7      5      0           0      PAG  1:197            X        GRANT 7      5      0           0      PAG  1:196            X        GRANT 7      5      0           0      PAG  1:195            X        GRANT 7      5      0           0      PAG  1:194            X        GRANT 7      5      0           0      PAG  1:193            X        GRANT 7      5      0           0      EXT  1:192            X        GRANT 7      5      0           0      PAG  1:166            X        GRANT 7      5      0           0      PAG  1:165            X        GRANT 7      5      3           2      KEY  (0693cc100b75)   X        GRANT 7      5      3           2      KEY  (3790f0cf7f55)   X        GRANT 7      5      2           1      KEY  (0257f4438466)   X        GRANT 7      5      3           2      KEY  (242ad7320fa2)   X        GRANT 7      5      3           2      KEY  (e794778e397a)   X        GRANT 7      5      1061578820  0      TAB                   Sch-M    GRANT 7      5      1           2      KEY  (269dc7159f33)   X        GRANT 7      5      3           1      KEY  (024e7788d08c)   X        GRANT 7      5      3           1      KEY  (02567f88d08c)   X        GRANT (etc.) 

Very few of these locks are actually acquired on elements of the new table. In the ObjId column, notice that most of the objects have an ID of less than 100, which means that they are system tables. As the new newTitles table is built, SQL Server acquires locks on sysobjects and syscolumns to record information about this new table. Also notice the schema modification (Sch-M) lock on the new table as well as extent (EXT) locks. While the table is built, the extents are not marked as belonging to the table; you can see that the ObjId is 0. In the output above, the extent ID is shown as 1:192. This means that page 192 in file 1 is the first page of the extent. You can also see that the subsequent seven pages (193_199) in this extent are all exclusively locked while the table is being created.

Example 6: Row Locks

SQL BATCH

 USE PUBS SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRAN UPDATE newTitles SET price = 3.99 WHERE type = 'business' EXEC sp_lock @@spid ROLLBACK TRAN 

OUTPUT OF sp_lock

 spid   dbid   ObjId       IndId  Type Resource         Mode     Status  ------ ------ ----------- ------ ---- ---------------- -------- ------ 13     1      0           0      DB                    S        GRANT 13     2      0           0      DB                    S        GRANT 13     5      0           0      DB                    S        GRANT 13     5      1077578877  0      RID  1:184:0          X        GRANT 13     5      1077578877  0      PAG  1:184            IX       GRANT 13     5      1077578877  0      TAB                   IX       GRANT 13     1      117575457   0      TAB                   IS       GRANT 

There are no indexes on the newTitles table, so the lock on the actual row meeting our criterion is an exclusive (X) lock on the row (RID). As expected, IX locks are taken on the page and the table.



Inside Microsoft SQL Server 7.0
Inside Microsoft SQL Server 7.0 (Mps)
ISBN: 0735605173
EAN: 2147483647
Year: 1999
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net