How to Fix SQL Database Stuck in Recovery Mode
Learn how to resolve an SQL database stuck in recovery mode. Discover phases of SQL database recovery, reasons that may cause the issue, and ways to bring it back to a consistent state.
Join the DZone community and get the full member experience.
Join For FreeSQL Server databases occasionally enter "In Recovery" mode, which can often catch database administrators off guard. This status occurs during a restart, database restore, or unexpected shutdown, as SQL Server replays or undoes incomplete transactions to maintain data integrity. While this process is typically automatic, it can sometimes take longer than expected — or even appear stuck — leaving administrators unsure of how to proceed.
If you’ve encountered this issue, don’t worry. This article will help you understand what’s happening behind the scenes and teach you how to respond. Here's a quick look at what you'll learn:
- What "In Recovery" Mode Means — Why your database enters this state and what SQL Server is doing in the background.
- The 3 Phases of Recovery — A clear breakdown of the Analysis, Redo, and Undo phases that SQL Server follows during recovery.
- Common Causes of Delays — From large transaction logs to excessive virtual log files (VLFs), see what might be slowing down the process.
- How to Get Back Online — Learn practical steps to restore your database to a consistent state, from waiting it out to using SQL repair tools.
- When to Seek Advanced Help — What to do if the recovery process seems stuck and no progress is being made.
By the end of this guide, you'll have a solid understanding of SQL Server’s recovery process and the tools you can use to get your database back online as quickly as possible.
Understanding "In Recovery" Mode in SQL Server
When SQL Server restarts or a database is restored from backup, it enters 'In Recovery' mode to maintain data integrity. During this phase, SQL Server replays or undoes incomplete transactions to prevent data corruption and ensure transactional consistency.
After restarting SQL Server, the database moves to “In Recovery” mode. You may also see the SQL Server database in recovery state on its startup or when restoring it from backup.
Figure 1- SQL Database "In Recovery" Mode
The database "recovering" state means that the database performs a recovery process and will automatically come online once the process is complete. However, you may experience that the recovery is slow, and the database is stuck in a recovery state. Your DB might still be in recovery state, as SQL databases undergo three phases of recovery, which can take time depending on your database files' size.
The 3 Phases of SQL Database Recovery
Usually, when the database is not shut down properly on SQL Server restart, it undergoes crash recovery, ensuring that the DB remains consistent. There are three phases of recovery that an SQL DB needs to go through:
Phase 1: Analysis
This phase starts from the "last checkpoint till the end of the transaction log." It creates a 'Dirty Page Table' (DPT) table that helps determine all the dirty pages at the time of the crash. Also, it creates an 'Active Transaction Table' (ATT) table to identify uncommitted transactions when the SQL Server stopped.
Phase 2: Redo
In this phase, SQL Server rolls forward all the changes that happened after the checkpoint and before the crash. Essentially, in the redo phase, all the transactions which are committed but not yet written to the SQL data file (.mdf/.ldf) via checkpoint need to be rolled forward.
Phase 3: Undo
If there were any uncommitted transactions at the time of database recovery, they have to be rolled back in the undo phase to bring the DB to a consistent state.
What to Do if Your Database Is Stuck in Recovery Mode?
Check the SQL Server Error Log to see the very first message in the database that may look similar to:
Starting up database ‘DatabaseName’
This means that the DB files are opened, and the recovery process has started. Sometime later, you should see SQL Server undergoing 3 phases of recovery. If you're looking for guidance on how to back up and restore your database, check out this guide on backing up and restoring Azure SQL Databases.
Phase 1 of database recovery is shown below:
Recovery of database ‘DatabaseName’ (9) is 0% complete (approximately 95 seconds remain). Phase 1 of 3. This is an informational message only. No user action is required.
Recovery of database ‘DatabaseName’ (9) is 3% complete (approximately 90 seconds remain). Phase 1 of 3. This is an informational message only. No user action is required.
After completion of phase 1, SQL Server will undergo phase 2 and 3 of recovery:
Recovery of database ‘DatabaseName’ (9) is 5% complete (approximately 85 seconds remain). Phase 2 of 3. This is an informational message only. No user action is required…
Recovery of database ‘DatabaseName’ (9) is 95% complete (approximately 40 seconds remain). Phase 2 of 3. This is an informational message only. No user action is required.
Phase 3 of 3. This is an informational message only. No user action is required.
Once Phase 2 and 3 is complete, you will see something similar to:
3807 transactions rolled forward in database ‘DatabaseName’ (9). This is an informational message only. No user action is required.
0 transactions rolled back in database ‘DatabaseName’ (9). This is an informational message only. No user action is required.
Recovery is writing a checkpoint in database ‘DatabaseName’ (9). This is an informational message only. No user action is required.
Recovery completed for database DatabaseName (database ID 9) in 30 second(s) (analysis 1289 ms, redo 29343 ms, undo 72 ms.) This is an informational message only. No user action is required.
In the error log, pay attention to the message ‘no user action is required’. This indicates that the database is in recovery state. However, the recovery may take longer than expected, and the database will be stuck in recovery mode.
Reasons Behind SQL Database Stuck in “In Recovery” Mode
Following are the reasons that may cause an SQL database stuck in recovery mode:
- A long-running transaction is rolling back
- Transaction log file size is huge
- Too many Virtual Log Files (VLFs) are inside the DB transaction log
- There was a bug in the SQL Server, which is now fixed.
What Can You Do to Bring Database Back to a Consistent State?
Workaround 1: Wait for the Database Recovery to Complete
The most obvious solution to bring the database back online is to be patient and wait for the recovery process to complete; this could take hours or days. If the recovery is taking too long than expected for a DB in an SQL Server 2008 or 2008 R2, applying Microsoft fixes may help.
Note: Avoid running the RESTORE command to bring DB online in a consistent state, as SQL Server is already attempting to perform the same task. And, running ‘RESTORE with..Recovery’ means putting the DB to go through the same steps again.
Workaround 2: Use a Professional SQL Database Repair Tool
If the recovery gets completed but fails to bring the database in a consistent state, using a specialized SQL repair tool may help restore the DB to its original state.
- Stellar Repair for MS SQL — A specialized tool that helps restore SQL databases to their original state after corruption or failure.
- ApexSQL Recover — This tool helps recover deleted, truncated, or corrupted SQL Server database data.
- dbForge SQL Complete — While primarily an IDE extension, it offers useful error handling and troubleshooting features.
- Redgate SQL Data Recovery — Redgate provides a range of SQL Server tools, including data recovery features.
- SysTools SQL Recovery Tool — Known for recovering corrupted or damaged SQL database files (.MDF and .NDF) and bringing them back to a usable state.
- Kernel for SQL Database Recovery — This tool can recover and restore SQL Server databases from both corruption and unexpected shutdowns.
- Aryson SQL Database Recovery — Another tool that can repair and restore MDF and NDF files in SQL Server.
Conclusion
In this article, we covered what it means when a SQL database is stuck in 'In Recovery' mode, the three critical phases of recovery (Analysis, Redo, and Undo), and what you can do to bring the database back online. We also discussed possible causes, from large transaction logs to too many virtual log files (VLFs).
If your SQL database is still stuck in recovery mode, remember that patience is key. Avoid running a RESTORE command, as this restarts the recovery process. For severe issues where manual intervention fails, consider using a professional SQL database repair tool to restore your data.
If you found this guide helpful, share it with your team or leave a comment with your SQL recovery experiences.
Opinions expressed by DZone contributors are their own.
Comments