Interesting one today:
A few months ago, we had an issue with a database in our lab environment where the database ended up in SUSPECT mode due to storage issues. Once the storage was fixed, we were able to perform troubleshooting steps on the database. Of those, today we’ll only cover the step that was used to rebuild the transactional log file (rest of the steps will be covered in a future post).
Error Message:
An error occurred while processing the log for database 'SampleDB'.
If possible, restore from backup.
If a backup is not available, it might be necessary to rebuild the log.
NOTE: Rebuilding the transaction log is always the last option. There are other, safer options to troubleshoot when databases are in SUSPECT mode. Use this mode only after you’ve exhausted all other options like
- CHECKDB
- Restore from valid backup
- Repairing with EMERGENCY mode
NOTE: Rebuilding transactional log file will break the restore chain; So any previous transactional log files could not be applied with the backups going forward.
Rebuild Transactional Log File
Step 1: Let’s identify the transactional log file name and path. Use the following query:
--
-- Gather Logical & Physical names of the database
--
SELECT D.name AS [DatabaseName]
, M.name AS [LogicalName]
, M.physical_name AS [PhysicalName]
FROM sys.master_files AS M
INNER JOIN sys.databases AS D
ON d.database_id = m.database_id
AND D.name = 'SampleDB'
GO
Let’s use the name & path in the script below.
Step 2: Prepare the database
Before we rebuild the transactional log file, we need to set the database in EMERGENCY mode & SINGLE_USER mode. Use the script below:
--
-- Set database in EMERGENCY mode & SINGLE_USER mode
--
ALTER DATABASE SampleDB SET EMERGENCY
GO
ALTER DATABASE SampleDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
Step 3: Rebuild Transactional Log
This is an undocumented & unsupported command, so use caution before you run this in production. Steps like this must be taken only upon guidance from Microsoft CSS.
With this script, we could create a new transactional log file. After you runs it, make sure you run CHECKDB & backups.
--
-- Rebuild log file
--
ALTER DATABASE SampleDB
REBUILD LOG
ON
( NAME = 'SampleDB_log'
, FILENAME = 'L:\MSSQL\LOGS\SampleDB_log.MDF'
)
GO
Step 4: Post Rebuild
Upon successful log rebuild, lets take a few precautions to make sure everything is till good.
With this script, we could create a new transactional log file. After you runs it, make sure you run CHECKDB & backups.
--
-- Post rebuild steps
--
DBCC checkdb(SampleDB)
GO
ALTER DATABASE SampleDB SET MULTI_USER
GO
SELECT DATABASEPROPERTYEX('SampleDB', 'Status')
GO
BACKUP DATABASE SampleDB TO DISK = N'Z:\MSSQL\Backup\SampleDB_AfterTLogRebuild.BAK'
GO
Read Full Post »