You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: public/Expand-DbaDbLogFile.ps1
+94Lines changed: 94 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -56,6 +56,10 @@ function Expand-DbaDbLogFile {
56
56
Controls the size of each growth operation in megabytes during the expansion process.
57
57
If not specified, the function calculates an optimal increment size based on your target size and SQL Server version to minimize VLF fragmentation. Only specify this if you need to override the intelligent defaults.
58
58
59
+
.PARAMETERTargetVlfCount
60
+
Sets the desired maximum number of Virtual Log Files (VLFs) after the expansion completes.
61
+
When specified, the function calculates the optimal increment size to keep total VLFs at or below this value. If the current VLF count already meets or exceeds this target, a warning is issued and the database is skipped — use -ShrinkLogFile to first reduce the VLF count. If the target is mathematically impossible given the required growth, a warning is issued and the database is skipped.
62
+
59
63
.PARAMETERLogFileId
60
64
Targets a specific transaction log file by its file ID number when databases have multiple log files.
61
65
Use this when you need to expand secondary log files instead of the primary log file. Get the file ID from sys.database_files or SSMS properties.
@@ -161,6 +165,16 @@ function Expand-DbaDbLogFile {
161
165
162
166
Grows the transaction logs for databases db1 and db2 on SQL server SQLInstance to 100MB, sets the incremental growth to 10MB, shrinks the transaction log to 10MB and uses the directory R:\MSSQL\Backup for the required backups.
Grows the transaction log for database db1 on sqlcluster to 10000MB and automatically calculates an increment size that keeps the total VLF count at or below 16.
Write-Message-Level Warning -Message "$step - Current VLF count ($($vlfCountBaseline.Total)) is already at or above the target VLF count ($TargetVlfCount) for database '$dbName'. Use -ShrinkLogFile to reduce VLF count first, then re-expand."
487
+
continue
488
+
}
489
+
490
+
$totalGrowthKB=$TargetLogSizeKB-$currentSize
491
+
492
+
if ($totalGrowthKB-gt0) {
493
+
$calculatedIncrementKB=$null
494
+
495
+
# For SQL 2014+ (version 12+), a growth smaller than 1/8 of the current file size creates only 1 VLF
Write-Message-Level Verbose -Message "$step - Using 16-VLF-per-growth strategy with increment $([Math]::Round($calculatedIncrementKB/1024.0,2))MB."
539
+
}
540
+
}
541
+
542
+
if ($null-eq$calculatedIncrementKB) {
543
+
Write-Message-Level Warning -Message "$step - Cannot achieve target VLF count of $TargetVlfCount for database '$dbName': the VLF budget is too small for the required growth from $currentSizeMB MB to $TargetLogSize MB. Increase -TargetVlfCount or use -ShrinkLogFile to start from a lower base."
0 commit comments