Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .chloggen/sqlserver-schema-lock-blocking-fix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
component: receiver/sqlserver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix query sample events silently omitting sessions blocked on schema locks.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [49983]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
60 changes: 60 additions & 0 deletions receiver/sqlserverreceiver/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,66 @@ func TestGetSQLServerQuerySamplesQuery(t *testing.T) {
}
}

// TestQuerySampleQueryDetectsSchemaLockBlocking is a regression test for the class of
// bugs where sessions blocked on LCK_M_SCH_S / LCK_M_SCH_M (schema stability / modification
// locks) were silently dropped from the query sample results. The root cause was
// `CROSS APPLY sys.dm_exec_sql_text(r.plan_handle)`: for a session waiting on a schema
// lock, `sys.dm_exec_sql_text` returns zero rows, and `CROSS APPLY` therefore eliminates
// the outer row entirely, hiding the blocking session from downstream reporting.
//
// The fix uses `OUTER APPLY` (keeps the row when the TVF returns nothing) and adds
// `OUTER APPLY sys.dm_exec_input_buffer(...)` as a fallback source for statement_text.
//
// This test asserts the invariants that prevent the bug from being re-introduced.
// If a future change violates any of these, the failing assertion pinpoints the
// specific problem instead of just a diff error.
func TestQuerySampleQueryDetectsSchemaLockBlocking(t *testing.T) {
query := getSQLServerQuerySamplesQuery()

// Invariant 1: must not use CROSS APPLY on dm_exec_sql_text.
// CROSS APPLY drops rows when the TVF returns zero rows, which is exactly what
// happens for sessions blocked on LCK_M_SCH_S / LCK_M_SCH_M.
require.NotContains(t, query, "CROSS APPLY sys.dm_exec_sql_text",
"query sample must not use CROSS APPLY on sys.dm_exec_sql_text — it silently drops "+
"sessions blocked on LCK_M_SCH_S / LCK_M_SCH_M. Use OUTER APPLY instead.")

// Invariant 2: must use OUTER APPLY on dm_exec_sql_text.
require.Contains(t, query, "OUTER APPLY sys.dm_exec_sql_text(r.plan_handle)",
"query sample must use OUTER APPLY on sys.dm_exec_sql_text(r.plan_handle) so that "+
"sessions with unresolvable plan_handle (e.g. blocked on schema locks) are still emitted.")

// Invariant 3: must include sys.dm_exec_input_buffer as a fallback source of
// statement_text. When plan_handle is unresolvable, dm_exec_input_buffer reads
// directly from the client connection buffer and still returns the submitted SQL.
require.Contains(t, query, "sys.dm_exec_input_buffer",
"query sample must join sys.dm_exec_input_buffer to provide fallback statement_text "+
"when plan_handle cannot be resolved.")

// Invariant 4: WHERE clause must be NULL-safe for o.TEXT.
// A bare `WHERE o.TEXT NOT LIKE ...` treats NULL as UNKNOWN and drops the row —
// silently re-introducing the same class of bug for encrypted procs and other
// cases where dm_exec_sql_text returns a row with NULL text.
require.Contains(t, query, "o.TEXT IS NULL OR o.TEXT NOT LIKE",
"query sample WHERE clause must be NULL-safe for o.TEXT so that rows with NULL "+
"plan text (e.g. encrypted stored procedures) are not silently dropped.")

// Invariant 5: WHERE clause must be NULL-safe for input_buffer text as well.
require.Contains(t, query, "ib.event_info IS NULL OR ib.event_info NOT LIKE",
"query sample WHERE clause must be NULL-safe for ib.event_info.")

// Invariant 6: statement_text must prefer plan_handle-based extraction (which uses
// statement_start_offset / statement_end_offset to isolate the *specific* statement
// inside a multi-statement batch), and only fall back to input_buffer (which
// returns the whole batch) when the plan_handle lookup failed. Using COALESCE
// with SUBSTRING first, then ib.event_info, preserves statement-level granularity
// for healthy sessions.
require.Contains(t, query, "COALESCE(",
"query sample must COALESCE plan_handle SUBSTRING with input_buffer fallback so "+
"statement-level granularity is preserved for healthy sessions.")
require.Contains(t, query, "ib.event_info,",
"query sample must reference ib.event_info in the COALESCE fallback chain.")
}

