SQL Server Transaction Log
The transaction log is the log file (.ldf) that each SQL Server database has. Before a data page is changed, the change is first written as a log record, and a commit completes after the log record reaches disk. A log record holds both the before value and the after value, so the same log serves both Redo and Undo at restart.
Key Specs
| Item | Value |
|---|---|
| File | One .ldf per database. Adding more files gives no benefit, because they are written one at a time in sequence |
| Internal unit | Virtual log file (VLF). When the file is created or grown, it is divided into several VLFs depending on size |
| Write method | After writing to the end, it wraps around to an earlier inactive VLF and overwrites it (circular) |
| Log record content | Before value and after value, LSN (log sequence number) |
| Log buffer | Up to 60KB per database. Written to disk on commit or when the buffer fills |
| Recovery models | FULL, BULK_LOGGED, SIMPLE |
| When it can be reused | SIMPLE: after a checkpoint. FULL and BULK_LOGGED: after a log backup |
| Why it cannot be reused | sys.databases.log_reuse_wait_desc |
| Accelerated database recovery | ADR (since 2019). Recovers without reaching far back through the log |
How It Works
Figure. Circular use of virtual log files
block-beta
columns 6
v1["VLF 1<br/>Inactive, reusable"] v2["VLF 2<br/>Active"] v3["VLF 3<br/>Active"] v4["VLF 4<br/>Current write position"] v5["VLF 5<br/>Unused"] v6["VLF 6<br/>Unused"]
class v1 muted
class v2,v3 data
class v4 hot
class v5,v6 free
Diagram explanation: The log is appended after the current write position, such as VLF 4. VLF 2 and VLF 3 are active because they still hold log that is needed (uncommitted transactions, changes since the checkpoint, log that has not been backed up, and so on). VLF 1, which is no longer needed, is marked inactive (log truncation), and when writing reaches the end of the file it wraps around to this spot and overwrites it. If no inactive VLF exists, the file grows, and if it cannot grow, error 9002 (log full) occurs.
Log truncation. Truncation does not shrink the file. It marks VLFs as reusable. The file size shrinks only with DBCC SHRINKFILE.
Recovery models. SIMPLE reuses the log immediately after a checkpoint passes. There are no log backups, so you can restore only to the time of the last full or differential backup. FULL makes VLFs inactive only after a log backup, and in return you can restore to any point in time. BULK_LOGGED logs bulk operations (index rebuilds, BULK INSERT, and so on) minimally.
Common causes of log growth. FULL recovery model with no log backups (LOG_BACKUP), a long-open transaction (ACTIVE_TRANSACTION), or replication or an availability group that has not yet sent the log (REPLICATION, AVAILABILITY_REPLICA).
VLF count. If the log auto-grows often in small increments, VLFs grow to thousands and startup, restore, and recovery slow down. Create the log generously from the start and set a large growth increment.
ADR. When you turn on accelerated database recovery (since 2019), old row versions are kept in the persistent version store (PVS), so rollback of a long transaction and restart recovery finish quickly regardless of log length. The log can also be truncated even when a long-open transaction exists. It is always on in Azure SQL Database.
Configuration and Checks
-- Recovery model and why the log cannot be reused
SELECT name, recovery_model_desc, log_reuse_wait_desc FROM sys.databases;
-- Log size and usage
SELECT total_log_size_in_bytes / 1048576 AS size_mb, used_log_space_in_percent
FROM sys.dm_db_log_space_usage;
-- VLF count and state (since 2016 SP2)
SELECT COUNT(*) AS vlfs, SUM(CASE WHEN vlf_active = 1 THEN 1 ELSE 0 END) AS active
FROM sys.dm_db_log_info(DB_ID());
ALTER DATABASE tune SET RECOVERY FULL;
BACKUP LOG tune TO DISK = N'/var/opt/mssql/backup/tune.trn';
ALTER DATABASE tune SET ACCELERATED_DATABASE_RECOVERY = ON;
Comparison with Other DBMSs
| SQL Server | PostgreSQL | InnoDB | Oracle | |
|---|---|---|---|---|
| Name | Transaction log | WAL | Redo log | Redo log |
| Before value | Together inside the log | None. Old row versions remain in the table | Separate Undo log | Separate Undo |
| File structure | One file cycled in VLF units | Chain of fixed-size segment files | A few fixed-size files cycled | Log groups cycled, archived |
| Retention | Log backup | WAL archive | Binary log (separate) | Archived log |
In the Articles
- 5.2 Log Records and the Log Buffer
- 5.4 Redo and Undo
- 5.6 Differences Between DBMSs and a One-Line Summary
- 6.5 Restart Recovery
NOTE The rules for dividing VLFs and the scope of ADR support can vary by version.