The Transaction Log (SQL Server) - Microsoft Docs
The Transaction Log (SQL Server) - Microsoft Docs
Product
All SQL
Version
Hide nothing
Analytics Platform System (PDW)
Version
2016-AU7
2016-AU6
SQL Data Warehouse
Version
latest
SQL Database Logical Server
Version
current
SQL Database Managed Instance
Version
current
SQL Server
Version
vNext
2017
2016
2014
SQL Server on Linux
Version
vNext
2017
In this article
Operations supported by the transaction log
Transaction Log characteristics
Transaction log truncation
Factors that can delay log truncation
Operations that can be minimally logged
Related tasks
See also
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 1/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
THIS TOPIC APPLIES TO: SQL Server Azure SQL Database Azure SQL Data Warehouse
Parallel Data Warehouse
Every SQL Server database has a transaction log that records all transactions and the database
modifications made by each transaction.
The transaction log is a critical component of the database. If there is a system failure, you will need
that log to bring your database back to a consistent state.
For information about the transaction log architecture and internals, see the SQL Server Transaction
Log Architecture and Management Guide.
U Warning
Never delete or move this log unless you fully understand the ramifications of doing so.
Tip
Known good points from which to begin applying transaction logs during database recovery are
created by checkpoints. For more information, see Database Checkpoints (SQL Server).
If an application issues a ROLLBACK statement, or if the Database Engine detects an error such as the
loss of communication with a client, the log records are used to roll back the modifications made by
an incomplete transaction.
If a server fails, the databases may be left in a state where some modifications were never written
from the buffer cache to the data files, and there may be some modifications from incomplete
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 2/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
transactions in the data files. When an instance of SQL Server is started, it runs a recovery of each
database. Every modification recorded in the log which may not have been written to the data files is
rolled forward. Every incomplete transaction found in the transaction log is then rolled back to make
sure the integrity of the database is preserved.
Rolling a restored database, file, filegroup, or page forward to the point of failure
After a hardware loss or disk failure affecting the database files, you can restore the database to the
point of failure. You first restore the last full database backup and the last differential database
backup, and then restore the subsequent sequence of the transaction log backups to the point of
failure.
As you restore each log backup, the Database Engine reapplies all the modifications recorded in the
log to roll forward all the transactions. When the last log backup is restored, the Database Engine
then uses the log information to roll back all transactions that were not complete at that point.
The Log Reader Agent monitors the transaction log of each database configured for transactional
replication and copies the transactions marked for replication from the transaction log into the
distribution database. For more information, see How Transactional Replication Works.
The standby-server solutions, Always On availability groups, database mirroring, and log shipping,
rely heavily on the transaction log.
In an Always On availability groups scenario, every update to a database, the primary replica, is
immediately reproduced in separate, full copies of the database, the secondary replicas. The primary
replica sends each log record immediately to the secondary replicas which applies the incoming log
records to availability group databases, continually rolling it forward. For more information, see
Always On Failover Cluster Instances
In a log shipping scenario, the primary server sends the active transaction log of the primary
database to one or more destinations. Each secondary server restores the log to its local secondary
database. For more information, see About Log Shipping.
In a database mirroring scenario, every update to a database, the principal database, is immediately
reproduced in a separate, full copy of the database, the mirror database. The principal server instance
sends each log record immediately to the mirror server instance which applies the incoming log
records to the mirror database, continually rolling it forward. For more information, see Database
Mirroring.
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 3/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
The transaction log is implemented as a separate file or set of files in the database. The log
cache is managed separately from the buffer cache for data pages, which results in simple, fast,
and robust code within the SQL Server Database Engine. For more information, see Transaction
Log Physical Architecture.
The format of log records and pages is not constrained to follow the format of data pages.
The transaction log can be implemented in several files. The files can be defined to expand
automatically by setting the FILEGROWTH value for the log. This reduces the potential of
running out of space in the transaction log, while at the same time reducing administrative
overhead. For more information, see ALTER DATABASE (Transact-SQL) File and Filegroup
Options.
The mechanism to reuse the space within the log files is quick and has minimal effect on
transaction throughput.
For information about the transaction log architecture and internals, see the SQL Server Transaction
Log Architecture and Management Guide.
Log truncation deletes inactive virtual log files (VLFs) from the logical transaction log of a SQL Server
database, freeing space in the logical log for reuse by the Physical transaction log. If a transaction log
is never truncated, it will eventually fill all the disk space allocated to physical log files.
To avoid running out of space, unless log truncation is delayed for some reason, truncation occurs
automatically after the following events:
Under the full recovery model or bulk-logged recovery model, if a checkpoint has occurred
since the previous backup, truncation occurs after a log backup (unless it is a copy-only log
backup).
For more information, see Factors that can delay log truncation, later in this topic.
7 Note
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 4/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
Log truncation does not reduce the size of the physical log file. To reduce the physical size of a
physical log file, you must shrink the log file. For information about shrinking the size of the
physical log file, see Manage the Size of the Transaction Log File.
However, keep in mind Factors that can delay log truncation. If the storage space is required
again after a log shrink, the transaction log will grow again and by doing that, introduce
performance overhead during log grow operations.
) Important
For information about how to respond to a full transaction log, see Troubleshoot a Full
Transaction Log (SQL Server Error 9002).
Really, Log truncation can be delayed by a variety of reasons. Learn what, if anything, is preventing
your log truncation by querying the log_reuse_wait and log_reuse_wait_desc columns of the
sys.databases catalog view. The following table describes the values of these columns.
log_reuse_wait
value log_reuse_wait_desc value Description
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 5/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
log_reuse_wait
value log_reuse_wait_desc value Description
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 6/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
log_reuse_wait
value log_reuse_wait_desc value Description
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 7/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
7 Note
7 Note
Under the full recovery model, all bulk operations are fully logged. However, you can minimize
logging for a set of bulk operations by switching the database to the bulk-logged recovery
model temporarily for bulk operations. Minimal logging is more efficient than full logging, and it
reduces the possibility of a large-scale bulk operation filling the available transaction log space
during a bulk transaction. However, if the database is damaged or lost when minimal logging is
in effect, you cannot recover the database to the point of failure.
The following operations, which are fully logged under the full recovery model, are minimally logged
under the simple and bulk-logged recovery model:
Bulk import operations (bcp, BULK INSERT, and INSERT... SELECT). For more information about
when bulk import into a table is minimally logged, see Prerequisites for Minimal Logging in Bulk
Import.
When transactional replication is enabled, BULK INSERT operations are fully logged even under the
Bulk Logged recovery model.
When transactional replication is enabled, SELECT INTO operations are fully logged even under the
Bulk Logged recovery model.
Partial updates to large value data types, using the .WRITE clause in the UPDATE statement
when inserting or appending new data. Note that minimal logging is not used when existing
values are updated. For more information about large value data types, see Data Types
(Transact-SQL).
WRITETEXT and UPDATETEXT statements when inserting or appending new data into the text,
ntext, and image data type columns. Note that minimal logging is not used when existing
values are updated.
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 8/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
U Warning
The WRITETEXT and UPDATETEXT statements are deprecated; avoid using them in new
applications.
If the database is set to the simple or bulk-logged recovery model, some index DDL operations
are minimally logged whether the operation is executed offline or online. The minimally logged
index operations are as follows:
U Warning
DROP INDEX new heap rebuild (if applicable). Index page deallocation during a DROP INDEX
operation is always fully logged.
Related tasks
Managing the transaction log
See also
SQL Server Transaction Log Architecture and Management Guide
Control Transaction Durability
Prerequisites for Minimal Logging in Bulk Import
Back Up and Restore of SQL Server Databases
Database Checkpoints (SQL Server)
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 9/10
7/25/2018 The Transaction Log (SQL Server) | Microsoft Docs
7 Note
The feedback system for this content will be changing soon. Old comments will not be carried
over. If content within a comment thread is important to you, please save a copy. For more
information on the upcoming change, we invite you to read our blog post.
https://docs.microsoft.com/en-us/sql/relational-databases/logs/the-transaction-log-sql-server?view=sql-server-2017#FactorsThatDelayTruncation 10/10