![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| ||||
| ||||
|
|
- The statement continues executing for a long time. If sp_who2 is run at that time, the following row is returned for the statement connection |
|
SPID Status BlkBy Command CPUTime DiskIO LastBatch 63 SUSPENDED 63 SELECT 29282 683 08/31 18:17:37 The statement appears to be blocked by itself. If sp_lock is run at that time, the following rows are returned: spid dbid ObjId IndId Type Resource Mode Status 63 2 1316624641 0 TAB Sch-S GRANT 63 2 1316624641 0 TAB Sch-M WAIT It appears that SQL Server waits indefinitely trying to obtain a schema- modification lock on a resource which already has a schema-stability lock placed on it by the same connection. |
|
The following is pure speculation, but it seems reasonable to assume that the server has materialized the result of the first call to the function using a temporary table in tempdb, and is trying to materialize the result of the second call using the same temporary table (same ObjId in sp_lock results). |
|
Unfortunately, I do not have a simple repro script for this. The actual code is rather complex. While I can devise a workaround, this does look like a bug. I am posting it here before submitting a bug on Connect, in case anyone can shed some light. Thanks. |
#3
| |||
| |||
|
|
SQL Server 2005 SP2 (build 3054) Consider the following scenario: - A complex multi-statement table valued function is created. Let's call it dbo.tfFunc(@Param1, @Param2) - A SELECT statement is executed, that calls the above function twice, each time with a different set of parameters. In pseudocode: SELECT <column list FROM dbo.tfFunc(1, 2) AS f1 some JOIN operator> dbo.tfFunc(3, 4) AS f2 ON f1.col = f2.col INNER JOIN dbo.Table1 AS t1 ON ... etc. The exact statement is probably irrelevant, as long as the same table- valued function is called twice (I have observed the issue in two very different statements calling the same function). The statement is executed in a SNAPSHOT isolation level transaction, although this may also be irrelevant. - The statement continues executing for a long time. If sp_who2 is run at that time, the following row is returned for the statement connection (only relevant columns are shown): SPID Status BlkBy Command CPUTime DiskIO LastBatch 63 SUSPENDED 63 SELECT 29282 683 08/31 18:17:37 The statement appears to be blocked by itself. If sp_lock is run at that time, the following rows are returned: spid dbid ObjId IndId Type Resource Mode Status 63 2 1316624641 0 TAB Sch-S GRANT 63 2 1316624641 0 TAB Sch-M WAIT It appears that SQL Server waits indefinitely trying to obtain a schema- modification lock on a resource which already has a schema-stability lock placed on it by the same connection. The following is pure speculation, but it seems reasonable to assume that the server has materialized the result of the first call to the function using a temporary table in tempdb, and is trying to materialize the result of the second call using the same temporary table (same ObjId in sp_lock results). I do not know why this does not cause a deadlock error. Unfortunately, I do not have a simple repro script for this. The actual code is rather complex. While I can devise a workaround, this does look like a bug. I am posting it here before submitting a bug on Connect, in case anyone can shed some light. Thanks. -- remove a 9 to reply by email |
#4
| ||||
| ||||
|
|
Dimitri Furman (dfurman (AT) cloud99 (DOT) net) writes: - The statement continues executing for a long time. Long time? But does it ever complete? |
|
Is this a parallel plan? |
|
The table in question is likely to be the return table for the UDF. You should be able to find out more about this table by looking in sys.objects and sys.columns. |
|
4) The XML showplan. (You can save this from the graphical plan in Mgmt Studio.) |
#5
| ||||
| ||||
|
|
The longest time I let it run for is 40 minutes. Considering that it usually runs in less than 10 seconds, the likely answer is no. |
|
Hard to tell. I forgot to mention that the problem is intermittent. When the statement completes successfully, there is no indication of parallelism in the actual plan. When it does not, there is obviously no plan to look at (in fact, the only way to kill the connection in that case is to restart the server). The estimated plan doesn't show any parallelism either. I am talking here about the plan for the statement, not the plan for the called function, which I apparently cannot see. |
|
I did, and this is where it gets a bit interesting. The UDF in question includes a table variable, and it turns out that the mentioned schema locks are placed on the table in tempdb corresponding to that table variable, not the return table for the UDF. I am not sure if this makes any substantive difference though. |
|
4) The XML showplan. (You can save this from the graphical plan in Mgmt Studio.) I'm not sure how I could save the plan if the statement never completes... |
#6
| |||
| |||
|
|
Anyway, you can easily examine this next time it happens by running SELECT * FROM sys.dm_os_tasks WHERE session_id = <trouble spid If there are rows with non-zero exec_context_id, there are parallel threads. |
|
It's also available in sys.dm_exec_text_query_plan. A way to get the plan, sys.os_waiting_tasks and more packaged into one result set, is to use my beta_lockinfo, available at http://www.sommarskog.se/sqlutil/beta_lockinfo.html |
|
There have been some bugs around temp-table caching, I don't if they could be related to what you see. There is a Cumultative Update, including these two bugfixes at http://support.microsoft.com/kb/939537. |
#7
| |||
| |||
|
|
If it still happens, will try to find some time to work on a repro. Will follow-up with any news. |
#8
| |||
| |||
|
|
On Sep 02 2007, 11:44 pm, Dimitri Furman <dfurman (AT) cloud99 (DOT) net> wrote in news:Xns999FF19D6AD96dfurmancloud99 (AT) 127 (DOT) 0.0.1: If it still happens, will try to find some time to work on a repro. Will follow-up with any news. Submitted feedback on Connect that includes a repro: https://connect.microsoft.com/SQLSer...Feedback.aspx? FeedbackID=300465 |
#9
| |||
| |||
|
|
If it's possible for you to post the repro files here, I'd be interested. |
#10
| |||
| |||
|
|
On Sep 27 2007, 06:01 pm, Erland Sommarskog <esquel (AT) sommarskog (DOT) se> wrote in news:Xns99B9FF37989Yazorman (AT) 127 (DOT) 0.0.1: If it's possible for you to post the repro files here, I'd be interested. Here it is: http://iridule.net/cu/files/SS2005LockingBugRepro1.zip |
![]() |
| Thread Tools | |
| Display Modes | |
| |