Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Lock escalation concept


Lock escalation concept

Author
Message
dev.tridib
dev.tridib
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 217
Hi All,
Please clear my doubt about lock escalation topic in MSDN.

Here is the topic:
"Lock Escalation (Database Engine)

For example, assume that a session performs these operations:

Begins a transaction.

Updates TableA. This generates exclusive row locks in TableA that are held until the transaction completes.

Updates TableB. This generates exclusive row locks in TableB that are held until the transaction completes.

Performs a SELECT that joins TableA with TableC. The query execution plan calls for the rows to be retrieved from TableA before the rows are retrieved from TableC.

The SELECT statement triggers lock escalation while it is retrieving rows from TableA and before it has accessed TableC.

If lock escalation succeeds, only the locks held by the session on TableA are escalated. This includes both the shared locks from the SELECT statement and the exclusive locks from the previous UPDATE statement. While only the locks the session acquired in TableA for the SELECT statement are counted to determine if lock escalation should be done, once escalation is successful all locks held by the session in TableA are escalated to an exclusive lock on the table, and all other lower-granularity locks, including intent locks, on TableA are released"

My question is:

If all locks held by the session in TableA are escalated to an exclusive lock on the table (if escalation succeeds) then how the select statement will be able to have a shared lock or in other words will the select statement will give any result.


In the same page of MSDN:
"Escalating Mixed Lock Types:

When lock escalation occurs, the lock selected for the heap or index is strong enough to meet the requirements of the most restrictive lower level lock.

For example, assume a session:

Begins a transaction.

Updates a table containing a clustered index.

Issues a SELECT statement that references the same table.

The UPDATE statement acquires these locks:

Exclusive (X) locks on the updated data rows.

Intent exclusive (IX) locks on the clustered index pages containing those rows.

An IX lock on the clustered index and another on the table.

The SELECT statement acquires these locks:

Shared (S) locks on all data rows it reads, unless the row is already protected by an X lock from the UPDATE statement.

Intent Share locks on all clustered index pages containing those rows, unless the page is already protected by an IX lock.

No lock on the clustered index or table because they are already protected by IX locks.

If the SELECT statement acquires enough locks to trigger lock escalation and the escalation succeeds, the IX lock on the table is converted to an X lock, and all the row, page, and index locks are freed. Both the updates and reads are protected by the X lock on the table.

My Question is:

If both the updates and reads are protected by the X lock on the table, how select will work



Thanks n Regards,
Dev
Ken McKelvey
Ken McKelvey
SSC Eights!
SSC Eights! (962 reputation)SSC Eights! (962 reputation)SSC Eights! (962 reputation)SSC Eights! (962 reputation)SSC Eights! (962 reputation)SSC Eights! (962 reputation)SSC Eights! (962 reputation)SSC Eights! (962 reputation)

Group: General Forum Members
Points: 962 Visits: 7274
Locks are acquired by a SQL connection.

If one connection acquires an X lock on a table that connection can still do a SELECT; it is just other connections that will not be able to SELECT against the table until the X lock is released.
dev.tridib
dev.tridib
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 217
Thanks for your reply Ken.
What you said is fair enough. But if you can please look into the second topic from MSDN which I have mentioned. Please check the underlined lines which is confusing and contradict with the last conclusion marked in bold:

Begins a transaction.

Updates a table containing a clustered index.

Issues a SELECT statement that references the same table.

The UPDATE statement acquires these locks:

Exclusive (X) locks on the updated data rows.

Intent exclusive (IX) locks on the clustered index pages containing those rows.

An IX lock on the clustered index and another on the table.

The SELECT statement acquires these locks:

Shared (S) locks on all data rows it reads, unless the row is already protected by an X lock from the UPDATE statement.

Intent Share locks on all clustered index pages containing those rows, unless the page is already protected by an IX lock.

No lock on the clustered index or table because they are already protected by IX locks."


If the SELECT statement acquires enough locks to trigger lock escalation and the escalation succeeds, the IX lock on the table is converted to an X lock, and all the row, page, and index locks are freed. Both the updates and reads are protected by the X lock on the table


Please advice!
Thanks n Regards,
Dev
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47388 Visits: 44399
No contradiction there. What's confusing you?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


dev.tridib
dev.tridib
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 217
Hi Gail,

My confusion is:

If both update and select are part of the same transaction. and update is having a X lock on the data rows, IX lock on the index page and table. Then when select requires a shared lock on the data row will it be able to acquire?
What i have understood (or misunderstood :dozeSmile from the topic is select will not be able to acquire the shared lock if the data row is protected by an X lock from the Update statement neither can have a IS on clustered index pages nor on table as those are already protected by IX locks.

Now the statement which says if the select statement acquires enough locks to trigger lock escalation and the escalation succeeds the IX lock on the table is converted to X lock. Now if the table is protected by X lock how select will succeed?

Thanks
Dev
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47388 Visits: 44399
dev.tridib (8/24/2012)

If both update and select are part of the same transaction. and update is having a X lock on the data rows, IX lock on the index page and table. Then when select requires a shared lock on the data row will it be able to acquire?


Yes, absolutely. Locks are to prevent other sessions from accessing data that's changed. You can always read changes that you yourself have made in the current session

What i have understood (or misunderstood :dozeSmile from the topic is select will not be able to acquire the shared lock if the data row is protected by an X lock from the Update statement neither can have a IS on clustered index pages nor on table as those are already protected by IX locks.


The select will not be able to get a shared lock if the row is protected by an X lock taken by another session. You never block yourself (could you imagine selecting rows but then not being able to update them because of the lock that the select took? Doesn't make sense does it?)

Now the statement which says if the select statement acquires enough locks to trigger lock escalation and the escalation succeeds the IX lock on the table is converted to X lock. Now if the table is protected by X lock how select will succeed?


Because they're on the same connection. A select that someone else runs (or you run from a different session) will be blocked. The select you run on the current session must succeed.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


dev.tridib
dev.tridib
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 217
Go it..
Thanks Gail for clearing my doubt.

Thanks n regards,
Dev
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search