Skip to content

Commit 5c94d66

Browse files
Merge pull request #548 from erikdarlingdata/fix/issue-547-smallint-overflow
Fix arithmetic overflow in query_stats collector
2 parents c0b1dec + ab0a917 commit 5c94d66

4 files changed

Lines changed: 84 additions & 12 deletions

File tree

install/02_create_tables.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,18 @@ BEGIN
119119
max_rows bigint NOT NULL,
120120
statement_sql_handle varbinary(64) NULL,
121121
statement_context_id bigint NULL,
122-
min_dop smallint NOT NULL,
123-
max_dop smallint NOT NULL,
122+
min_dop bigint NOT NULL,
123+
max_dop bigint NOT NULL,
124124
min_grant_kb bigint NOT NULL,
125125
max_grant_kb bigint NOT NULL,
126126
min_used_grant_kb bigint NOT NULL,
127127
max_used_grant_kb bigint NOT NULL,
128128
min_ideal_grant_kb bigint NOT NULL,
129129
max_ideal_grant_kb bigint NOT NULL,
130-
min_reserved_threads integer NOT NULL,
131-
max_reserved_threads integer NOT NULL,
132-
min_used_threads integer NOT NULL,
133-
max_used_threads integer NOT NULL,
130+
min_reserved_threads bigint NOT NULL,
131+
max_reserved_threads bigint NOT NULL,
132+
min_used_threads bigint NOT NULL,
133+
max_used_threads bigint NOT NULL,
134134
total_spills bigint NOT NULL,
135135
min_spills bigint NOT NULL,
136136
max_spills bigint NOT NULL,

install/08_collect_query_stats.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,18 @@ BEGIN
202202
max_rows bigint NOT NULL,
203203
statement_sql_handle varbinary(64) NULL,
204204
statement_context_id bigint NULL,
205-
min_dop smallint NOT NULL,
206-
max_dop smallint NOT NULL,
205+
min_dop bigint NOT NULL,
206+
max_dop bigint NOT NULL,
207207
min_grant_kb bigint NOT NULL,
208208
max_grant_kb bigint NOT NULL,
209209
min_used_grant_kb bigint NOT NULL,
210210
max_used_grant_kb bigint NOT NULL,
211211
min_ideal_grant_kb bigint NOT NULL,
212212
max_ideal_grant_kb bigint NOT NULL,
213-
min_reserved_threads integer NOT NULL,
214-
max_reserved_threads integer NOT NULL,
215-
min_used_threads integer NOT NULL,
216-
max_used_threads integer NOT NULL,
213+
min_reserved_threads bigint NOT NULL,
214+
max_reserved_threads bigint NOT NULL,
215+
min_used_threads bigint NOT NULL,
216+
max_used_threads bigint NOT NULL,
217217
total_spills bigint NOT NULL,
218218
min_spills bigint NOT NULL,
219219
max_spills bigint NOT NULL,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2026 Darling Data, LLC
3+
https://www.erikdarling.com/
4+
5+
Upgrade from 2.2.0 to 2.3.0
6+
Widens query_stats columns to match sys.dm_exec_query_stats DMV types:
7+
- min_dop, max_dop: smallint -> bigint
8+
- min_reserved_threads, max_reserved_threads: integer -> bigint
9+
- min_used_threads, max_used_threads: integer -> bigint
10+
Fixes arithmetic overflow error on INSERT (#547)
11+
*/
12+
13+
SET ANSI_NULLS ON;
14+
SET ANSI_PADDING ON;
15+
SET ANSI_WARNINGS ON;
16+
SET ARITHABORT ON;
17+
SET CONCAT_NULL_YIELDS_NULL ON;
18+
SET QUOTED_IDENTIFIER ON;
19+
SET NUMERIC_ROUNDABORT OFF;
20+
SET IMPLICIT_TRANSACTIONS OFF;
21+
SET STATISTICS TIME, IO OFF;
22+
GO
23+
24+
USE PerformanceMonitor;
25+
GO
26+
27+
IF OBJECT_ID(N'collect.query_stats', N'U') IS NOT NULL
28+
BEGIN
29+
PRINT 'Widening collect.query_stats columns to match DMV types...';
30+
31+
IF EXISTS
32+
(
33+
SELECT
34+
1/0
35+
FROM INFORMATION_SCHEMA.COLUMNS
36+
WHERE TABLE_SCHEMA = N'collect'
37+
AND TABLE_NAME = N'query_stats'
38+
AND COLUMN_NAME = N'min_dop'
39+
AND DATA_TYPE = N'smallint'
40+
)
41+
BEGIN
42+
ALTER TABLE collect.query_stats ALTER COLUMN min_dop bigint NOT NULL;
43+
ALTER TABLE collect.query_stats ALTER COLUMN max_dop bigint NOT NULL;
44+
PRINT ' min_dop, max_dop: smallint -> bigint';
45+
END;
46+
47+
IF EXISTS
48+
(
49+
SELECT
50+
1/0
51+
FROM INFORMATION_SCHEMA.COLUMNS
52+
WHERE TABLE_SCHEMA = N'collect'
53+
AND TABLE_NAME = N'query_stats'
54+
AND COLUMN_NAME = N'min_reserved_threads'
55+
AND DATA_TYPE = N'int'
56+
)
57+
BEGIN
58+
ALTER TABLE collect.query_stats ALTER COLUMN min_reserved_threads bigint NOT NULL;
59+
ALTER TABLE collect.query_stats ALTER COLUMN max_reserved_threads bigint NOT NULL;
60+
ALTER TABLE collect.query_stats ALTER COLUMN min_used_threads bigint NOT NULL;
61+
ALTER TABLE collect.query_stats ALTER COLUMN max_used_threads bigint NOT NULL;
62+
PRINT ' min/max_reserved_threads, min/max_used_threads: int -> bigint';
63+
END;
64+
65+
PRINT 'Column widening complete.';
66+
END;
67+
ELSE
68+
BEGIN
69+
PRINT 'Table collect.query_stats does not exist, skipping.';
70+
END;
71+
GO
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01_widen_query_stats_columns.sql

0 commit comments

Comments
 (0)