Fix for SQL Server Log Space (Error : 9002) through PowerShell


Scenario:

 Error : 9002 The Transaction Log for database 'DB' is full due to 'LOG_BACKUP'...

One of the most common issue/error faced by developers working with databases i.e., SQL Server, is when LOG size reaches 100% or disk space reaches capacity. Usually this can be resolved by running SQL queries in SSMS to truncate the LOG file, in order words, reducing its size.

Let us dive into a specific situation, suppose, there is only one disk in a development environment and the disk space reaches capacity, no programs can be opened e.g. SSMS or even internet browsers. In such a case, we can either increase the disk space or truncate the LOG file using a system utility program, which require minimal temporary storage on disk to execute. 

One such program is PowerShell and by executing the following commands a quick-fix can be achieved.


Prerequisites:

1. Access to the development environment, where the database is housed.

2. Administrator access.


Process:

On the development environment, open Windows PowerShell as admin. 

To execute the PowerShell commands the hostname will be required.

 

Step 1: Fetch hostname

# To get hostname.
[System.Net.Dns]::GetHostName()
# Or use this
$env:COMPUTERNAME


Step 2: Check log space usage for database logs

From Step 1, enter the relevant hostname. 

# To check log space usage for database logs
# In this case Hostname is "TEST_HOSTNANME".
Invoke-Sqlcmd -ServerInstance "TEST_HOSTNANME" -Query "DBCC SQLPERF (LOGSPACE)"


Step 3: Check Database log details

From Step 2, enter the relevant database name. 

# To check a Database log details (Name, Location, Size).
# In this case Hostname is "TEST_HOSTNANME".
# In this case Database name is "AxDB".
Invoke-Sqlcmd -ServerInstance "TEST_HOSTNANME" -Query "USE AxDB; select * from sys.database_files;"

 

Step 4: Change the database recovery model to SIMPLE

# In order to truncate the log, we need to change the database recovery model to SIMPLE.
# In this case Hostname is "TEST_HOSTNANME".
# In this case Database name is "AxDB".
Invoke-Sqlcmd -ServerInstance "TEST_HOSTNANME" -Query "ALTER DATABASE AxDB SET RECOVERY SIMPLE;"

 

Step 5: Shrink the log file

From Step 3, enter the name of the LOG file.

# Shrink the log file to 1 MB.
# In this case Hostname is "TEST_HOSTNANME".
# In this case Database name is "AxDB" and log file name is "AxDB_UAT_log".
Invoke-Sqlcmd -ServerInstance "TEST_HOSTNANME" -Query "USE AxDB; DBCC SHRINKFILE (AxDB_UAT_log, 1)"

 

Step 6: Reset the database recovery model

# To reset the database recovery model.
# In this case Hostname is "TEST_HOSTNANME".
# In this case Database name is "AxDB".
Invoke-Sqlcmd -ServerInstance "TEST_HOSTNANME" -Query "ALTER DATABASE AxDB SET RECOVERY FULL;"

 

 

Comments

Popular posts from this blog

How to Deploy a Package from DEV to UAT using LCS

How to Deploy a Package from UAT to PROD using LCS

Power BI Dashboards Built on D365F&O AxDB (For On-Prem Environments)