Read Committed and Updates

Published Mar 23 2019 05:01 AM 192 Views
Microsoft
First published on MSDN on May 22, 2007

Let's try an experiment.  Begin by creating the following simple schema:



create table t1 (a int, b int)
create clustered index t1a on t1(a)
insert t1 values (1, 1)
insert t1 values (2, 2)
insert t1 values (3, 3)


create table t2 (a int)
insert t2 values (9)


In session 1, lock the third row of table t1:



begin tran
update t1 set b = b where a = 3


Now, in session 2 check the spid (you'll need it later) and run the following update at the default read committed isolation level:



select @@spid


update t1 set t1.b = t1.b
where exists (select * from t2 where t2.a = t1.b)


This update uses the following plan:


|--Clustered Index Update(OBJECT:([t1].[t1a]), SET:([t1].[b] = [t1].[b]))
|--Top(ROWCOUNT est 0)
|--Nested Loops(Left Semi Join, WHERE:([t2].[a]=[t1].[b]))
|--Clustered Index Scan(OBJECT:([t1].[t1a]))
|--Table Scan(OBJECT:([t2]))


This plan scans table t1 and looks each row up in table t2 to see whether the row must be updated.  The scan acquires U locks on each row of t1.  If the row is updated, the update upgrades the lock to an X lock.  If the row is not updated, the scan releases the row and the lock since we are running in read committed isolation.


Since session 1 is holding a lock on the third row of table t1, the udpate blocks when the scan of t1 reaches the third row.  At this point, we can check what locks session 2 is holding by running the following query in session 1 (or any other session):



select resource_type, request_mode, request_type, request_status
from sys.dm_tran_locks
where request_session_id = <session_2_spid>

resource_type request_mode request_type request_status
------------- ------------ ------------ --------------
DATABASE S LOCK GRANT
OBJECT IS LOCK GRANT
KEY U LOCK WAIT
PAGE IU LOCK GRANT
OBJECT IX LOCK GRANT

As expected, we see only one outstanding U lock request.


Next, return to session 2, abort the blocked update, and run the following statement:



update t1 set t1.a = t1.a
where exists (select * from t2 where t2.a = t1.b)


Notice that this time we are updating the clustering key of the index.  Updates to the clustering key can cause rows to move within the index.  To ensure that a row is not updated, encountered again by the same scan, and updated a second time (which would be incorrect), SQL Server must add a blocking operator between the scan and update of table t1.  This requirement is known as "Halloween protection."  Indeed, the new plan includes a sort:


|--Clustered Index Update(OBJECT:([t1].[t1a]), SET:([t1].[a] = [t1].[a]))
|--Top(ROWCOUNT est 0)
|--Sort(DISTINCT ORDER BY:([t1].[a] ASC, [Uniq1002] ASC))
|--Nested Loops(Inner Join, WHERE:([t2].[a]=[t1].[b]))
|--Clustered Index Scan(OBJECT:([t1].[t1a]), ORDERED FORWARD)
|--Table Scan(OBJECT:([t2]))


Once again this update blocks.  Let's check the which locks it is holding by running the above query on the sys.dm_tran_locks DMV:

resource_type request_mode request_type request_status
------------- ------------ ------------ --------------
DATABASE S LOCK GRANT
OBJECT IS LOCK GRANT
KEY U LOCK WAIT
KEY U LOCK GRANT
KEY U LOCK GRANT
PAGE IU LOCK GRANT
OBJECT IX LOCK GRANT

This time we see that there are two granted U locks.  What's going on?  Shouldn't these locks have been released since we are running a read committed scan?  Not so fast!  With the blocking sort operator in the plan, no rows are updated until the scan completes.  If SQL Server simply released each U lock when the scan of t1 released each row, none of the rows would be locked when the update started.  Without any locks, another session could slip in and modify the rows that we'd already scanned and which we were planning to update.  Allowing another session to modify these rows could lead to incorrect results and data corruption.  Thus, SQL Server retains these locks until the statement (not the transaction) finishes executing.

%3CLINGO-SUB%20id%3D%22lingo-sub-383284%22%20slang%3D%22en-US%22%3ERead%20Committed%20and%20Updates%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383284%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20May%2022%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ELet's%20try%20an%20experiment.%26nbsp%3B%20Begin%20by%20creating%20the%20following%20simple%20schema%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20t1%20(a%20int%2C%20b%20int)%20%3CBR%20%2F%3E%20create%20clustered%20index%20t1a%20on%20t1(a)%20%3CBR%20%2F%3E%20insert%20t1%20values%20(1%2C%201)%20%3CBR%20%2F%3E%20insert%20t1%20values%20(2%2C%202)%20%3CBR%20%2F%3E%20insert%20t1%20values%20(3%2C%203)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ecreate%20table%20t2%20(a%20int)%20%3CBR%20%2F%3E%20insert%20t2%20values%20(9)%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EIn%20session%201%2C%20lock%20the%20third%20row%20of%20table%20t1%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ebegin%20tran%20%3CBR%20%2F%3E%20update%20t1%20set%20b%20%3D%20b%20where%20a%20%3D%203%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ENow%2C%20in%20session%202%20check%20the%20spid%20(you'll%20need%20it%20later)%20and%20run%20the%20following%20update%20at%20the%20default%20read%20committed%20isolation%20level%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20%40%40spid%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eupdate%20t1%20set%20t1.b%20%3D%20t1.b%20%3CBR%20%2F%3E%20where%20exists%20(select%20*%20from%20t2%20where%20t2.a%20%3D%20t1.b)%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThis%20update%20uses%20the%20following%20plan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Clustered%20Index%20Update(OBJECT%3A(%5Bt1%5D.%5Bt1a%5D)%2C%20SET%3A(%5Bt1%5D.%5Bb%5D%20%3D%20%5Bt1%5D.%5Bb%5D))%20%3CBR%20%2F%3E%20%7C--Top(ROWCOUNT%20est%200)%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Left%20Semi%20Join%2C%20WHERE%3A(%5Bt2%5D.%5Ba%5D%3D%5Bt1%5D.%5Bb%5D))%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Bt1%5D.%5Bt1a%5D))%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5Bt2%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThis%20plan%20scans%20table%20t1%20and%20looks%20each%20row%20up%20in%20table%20t2%20to%20see%20whether%20the%20row%20must%20be%20updated.%26nbsp%3B%20The%20scan%20acquires%20U%20locks%20on%20each%20row%20of%20t1.%26nbsp%3B%20If%20the%20row%20is%20updated%2C%20the%20update%20upgrades%20the%20lock%20to%20an%20X%20lock.%26nbsp%3B%20If%20the%20row%20is%20not%20updated%2C%20the%20scan%20releases%20the%20row%20and%20the%20lock%20since%20we%20are%20running%20in%20read%20committed%20isolation.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESince%20session%201%20is%20holding%20a%20lock%20on%20the%20third%20row%20of%20table%20t1%2C%20the%20udpate%20blocks%20when%20the%20scan%20of%20t1%20reaches%20the%20third%20row.%26nbsp%3B%20At%20this%20point%2C%20we%20can%20check%20what%20locks%20session%202%20is%20holding%20by%20running%20the%20following%20query%20in%20session%201%20(or%20any%20other%20session)%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eselect%20resource_type%2C%20request_mode%2C%20request_type%2C%20request_status%20%3CBR%20%2F%3E%20from%20sys.dm_tran_locks%20%3CBR%20%2F%3E%20where%20request_session_id%20%3D%20%3CI%3E%20%3CSESSION_2_SPID%3E%3C%2FSESSION_2_SPID%3E%3C%2FI%3E%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3Eresource_type%20request_mode%20request_type%20request_status%20%3CBR%20%2F%3E%20-------------%20------------%20------------%20--------------%20%3CBR%20%2F%3E%20DATABASE%20S%20LOCK%20GRANT%20%3CBR%20%2F%3E%20OBJECT%20IS%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20U%20LOCK%20WAIT%20%3CBR%20%2F%3E%20PAGE%20IU%20LOCK%20GRANT%20%3CBR%20%2F%3E%20OBJECT%20IX%20LOCK%20GRANT%20%3CBR%20%2F%3E%3CP%3EAs%20expected%2C%20we%20see%20only%20one%20outstanding%20U%20lock%20request.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENext%2C%20return%20to%20session%202%2C%20abort%20the%20blocked%20update%2C%20and%20run%20the%20following%20statement%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Eupdate%20t1%20set%20t1.a%20%3D%20t1.a%20%3CBR%20%2F%3E%20where%20exists%20(select%20*%20from%20t2%20where%20t2.a%20%3D%20t1.b)%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ENotice%20that%20this%20time%20we%20are%20updating%20the%20clustering%20key%20of%20the%20index.%26nbsp%3B%20Updates%20to%20the%20clustering%20key%20can%20cause%20rows%20to%20move%20within%20the%20index.%26nbsp%3B%20To%20ensure%20that%20a%20row%20is%20not%20updated%2C%20encountered%20again%20by%20the%20same%20scan%2C%20and%20updated%20a%20second%20time%20(which%20would%20be%20incorrect)%2C%20SQL%20Server%20must%20add%20a%20blocking%20operator%20between%20the%20scan%20and%20update%20of%20table%20t1.%26nbsp%3B%20This%20requirement%20is%20known%20as%20%22Halloween%20protection.%22%26nbsp%3B%20Indeed%2C%20the%20new%20plan%20includes%20a%20sort%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Clustered%20Index%20Update(OBJECT%3A(%5Bt1%5D.%5Bt1a%5D)%2C%20SET%3A(%5Bt1%5D.%5Ba%5D%20%3D%20%5Bt1%5D.%5Ba%5D))%20%3CBR%20%2F%3E%20%7C--Top(ROWCOUNT%20est%200)%20%3CBR%20%2F%3E%20%7C--Sort(DISTINCT%20ORDER%20BY%3A(%5Bt1%5D.%5Ba%5D%20ASC%2C%20%5BUniq1002%5D%20ASC))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Inner%20Join%2C%20WHERE%3A(%5Bt2%5D.%5Ba%5D%3D%5Bt1%5D.%5Bb%5D))%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Bt1%5D.%5Bt1a%5D)%2C%20ORDERED%20FORWARD)%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5Bt2%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EOnce%20again%20this%20update%20blocks.%26nbsp%3B%20Let's%20check%20the%20which%20locks%20it%20is%20holding%20by%20running%20the%20above%20query%20on%20the%20sys.dm_tran_locks%20DMV%3A%3C%2FP%3Eresource_type%20request_mode%20request_type%20request_status%20%3CBR%20%2F%3E%20-------------%20------------%20------------%20--------------%20%3CBR%20%2F%3E%20DATABASE%20S%20LOCK%20GRANT%20%3CBR%20%2F%3E%20OBJECT%20IS%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20U%20LOCK%20WAIT%20%3CBR%20%2F%3E%20KEY%20U%20LOCK%20GRANT%20%3CBR%20%2F%3E%20KEY%20U%20LOCK%20GRANT%20%3CBR%20%2F%3E%20PAGE%20IU%20LOCK%20GRANT%20%3CBR%20%2F%3E%20OBJECT%20IX%20LOCK%20GRANT%20%3CBR%20%2F%3E%3CP%3EThis%20time%20we%20see%20that%20there%20are%20two%20granted%20U%20locks.%26nbsp%3B%20What's%20going%20on%3F%26nbsp%3B%20Shouldn't%20these%20locks%20have%20been%20released%20since%20we%20are%20running%20a%20read%20committed%20scan%3F%26nbsp%3B%20Not%20so%20fast!%26nbsp%3B%20With%20the%20blocking%20sort%20operator%20in%20the%20plan%2C%20no%20rows%20are%20updated%20until%20the%20scan%20completes.%26nbsp%3B%20If%20SQL%20Server%20simply%20released%20each%20U%20lock%20when%20the%20scan%20of%20t1%20released%20each%20row%2C%20none%20of%20the%20rows%20would%20be%20locked%20when%20the%20update%20started.%26nbsp%3B%20Without%20any%20locks%2C%20another%20session%20could%20slip%20in%20and%20modify%20the%20rows%20that%20we'd%20already%20scanned%20and%20which%20we%20were%20planning%20to%20update.%26nbsp%3B%20Allowing%20another%20session%20to%20modify%20these%20rows%20could%20lead%20to%20incorrect%20results%20and%20data%20corruption.%26nbsp%3B%20Thus%2C%20SQL%20Server%20retains%20these%20locks%20until%20the%20statement%20(not%20the%20transaction)%20finishes%20executing.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383284%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20May%2022%2C%202007%20Let's%20try%20an%20experiment.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383284%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 05:02 AM
Updated by: