I am still troubleshooting deadlock mentioned in this questions. In that I got help in understanding why a select would hold on to a shared lock and I was able to test and confirm that also.
Now I am looking at second spid involved in the deadlock. That session does an update using a primary key(WHERE BatchId = @BatchI , BatchId is the PK) and it needs IX on two pages. Initially I couldn't figure out why it needed locks on two pages of a non clustered index when it was just updating one row. Then I realized that the update to that row would change the location of the row in the non clustered index and that could be reason why it needs locks on two pages.
Is that correct?
Secondly, please help confirm my understanding of this deadlock is correct.
For simplicity sake lets assume that here are only two pages in that non clustered index. Page1 and Page2. So is this what is roughly happening
Best Answer
So did some testing and the deadlock is in fact caused by page movement within the non clustered index caused by an update which changes the position of the row in the index.
So initially Session 2 (update) takes out IX lock on Page2 where the index row is originally located. Since the update would change the value and in turn change the position of the index row within the index, session 2 will also want to take out another IX lock on the target page where the index row will end up after the update.
For understanding the reason why a select would want to hold on to S lock please refer to this question.
REPO