Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Lock escalation concept Expand / Collapse
Author
Message
Posted Thursday, August 23, 2012 3:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 21, Visits: 107
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
Post #1348966
Posted Thursday, August 23, 2012 4:01 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 4:59 AM
Points: 850, Visits: 5,581
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.
Post #1348983
Posted Friday, August 24, 2012 1:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 21, Visits: 107
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
Post #1349507
Posted Friday, August 24, 2012 3:28 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 8:31 AM
Points: 40,404, Visits: 36,844
No contradiction there. What's confusing you?


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1349537
Posted Friday, August 24, 2012 4:08 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 21, Visits: 107
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 ) 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
Post #1349551
Posted Friday, August 24, 2012 4:20 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 8:31 AM
Points: 40,404, Visits: 36,844
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 ) 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 2008, MVP
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

Post #1349557
Posted Sunday, August 26, 2012 11:18 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 21, Visits: 107
Go it..
Thanks Gail for clearing my doubt.

Thanks n regards,
Dev
Post #1350198
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse