Troubleshooting Guide: SQL Server Error 5299 (Query Store Error)
Why This Error Happens
Error 5299 indicates that the Query Store feature has encountered an internal error while attempting to capture, persist, or flush query execution statistics to disk. This typically occurs due to corruption within the Query Store internal tables (located in the user database), insufficient disk space on the volume hosting the database files, or the database reaching its configured Query Store size quota while in a state that prevents cleanup. In rarer cases, this error surfaces when the underlying storage experiences I/O latency spikes or when Query Store metadata becomes inconsistent after an unexpected shutdown or forced failover.
Step-by-Step Resolution Guide
Step 1: Check Query Store Operational State and Disk Space
- Open SQL Server Management Studio (SSMS) and connect to the affected instance.
- Run the following query to check the current state:
SELECT actual_state_desc, readonly_reason, current_storage_size_mb, max_storage_size_mb FROM sys.database_query_store_options; - Verify the volume hosting your
.mdf/.ldffiles (commonlyC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\) has at least 20% free space. - If
readonly_reasonshows a non-zero value (e.g., 65536 indicating size cap reached), proceed to Step 2.
Step 2: Clear or Resize Query Store Data
- Execute
ALTER DATABASE [YourDatabaseName] SET QUERY_STORE CLEAR;to purge existing captured data if corruption or quota is suspected. - If clearing doesn’t resolve it, temporarily disable Query Store using
ALTER DATABASE [YourDatabaseName] SET QUERY_STORE = OFF; - Increase the
MAX_STORAGE_SIZE_MBparameter before re-enabling:ALTER DATABASE [YourDatabaseName] SET QUERY_STORE (MAX_STORAGE_SIZE_MB = 2048); - Re-enable Query Store:
ALTER DATABASE [YourDatabaseName] SET QUERY_STORE = ON;
Step 3: Run Consistency Checks on the Database
- Execute
DBCC CHECKDB ('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS;to rule out physical corruption affecting the internal Query Store tables. - Review the SQL Server Error Log (via SSMS > Management > SQL Server Logs, or physically at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOG) for accompanying stack dumps or I/O warnings referencing the same timestamp as Error 5299. - If corruption is confirmed specifically in Query Store internal structures, you may need to drop and recreate Query Store entirely via the CLEAR command in Step 2, as these internal tables are not user-accessible for direct repair.
Step 4: Restart the SQL Server Service (If State Persists)
- Open Services.msc on Windows Server 2025 / Windows 11.
- Locate SQL Server (MSSQLSERVER) or your named instance.
- Right-click and select Restart to force Query Store to re-initialize its in-memory state and flush pending writes cleanly.
Alternative or Verification Steps
- Verify the fix by running
SELECT actual_state_desc FROM sys.database_query_store_options;and confirming the state reads READWRITE rather than READONLY or ERROR. - If the error persists after all steps, consider migrating Query Store to QUERYCAPTUREMODE = CUSTOM with a lower capture threshold to reduce write volume, or temporarily move the database to SIMPLE recovery mode to rule out transaction log pressure as a contributing factor.