Skip to content

Commit 57fa89a

Browse files
Invoke-DbaDbShrink - Add error message output for failed shrink operations (#10258)
1 parent 444659b commit 57fa89a

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

public/Invoke-DbaDbShrink.ps1

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ function Invoke-DbaDbShrink {
295295
$start = Get-Date
296296
# saving previous timeout to be restored at the end
297297
$previousStatementTimeout = $instance.ConnectionContext.StatementTimeout
298+
$errorDetails = $null
298299
try {
299300
Write-Message -Level Verbose -Message 'Beginning shrink of files'
300301
$instance.ConnectionContext.StatementTimeout = $StatementTimeoutSeconds
@@ -327,8 +328,8 @@ function Invoke-DbaDbShrink {
327328
$success = $true
328329
} catch {
329330
$success = $false
330-
Stop-Function -Message 'Failure' -EnableException $EnableException -ErrorRecord $_ -Continue
331-
continue
331+
$errorDetails = $_.Exception.Message
332+
Stop-Function -Message "Shrink operation failed for file $($file.Name): $errorDetails" -ErrorRecord $_
332333
} finally {
333334
$instance.ConnectionContext.StatementTimeout = $previousStatementTimeout
334335
}
@@ -338,6 +339,14 @@ function Invoke-DbaDbShrink {
338339
Write-Message -Level Verbose -Message "Final file size: $($finalFileSizeKB)"
339340
Write-Message -Level Verbose -Message "Final file space available: $($finalSpaceAvailableKB)"
340341

342+
# Check if shrink didn't achieve target and provide feedback
343+
if ($success -and $finalFileSizeKB -gt $desiredFileSizeKB) {
344+
$shrinkShortfall = $finalFileSizeKB - $desiredFileSizeKB
345+
$partialShrinkMessage = "File only shrunk to $finalFileSizeKB (target was $desiredFileSizeKB). Shortfall: $shrinkShortfall. This may be due to active transactions, data distribution, or minimum file size constraints."
346+
Write-Message -Level Warning -Message $partialShrinkMessage
347+
$errorDetails = $partialShrinkMessage
348+
}
349+
341350
if ($server.VersionMajor -gt 8 -and $ExcludeIndexStats -eq $false -and $success -and $FileType -ne 'Log') {
342351
Write-Message -Level Verbose -Message 'Getting ending average fragmentation'
343352
$dataRow = $server.Query($sql, $db.name)
@@ -351,6 +360,13 @@ function Invoke-DbaDbShrink {
351360
$ts = [TimeSpan]::FromSeconds($timSpan.TotalSeconds)
352361
$elapsed = "{0:HH:mm:ss}" -f ([datetime]$ts.Ticks)
353362

363+
$notesText = 'Database shrinks can cause massive index fragmentation and negatively impact performance. You should now run DBCC INDEXDEFRAG or ALTER INDEX ... REORGANIZE'
364+
if ($errorDetails) {
365+
$notesText = "$errorDetails | $notesText"
366+
} else {
367+
$notesText = $notesText
368+
}
369+
354370
$object = [PSCustomObject]@{
355371
ComputerName = $server.ComputerName
356372
InstanceName = $server.ServiceName
@@ -371,7 +387,7 @@ function Invoke-DbaDbShrink {
371387
FinalAverageFragmentation = [math]::Round($endingDefrag, 1)
372388
InitialTopFragmentation = [math]::Round($startingTopFrag, 1)
373389
FinalTopFragmentation = [math]::Round($endingTopDefrag, 1)
374-
Notes = 'Database shrinks can cause massive index fragmentation and negatively impact performance. You should now run DBCC INDEXDEFRAG or ALTER INDEX ... REORGANIZE'
390+
Notes = $notesText
375391
}
376392
if ($ExcludeIndexStats) {
377393
Select-DefaultView -InputObject $object -ExcludeProperty InitialAverageFragmentation, FinalAverageFragmentation, InitialTopFragmentation, FinalTopFragmentation

0 commit comments

Comments
 (0)