SQL Server Storage Engine

Ta da! Emergency mode repair

Emergency mode repair? In a couple of previous posts I explained how the two worst things you could do to your database are rebuilding your transaction log and running REPAIR_ALLOW_DATA_LOSS. Well, in SQL Server 2005, we combined them into a new documented feature!

Are you off your head, I hear you ask? No - and here's why.

(Maybe some other people are asking what 'off your head' means. It's a Scottish thing, as in 'Yer off yer head ya eejit, that's no a real dug'. Translation for all those who watched Trainspotting with subtitles or couldn't understand Brad Pitt's character in Snatch - 'Surely you are mistaken my friend, that's clearly not a real dog'). But I digress...

As you may know, if you don't have a backup strategy then its possible for some hardware problem to corrupt your transaction log so the database in unusable. In this case, you'd be forced to call up Product Support and have them walk you through rebuilding the log and running repair, possibly losing lots of precious business data in the process.

We created emergency mode repair as a documented and supported way of doing all this in a single operation, without the need for calling Product Support (saving you hassle and saving us hassle) and ensuring that anyone who does have to do this runs all the correct steps (saving you hassle and saving us even more hassle - you get the idea)

So what does it do? Once you've switched the database with the corrupt transaction log into emergency mode then the only repair option available is REPAIR_ALLOW_DATA_LOSS. In emergency mode, this does some special operations that it doesn't usually do:

  1. force transaction recovery to run, skipping all errors (kind of similar to the CONTINUE_AFTER_ERROR option that we added to RESTORE for SQL Server 2005). This operation scavenges as much out of the transaction log as it can.
  2. if any errors were seen in step 1, the transaction log is rebuilt (with the same gut-wrenching tearing noise I alluded to in a previous post - imaging the noise of a boot being pulled out of wet mud)
  3. run REPAIR_ALLOW_DATA_LOSS to ensure the database is returned to a structurally and transactionally consistent state.

And if everything goes well then your database should be available again. Now, you'll most likely have lost a bunch of data, broken constraints and inherent business logic in the database but at least you haven't lost everything. Now would be the time to do root cause analysis to figure out what happened and you've just had a painful lesson on why you should have a comprehensive backup strategy ('Yer off yer head ya eejit, that's no a real backup strategy...')

Here are a few things to bear in mind about emergency mode repair:

  • it's fully documented and supported. This isn't some dodgy advice off the internet from Random User, its right there in Books Online.
  • it's a one-way operation. Anything it does cannot be rolled back or undone. If this worries you (which would surprise me because if someone ever got into that state then surely they don't have the healthy concern about their data that they should have in the first place) then make a copy of the damaged database before you run emergency mode repair.
  • as its a one-way operation, you cannot wrap it in an explicit user-transaction.
  • it's the only repair option available in emergency mode - if you try to use REPAIR_REBUILD then it won't work.

What if it doesn't work? I've never seen it fail, but it really is the last resort - documented or not - so if it doesn't work or your lack of backups causes you to lose the data from your CEO's favorite customer, start working on your resume...

Published Sunday, June 18, 2006 4:00 PM by Paul Randal - MSFT
Filed under: ,
Anonymous comments are disabled

About Paul Randal - MSFT

Paul started in the industry in 1994 working for DEC on the VMS file system and check/repair tools. In 1999 he moved to Microsoft to work on SQL Server, specifically on DBCC. For SQL Server 2000, he concentrated on index fragmentation (writing DBCC INDEXDEFRAG and DBCC SHOWCONTIG) plus various algorithms in DBCC CHECKDB. During SQL Server 2005 development Paul was the lead developer/manager of one the core dev teams in the Storage Engine, responsible for data access and storage (DBCC, allocation, indexes & heaps, pages/records, text/LOB storage, snapshot isolation, etc). He also spent several years rewriting DBCC CHECKDB and repair. For SQL Server 2008, Paul managed the Program Management team for the core Storage Engine to become more focused on customer/partner engagement and feature set definition. In 2007, after 8.5 years on the SQL Server team, Paul left Microsoft to join his wife, Kimberly Tripp, running SQLskills.com and pursuing his passion for presenting and consulting. Paul regularly presents at conferences and user groups around the world on high-availability, disaster recovery and Storage Engine internals. His popular blog is at http://www.sqlskills.com/blogs/paul/.

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker