Skip to content

Commit f6f0778

Browse files
Fix 27 logical bugs across all 10 stored procedures
Bugs found via automated review, validated one-by-one against SQL2022: - sp_HealthParser: Fix XPath inputbuf extraction - sp_HumanEvents: Fix memory filter, CATCH cleanup, QUOTENAME schema/table - sp_HumanEventsBlockViewer: Fix table mode XML, DATEADD overflow guard - sp_LogHunter: Add archive > 0 guard - sp_IndexCleanup: Fix LEN trailing space (use DATALENGTH), fix t.max_length vs c.max_length for detecting (max) columns - sp_PerfCheck: Fix NULL @processors check, TOKENANDPERMUSERSTORE priority gap, wrap DMV queries in VIEW SERVER STATE guard - sp_QueryReproBuilder: Fix version gates from @new to @sql_2017 for columns available since SQL 2017 - sp_QueryStoreCleanup: Fix COUNT_BIG(*) false positives with COUNT(DISTINCT) (39% false positive rate eliminated) - sp_QuickieStore: Fix cursor FETCH NEXT/CONTINUE for @get_all_databases, remove compile memory 8x inflation, fix log bytes 95x deflation, remove duplicate TRUNCATEs, add @@DATEFIRST ELSE warning for @workdays - sp_PressureDetector: Add missing SET LOCK_TIMEOUT -1, fix DATEDIFF divide-by-zero, fix sampled avg_ms_per_wait delta calculation, fix @Prefix NULL on Azure All 10 procedures installed and executed successfully on SQL2022. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 01644dc commit f6f0778

10 files changed

Lines changed: 109 additions & 74 deletions

File tree

sp_HealthParser/sp_HealthParser.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5677,7 +5677,7 @@ AND ca.utc_timestamp < @end_date';
56775677
isolation_level = e.x.value('@isolationlevel', 'sysname'),
56785678
clientoption1 = e.x.value('@clientoption1', 'bigint'),
56795679
clientoption2 = e.x.value('@clientoption2', 'bigint'),
5680-
query_text_pre = e.x.value('(//process/inputbuf/text())[1]', 'nvarchar(max)'),
5680+
query_text_pre = e.x.value('(inputbuf/text())[1]', 'nvarchar(max)'),
56815681
process_xml = e.x.query(N'.'),
56825682
deadlock_resources = d.xml_deadlock_report.query('//deadlock/resource-list')
56835683
FROM #deadlocks AS d

sp_HumanEvents/sp_HumanEvents.sql

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,8 @@ SET @session_filter_query_plans +=
15441544
ISNULL(@database_name_filter, N'') +
15451545
ISNULL(@session_id_filter, N'') +
15461546
ISNULL(@username_filter, N'') +
1547-
ISNULL(@object_name_filter, N'')
1547+
ISNULL(@object_name_filter, N'') +
1548+
ISNULL(@requested_memory_mb_filter, N'')
15481549
);
15491550

15501551
/* Recompile can have almost everything except... duration */
@@ -3754,7 +3755,7 @@ BEGIN
37543755
N'.' +
37553756
QUOTENAME(hew.output_schema) +
37563757
N'.' +
3757-
hew.output_table
3758+
QUOTENAME(hew.output_table)
37583759
FROM #human_events_worker AS hew
37593760
WHERE hew.id = @min_id
37603761
AND hew.is_table_created = 0;
@@ -4105,7 +4106,7 @@ END;
41054106
N'.' +
41064107
QUOTENAME(hew.output_schema) +
41074108
N'.' +
4108-
hew.output_table,
4109+
QUOTENAME(hew.output_table),
41094110
@date_filter =
41104111
DATEADD
41114112
(
@@ -4848,7 +4849,7 @@ BEGIN
48484849
SELECT
48494850
@i_cleanup_tables +=
48504851
N''DROP TABLE '' +
4851-
SCHEMA_NAME(s.schema_id) +
4852+
QUOTENAME(SCHEMA_NAME(s.schema_id)) +
48524853
N''.'' +
48534854
QUOTENAME(s.name) +
48544855
''; '' +
@@ -4879,7 +4880,7 @@ BEGIN
48794880
SELECT
48804881
@i_cleanup_views +=
48814882
N''DROP VIEW '' +
4882-
SCHEMA_NAME(v.schema_id) +
4883+
QUOTENAME(SCHEMA_NAME(v.schema_id)) +
48834884
N''.'' +
48844885
QUOTENAME(v.name) +
48854886
''; '' +
@@ -4912,7 +4913,7 @@ BEGIN CATCH
49124913

49134914
/*Only try to drop a session if we're not outputting*/
49144915
IF (@output_database_name = N''
4915-
AND @output_schema_name = N'')
4916+
AND @output_schema_name IN (N'', N'dbo'))
49164917
BEGIN
49174918
IF @debug = 1
49184919
BEGIN

