Skip to content

Commit 39c969e

Browse files
Fix Lite collectors for mirroring/RESTORING database crash (#384)
Apply same fixes as PR #385 to Lite's embedded SQL queries: - ProcedureStats: LEFT JOIN → INNER JOIN sys.databases + d.state = 0 filter (3 sub-queries) - FileIo: DB_NAME(database_id) → LEFT JOIN sys.databases + d.name - WaitingTasks: DB_NAME(database_id) → LEFT JOIN sys.databases + d.name - QuerySnapshots: DB_NAME(database_id) → LEFT JOIN sys.databases + d.name Prevents OBJECT_NAME/OBJECT_SCHEMA_NAME from opening RESTORING database metadata catalog, which causes severity 22 engine crashes and SQL dumps. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent acf5a6e commit 39c969e

4 files changed

Lines changed: 18 additions & 9 deletions

Lite/Services/RemoteCollectorService.FileIo.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ LEFT JOIN sys.database_files AS df
5959
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
6060
6161
SELECT
62-
database_name = DB_NAME(vfs.database_id),
62+
database_name = ISNULL(d.name, N'Unknown'),
6363
file_name = mf.name,
6464
file_type = mf.type_desc,
6565
physical_name = mf.physical_name,
@@ -78,6 +78,8 @@ FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS vfs
7878
LEFT JOIN sys.master_files AS mf
7979
ON mf.database_id = vfs.database_id
8080
AND mf.file_id = vfs.file_id
81+
LEFT JOIN sys.databases AS d
82+
ON d.database_id = vfs.database_id
8183
WHERE (vfs.database_id > 4 OR vfs.database_id = 2)
8284
AND vfs.database_id < 32761
8385
AND vfs.database_id <> ISNULL(DB_ID(N'PerformanceMonitor'), 0)

Lite/Services/RemoteCollectorService.ProcedureStats.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ CROSS APPLY
7676
FROM sys.dm_exec_plan_attributes(s.plan_handle) AS pa
7777
WHERE pa.attribute = N''dbid''
7878
) AS pa
79-
LEFT JOIN sys.databases AS d
79+
INNER JOIN sys.databases AS d
8080
ON pa.dbid = d.database_id
81-
WHERE pa.dbid NOT IN (1, 3, 4, 32761, 32767, ISNULL(DB_ID(N''PerformanceMonitor''), 0))
81+
WHERE d.state = 0
82+
AND pa.dbid NOT IN (1, 3, 4, 32761, 32767, ISNULL(DB_ID(N''PerformanceMonitor''), 0))
8283
AND s.last_execution_time >= DATEADD(MINUTE, -10, GETDATE())
8384
8485
UNION ALL
@@ -130,9 +131,10 @@ CROSS APPLY
130131
FROM sys.dm_exec_plan_attributes(s.plan_handle) AS pa
131132
WHERE pa.attribute = N''dbid''
132133
) AS pa
133-
LEFT JOIN sys.databases AS d
134+
INNER JOIN sys.databases AS d
134135
ON pa.dbid = d.database_id
135-
WHERE pa.dbid NOT IN (1, 3, 4, 32761, 32767, ISNULL(DB_ID(N''PerformanceMonitor''), 0))
136+
WHERE d.state = 0
137+
AND pa.dbid NOT IN (1, 3, 4, 32761, 32767, ISNULL(DB_ID(N''PerformanceMonitor''), 0))
136138
AND s.last_execution_time >= DATEADD(MINUTE, -10, GETDATE())
137139
138140
UNION ALL
@@ -171,9 +173,10 @@ CROSS APPLY
171173
FROM sys.dm_exec_plan_attributes(s.plan_handle) AS pa
172174
WHERE pa.attribute = N''dbid''
173175
) AS pa
174-
LEFT JOIN sys.databases AS d
176+
INNER JOIN sys.databases AS d
175177
ON pa.dbid = d.database_id
176-
WHERE pa.dbid NOT IN (1, 3, 4, 32761, 32767, ISNULL(DB_ID(N''PerformanceMonitor''), 0))
178+
WHERE d.state = 0
179+
AND pa.dbid NOT IN (1, 3, 4, 32761, 32767, ISNULL(DB_ID(N''PerformanceMonitor''), 0))
177180
AND s.last_execution_time >= DATEADD(MINUTE, -10, GETDATE())
178181
) AS combined
179182
ORDER BY total_elapsed_time DESC

Lite/Services/RemoteCollectorService.QuerySnapshots.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public partial class RemoteCollectorService
2525
2626
SELECT /* PerformanceMonitorLite */
2727
der.session_id,
28-
database_name = DB_NAME(der.database_id),
28+
database_name = d.name,
2929
elapsed_time_formatted =
3030
CASE
3131
WHEN der.total_elapsed_time < 0
@@ -72,6 +72,8 @@ JOIN sys.dm_exec_sessions AS des
7272
ON des.session_id = der.session_id
7373
OUTER APPLY sys.dm_exec_sql_text(COALESCE(der.sql_handle, der.plan_handle)) AS dest
7474
OUTER APPLY sys.dm_exec_text_query_plan(der.plan_handle, der.statement_start_offset, der.statement_end_offset) AS deqp
75+
LEFT JOIN sys.databases AS d
76+
ON d.database_id = der.database_id
7577
{1}
7678
WHERE der.session_id <> @@SPID
7779
AND der.session_id >= 50

Lite/Services/RemoteCollectorService.WaitingTasks.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ private async Task<int> CollectWaitingTasksAsync(ServerConnection server, Cancel
3232
wait_type = wt.wait_type,
3333
wait_duration_ms = wt.wait_duration_ms,
3434
blocking_session_id = wt.blocking_session_id,
35-
database_name = DB_NAME(er.database_id)
35+
database_name = d.name
3636
FROM sys.dm_os_waiting_tasks AS wt
3737
LEFT JOIN sys.dm_exec_requests AS er
3838
ON er.session_id = wt.session_id
39+
LEFT JOIN sys.databases AS d
40+
ON d.database_id = er.database_id
3941
WHERE wt.session_id >= 50
4042
AND wt.session_id <> @@SPID
4143
AND wt.wait_type IS NOT NULL

0 commit comments

Comments
 (0)