Microsoft KB Archive/317375

= A transaction log grows unexpectedly or becomes full on a computer that is running SQL Server =

Article ID: 317375

Article Last Modified on 6/5/2007

-

APPLIES TO


 * Microsoft SQL Server 2005 Standard Edition
 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Enterprise Edition
 * Microsoft SQL Server 2005 Express Edition
 * Microsoft SQL Server 2005 Workgroup Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition

-



This article was previously published under Q317375



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 &quot;Insufficient Disk Space&quot; 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
 * Extremely large transactions
 * Operations: DBCC DBREINDEX and CREATE INDEX
 * While restoring from transaction log backups
 * Client applications do not process all results
 * Queries time out before a transaction log completes the expansion and you receive false 'Log full' error messages
 * Unreplicated transactions

Note In SQL Server 2005, you can review the log_reuse_wait and log_reuse_wait_desc columns of the sys.databases catalog view to determine the following things:
 * Why the transaction log space is not reused
 * Why the transaction log cannot be truncated

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 more information about this particular scenario, click the following article numbers to view the articles in the Microsoft Knowledge Base:

295108 Incomplete transaction may hold large number of locks and case blocking

171224 Understanding how the Transact-SQL KILL command works

Additionally, see the &quot;DBCC OPENTRAN&quot; 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 &quot;Nested Transactions&quot; or &quot;Inside SQL Server&quot; 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 &quot;nest&quot; 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.</li></ul> </li> 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.</li> An application that does not check @@TRANCOUNT to verify that there are no open transactions.</li> Network or other errors that close the client application connection to SQL Server without informing it.</li> 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 more information about connection pooling, click the following article number to view the article in the Microsoft Knowledge Base:

164221 How to enable connection pooling in an ODBC application

</li></ul>

back to the top

Extremely large transactions
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 &quot;transaction log full&quot; 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 &quot;More Information&quot; 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 &quot;Trunc. Log on chkpt.&quot; 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 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 &quot;out of space&quot; 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 &quot;For Replication.&quot; 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 &quot;sp_replcounters&quot; and &quot;sp_repldone&quot; topics in SQL Server Books Online.

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

306769 FIX: Transaction log of snapshot published database 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

<div class="moreinformation_section">

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.
 * REUSABLE: 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 &quot;Transaction Log Physical Architecture&quot; topic in SQL Server Books Online. Additionally, you can see an excellent diagram and discussion of this on page 190 of &quot;Inside SQL Server 7.0&quot; (Soukup, Ron. Inside Microsoft SQL Server 7.0, Microsoft Press, 1999), and also in pages 182 through 186 of &quot;Inside SQL Server 2000&quot; (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 more information about how these options can affect your server, click the following article number to view the article in the Microsoft Knowledge Base:

315512 Considerations for Autogrow and Autoshrink configuration in SQL Server

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 more information about how to shrink transaction logs, click the following article numbers to view the articles in the Microsoft Knowledge Base:

256650 How to shrink the SQL Server 7.0 transaction log

272318 Shrinking the transaction log in SQL Server 2000 with DBCC SHRINKFILE

For more information about SQL Server 6.5 transaction log usage, click the following article number to view the article in the Microsoft Knowledge Base:

110139 Causes of SQL transaction log filling up

Keywords: kbinfo KB317375

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.