

In this article, we will use Department and Employee tables. In the next sections of this article, we will explore how a foreign key leads to deadlocks and how we can resolve these types of issues. These extra operations will sometimes reflect as lock issues to us and often lead to deadlock problems. Due to this structure of the foreign keys, when we insert a row into the child table, this inserted data must also be verified in the parent table. The table that holds the foreign key is called the child table, and the table that holds the primary key is called the parent table. At its simplest, a foreign key is a column (or set of columns ) that refers to the primary key of another table. In relational databases, foreign keys are used to enforce data integrity between two tables. RSS feed for comments on this post.In this article, we will try to find out the answer to the question of “Does a foreign key lead to a deadlock?” Just ran into this today – thanks Michael!Ĭomment by Andy Galbraith - Decem 1:19 pm Swart - Ma 10:43 amĪh, whaddya know… I ran into this today when blogging about… That thing. Pingback by Victimless Deadlocks And SSMS – Curated SQL - Ma 8:00 amĪlso, a recent CU for SQL Server 2017 provides more information for these intra-query parallelism deadlocks:Ĭomment by Michael J. Swart shows a scenario where the deadlock graph fails to open in SQL Server Management St…:

SQL Server 2016 SP1 CU RTM CU2 contained a fix for intraquery exchange deadlocks in parallel merge joins. Typically if intraquery deadlock is resulting in significant “lost time” during a query execution, the “system health” Extended Events session will show many deadlocks for the same session separated by 100 ms. Joe Obbish blogged about the effects of exchange deadlocks in parallel merge joins. In some cases, intraquery exchange deadlocks can result in severe performance degradation. There’s a request you can vote on to add extra info to the xml_deadlock_report that would allow us to filter these non-deadlocks out: Add columns has_victims and is_intra_query_deadlock to the event xml_deadlock_report Update Turns out this is a bit of a known issue. This “interquery parallelism” kind of deadlock is resolved internally somehow (yay!) but an xml_deadlock_report event is still raised (boo!).Management Studio can’t handle deadlock graphs with zero victims.Take this with a grain of salt, but personally, I ignore these kinds of deadlock because there is no victim and my applications are unaffected. The original deadlock graph starts like this:

The reason that the management studio can’t display the deadlock is because it assumes that there is at least one victim in a deadlock graph. My guess is that the deadlock monitor detects this kind of deadlock cycle, but knows how to resolve it without choosing a victim and then issues a deadlock report any way. Most importantly, there is no victim here! And our application that issued the query didn’t receive an error from the database.The “processes” are all the same process, just different threads (The spids on all processes are the same but the ecids are different).The resource list is full of “exchangeEvent” elements (which has to do with parallelism) and not the usual key locks I’m used to seeing.In fact it kind of helped me figure out what’s going on: No luck in Management Studio, but SentryOne’s Plan Explorer had no trouble displaying the deadlock graph visually. I copy-pasted the deadlock graph information into an xdl file, I opened it in management studio and I still got the same error. So I couldn’t view the deadlock graph, but the xml for the deadlock was still accessible. I chose to “View Target Data” and then clicked the deadlock tab: I found this error in a session that included the xml_deadlock_report event. I recently got this error in Management Studio when trying to view a deadlock graph that was collected with an extended events session:
