Lock escalation concept

  • 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

  • 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.

  • 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

  • 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
  • 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 :doze:) 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

  • 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 :doze:) 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
  • Go it..

    Thanks Gail for clearing my doubt.

    Thanks n regards,

    Dev

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply