We have a table where we're updating a table column, increasing the 3length of an NVARCHAR column, but results in DEADLOCK

Cobb Kevin 21 Reputation points
2021-07-26T19:37:54.71+00:00

Not sure why this is happening. It's variable and if we try a few times, it eventually updates. The confusing part is, if we're just updating ONE table, how is it involved in a DEADLOCK? The assumption is that it should only be locking the ONE table, but it has to also be locking other tables or there could not be a DEADLOCK. Our DBA thinks it's also locking tables that use the primary key of this table as a foreign key, and that would explain it. If that's the case, is there a way around this?

EDIT: Adding what a DBA sent me when I asked for deadlock trace:

118667-image.png

SQL Server | Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-07-29T21:47:48.363+00:00

    I don't fully understand this deadlock, but a key factor is that the ALTER TABLE statement also wants to take a log on the DB_Name_Obfuscated.dbo.PrefixObfuscated_Merchants. Why I don't know, but may be there some dependency.

    The first reader process runs a join against merchants and merchants (and more tables). For the second reader process, the statement have been truncated, so we don't see all, but it may be something similar.

    Both reader processes are running with NOLOCK/READ UNCOMMITTED. Despite the mode, process do still take locks in this mode, to wit a Sch-S, Schema-stability, locks. While they can live with the rows changing while they are reading, they don't want the schema to change.

    The ALTER TABLE statement on the other hand, wants a Sch-M, schema-modification lock, on both tables. It is obvious why this is needed on Organization, less so on Merchants. The ALTER TABLE first takes the lock on Organization, whereas the read first gets a lock Merchants, and then they try to take the lock they want on the other table, blocking each other.

    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2021-07-26T19:52:29.89+00:00

    If you are increasing the size of the PK, then every table with an FK to the table would also need to be touched.

    0 comments No comments

  2. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-07-27T03:11:48.287+00:00

    Hi @Cobb Kevin ,

    Updating the primary key colum? Make sure the parameter data type in the code matches the column data type.

    Please check if the thread Concurrent Update On Primary Key Column Making Deadlock could help you.


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.


  3. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-07-27T12:03:37.517+00:00

    I would, however, like to know how a single table can be involved in a deadlock, please and thanks.

    Very simple:

    1. Process A starts a transaction.
    2. Process B starts a transaction.
    3. Process A updates row with id = 23
    4. Process B updates row with id = 98.
    5. Process A attempts to update row with id = 98, but gets blocked by B.
    6. Process B attempts to update row with id = 23, but gets blocked by A.
    7. A few seconds later: SQL Server detects the deadlock.

    This is just one example.


  4. Cobb Kevin 21 Reputation points
    2021-07-27T12:12:44.583+00:00

    I see, thanks for the explanation.

    I am wondering, in our case (see below), why this could cause a deadlock. It's a really quick ALTER TABLE statement on a non-primary key. I assume that the only table being locked is Organizations, so Process A, running the SQL below, locks the table, but if one or more other processes are trying to insert/update/delete from Organizations, how could this result in a deadlock? Process A either got the lock or it didn't, and if it didn't then it would just have to wait to get it.

    ALTER TABLE dbo.[Organizations] ALTER COLUMN [AckOutputFileName] NVARCHAR (255) NULL


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.