func TestGetSQLServerIdleBlockingSessionsQuery(t *testing.T) {
expectedBytes, err := os.ReadFile(path.Join(".", "testdata", "testIdleBlockerQuerySampleQuery.txt"))
require.NoError(t, err)
Expand Down
27 changes: 16 additions & 11 deletions receiver/sqlserverreceiver/templates/sqlServerQuerySample.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
r.STATUS AS request_status,
ISNULL(s.host_name, '') AS host_name,
r.command,
SUBSTRING(o.TEXT, (r.statement_start_offset / 2) + 1, (
(
CASE r.statement_end_offset
WHEN - 1
THEN DATALENGTH(o.TEXT)
ELSE r.statement_end_offset
END - r.statement_start_offset
) / 2
) + 1) AS statement_text,
COALESCE(
SUBSTRING(o.TEXT, (r.statement_start_offset / 2) + 1, (
(
CASE r.statement_end_offset
WHEN - 1
THEN DATALENGTH(o.TEXT)
ELSE r.statement_end_offset
END - r.statement_start_offset
) / 2
) + 1),
ib.event_info,
'') AS statement_text,
r.blocking_session_id,
CASE
WHEN r.blocking_session_id > 0 AND r.wait_time > 0
Expand Down Expand Up @@ -56,6 +59,8 @@
FROM sys.dm_exec_requests r
INNER JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id
INNER JOIN sys.dm_exec_connections c ON s.session_id = c.session_id
CROSS APPLY sys.dm_exec_sql_text(r.plan_handle) AS o
WHERE o.TEXT NOT LIKE N'/* otel-collector */%'
OUTER APPLY sys.dm_exec_sql_text(r.plan_handle) AS o
OUTER APPLY sys.dm_exec_input_buffer(r.session_id, r.request_id) AS ib
WHERE (o.TEXT IS NULL OR o.TEXT NOT LIKE N'/* otel-collector */%')
AND (ib.event_info IS NULL OR ib.event_info NOT LIKE N'/* otel-collector */%')
AND r.session_id != @@SPID;
27 changes: 16 additions & 11 deletions receiver/sqlserverreceiver/testdata/testQuerySampleQuery.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
r.STATUS AS request_status,
ISNULL(s.host_name, '') AS host_name,
r.command,
SUBSTRING(o.TEXT, (r.statement_start_offset / 2) + 1, (
(
CASE r.statement_end_offset
WHEN - 1
THEN DATALENGTH(o.TEXT)
ELSE r.statement_end_offset
END - r.statement_start_offset
) / 2
) + 1) AS statement_text,
COALESCE(
SUBSTRING(o.TEXT, (r.statement_start_offset / 2) + 1, (
(
CASE r.statement_end_offset
WHEN - 1
THEN DATALENGTH(o.TEXT)
ELSE r.statement_end_offset
END - r.statement_start_offset
) / 2
) + 1),
ib.event_info,
'') AS statement_text,
r.blocking_session_id,
CASE
WHEN r.blocking_session_id > 0 AND r.wait_time > 0
Expand Down Expand Up @@ -56,6 +59,8 @@
FROM sys.dm_exec_requests r
INNER JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id
INNER JOIN sys.dm_exec_connections c ON s.session_id = c.session_id
CROSS APPLY sys.dm_exec_sql_text(r.plan_handle) AS o
WHERE o.TEXT NOT LIKE N'/* otel-collector */%'
OUTER APPLY sys.dm_exec_sql_text(r.plan_handle) AS o
OUTER APPLY sys.dm_exec_input_buffer(r.session_id, r.request_id) AS ib
WHERE (o.TEXT IS NULL OR o.TEXT NOT LIKE N'/* otel-collector */%')
AND (ib.event_info IS NULL OR ib.event_info NOT LIKE N'/* otel-collector */%')
AND r.session_id != @@SPID;
Loading