Skip to content

Commit bf29410

Browse files
Fix CREATE DATABASE failure when model database has large files (#676) (#678)
If the model database has been configured with file sizes larger than our defaults (1024MB data, 256MB log), CREATE DATABASE fails with error 1803. Two layers of protection: 1. Pre-check model file sizes via sys.master_files (TRY/CATCH for restricted permissions, NULL-safe fallback to defaults) 2. If CREATE DATABASE still fails with error 1803, parse the required size from the error message and retry once Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c953158 commit bf29410

1 file changed

Lines changed: 130 additions & 4 deletions

File tree

install/01_install_database.sql

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ BEGIN
6060
END;
6161
ELSE
6262
BEGIN
63+
DECLARE
64+
@data_size_mb integer = 1024,
65+
@log_size_mb integer = 256;
66+
6367
/*
6468
Get the default data and log directories from instance properties
6569
*/
@@ -79,6 +83,42 @@ BEGIN
7983
) +
8084
N'PerformanceMonitor_log.ldf';
8185

86+
/*
87+
Check model database file sizes so CREATE DATABASE doesn't fail
88+
if model is larger than our defaults (#676).
89+
Wrapped in TRY/CATCH for restricted permission environments.
90+
*/
91+
BEGIN TRY
92+
SELECT
93+
@data_size_mb =
94+
MAX
95+
(
96+
CASE
97+
WHEN mf.type = 0
98+
THEN CONVERT(integer, mf.size / 128)
99+
END
100+
),
101+
@log_size_mb =
102+
MAX
103+
(
104+
CASE
105+
WHEN mf.type = 1
106+
THEN CONVERT(integer, mf.size / 128)
107+
END
108+
)
109+
FROM sys.master_files AS mf
110+
WHERE mf.database_id = DB_ID(N'model');
111+
112+
IF @data_size_mb < 1024 OR @data_size_mb IS NULL
113+
SET @data_size_mb = 1024;
114+
IF @log_size_mb < 256 OR @log_size_mb IS NULL
115+
SET @log_size_mb = 256;
116+
END TRY
117+
BEGIN CATCH
118+
SET @data_size_mb = 1024;
119+
SET @log_size_mb = 256;
120+
END CATCH;
121+
82122
/*
83123
Build and execute CREATE DATABASE statement with proper file paths
84124
*/
@@ -89,21 +129,107 @@ BEGIN
89129
(
90130
NAME = N''PerformanceMonitor'',
91131
FILENAME = N''' + @data_path + N''',
92-
SIZE = 1024MB,
132+
SIZE = ' + CONVERT(nvarchar(20), @data_size_mb) + N'MB,
93133
MAXSIZE = UNLIMITED,
94134
FILEGROWTH = 1024MB
95135
)
96136
LOG ON
97137
(
98138
NAME = N''PerformanceMonitor_log'',
99139
FILENAME = N''' + @log_path + N''',
100-
SIZE = 256MB,
140+
SIZE = ' + CONVERT(nvarchar(20), @log_size_mb) + N'MB,
101141
MAXSIZE = UNLIMITED,
102142
FILEGROWTH = 64MB
103143
);';
104144

105-
EXECUTE sys.sp_executesql
106-
@sql;
145+
BEGIN TRY
146+
EXECUTE sys.sp_executesql
147+
@sql;
148+
END TRY
149+
BEGIN CATCH
150+
/*
151+
If model is larger than expected and sys.master_files was
152+
inaccessible, error 1803 tells us the required size in MB.
153+
Parse it from the error message and retry once.
154+
*/
155+
IF ERROR_NUMBER() = 1803
156+
BEGIN
157+
DECLARE
158+
@error_msg nvarchar(4000) = ERROR_MESSAGE(),
159+
@mb_pos integer = 0,
160+
@num_start integer = 0,
161+
@required_mb integer = NULL;
162+
163+
/*MB is language-independent in error messages*/
164+
SET @mb_pos = CHARINDEX(N' MB', @error_msg);
165+
166+
IF @mb_pos > 0
167+
BEGIN
168+
SET @num_start = @mb_pos - 1;
169+
170+
WHILE @num_start > 0
171+
AND SUBSTRING(@error_msg, @num_start, 1) LIKE N'[0-9]'
172+
SET @num_start = @num_start - 1;
173+
174+
SET @num_start = @num_start + 1;
175+
176+
SET @required_mb =
177+
TRY_CONVERT
178+
(
179+
integer,
180+
SUBSTRING
181+
(
182+
@error_msg,
183+
@num_start,
184+
@mb_pos - @num_start
185+
)
186+
);
187+
END;
188+
189+
IF @required_mb IS NOT NULL
190+
BEGIN
191+
PRINT N'Model database requires at least '
192+
+ CONVERT(nvarchar(20), @required_mb)
193+
+ N' MB, retrying CREATE DATABASE...';
194+
195+
IF @required_mb > @data_size_mb
196+
SET @data_size_mb = @required_mb;
197+
IF @required_mb > @log_size_mb
198+
SET @log_size_mb = @required_mb;
199+
200+
SET @sql = N'
201+
CREATE DATABASE
202+
PerformanceMonitor
203+
ON PRIMARY
204+
(
205+
NAME = N''PerformanceMonitor'',
206+
FILENAME = N''' + @data_path + N''',
207+
SIZE = ' + CONVERT(nvarchar(20), @data_size_mb) + N'MB,
208+
MAXSIZE = UNLIMITED,
209+
FILEGROWTH = 1024MB
210+
)
211+
LOG ON
212+
(
213+
NAME = N''PerformanceMonitor_log'',
214+
FILENAME = N''' + @log_path + N''',
215+
SIZE = ' + CONVERT(nvarchar(20), @log_size_mb) + N'MB,
216+
MAXSIZE = UNLIMITED,
217+
FILEGROWTH = 64MB
218+
);';
219+
220+
EXECUTE sys.sp_executesql
221+
@sql;
222+
END;
223+
ELSE
224+
BEGIN
225+
THROW;
226+
END;
227+
END;
228+
ELSE
229+
BEGIN
230+
THROW;
231+
END;
232+
END CATCH;
107233

108234
ALTER DATABASE
109235
PerformanceMonitor

0 commit comments

Comments
 (0)