Eliminating the PREEMPTIVE_OS_QUERYREGISTRY Wait Anomaly in SQL Server 2022
Client: Two independent enterprise customers running high-throughput OLTP workloads
Technology: MS SQL Server 2022, Query Store (enabled)
Primary Challenge: Identifying and eliminating a hidden SQL Server 2022 bottleneck caused by excessive PREEMPTIVE_OS_QUERYREGISTRY waits during high-concurrency query compilation, which threatened system throughput and worker thread availability.
1. Executive Summary
During proactive infrastructure monitoring, our database engineering team detected a critical bottleneck on the SQL Server instances of two enterprise clients. The dominant wait type across both environments was identified as a highly unusual OS-level state: PREEMPTIVE_OS_QUERYREGISTRY. This case study details how our team isolated the root cause, correlated engine telemetry, collaborated with Microsoft Support, and deployed a specific, multi-layered mitigation strategy to restore optimal system throughput and prevent worker thread starvation.
2. Problem Statement
The wait type PREEMPTIVE_OS_QUERYREGISTRY indicates that a SQL Server worker thread has temporarily yielded its non-preemptive (cooperative) SQLOS scheduler and switched to Windows preemptive mode. This context switch is required when the engine must wait for an external operating system API call to finish in this case, querying the Windows Registry.
During our deep-dive analysis using high-resolution telemetry, we identified the following patterns:
Compilation Correlation: Massive spikes in the wait time metric matched the query compilation graph (SQL Compilations/sec) down to the exact millisecond.
System Query Impact: The wait state was generated not only by user transactions but also by routine infrastructure monitoring scripts (e.g., frequently executed instances of sp_WhoIsActive).
Environment Baseline: Both affected instances shared an identical architectural footprint: SQL Server 2022 with Query Store actively tracking database performance.
3. Root Cause & Telemetry Correlation
Data exported from our monitoring platform proved a 100% causal relationship between the velocity of query compilations and total accumulated registry wait times.
When the compilation rate crossed a high-concurrency threshold, the SQL Server engine executed redundant Windows API calls to the registry. Because transitioning between SQLOS cooperative mode and Windows preemptive mode carries inherent CPU context-switching overhead, the sheer volume of these registry checks flooded the system, creating a severe bottleneck during the compilation phase of the query execution lifecycle.
4. Mitigation & Microsoft Collaboration
Given that registry access should be an asynchronous, minimal-impact background task, this behavior pointed to an internal engine anomaly. We escalated the telemetry findings and memory dumps to Microsoft Product Support.
The joint investigation confirmed a software defect within the Query Store heuristic engine introduced in SQL Server 2022. Specifically, a code path linked to the new Optimized Plan Forcing (OPF) feature incorrectly triggered synchronous, redundant registry checks during every individual query compilation event.
To bypass this defect and stabilize the engine, Microsoft advised a strict two-part reconfiguration workaround:
Step 1: Disable Optimized Plan Forcing at the Database Level This command deactivates the primary inefficient code path within the Query Store capture engine, stopping the asynchronous compilation replays that trigger the anomaly:
USE [YourDatabaseName];
GO
ALTER DATABASE SCOPED CONFIGURATION SET OPTIMIZED_PLAN_FORCING = OFF;
GO
Note: This step alone does not fully stop the operating system calls, as certain background heuristic checks continue running even when the database-scoped feature flag is disabled.
Step 2: Deploy Global Trace Flag 11074 To completely suppress the background internal checks causing the constant Windows Registry querying during compilation, we applied the undocumented Trace Flag -T11074 to the SQL Server startup parameters and recycled the database engine service.
5. Measurable Outcomes
Immediately following the configuration change and service restart, the telemetry showed:
- An instant drop in
PREEMPTIVE_OS_QUERYREGISTRYwait times to near-zero levels. - Elimination of SQLOS context-switching overhead, resulting in complete stabilization of CPU metrics during high-compilation workloads.
- Full resolution of the performance issue for both enterprise clients, all while maintaining the core Query Store telemetry collection capabilities intact.
6. Conclusion & Final Verdict
This incident highlights that deploying modern database platform standards (such as SQL Server 2022) does not inherently guarantee optimal performance without continuous, expert engineering oversight. New performance optimization features like Optimized Plan Forcing, while conceptually powerful, introduce complex background asynchronous workloads that can manifest as OS-level bottlenecks under specific high-concurrency conditions.
The definitive catalyst for resolving this issue was the utilization of our proprietary monitoring tool, SMT. Without granular wait-statistic monitoring, isolating this bottleneck within the engine’s vast codebase would have been virtually impossible. Proactive, deep-tier monitoring and the engineering expertise to interpret low-level SQLOS telemetry remain mission-critical requirements for maintaining enterprise operational continuity.
The core takeaway for modern enterprise environments remains clear: Proactive deep-tier monitoring powered by our SMT tool, combined with the engineering expertise to interpret low-level telemetry, is not an optional luxury, it is a mission-critical requirement for maintaining operational continuity across any deployed SQL Server version.
