Get-DbaWaitingTask
View SourceSynopsis
Retrieves detailed information about currently waiting sessions and their wait types from SQL Server dynamic management views.
Description
Queries sys.dm_os_waiting_tasks and related DMVs to identify sessions that are currently waiting, along with comprehensive diagnostic information including wait types, durations, blocking sessions, SQL text, and query plans. This function helps DBAs quickly identify performance bottlenecks, troubleshoot blocking issues, and analyze what’s causing slowdowns in real-time. The output includes helpful context like degree of parallelism for CXPACKET waits, resource descriptions, and direct links to SQLSkills wait type documentation for further analysis.
This command is based on the waiting task T-SQL script published by Paul Randal.
Reference: https://www.sqlskills.com/blogs/paul/updated-sys-dm_os_waiting_tasks-script-2/
Syntax
Get-DbaWaitingTask
[-SqlInstance] <DbaInstanceParameter[]>
[[-SqlCredential] <PSCredential>]
[[-Spid] <Object[]>]
[-IncludeSystemSpid]
[-EnableException]
[<CommonParameters>]
Examples
Example: 1
PS C:\> Get-DbaWaitingTask -SqlInstance sqlserver2014a
Returns the waiting task for all sessions on sqlserver2014a
Example: 2
PS C:\> Get-DbaWaitingTask -SqlInstance sqlserver2014a -IncludeSystemSpid
Returns the waiting task for all sessions (user and system) on sqlserver2014a
Required Parameters
-SqlInstance
The target SQL Server instance or instances. Server version must be SQL Server version XXXX or higher.
| Property | Value |
|---|---|
| Alias | |
| Required | True |
| Pipeline | true (ByValue) |
| Default Value |
Optional Parameters
-SqlCredential
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-Spid
Filters results to show waiting tasks for specific session IDs only. Accepts one or more SPIDs as an array.
Use this when troubleshooting known problematic sessions or when you want to focus on specific user connections instead of scanning all active sessions.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | true (ByPropertyName) |
| Default Value |
-IncludeSystemSpid
Includes system sessions (SPIDs) in the results along with user sessions. By default, only user sessions are returned.
Enable this when diagnosing system-level performance issues or when system processes might be causing blocking or resource contention.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
-EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
Outputs
PSCustomObject
Returns one object per waiting session found on the SQL Server instance, with the following default display properties (via Select-DefaultView):
- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance)
- Spid: The session ID (SPID) of the waiting session
- Thread: The execution context ID (thread number within the session)
- Scheduler: The scheduler ID managing this task
- WaitMs: The duration of the wait in milliseconds
- WaitType: The type of wait (e.g., CXPACKET, LCK_M_IX, PAGEIOLATCH_SH, etc.)
- BlockingSpid: The session ID (SPID) blocking this session, or 0 if no blocking
*Additional properties available with Select-Object :
- ResourceDesc: Detailed resource description from the wait (e.g., database:file:page IDs for page waits)
- NodeId: For CXPACKET waits, the parallel exchange node ID from ResourceDesc
- Dop: Degree of Parallelism for parallel execution waits; null for serial execution
- DbId: Database ID where the wait is occurring
- SqlText: The SQL text being executed in the waiting session (excluded from default display)
- QueryPlan: The query execution plan as XML (excluded from default display)
- InfoUrl: URL to SQLSkills wait type documentation for this specific wait type (excluded from default display) When -Spid is specified, only waiting tasks for those session IDs are returned. When -IncludeSystemSpid is specified, system sessions are included in results along with user sessions.
dbatools