sp_HumanEvents/sp_HumanEventsBlockViewer.sql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,9 +1835,7 @@ BEGIN
18351835
/* Build dynamic SQL to extract the XML */
18361836
SET @extract_sql = N'
18371837
SELECT TOP (' + CONVERT(nvarchar(20), CASE WHEN @max_blocking_events > 0 THEN @max_blocking_events ELSE 2147483647 END) + N')
1838-
human_events_xml = ' +
1839-
QUOTENAME(@target_column) +
1840-
N'
1838+
human_events_xml = e.x.query(''.'')
18411839
FROM ' +
18421840
QUOTENAME(@target_database) +
18431841
N'.' +
@@ -3622,7 +3620,7 @@ BEGIN
36223620
bigint,
36233621
b.wait_time_ms
36243622
)
3625-
) / 1000
3623+
) / 1000 % 86400
36263624
),
36273625
'19000101'
36283626
),
@@ -3711,7 +3709,7 @@ BEGIN
37113709
bigint,
37123710
b.wait_time_ms
37133711
)
3714-
) / 1000
3712+
) / 1000 % 86400
37153713
),
37163714
'19000101'
37173715
),

sp_IndexCleanup/sp_IndexCleanup.sql

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,16 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10271027
ON #filtered_index_columns_analysis
10281028
(database_id, schema_id, object_id, index_id);
10291029

1030+
CREATE TABLE
1031+
#merged_includes
1032+
(
1033+
scope_hash varbinary(32) NOT NULL,
1034+
index_name sysname NOT NULL,
1035+
key_columns nvarchar(max) NOT NULL,
1036+
merged_includes nvarchar(max) NULL,
1037+
PRIMARY KEY (scope_hash, index_name)
1038+
);
1039+
10301040
/* Parse @include_databases comma-separated list */
10311041
IF @get_all_databases = 1
10321042
AND @include_databases IS NOT NULL
@@ -1135,10 +1145,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11351145
OPTION(RECOMPILE);
11361146

11371147
/* If we found any conflicts, raise an error */
1138-
IF LEN(@conflict_list) > 0
1148+
IF DATALENGTH(@conflict_list) > 0
11391149
BEGIN
11401150
/* Remove trailing comma and space */
1141-
SET @conflict_list = LEFT(@conflict_list, LEN(@conflict_list) - 2);
1151+
SET @conflict_list = LEFT(@conflict_list, DATALENGTH(@conflict_list) / 2 - 2);
11421152

11431153
SET @error_msg =
11441154
N'The following databases appear in both @include_databases and @exclude_databases, which creates ambiguity: ' +
@@ -1380,6 +1390,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13801390
#check_constraints_analysis;
13811391
TRUNCATE TABLE
13821392
#filtered_index_columns_analysis;
1393+
TRUNCATE TABLE
1394+
#merged_includes;
13831395

13841396
/*Validate searched objects per-database*/
13851397
IF @schema_name IS NOT NULL
@@ -2178,7 +2190,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21782190
WHERE c.system_type_id = t.system_type_id
21792191
AND c.user_type_id = t.user_type_id
21802192
AND t.name IN (N''varchar'', N''nvarchar'')
2181-
AND t.max_length = -1
2193+
AND c.max_length = -1
21822194
)
21832195
THEN 1
21842196
ELSE 0
@@ -3374,16 +3386,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33743386
OPTION(RECOMPILE);
33753387
END;
33763388

3377-
CREATE TABLE
3378-
#merged_includes
3379-
(
3380-
scope_hash bigint NOT NULL,
3381-
index_name sysname NOT NULL,
3382-
key_columns nvarchar(max) NOT NULL,
3383-
merged_includes nvarchar(max) NULL,
3384-
PRIMARY KEY (scope_hash, index_name)
3385-
);
3386-
33873389
/* Gather all supersets that need include merging */
33883390
INSERT INTO
33893391
#merged_includes

sp_LogHunter/sp_LogHunter.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ BEGIN
367367
DELETE
368368
e WITH(TABLOCKX)
369369
FROM #enum AS e
370-
WHERE e.log_date < CONVERT(date, @start_date)
371-
OR e.log_date > CONVERT(date, @end_date)
370+
WHERE (e.log_date < CONVERT(date, @start_date)
371+
OR e.log_date > CONVERT(date, @end_date))
372+
AND e.archive > 0
372373
OPTION(RECOMPILE);
373374
END;
374375

sp_PerfCheck/sp_PerfCheck.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
990990
END;
991991
END;
992992

993+
IF @has_view_server_state = 1
994+
BEGIN
993995
/* Check for high number of deadlocks */
994996
INSERT INTO
995997
#results
@@ -1101,7 +1103,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11011103
CASE
11021104
WHEN CONVERT(decimal(10, 2), (domc.pages_kb / 1024.0 / 1024.0)) > 5
11031105
THEN 20 /* Very high priority >5GB */
1104-
WHEN CONVERT(decimal(10, 2), (domc.pages_kb / 1024.0 / 1024.0)) BETWEEN 3 AND 5
1106+
WHEN CONVERT(decimal(10, 2), (domc.pages_kb / 1024.0 / 1024.0)) BETWEEN 2 AND 5
11051107
THEN 30 /* High priority >2GB */
11061108
WHEN CONVERT(decimal(10, 2), (domc.pages_kb / 1024.0 / 1024.0)) BETWEEN 1 AND 2
11071109
THEN 40 /* Medium-high priority >1GB */
@@ -1130,6 +1132,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11301132
osi.physical_memory_kb / 1024.0 / 1024.0
11311133
)
11321134
FROM sys.dm_os_sys_info AS osi;
1135+
END;
11331136

11341137
/* Check if Lock Pages in Memory is enabled (on-prem and managed instances only) */
11351138
IF @azure_sql_db = 0
@@ -3306,7 +3309,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33063309
END;
33073310

33083311
/* Check for single data file */
3309-
IF @tempdb_data_file_count = 1
3312+
IF @tempdb_data_file_count = 1
3313+
AND @processors IS NOT NULL
33103314
BEGIN
33113315
INSERT INTO
33123316
#results

sp_PressureDetector/sp_PressureDetector.sql

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -441,17 +441,21 @@ OPTION(MAXDOP 1, RECOMPILE);',
441441
ELSE 0
442442
END,
443443
@prefix sysname =
444+
ISNULL
444445
(
445-
SELECT TOP (1)
446-
SUBSTRING
447-
(
448-
dopc.object_name,
449-
1,
450-
CHARINDEX(N':', dopc.object_name)
451-
)
452-
FROM sys.dm_os_performance_counters AS dopc
453-
) +
454-
N'%',
446+
(
447+
SELECT TOP (1)
448+
SUBSTRING
449+
(
450+
dopc.object_name,
451+
1,
452+
CHARINDEX(N':', dopc.object_name)
453+
)
454+
FROM sys.dm_os_performance_counters AS dopc
455+
) +
456+
N'%',
457+
N'%'
458+
),
455459
@memory_grant_cap xml,
456460
@cache_xml xml,
457461
@cache_sql nvarchar(max) = N'',
@@ -1452,7 +1456,16 @@ OPTION(MAXDOP 1, RECOMPILE);',
14521456
CONVERT
14531457
(
14541458
decimal(38,1),
1455-
(w2.avg_ms_per_wait + w.avg_ms_per_wait) / 2
1459+
ISNULL
1460+
(
1461+
(w2.hours_wait_time - w.hours_wait_time) /
1462+
NULLIF
1463+
(
1464+
1. * (w2.waiting_tasks_count_n - w.waiting_tasks_count_n),
1465+
0.
1466+
),
1467+
0.
1468+
)
14561469
),
14571470
percent_signal_waits =
14581471
CONVERT
@@ -2106,11 +2119,15 @@ OPTION(MAXDOP 1, RECOMPILE);',
21062119
dopc.cntr_value /
21072120
ISNULL
21082121
(
2109-
DATEDIFF
2122+
NULLIF
21102123
(
2111-
SECOND,
2112-
dopc.sample_time,
2113-
SYSDATETIME()
2124+
DATEDIFF
2125+
(
2126+
SECOND,
2127+
dopc.sample_time,
2128+
SYSDATETIME()
2129+
),
2130+
0
21142131
),
21152132
1
21162133
),
@@ -3932,7 +3949,10 @@ OPTION(MAXDOP 1, RECOMPILE);',
39323949
THEN N'
39333950
der.cpu_time DESC,
39343951
der.parallel_worker_count DESC
3935-
OPTION(MAXDOP 1, RECOMPILE);'
3952+
OPTION(MAXDOP 1, RECOMPILE);
3953+
3954+
SET LOCK_TIMEOUT -1;
3955+
'
39363956
ELSE N'
39373957
der.cpu_time DESC
39383958
OPTION(MAXDOP 1, RECOMPILE);

sp_QueryReproBuilder/sp_QueryReproBuilder.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ SELECT
17701770
MAX(qsrs_with_lasts.max_rowcount),';
17711771

17721772
/*Add SQL 2017+ columns*/
1773-
IF @new = 1
1773+
IF @sql_2017 = 1
17741774
BEGIN
17751775
SELECT @sql += N'
17761776
AVG((qsrs_with_lasts.avg_num_physical_io_reads * 8.) / 1024.),
@@ -1911,7 +1911,7 @@ FROM
19111911
),';
19121912

19131913
/*Add SQL 2017+ windowing columns*/
1914-
IF @new = 1
1914+
IF @sql_2017 = 1
19151915
BEGIN
19161916
SELECT @sql += N'
19171917
partitioned_last_num_physical_io_reads =
@@ -2210,7 +2210,7 @@ BEGIN
22102210
qsp.is_optimized_plan_forcing_disabled,
22112211
qsp.plan_type_desc';
22122212
END;
2213-
ELSE IF @new = 1
2213+
ELSE IF @sql_2017 = 1
22142214
BEGIN
22152215
SELECT @sql += N'
22162216
qsp.plan_forcing_type_desc,

sp_QueryStoreCleanup/sp_QueryStoreCleanup.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ WITH
563563
)
564564
SELECT
565565
qsq.query_hash,
566-
total_plans = COUNT_BIG(*)
566+
total_plans = COUNT_BIG(DISTINCT qsq.query_id)
567567
FROM ' + @database_name_quoted + N'.sys.query_store_runtime_stats AS qsrs
568568
JOIN ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
569569
ON qsrs.plan_id = qsp.plan_id
@@ -585,7 +585,7 @@ AND EXISTS
585585
GROUP BY
586586
qsq.query_hash
587587
HAVING
588-
COUNT_BIG(*) > 1
588+
COUNT_BIG(DISTINCT qsq.query_id) > 1
589589
OPTION(RECOMPILE);';
590590

591591
IF @debug = 1
@@ -627,7 +627,7 @@ WITH
627627
)
628628
SELECT
629629
qsp.query_plan_hash,
630-
total_plans = COUNT_BIG(*)
630+
total_plans = COUNT_BIG(DISTINCT qsp.plan_id)
631631
FROM ' + @database_name_quoted + N'.sys.query_store_runtime_stats AS qsrs
632632
JOIN ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
633633
ON qsrs.plan_id = qsp.plan_id
@@ -649,7 +649,7 @@ AND EXISTS
649649
GROUP BY
650650
qsp.query_plan_hash
651651
HAVING
652-
COUNT_BIG(*) > 1
652+
COUNT_BIG(DISTINCT qsp.plan_id) > 1
653653
OPTION(RECOMPILE);';
654654

655655
IF @debug = 1

0 commit comments

Comments
 (0)