Sunday, 9 October 2011

Database Transaction Log Cannot be Shrunk Because it is Marked as “REPLICATION” (and Replication has not been Configured)



Finding the reason for not being able to truncate the transaction log requires using the DMV sys.databases and looking for the information under the log_reuse_wait_desc column. Most of the times the description on this column will be LOG_BACKUP, indicating that a transaction log backup operation is needed in order to truncate the transaction log file. In our case the result from this query was REPLICATION (I am using the AdventureWorks sample database here to illustrate this):
USE master;
GO
SELECT name, log_reuse_wait_desc, * FROM sys.databases
WHERE name = 'AdventureWorks';

Trying to restore the status of the transaction log file using the SQL Server Replication stored proceduresp_repldone will not fix the issue as the stored procedure will complain the database is not configured for replication. The fastest and easiest way to get rid of the REPLICATION mark in the transaction log file is to configure Snapshot replication for the database and then, afterwards, removing this configuration from the server.

Select the affected database as the database for Replication
You do not need to select all the objects for the Replication process, just select any table (one) on the database
Remember to create an initial Snapshot when asked to do so during the wizard
You can safely use the SQL Server Agent service account in the Security settings as this Replication will only be used temporarily
Once the Snapshot Replication Wizard completes the replication scenario successfully, run again the previous SELECT in sys.database; at this point the ‘log_reuse_wait_desc’ column should show either NOTHING or LOG_BACKUP:



If the status is still REPLICATION, execute the following instruction to force all pending transactions to be distributed:

EXEC sp_repldone @xactid = NULL, @xact_sgno = NULL, @numtrans = 0, @time = 0, @reset = 1;

Then we need to remove all replication objects from the database as we do not longer need any Replication for this database. First we drop the Publication (we do not need to drop subscribers firsts as we did not configured any):
USE AdventureWorks;
GO
EXEC sp_droppublication @publication = N'AW_Test_Publication'
USE master
GO
EXEC sp_replicationdboption @dbname = N'AdventureWorks', @optname = N'publish', @value = N'false';
Note that in this example our Publication name is “AW_Test_Publication”. In your case this name will vary depending on what you have specified during the Snapshot Replication Wizard.
Then we need to drop the Distributor:
USE master;
GO
exec sp_dropdistributor @no_checks = 1;
And make sure, finally, no replication objects remain on the database by running the following stored procedure:
USE master;
GO
sp_removedbreplication 'AdventureWorks';

No comments:

Post a Comment