SQL Server  

How to Fix SQL Server Allocation Errors and Recover Corrupted Databases

IT admins may encounter allocation errors in SQL Server when the allocation structures (pages) in database files become inaccessible due to logical or physical corruption. You can resolve such errors by repairing the database pages with the DBCC CHECKDB command, although it is not always effective. It may delete corrupt pages if used without caution, for example, if you are using the DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS. If the native SQL recovery methods fail, then you can use a professional SQL repair tool to resolve allocation errors and integrity issues quickly. In this article, we will explain what SQL allocation errors are, how to detect them, and the most tested methods. filecite turn0file0

A Database Admin Query Related to Page Allocation Error in SQL Server

Could someone help me, please? While using SQL Server 2019, when I failed to insert data in database, I ran DBCC CHECKDB and received the following messages:

  • Server: Msg 8946, Level 16, State 12, Line 2 Table error: Allocation page (1:4197672) has invalid PFS_PAGE header values. Type is 0. Check type, object ID, and page ID on the page.

  • Server: Msg 7995, Level 16, State 1, Line 1 Database 'Database_Name' consistency errors in sysobjects, sysindexes, syscolumns, or systypes prevent further CHECKDB processing.

  • DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Considering I don’t know if a DBCC CHECKALLOC could repair this problem, could someone help me?

What are SQL Server Allocation Errors?

Allocation errors in SQL Server occur when page links in database files are corrupted, disrupting mapping of pages to objects. This process is managed by system pages - Index Allocation Map (IAM), Page Free Space (PFS), and Global Allocation Map (GAM/SGAM). These errors prevent tables, indexes, and procedures from loading, damage the database's core structure, and cause inconsistencies. If they are ignored, this can lead to query failures and downtime.

How SQL Server Manages Pages, Extents, and Allocation Maps?

SQL Server saves data in small blocks called pages (8KB each). Eight pages together make an extent (64KB). To keep track of which pages and extents are used or free, SQL Server uses special system pages called allocation maps. Such maps help the server to organize and manage data correctly and find space quickly. If these pages get corrupt, the database fails to load objects properly, leading to allocation errors and downtime.

Types of Allocation Maps in SQL Server

  • Global Allocation Map (GAM): It shows which extents are already taken. It helps in handling allocation extents in database and to identify free extents.

  • Shared Global Allocation Map (SGAM): It shows extents that are partly used and still have space. It helps in handling extents with specific file groups in database.

  • Page Free Space (PFS): It shows information about page allocation and how much free space is left inside each page.

  • Index Allocation Map (IAM): It links extents to specific tables or indexes.

Common Causes of Allocation Errors in SQL Server

Here are some common reasons of allocation errors in SQL:

  • Sudden power failure

  • Storage subsystem or disk I/O issues

  • Hardware failure

  • Virtual machine snapshot issues (if using SQL Server on VM)

  • Operating system or SQL Server crash

  • Contention in tempdb

  • Sudden server or system shutdown

How to Detect Allocation Errors in SQL Server?

You can detect SQL Server allocation errors with DBCC CHECKDB, which reports page or extent mismatches in GAM, SGAM, PFS, or IAM. Its output highlights corrupted page links or allocation inconsistencies. Error logs also record messages like logical I/O errors or failed checksums. This helps correlate corruption events with system activity.

Using DBCC CHECKDB

You can use DBCC CHECKDB to check the allocation-related problems with the database. It runs the DBCC CHECKALLOC command internally to verify the consistency of disk space allocation structures for a specific database. The command automatically checks the allocation of database pages, including incorrect page links or mismatched extent allocations. Here’s how to use it:

DBCC CHECKDB ('DatabaseName')

If the database is free of allocation errors, it shows a success message with 0 Allocation. Else, it will report error messages, like:

  • Msg 8909, Level 16, State 1: Table error: Object ID …, page ID …, incorrect page linkage.

  • Msg 8905: Extent (file:page) in database ID … is marked allocated in GAM but not in SGAM.

  • Incorrect PFS Free Space Information error

  • Server: Msg 7995, Level 16, State 1, Line 1

  • Server Error 8966 – Unable to Read and Latch Page due to Corruption

  • Error 8939

The error messages mention the map mismatch information and how pages/extents are tracked by allocation maps (GAM, SGAM, PFS, and IAM).

Checking SQL Server Error Logs

With the help of SQL Server error logs, you can detect allocation-related issues that might have affected database integrity. In logs, you can check the logical consistency-based I/O error and page-related error messages pointing to corruption in page allocation maps. You can even check system disk issues and hardware problems (if any) that might have triggered the allocation errors.

