Search code examples
sql-serverpowershellsql-server-2016

Query on @@IDLE or @@IO_BUSY fails on some databases from Invoke-Sqlcmd


Running the same query on multiple database sometimes fails when @@IDLE or @@IO_BUSY is queried. It works on 6 databases and fails on 4 databases. When using -Verbose, VERBOSE: Arithmetic overflow occurred. is output, but there is no indication of what object overflowed.

PS C:\src\Modules> $Query
SELECT
    SERVERPROPERTY('ServerName') AS ServerName
    ,SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS ComputerNamePhysicalNetBIOS
    ,@@IDENTITY AS I_DENTITY
    ,@@IDLE AS I_DLE
    ,@@IO_BUSY AS I_O_BUSY
    ,@@MAX_PRECISION AS MAX_PRECISION
PS C:\src\Modules> $ServerInstance = 'DBSERVER1'
PS C:\src\Modules> Invoke-Sqlcmd -Query $Query -ServerInstance $ServerInstance -Verbose

ServerName                  : DBSESRVER1
ComputerNamePhysicalNetBIOS : DGEDW284
I_DENTITY                   :
I_DLE                       : -869467476
I_O_BUSY                    : 1767922
MAX_PRECISION               : 38


PS C:\src\Modules> $ServerInstance = 'DBSERVER2'
PS C:\src\Modules> Invoke-Sqlcmd -Query $Query -ServerInstance $ServerInstance -Verbose
PS C:\src\Modules>

The code used sets $Results to $null on the failing databases.

try {
    $Results = Invoke-Sqlcmd -Query $Query `
        -ServerInstance $Instance `
        -ErrorAction SilentlyContinue
    if ($null -ne $Results) {
        foreach ($Result in $Results) {
            $result | Add-Member -NotePropertyName instance -NotePropertyValue $Instance
            $ResultList += $result
        }
    } else {
        Write-Verbose "Results is set to `$null for instance $Instance"
    }
}
catch {
    Write-Verbose "in catch"
}

Solution

  • Known bug on @@IO_BUSY

    Note copied from docs for posterity- If the time returned in @@CPU_BUSY, or @@IO_BUSY exceeds approximately 49 days of cumulative CPU time, you receive an arithmetic overflow warning. In that case, the value of @@CPU_BUSY, @@IO_BUSY and @@IDLE variables are not accurate.