SUMMARY
In SQL Server 7.0, in SQL Server 2000, and in SQL Server 2005, with the autogrow
setting, transaction log files can expand automatically.
Typically,
the size of the transaction log file stabilizes when it can hold the maximum
number of transactions that can occur between transaction log truncations that
either checkpoints or transaction log backups trigger.
However, in
some situations the transaction log may become very large and run out of space
or become full. Typically, you receive the following error message when a
transaction log file takes up the available disk space and cannot expand any
more:
Error: 9002, Severity: 17, State: 2
The log
file for database '%.*ls' is full.
If you are using SQL Server 2005, you receive an error message that is similar to the following:
Error: 9002, Severity: 17, State: 2
The transaction log for database '%.*ls' is full.
To find out why space in the log cannot be reused,
see the log_reuse_wait_desc column in sys.databases
In addition to this error
message, SQL Server may mark databases suspect because of a lack of space for
transaction log expansion. For additional information about how to recover from
this situation, see the "Insufficient Disk Space" topic in SQL Server Books
Online.
Additionally, transaction log expansion may result in the
following situations:
- A very large transaction log file.
- Transactions may fail and may start to roll back.
- Transactions may take a long time to complete.
- Performance issues may occur.
- Blocking may occur.
Causes
Transaction log expansion may occur because of the following
reasons or scenarios:
Uncommitted Transactions
Explicit transactions remain uncommitted if you do not issue an
explicit COMMIT or ROLLBACK command. This most frequently occurs when an
application issues a CANCEL or a Transact SQL KILL command without a
corresponding ROLLBACK command. The transaction cancellation occurs, but it
does not roll back; therefore, SQL Server cannot truncate every transaction
that occurs after this because the aborted transaction is still open. You can
use the DBCC OPENTRAN Transact-SQL reference to verify if there is an active
transaction in a database at a particular time.
For additional
information about this particular scenario, click the article number below to
view the article in the Microsoft Knowledge Base:
295108 INF: Incomplete Transaction May Hold Large Number of Locks and Cause Blocking
171224 INF: Understanding How the Transact-SQL KILL Command Works
Additionally, see the "DBCC OPENTRAN" topic in SQL
Server Books Online.
Scenarios that may result in uncommitted transactions:
- An application design that assumes that all errors cause
rollbacks.
- An application design that does not completely take into
account SQL Server behavior when it rolls back to named transactions or
specially-nested named transactions. If you try to roll back to an inner-named
transaction, you receive the following error message:
Server: Msg 6401, Level 16, State 1, Line 13 Cannot roll back InnerTran. No
transaction or savepoint of that name was found.
After SQL Server
generates the error message, it continues to the next statement. This is by
design. For more information, see the "Nested Transactions" or "Inside SQL
Server" topic in SQL Server Books Online.
Microsoft recommends the
following when you design your application:
- Only open one transaction unit (consider the
possibility that another process may call yours).
- Check @@TRANCOUNT before you issue a COMMIT, a
ROLLBACK, a RETURN, or a similar command or statement.
- Write your code with the assumption that another
@@TRANCOUNT might "nest" yours and plan for the outer @@TRANCOUNT to be rolled
back when an error occurs.
- Review savepoint and mark options for transactions.
(These do not release locks!)
- Perform complete testing.
- An application that permits user interaction inside
transactions. This causes the transaction to remain open for a long time, which
causes blocking and transaction log growth because the open transaction cannot
be truncated and new transactions are added to the log after the open
transaction.
- An application that does not check @@TRANCOUNT to verify
that there are no open transactions.
- Network or other errors that close the client application
connection to SQL Server without informing it.
- Connection pooling. After worker threads are created, SQL
Server reuses them if they are not servicing a connection. If a user connection
starts a transaction and disconnects before committing or rolling back the
transaction, and a connection thereafter reuses the same thread, the previous
transaction still stays open. This situation results in locks that stay open
from the previous transaction and prevents the truncation of the committed
transactions in the log, which results in large log file sizes.For additional information about connection pooling,
click the article number below to view the article in the Microsoft Knowledge
Base:
164221 INFO: How to Enable Connection Pooling in an ODBC Application
back to the top
Extremely Large Transaction
Log records in the transaction log files are truncated on a
transaction-by-transaction basis. If the transaction scope is large, that
transaction and any transactions started after it are not removed from the
transaction log unless it completes. This can result in large log files. If the
transaction is large enough, the log file might use up the available disk space
and cause the "transaction log full" type of error message such as Error 9002.
For additional information about what to do when you receive this type of error
message is provided in the "More Information" section in this article.
Additionally, it takes a lot of time and SQL Server overhead to roll back large
transactions.
back to the top
Operations: DBCC DBREINDEX and CREATE INDEX
Because of the changes in the recovery model in SQL Server 2000,
when you use the Full recovery mode and you run DBCC DBREINDEX, the transaction
log may expand significantly more compared to that of SQL Server 7.0 in an
equivalent recovery mode with the use of SELECT INTO or BULK COPY and with
"Trunc. Log on chkpt." off.
Although the size of the transaction log
after the DBREINDEX operation might be an issue, this approach provides better
log restore performance.
back to the top
While Restoring from Transaction Log Backups
This is described in the following Microsoft Knowledge Base
article:
232196 INF: Log Space Used Appears to Grow After Restoring from Backup
If you set SQL Server 2000 to use Bulk-Logged
mode and you issue a BULK COPY or SELECT INTO statement, every changed extent
is marked and then backed up when you back up the transaction log. Although
this permits you to back up transaction logs and recover from failures even
after you perform bulk operations, this adds to the size of the transaction
logs. SQL Server 7.0 does not include this feature. SQL Server 7.0 only records
which extents are changed, but it does not record the actual extents.
Therefore, the logging takes up significantly more space in SQL Server 2000
than in SQL Server 7.0 in Bulk-Log mode but not as much as it does in Full
mode.
back to the top
Client Applications Do Not Process All Results
If you issue a query to SQL Server and you do not handle the
results immediately, you may be holding locks and reducing concurrency on your
server.
For example, suppose you issue a query that requires rows
from two pages to populate your result set. SQL Server parses, compiles, and
runs the query. This means that shared locks are placed on the two pages that
contain the rows that you must have to satisfy your query. Additionally,
suppose that not all rows fit onto one SQL Server TDS packet (the method by
which the server communicates with the client). TDS packets are filled and sent
to the client. If all rows from the first page fit on the TDS packet, SQL
Server releases the shared lock on that page but leaves a shared lock on the
second page. SQL Server then waits for the client to request more data (you can
do this by using DBNEXTROW/DBRESULTS, SQLNextRow/SQLResults, or
FetchLast/FetchFirst for example).
This means that the shared lock is
held until the client requests the rest of the data. Other processes that
request data from the second page may be blocked.
back to the top
Queries Time Out Before a Transaction Log Completes the Expansion and You Receive False 'Log Full' Error Messages
In this situation, although there is enough disk space, you still
receive an "out of space" error message.
This situation varies for
SQL Server 7.0 and SQL Server 2000.
A query can cause the transaction
log to automatically expand if the transaction log is almost full. This may
take additional time, and a query may be stopped or may exceed its time-out
period because of this. SQL Server 7.0 returns error 9002 in this situation.
This issue does not apply to SQL Server 2000.
In SQL Server 2000, if you have the
auto-shrink option turned on for a database, there is an extremely small time
during which a transaction log tries to automatically expand, but it cannot
because the
auto-shrink function is running simultaneously. This may also cause false
instances of error 9002.
Typically, the automatic expansion of transaction log files
occurs quickly. However, in the following situations, it may take longer than
usual:
- Growth increments are too small.
- Server is slow for various reasons.
- Disk drives are not fast enough.
back to the top
Unreplicated Transactions
The transaction log size of the
publisher database can expand if you are using replication. Transactions
that affect the objects that are replicated are marked as "For Replication."
These transactions, such as uncommitted transactions, are not deleted after
checkpoint or after you back up the transaction log until the log-reader task
copies the transactions to the distribution database and unmarks them. If an
issue with the log-reader task prevents it from reading these transactions in
the
publisher database, the size of the transaction log may continue to expand
as the number of non-replicated transactions increases. You can use the DBCC
OPENTRAN Transact-SQL reference to identify the oldest non-replicated
transaction.
For more information about troubleshooting unreplicated
transactions, see the "sp_replcounters" and "sp_repldone" topics in SQL Server
Books Online.
For
additional information, click the article number below to view the article in
the Microsoft Knowledge Base:
306769 FIX: Transaction Log of Snapshot Published DB Cannot Be Truncated
240039 FIX: DBCC OPENTRAN Does Not Report Replication Information
198514 FIX: Restore to New Server Causes Transactions to Remain in Log
back to the top
MORE INFORMATION
The transaction log for any database is managed as a set of
virtual log files (VLFs) whose size SQL Server determines internally based on
the total size of the log file and the growth increment in use when the log
expands. A log always expands in units of whole VLFs and it can only compress
to a VLF boundary. A VLF can exist in one of three states: ACTIVE, RECOVERABLE,
and REUSABLE.
- ACTIVE: The active portion of the log begins at the minimum log sequence
number (LSN) that represents an active (uncommitted) transaction. The active
portion of the log ends at the last-written LSN. Any VLFs that contain any part
of the active log are considered active VLFs. (Unused space in the physical log
is not part of any VLF.)
- RECOVERABLE: The portion of the log that precedes the oldest active
transaction is only necessary to maintain a sequence of log backups for
recovery purposes.
- RESUABLE: If you are not maintaining transaction log backups, or if you
already backed up the log, SQL Server reuses VLFs before the oldest active
transaction.
When SQL Server reaches the end of the physical log file, it
starts reusing that space in the physical file by issuing a CIRCLING BACK
operation to the beginning of the files. In effect, SQL Server recycles the
space in the log file that is no longer necessary for recovery or backup
purposes. If a log backup sequence is being maintained, the part of the log
before the minimum LSN cannot be overwritten until you back up or truncate
those log records. After you perform the log backup, SQL Server can circle back
to the beginning of the file. After SQL Server circles back to start writing
log records earlier in the log file, the reusable portion of the log is then
between the end of the logical log and active portion of the log.
For
additional information, see the "Transaction Log Physical Architecture" topic
in SQL Server Books Online. Additionally, you can see an excellent diagram and
discussion of this on page 190 of "Inside SQL Server 7.0" (Soukup, Ron. Inside
Microsoft SQL Server 7.0, Microsoft Press, 1999), and also in pages 182 through
186 of "Inside SQL Server 2000" (Delaney, Kalen. Inside Microsoft SQL Server
2000, Microsoft Press, 2000).
SQL Server 7.0 and SQL Server 2000 databases have the
options to autogrow and autoshrink. You can use these options to help you to
compress or expand your transaction log.
For additional information about how
these options can affect your server, click the article number below to view
the article in the Microsoft Knowledge Base:
315512 INF: Considerations for Autogrow and Autoshrink Configuration
There is a difference between the truncation versus
the compression of the transaction log file. When SQL Server truncates a
transaction log file, this means that the contents of that file (for example,
the committed transactions) are deleted. However, when you are viewing the size
of the file from a disk space perspective (for example, in Windows Explorer or
by using the
dir command) the size remains unchanged. However, the space inside
the .ldf file can now be reused by new transactions. Only when SQL Server
shrinks the size of the transaction log file, do you actually see a change in
the physical size of the log file.
For
additional information about how to shrink transaction logs, click the article
number below to view the article in the Microsoft Knowledge Base:
256650 INF: How to Shrink the SQL Server 7.0 Transaction Log
272318 INF: Shrinking the Transaction Log in SQL Server 2000 with DBCC
For additional information
about SQL Server 6.5 transaction log usage, click the article number below to
view the article in the Microsoft Knowledge Base:
110139 INF: Causes of SQL Transaction Log Filling Up