• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

Day 6 of 31 Days of Disaster Recovery: Dealing With Corruption in Allocation Pages

January 6, 2013 12:08 pm / 14 Comments / SQLSoldier

Day 6 of 31 Days of Disaster Recovery: Dealing With Corruption in Allocation Pages

31 Days of Disaster Recovery

31 Days of Disaster Recovery

Yesterday, I covered corruption in nonclustered indexes, the easiest type of corruption to handle. Today, I’m going to move on to something slightly more complex, yet still really simple to manage. Today, I’m going to talk about what to do when you encounter corruption of an allocation page. Allocation pages cannot be repaired not can they be single-page restored. If you have corruption of an allocation page, you need to restore the whole database.

If you missed any of the earlier posts in the series, you can check them out here:

    31 Days of disaster Recovery

  1. Does DBCC Automatically Use Existing Snapshot?
  2. Protection From Restoring a Backup of a Contained Database
  3. Determining Files to Restore Database
  4. Back That Thang Up
  5. Dealing With Corruption in a Nonclustered Index

What are Allocation Pages?

I’ve talked in great detail previously about allocation pages in my posts on dealing with tempdb contention here (part 1) and here (part 2). Allocation pages are special pages in the data files that track and manage extent allocations. There are 3 types of allocation pages that we are going to focus on today.

Global Allocation Map (GAM): Tracks which extents have been allocated. There is 1 GAM page for every 4 GB of data file. It is always page 2 in the data file and then repeats every 511,232 pages.

  • Page ID = 2 or Page ID % 511232

Shared Global Allocation Map (SGAM): Tracks which extents are being used as mixed (shared) extents. There is 1 SGAM page for every 4 GB of data file. It is always page 3 in the data file and then repeats every 511,232 pages.

  • Page ID = 3 or (Page ID – 1) % 511232

Page Free Space (PFS): Tracks the allocation status of each page and approximately how much free space it has. There is 1 PFS page for every 64 MB of data file. It is always page 1 in the data file and then repeats every 8,088 pages.

  • Page ID = 1 or Page ID % 8088

If the page ID is 1, 2, or 3, it’s obvious that it’s one of the above allocation pages. If it’s a high number, then you have a couple of ways to figure out what type of page it is. Of course, you can do the math and calculate whether it’s an allocation page or not. I’ve included the below script to do the math for you.

Declare @PageID int;

-- Enter page number
-- e.g., 8088 = PFS page
Set @PageID = 8088;

Select Case
    When @PageID = 1 Or @PageID % 8088 = 0 Then 'Is PFS Page'
    When @PageID = 2 Or @PageID % 511232 = 0 Then 'Is GAM Page'
    When @PageID = 3 Or (@PageID - 1) % 511232 = 0 Then 'Is SGAM Page'
    Else 'Is Not PFS, GAM, or SGAM page'
    End

Another way you could determine the page type is to dump the page using DBCC PAGE and look for the m_type value in the header output. The type values are different from the page IDs (m_pageid in DBCC PAGE output).

 Page Type   m_type 
 PFS   11 
 GAM   8 
 SGAM   9 

Follow the Steps

Now that we know what allocation pages are and how to identify them, we can move on to following the three general steps for handling corruption.

  1. Identify the corruption (DBCC CheckDB)
  2. Identify the objects and types of objects involved
  3. Take the appropriate steps to correct

Step 1 is to identify the corruption. For today’s exercise, I’m going to my cleverly named sample database PFSCorruption. If I run DBCC CheckDB on the database, I see that it is corrupted.

DBCC CheckDB(PFSCorruption)
    With No_InfoMsgs, All_ErrorMsgs, TableResults;

That gives us a lot of columns that we don’t need, so I’m going to pare that down to just the really important columns. This will make it easier for us to find the bits we really want. I don’t use this when troubleshooting corruption, this is just to make it easier to find the key info for this demonstration.

Declare @DBCC Table (
    Error int,
    Level smallint,
    State tinyint,
    MessageText varchar(2500),
    RepairLevel varchar(30) null,
    Status tinyint,
    DbId int,
    DbFragId int,
    ObjectId int,
    IndexId int,
    PartitionId bigint,
    AllocUnitId bigint,
    RidDbId int,
    RidPruId int,
    [File] int,
    Page int,
    Slot int,
    RefDbId int,
    RefPruId int,
    RefFile int,
    RefPage int,
    RefSlot int,
    Allocation bigint)

Insert Into @DBCC
Exec sp_executesql N'DBCC CheckDB(PFSCorruption)
    With No_InfoMsgs, All_ErrorMsgs, TableResults;';

