Or “Remind me again why we’re doing these backups in the first place”
If there’s one topic that comes up again and again on the forums, it has to be the problem of full transaction logs, usually followed by the discovery that the DB is in full recovery mode and no log backups are running.
Quite often, someone will suggest to just truncate the log and shrink it. It’s a dangerous suggestion, not so much for what is said, but for what is not said. To understand why, requires a little background information. First, a look at recovery models and how they affect the transaction log.
I’m going to ignore bulk-logged mode for now, mainly it’s less used than the other two and I’m not completely comfortable with how it works. I’m also going to ignore replication and database mirroring, as they complicate the issue.
Regardless of the recovery model that the database is in, transactions are logged into the transaction log before they are considered complete. The entries in the transaction log are considered active until all the data pages that were modified by the transaction have been written to disk. The two processes that write pages to disk are the lazy writer and the checkpoint process. Once all the pages that the transaction have been written to disk, the log records for that transaction are marked as inactive.
Simple recovery mode
In simple recovery mode, once a checkpoint completes, inactive log records are discarded. Hence no record is kept of transactions that occurred before the last checkpoint
Full recovery mode
In full recovery mode, log records are retained within the transaction log until a log backup occurs. When a log backup occurs, SQL checks when the last log backup occurred, and writes out log entries since that time into the log backup. Then it updates the last log backup entry and discards the transaction log entries that were included in the backup. Hence, provided the log backup files are kept, there is a complete records of all transactions that occurred.
That’s a massive simplification, but it should be enough for now. Second bit of background information is on the types of backups, what they do to the transaction log and the implications they have for recovery
The full database backup copies the entire contents of the database to the backup file. The full backup also includes enough of the transaction log to allow for the backup to be restored into a transactionally consistent state. It does not truncate the transaction log.
Transaction Log Backup
The transaction log backup does not contain contents of the database, but rather just the transaction log. When the log backup runs, it backups all entries in the transaction log that were not backed up by the previous log backup. Once completed, the inactive log records are discarded, freeing up space in the log.
Transaction log backups form a chain, starting with the a full or differential backup and continuing until the most recent log backup. Because full backups do not truncate the transaction log, they do not break the log chain. An unbroken log chain is essential to restoring to a point in time.
So with all that out of the way, I can finally come to my point.
If a database is in full recovery mode and transaction log backups are being done, the reason for all those backups is to allow the database to be recovered to a point in time in the case of a failure. Truncating the transaction log and discarding log records is contrary to that goal.
If a database is in full recovery mode just because that’s the default, no log backups are being done and point in time recovery is not required, then truncating the transaction log is just an interim fix and the log will get full again sometime in the future. In that situation, rather set the database into simple recovery mode where the log will truncate itself.
If a database is in full recovery mode because that’s the default, no log backups are being done and point in time recovery is required, then set up the appropriate log backups so that point in time recovery is possible.
Truncating the transaction log should be a last resort, not the first thing suggested.