You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/relational-databases/logs/troubleshoot-a-full-transaction-log-sql-server-error-9002.md
+59-43Lines changed: 59 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ helpviewer_keywords:
12
12
- "troubleshooting [SQL Server], full transaction log"
13
13
- "9002 (Database Engine error)"
14
14
- "transaction logs [SQL Server], truncation"
15
-
- "backing up transaction logs [SQL Server], full logs"
15
+
- "back up transaction logs [SQL Server], full logs"
16
16
- "transaction logs [SQL Server], full log"
17
17
- "full transaction logs [SQL Server]"
18
18
author: "MashaMSFT"
@@ -43,20 +43,17 @@ ms.custom: "seo-lt-2019"
43
43
> **This article is focused on SQL Server.** For more specific information on this error in Azure SQL Database and Azure SQL Managed Instance, see [Troubleshooting transaction log errors with Azure SQL Database and Azure SQL Managed Instance](/azure/azure-sql/database/troubleshoot-transaction-log-errors-issues). Azure SQL Database and Azure SQL Managed Instance are based on the latest stable version of the Microsoft SQL Server database engine, so much of the content is similar though troubleshooting options and tools may differ.
44
44
45
45
## Common reasons for a full transaction log
46
+
46
47
The appropriate response to a full transaction log depends on what conditions caused the log to fill. Common causes include:
48
+
47
49
- Log not being truncated
48
50
- Disk volume is full
49
-
- Log size is set to a fixed maximum value (autogrow is disabled)
51
+
- Log size is set to a fixed maximum value or autogrow is disabled
50
52
- Replication or availability group synchronization that is unable to complete
51
53
52
54
53
-
If no recent transaction log history is indicated for the database with a full transaction log, the solution to the problem is straightforward: resume regular transaction log backups of the database. For more information and a script to review backup history, see the section [Backing up the log](#backing-up-the-log) in this article.
54
-
55
-
56
-
57
55
## Resolving a full transaction log
58
56
59
-
60
57
The following specific steps will help you find the reason for a full transaction log and resolve the issue.
61
58
62
59
## Truncate the Log
@@ -66,8 +63,7 @@ There is a difference between truncating a transaction log and shrinking a trans
66
63
> [!WARNING]
67
64
> Data that is moved to shrink a file can be scattered to any available location in the file. This causes index fragmentation and might slow the performance of queries that search a range of the index. To eliminate the fragmentation, consider rebuilding the indexes on the file after shrinking. For more information, see [Shrink a database](../databases/shrink-a-database.md).
68
65
69
-
70
-
To discover what is preventing log truncation in a given case, use the `log_reuse_wait` and `log_reuse_wait_desc` columns of the `sys.databases` catalog view. For more information, see [sys.databases (Transact-SQL)](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md). For descriptions of factors that can delay log truncation, see [The Transaction Log (SQL Server)](../../relational-databases/logs/the-transaction-log-sql-server.md).
66
+
To discover what is preventing log truncation in a given case, use the `log_reuse_wait` and `log_reuse_wait_desc` columns of the `sys.databases` catalog view. For more information, see [sys.databases (Transact-SQL)](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md). For descriptions of factors that can delay log truncation, see [The Transaction Log (SQL Server)](../../relational-databases/logs/the-transaction-log-sql-server.md).
71
67
72
68
The following set of T-SQL commands will help you identify if a database transaction log is not truncated and the reason for it. The following script will also recommend steps to resolve the issue:
73
69
@@ -180,7 +176,7 @@ BEGIN
180
176
BEGIN
181
177
select 'Consider running the checkpoint command to attempt resolving this issue or further t-shooting may be required on the checkpoint process. Also, examine the log for active VLFs at the end of file' as Recommendation
182
178
select 'USE ''' + @dbname+ '''; CHECKPOINT' as CheckpointCommand
183
-
select 'select * from sys.dm_db_log_info(' + convert(varchar,@database_id)+ ')' as VLF_LogInfo
179
+
select 'select * from sys.dm_db_log_info(' + CONVERT(varchar,@database_id)+ ')' as VLF_LogInfo
184
180
END
185
181
else if (@log_reuse_wait = 2)
186
182
BEGIN
@@ -197,7 +193,7 @@ BEGIN
197
193
BEGIN
198
194
select 'Active transactions currently running for database ''' +@dbname+ '''. To check for active transactions, run these commands:' as Recommendation
199
195
select 'DBCC OPENTRAN (''' +@dbname+ ''')' as FindOpenTran
200
-
select 'select database_id, db_name(database_id) dbname, database_transaction_begin_time, database_transaction_state, database_transaction_log_record_count, database_transaction_log_bytes_used, database_transaction_begin_lsn, stran.session_id from sys.dm_tran_database_transactions dbtran left outer join sys.dm_tran_session_transactions stran on dbtran.transaction_id = stran.transaction_id where database_id = ' + convert(varchar, @database_id) as FindOpenTransAndSession
196
+
select 'select database_id, db_name(database_id) dbname, database_transaction_begin_time, database_transaction_state, database_transaction_log_record_count, database_transaction_log_bytes_used, database_transaction_begin_lsn, stran.session_id from sys.dm_tran_database_transactions dbtran left outer join sys.dm_tran_session_transactions stran on dbtran.transaction_id = stran.transaction_id where database_id = ' + CONVERT(varchar, @database_id) as FindOpenTransAndSession
201
197
END
202
198
203
199
else if (@log_reuse_wait = 5)
@@ -253,12 +249,13 @@ DEALLOCATE no_truncate_db
253
249
> [!IMPORTANT]
254
250
> If the database was in recovery when the 9002 error occurred, after resolving the problem, recover the database by using [ALTER DATABASE *database_name* SET ONLINE.](../../t-sql/statements/alter-database-transact-sql-set-options.md)
255
251
256
-
More information about the following two actions is provided below:
252
+
###More information on LOG_BACKUP log_reuse_wait
257
253
258
-
- Backing up the log
259
-
- Completing or killing a long-running transaction
254
+
#### Recovery model
260
255
261
-
### Backing up the log
256
+
The transaction log may be failing to truncate with LOG_BACKUP log_reuse_wait category, because you have never backed it up. In many of those cases, you may find that your database is using FULL or BULK_LOGGED recovery model, but you do not really intend to back up transaction logs for it. In such cases, you should consider switching your database to SIMPLE recovery model and only perform full and differential database backups. For more information, see [Recovery Models](../backup-restore/recovery-models-sql-server.md)
257
+
258
+
#### Back up the log
262
259
263
260
Under the FULL or BULK_LOGGED recovery model, if the transaction log has not been backed up recently, backup might be what is preventing log truncation. If the log has never been backed up, you **must create two log backups** to permit the [!INCLUDE[ssDE](../../includes/ssde-md.md)] to truncate the log to the point of the last backup. Truncating the log frees logical space for new log records. To keep the log from filling up again, take log backups regularly and more frequently. For more information, see [Recovery Models](../backup-restore/recovery-models-sql-server.md).
264
261
@@ -300,8 +297,9 @@ ORDER BY bs.database_name asc, bs.Backup_Start_Date desc;
This statement lets you identify the user ID of the owner of the transaction, so you can potentially track down the source of the transaction for a more orderly termination (committing it rather than rolling it back).
314
312
315
-
### Kill a transaction
313
+
##### Kill a transaction
314
+
315
+
Sometimes you just have to end the transaction; you may have to use the [KILL](../../t-sql/language-elements/kill-transact-sql.md) statement. Please use this statement very carefully, especially when critical processes are running that you don't want to kill. For more information, see [KILL (Transact-SQL)](../../t-sql/language-elements/kill-transact-sql.md)
316
+
317
+
### More information on AVAILABILITY_REPLICA log_reuse_wait
316
318
317
-
Sometimes you just have to end the process; you may have to use the [KILL](../../t-sql/language-elements/kill-transact-sql.md) statement. Please use this statement very carefully, especially when critical processes are running that you don't want to kill. For more information, see [KILL (Transact-SQL)](../../t-sql/language-elements/kill-transact-sql.md)
319
+
When transaction changes at primary Availability replica are not yet hardened on the secondary replica the transaction log cannot be truncated. This can cause the log to grow. For information on how to troubleshoot this type of issue see [Error 9002. The transaction log for database is full due to AVAILABILITY_REPLICA error](/troubleshoot/sql/availability-groups/error-9002-transaction-log-large)[Error 9002. The transaction log for database is full due to AVAILABILITY_REPLICA error](/troubleshoot/sql/availability-groups/error-9002-transaction-log-large)
318
320
319
321
## Disk volume is full
320
322
@@ -337,7 +339,7 @@ See [Move Database Files](../../relational-databases/databases/move-database-fil
337
339
338
340
Add a new log file to the database on a different disk that has sufficient space by using `ALTER DATABASE <database_name> ADD LOG FILE`. Multiple log files for a single database should be considered a temporary condition to resolve a space issue, not a long-term condition. Most databases should only have one transaction log file. Continue to investigate the reason why the transaction log is full and cannot be truncated. Consider adding temporary additional transaction log files as an advanced troubleshooting step.
339
341
340
-
342
+
341
343
For more information see [Add Data or Log Files to a Database](../../relational-databases/databases/add-data-or-log-files-to-a-database.md).
342
344
343
345
@@ -353,14 +355,14 @@ DECLARE @log_reached_disk_size BIT = 0
FROM sys.master_files mf CROSS APPLY sys.dm_os_volume_stats (mf.database_id, file_id)
362
364
WHERE mf.[type_desc] = 'LOG'
363
-
and (convert(bigint, size)*8.0/1024)/(available_bytes/1024/1024 )*100 > 90 --log is 90% of disk drive
365
+
AND (CONVERT(bigint, size)*8.0/1024)/(available_bytes/1024/1024 )*100 > 90 --log is 90% of disk drive
364
366
ORDER BY size DESC
365
367
366
368
if @@ROWCOUNT > 0
@@ -380,7 +382,7 @@ BEGIN
380
382
name
381
383
FROM sys.master_files mf CROSS APPLY sys.dm_os_volume_stats (mf.database_id, file_id)
382
384
WHERE mf.[type_desc] = 'LOG'
383
-
and (convert(bigint, size)*8.0/1024)/(available_bytes/1024/1024 )*100 > 90 --log is 90% of disk drive
385
+
AND (convert(bigint, size)*8.0/1024)/(available_bytes/1024/1024 )*100 > 90 --log is 90% of disk drive
384
386
ORDER BY size desc
385
387
386
388
OPEN log_filled_disk
@@ -390,7 +392,7 @@ BEGIN
390
392
WHILE @@FETCH_STATUS = 0
391
393
BEGIN
392
394
393
-
SELECT 'Transaction log for database "' + @db_name_filled_disk + '" has nearly or comletely filled disk volume it resides on!' AS Finding
395
+
SELECT 'Transaction log for database "' + @db_name_filled_disk + '" has nearly or completely filled disk volume it resides on!' AS Finding
394
396
SELECT 'Consider using one of the below commands to shrink the "' + @log_name_filled_disk +'" transaction log file size or add a new file to a NEW volume' AS Recommendation
395
397
SELECT 'DBCC SHRINKFILE(''' + @log_name_filled_disk + ''')' AS Shrinkfile_Command
## Log size set to a fixed maximum or Autogrow is disabled
415
417
416
-
Error 9002 can be generated if the transaction log size has been set to an upper limit and autogrow is not allowed. In this case, enabling autogrow or increasing the log size manually can help resolve the issue. Use this T-SQL command to find such log files and follow the recommendations provided:
418
+
Error 9002 can be generated if the transaction log size has been set to an upper limit or Autogrow is not allowed. In this case, enabling autogrow or increasing the log size manually can help resolve the issue. Use this T-SQL command to find such log files and follow the recommendations provided:
DECLARE @db_name_max_size sysname, @log_name_max_size sysname, @configured_max_log_boundary bigint, @auto_grow int
434
438
435
439
DECLARE reached_max_size CURSOR FOR
436
440
SELECT db_name(database_id),
437
441
name,
438
-
convert(bigint, SIZE)*8/1024
442
+
CONVERT(bigint, SIZE)*8/1024,
443
+
growth
439
444
FROM sys.master_files
440
445
WHERE file_id = 2
441
-
AND max_size not in (-1,
442
-
268435456)
443
-
AND (SIZE*8.0/1024)/(max_size*8.0/1024)*100 > 90
446
+
AND (SIZE*8.0/1024)/(max_size*8.0/1024)*100 > 90
447
+
AND max_size not in (-1, 268435456)
448
+
OR growth = 0
444
449
445
450
OPEN reached_max_size
446
451
447
-
FETCH NEXT FROM reached_max_size into @db_name_max_size , @log_name_max_size, @configured_max_log_boundary
452
+
FETCH NEXT FROM reached_max_size into @db_name_max_size , @log_name_max_size, @configured_max_log_boundary, @auto_grow
448
453
449
454
WHILE @@FETCH_STATUS = 0
450
455
BEGIN
451
-
SELECT 'The database "' + @db_name_max_size+'" contains a log file "' + @log_name_max_size + '" whose max limit is set to ' + convert(varchar(24), @configured_max_log_boundary) + ' MB and this limit has been reached!' as Finding
452
-
SELECT 'Consider using one of the below ALTER DATABASE commands to either change the log file size or add a new file' as Recommendation
456
+
IF @auto_grow = 0
457
+
BEGIN
458
+
SELECT 'The database "' + @db_name_max_size+'" contains a log file "' + @log_name_max_size + '" whose autogrow has been DISABLED' as Finding
459
+
SELECT 'Consider enabling autogrow or increasing file size via these ALTER DATABASE commands' as Recommendation
SELECT 'The database "' + @db_name_max_size+'" contains a log file "' + @log_name_max_size + '" whose max limit is set to ' + convert(varchar(24), @configured_max_log_boundary) + ' MB and this limit has been reached!' as Finding
465
+
SELECT 'Consider using one of the below ALTER DATABASE commands to either change the log file size or add a new file' as Recommendation
FETCH NEXT FROM reached_max_size into @db_name_max_size , @log_name_max_size, @configured_max_log_boundary
472
+
FETCH NEXT FROM reached_max_size into @db_name_max_size , @log_name_max_size, @configured_max_log_boundary, @auto_grow
458
473
459
474
END
460
475
@@ -465,14 +480,14 @@ ELSE
465
480
SELECT 'Found no files that have reached max log file size' as Findings
466
481
```
467
482
468
-
### Increase log file size
483
+
### Increase log file size or enable Autogrow
469
484
470
485
If space is available on the log disk, you can increase the size of the log file. The maximum size for log files is two terabytes (TB) per log file.
471
486
472
487
If autogrow is disabled, the database is online, and sufficient space is available on the disk, do either of these:
473
488
474
489
- Manually increase the file size to produce a single growth increment.
475
-
- Turn on autogrow by using the ALTER DATABASE statement to set a non-zero growth increment for the FILEGROWTH option.
490
+
- Turn on autogrow by using the ALTER DATABASE statement to set a non-zero growth increment for the FILEGROWTH option. See [Considerations for the autogrow and autoshrink settings in SQL Server](/troubleshoot/sql/admin/considerations-autogrow-autoshrink)
476
491
477
492
> [!NOTE]
478
493
> In either case, if the current size limit has been reached, increase the MAXSIZE value.
@@ -483,3 +498,4 @@ If autogrow is disabled, the database is online, and sufficient space is availab
483
498
[Manage the Size of the Transaction Log File](../../relational-databases/logs/manage-the-size-of-the-transaction-log-file.md)
0 commit comments