Select Level,
    State,
    MessageText,
    RepairLevel,
    ObjectId,
    IndexId,
    [File],
    Page,
    RefFile,
    RefPage
From @DBCC;
 Level   State   MessageText   RepairLevel   ObjectId   IndexId   File   Page   RefFile   RefPage 
 16   5   Database error: Page (1:6) is marked with the wrong type in PFS page (1:1). PFS status 0xa expected 0×44.   NULL   0   -1   1   6   1   1 
 16   5   Database error: Page (1:7) is marked with the wrong type in PFS page (1:1). PFS status 0xa expected 0×44.   NULL   0   -1   1   7   1   1 
 10   1   CHECKDB found 2 allocation errors and 0 consistency errors not associated with any single object.   NULL   0   0   0   0   0   0 
 10   1   CHECKDB found 2 allocation errors and 0 consistency errors in database ‘PFSCorruption’.   NULL   0   0   0   0   0   0 

Identify the Objects

Again, we need to identify the real errors, and those are the ones that are Level 16. The higher the level, the more severe that error is so the higher level errors are the reals one we need to examine. Also, State 5 means that an unknown error occurred that terminated DBCC execution. Each of the 2 error messages mentions 2 different pages. The error messages can be misleading and make you think that pages (1, 6) and (1, 7) are corrupted, but the corruption is page (1, 1). Page (1, 1) has the wrong values for these two pages. So the columns we are interested in this time are RefFile and RefPage rather than File and Page.

We don’t need to do the math in this case. We already know that page 1 is the PFS page and in fact, the error message even called out that it is the PFS page. We know it’s an allocation page, so we know what action is appropriate.

Taking Appropriate Action

I said earlier that allocation pages cannot be repaired or restored. We need to do a full database restore. at this point, I would check my backup situation, back up the tail of the log, and then begin my restore process. If you don’t have backups to restore from, you will need to export the data, create a new version of the database, and re-import the data. It’s not going to be a fast process. There are a few people in the world that could hack the page with a hex editor to repair it manually, but that’s not something we are going to try to demonstrate here. If you try to do that, I highly advise you to make a copy of the files, and try to fix the copy of it.

Sample corrupt database and scripts: PFSCorruption.zip (116 KB)

Posted in: SQL Server / Tagged: 31 Days of Disaster Recovery, DBCC, Disaster Recovery, Internals

14 Thoughts on “Day 6 of 31 Days of Disaster Recovery: Dealing With Corruption in Allocation Pages”

  1. SQLSoldier on January 6, 2013 at 12:39 pm said:

    Thanks to Paul Randal for pointing out my math error. The PFS repeats every 64 MB, not every 1/2 GB. Corrected in this post and in the tempdb contention post.

    Reply↓
  2. ning on January 7, 2013 at 6:44 pm said:

    Hi Davis, thanks for sharing your tech, very very very nice post. please keep up the good work. I’m waitting for your next post, yeah !

    Reply↓
    • SQLSoldier on January 7, 2013 at 7:54 pm said:

      Thanks ning

      Reply↓
  3. Pingback: Day 7 of 31 Days of Disaster Recovery: Writing SLAs for Disaster Recover | SQLSoldier

  4. Pingback: Day 8 of 31 Days of Disaster Recovery (T-SQL Tuesday #38): Resolutions for All DBAs | SQLSoldier

  5. Pingback: Day 10 of 31 Days of Disaster Recovery: Monitoring for Corruption Errors | SQLSoldier

  6. Pingback: Day 6 of 31 Days of Disaster Recovery: Dealing With Corruption in … « Quick Disaster Recovery.com

  7. Pingback: Day 11 of 31 Days of Disaster: Converting LSN Formats | SQLSoldier

  8. Pingback: Day 14 of 31 Days of Disaster Recovery: Fixing a Corrupt Tempdb | SQLSoldier

  9. Pingback: Day 15 of 31 Days of Disaster Recovery: Running DBCC CheckTable in Parallel Jobs | SQLSoldier

  10. Pingback: Day 23 of 31 Days of Disaster Recovery: Restoring Differential Backups With New Files | SQLSoldier

  11. Pingback: Day 25 of 31 Days of Disaster Recovery: Improving Performance of Backups and Restores | SQLSoldier

  12. Pingback: Day 26 of 31 Days of Disaster Recovery: The Mysterious Case of the Long Backup | SQLSoldier

  13. Pingback: Day 29 of 31 Days of Disaster Recovery: Using Database Snapshots to Restore Replicated Databases in Test | SQLSoldier

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this