Shrinking a Log Without Breaking Log Shipping

Windows Services window with the SQL Server Agent service selected
2 min read

Do not switch the primary database to SIMPLE recovery to empty the log. The primary must use the full or bulk-logged recovery model, and switching to simple recovery stops log shipping. Find what blocks truncation first. Let the log backup job run. Then shrink the file, which only helps if the log has unused space.

On this page
  1. Symptom
  2. Cause
  3. Fix
  4. Check the result
  5. When it does not apply

Symptom

The log file on the primary is large. Setting SIMPLE recovery to empty it is tempting, but on a log-shipped database that switch stops log shipping.

Cause

Log shipping is built on log backups. It consists of three operations, and the first is the transaction log backup on the primary server instance. The docs list full or bulk-logged as the required recovery model for the primary.

Diagram of a master database sending its logs to a second database that applies them

A shrink is also limited. A shrink only makes a difference when a transaction log file contains unused space. No part of the active log can ever be truncated. If the log stays full, open transactions are the usual reason, and the docs say to investigate what is preventing truncation before shrinking.

Fix

The steps below are working practice, not quotations from the docs, except where a link is given.

Screenshot of SQL Server Management Studio 17.4

Take a full backup first. Then find the truncation blocker.

-- Recovery model must stay FULL or BULK_LOGGED
SELECT name, recovery_model_desc, log_reuse_wait_desc
FROM sys.databases
WHERE name = N'YourDb';

-- Oldest active transaction in the database
DBCC OPENTRAN (N'YourDb');
  1. Read log_reuse_wait_desc. A value of ACTIVE_TRANSACTION points to an open transaction; DBCC OPENTRAN shows the oldest one. A value of LOG_BACKUP means the log is waiting for a log backup, not for a transaction.
  2. For an open transaction, commit it, roll it back or let it finish. Check what it is before killing it.
  3. For LOG_BACKUP, let the log shipping backup job run on the primary so the log backup happens as normal.
  4. Shrink only after the log has unused space.
USE YourDb;
-- Target size in MB; pick a size that fits normal activity
DBCC SHRINKFILE (N'YourDb_log', 1024);

A shrink returns free space to the operating system by reducing the physical size of the log.

Check the result

SELECT name, recovery_model_desc, log_reuse_wait_desc
FROM sys.databases
WHERE name = N'YourDb';

SELECT name, size * 8 / 1024 AS size_mb
FROM sys.database_files
WHERE type_desc = N'LOG';
  • recovery_model_desc still reads FULL or BULK_LOGGED.
  • size_mb is lower than before.
  • The log shipping backup, copy and restore jobs still succeed.

When it does not apply

  • The shrink changes nothing if the log has no unused space. The active part stays, so the blocker must be cleared first.
  • A log that grows again right after the shrink needs a sizing decision, not another shrink.
  • The database is not log-shipped and SIMPLE recovery is already the intended model. Then this note does not apply.

Docs: About log shipping · Configure log shipping · Manage the size of the transaction log file · Transaction log architecture

SQL blogcasts

SET_NOCOUNT_ON

Technical notes on SQL Server for DBAs and developers: why a query or job behaves as it does and how to fix it, with T-SQL that runs and the version it was tested on.

More notes

Go up