Methods to Repair SQL Server Allocation Errors

To resolve the allocation errors in MSSQL, follow the methods below:

1. Restore from a Clean Backup

You can restore the backup to restore the corrupt pages in MS SQL. First check whether your backup file is readable, then run the following command:

USE [master]
RESTORE DATABASE [EMP121] FROM DISK = N'D:\Internal\BackupfileRD.BAK'
WITH FILE = 1, MOVE N'EMP121' TO N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\EMP121.mdf',
MOVE N'bank121_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\EMP121_log.ldf',
NOUNLOAD, STATS = 5
GO

Limitations of Restoring from Backup:

  • If the transaction log backup is not available, any transaction made after the last backup can be lost.

  • If the backup file is corrupted or incomplete, it can cause restore failed errors.

  • If the size of the backup file is large, it can consume a lot of space and take hours to restore.

  • Restoring backup is a database-level recovery. It does not allow you to recover a single object, like a stored procedure or a table.

2. Use DBCC CHECKDB Repair Options

You can use the DBCC CHECKDB command to resolve corruption-related, allocation, and structural issues in the database. To use it, first you need to set your database to SINGLE_USER mode. This helps prevent other users from modifying the data during the repair process. To set the database to SINGLE_USER mode, you can use the following command:

ALTER DATABASE EMP121 SET SINGLE_USER

Note: Make sure you have ALTER permissions on the database and that the AUTO-UPDATE_STATISTIC_ASYNC option is disabled.

Once your database is in single-user mode, you can use the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS option to repair the database.

DBCC CHECKDB (N 'EMP121', REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;
GO

Once the database repair process is complete, change the state of database from SINGLE_USER to MULTI_USER. Here’s the command to do so:

ALTER DATABASE EMP121 SET MULTI_USER

Risks Associated with DBCC CHECKDB Command

Using the DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS has the following risks:

  • It may deallocate rows or pages in the database. Deallocated data can sometimes become unrecoverable.

  • It may leave your database in a logically inconsistent state.

  • It does not guarantee complete data recovery. Some of the data can be lost permanently.

When to Use DBCC CHECKDB Command?

You can use DBCC CHECKDB if:

  • Your backup file is corrupted or it is not available.

  • If backup restore repeatedly fails due to any reason.

  • If the partial recovery of the database is acceptable.

  • If you don’t have a SQL repair tool handy.

Repair Allocation Errors without Data Loss

As we know, the DBCC CHECKDB command can cause data loss. In such a situation where you want complete database recovery and your backups are unavailable, you can opt for a professional MS SQL repair tool like Stellar Repair for MS SQL. It quickly repairs severely corrupted pages in the MDF/NDF file. And you don't need to run the tool multiple times to resolve page allocation errors, unlike the native tool. This tool is recommended by MVPs to quickly resolve any level of corruption or damage in the SQL database file. The best part is, it also allows specific object recovery. Watch video of corrupted SQL Database recovery.

Best Practices to Prevent Allocation Errors

Here are some best practices you can follow to reduce and prevent allocation errors:

Regularly check the database health

Running DBCC CHECKDB weekly or regularly helps detect allocation problems promptly. It helps validate allocation structures, page checksums, and page chain integrity. This helps prevent errors from spreading and ensures long-term database health.

Proper memory configuration

In SQL Server, how you can use memory varies with the values of min server memory (MB) and max server memory (MB) in configuration settings. By setting correct values for max server memory and min server memory, SQL Server avoids exhausting system RAM. This ensures queries get enough memory while leaving space for the operating system, preventing allocation errors.

Tempdb optimization

Adding multiple equally sized tempdb files reduces contention on allocation pages (PFS, GAM, SGAM). This spreads workload across files, preventing latch waits and allocation bottlenecks that slow down queries.

Keep SQL Server patched and updated

Regular updates fix bugs and improve memory handling, reducing risks of allocation errors. Patches often enhance tempdb management, buffer pool efficiency, and lock handling, ensuring smoother resource allocation. Keeping the SQL Server updated helps prevent issues like insufficient memory or lock failures.

Conclusion

Allocation errors can disrupt SQL Server availability and workflow. You can follow the methods discussed above to resolve them. Restoring a database from a .bak file can be time-consuming with large or complex databases. Using DBCC CHECKDB with REPAIR_ALLOW_DATA_LOSS may not provide complete recovery. To recover database files with complete integrity, you can opt for any professional SQL repair tools. Since most allocation errors stem from tempdb contention or corruption in allocation structures (PFS, GAM, and SGAM), you can follow preventive measures to avoid such errors and keep the database stable and efficient.