Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 55e589a

Browse files
committed
modified 9002 article - reorganized it and also updated sys.databases status values for log_wait_reuse_desc after source code research
1 parent f87cad0 commit 55e589a

2 files changed

Lines changed: 61 additions & 45 deletions

File tree

docs/relational-databases/logs/troubleshoot-a-full-transaction-log-sql-server-error-9002.md

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ helpviewer_keywords:
1212
- "troubleshooting [SQL Server], full transaction log"
1313
- "9002 (Database Engine error)"
1414
- "transaction logs [SQL Server], truncation"
15-
- "backing up transaction logs [SQL Server], full logs"
15+
- "back up transaction logs [SQL Server], full logs"
1616
- "transaction logs [SQL Server], full log"
1717
- "full transaction logs [SQL Server]"
1818
author: "MashaMSFT"
@@ -43,20 +43,17 @@ ms.custom: "seo-lt-2019"
4343
> **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.
4444
4545
## Common reasons for a full transaction log
46+
4647
The appropriate response to a full transaction log depends on what conditions caused the log to fill. Common causes include:
48+
4749
- Log not being truncated
4850
- 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
5052
- Replication or availability group synchronization that is unable to complete
5153

5254

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-
5755
## Resolving a full transaction log
5856

59-
6057
The following specific steps will help you find the reason for a full transaction log and resolve the issue.
6158

6259
## Truncate the Log
@@ -66,8 +63,7 @@ There is a difference between truncating a transaction log and shrinking a trans
6663
> [!WARNING]
6764
> 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).
6865
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).
7167

7268
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:
7369

@@ -180,7 +176,7 @@ BEGIN
180176
BEGIN
181177
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
182178
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
184180
END
185181
else if (@log_reuse_wait = 2)
186182
BEGIN
@@ -197,7 +193,7 @@ BEGIN
197193
BEGIN
198194
select 'Active transactions currently running for database ''' +@dbname+ '''. To check for active transactions, run these commands:' as Recommendation
199195
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
201197
END
202198
203199
else if (@log_reuse_wait = 5)
@@ -253,12 +249,13 @@ DEALLOCATE no_truncate_db
253249
> [!IMPORTANT]
254250
> 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)
255251
256-
More information about the following two actions is provided below:
252+
### More information on LOG_BACKUP log_reuse_wait
257253

258-
- Backing up the log
259-
- Completing or killing a long-running transaction
254+
#### Recovery model
260255

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
262259

263260
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).
264261

@@ -300,8 +297,9 @@ ORDER BY bs.database_name asc, bs.Backup_Start_Date desc;
300297

301298
- <xref:Microsoft.SqlServer.Management.Smo.Backup.SqlBackup%2A> (SMO)
302299

300+
### More information on ACTIVE_TRANSACTION log_reuse_wait
303301

304-
### Discovering long-running transactions
302+
#### Discover long-running transactions
305303

306304
A very long-running transaction can cause the transaction log to fill. To look for long-running transactions, use one of the following:
307305

@@ -312,9 +310,13 @@ This dynamic management view returns information about transactions at the datab
312310
- **[DBCC OPENTRAN](../../t-sql/database-console-commands/dbcc-opentran-transact-sql.md).**
313311
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).
314312

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
316318

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)
318320

319321
## Disk volume is full
320322

@@ -337,7 +339,7 @@ See [Move Database Files](../../relational-databases/databases/move-database-fil
337339

338340
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.
339341

340-
342+
341343
For more information see [Add Data or Log Files to a Database](../../relational-databases/databases/add-data-or-log-files-to-a-database.md).
342344

343345

@@ -353,14 +355,14 @@ DECLARE @log_reached_disk_size BIT = 0
353355
SELECT
354356
name LogName,
355357
physical_name,
356-
convert(bigint, size)*8/1024 LogFile_Size_MB,
358+
CONVERT(bigint, size)*8/1024 LogFile_Size_MB,
357359
volume_mount_point,
358360
available_bytes/1024/1024 Available_Disk_space_MB,
359-
(convert(bigint, size)*8.0/1024)/(available_bytes/1024/1024 )*100 file_size_as_percentage_of_disk_space,
361+
(CONVERT(bigint, size)*8.0/1024)/(available_bytes/1024/1024 )*100 file_size_as_percentage_of_disk_space,
360362
db_name(mf.database_id) DbName
361363
FROM sys.master_files mf CROSS APPLY sys.dm_os_volume_stats (mf.database_id, file_id)
362364
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
364366
ORDER BY size DESC
365367
366368
if @@ROWCOUNT > 0
@@ -380,7 +382,7 @@ BEGIN
380382
name
381383
FROM sys.master_files mf CROSS APPLY sys.dm_os_volume_stats (mf.database_id, file_id)
382384
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
384386
ORDER BY size desc
385387
386388
OPEN log_filled_disk
@@ -390,7 +392,7 @@ BEGIN
390392
WHILE @@FETCH_STATUS = 0
391393
BEGIN
392394
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
394396
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
395397
SELECT 'DBCC SHRINKFILE(''' + @log_name_filled_disk + ''')' AS Shrinkfile_Command
396398
SELECT 'ALTER DATABASE ' + @db_name_filled_disk + ' ADD LOG FILE ( NAME = N''' + @log_name_filled_disk + '_new'', FILENAME = N''NEW_VOLUME_AND_FOLDER_LOCATION\' + @log_name_filled_disk + '_NEW.LDF'', SIZE = 81920KB , FILEGROWTH = 65536KB )' AS AddNewFile
@@ -411,50 +413,63 @@ END
411413
412414
```
413415

414-
## Log size is set to a fixed maximum value
416+
## Log size set to a fixed maximum or Autogrow is disabled
415417

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:
417419

418420
```tsql
419-
SELECT db_name(database_id) DbName,
421+
SELECT DB_NAME(database_id) DbName,
420422
name LogName,
421423
physical_name,
422424
type_desc ,
423-
convert(bigint, SIZE)*8/1024 LogFile_Size_MB ,
424-
convert(bigint,max_size)*8/1024 LogFile_MaxSize_MB ,
425-
(SIZE*8.0/1024)/(max_size*8.0/1024)*100 percent_full_of_max_size
425+
CONVERT(bigint, SIZE)*8/1024 LogFile_Size_MB ,
426+
CONVERT(bigint,max_size)*8/1024 LogFile_MaxSize_MB ,
427+
(SIZE*8.0/1024)/(max_size*8.0/1024)*100 percent_full_of_max_size,
428+
CASE WHEN growth = 0 THEN 'AUTOGROW_DISABLED' ELSE 'Autogrow_Enabled' END as AutoGrow
426429
FROM sys.master_files
427430
WHERE file_id = 2
428-
AND max_size not in (-1, 268435456)
429-
AND (SIZE*8.0/1024)/(max_size*8.0/1024)*100 > 90
431+
AND (SIZE*8.0/1024)/(max_size*8.0/1024)*100 > 90
432+
AND max_size not in (-1, 268435456)
433+
OR growth = 0
430434
431435
if @@ROWCOUNT > 0
432436
BEGIN
433-
DECLARE @db_name_max_size sysname, @log_name_max_size sysname, @configured_max_log_boundary bigint
437+
DECLARE @db_name_max_size sysname, @log_name_max_size sysname, @configured_max_log_boundary bigint, @auto_grow int
434438
435439
DECLARE reached_max_size CURSOR FOR
436440
SELECT db_name(database_id),
437441
name,
438-
convert(bigint, SIZE)*8/1024
442+
CONVERT(bigint, SIZE)*8/1024,
443+
growth
439444
FROM sys.master_files
440445
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
444449
445450
OPEN reached_max_size
446451
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
448453
449454
WHILE @@FETCH_STATUS = 0
450455
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
460+
SELECT 'ALTER DATABASE ' + @db_name_max_size + ' MODIFY FILE ( NAME = N''' + @log_name_max_size + ''', FILEGROWTH = 65536KB)' as AutoGrowth
461+
END
462+
ELSE
463+
BEGIN
464+
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
466+
END
467+
453468
SELECT 'ALTER DATABASE ' + @db_name_max_size + ' MODIFY FILE ( NAME = N''' + @log_name_max_size + ''', MAXSIZE = UNLIMITED)' as UnlimitedSize
454469
SELECT 'ALTER DATABASE ' + @db_name_max_size + ' MODIFY FILE ( NAME = N''' + @log_name_max_size + ''', MAXSIZE = something_larger_than_' + CONVERT(varchar(24), @configured_max_log_boundary) +'MB )' as IncreasedSize
455470
SELECT 'ALTER DATABASE ' + @db_name_max_size + ' ADD LOG FILE ( NAME = N''' + @log_name_max_size + '_new'', FILENAME = N''SOME_FOLDER_LOCATION\' + @log_name_max_size + '_NEW.LDF'', SIZE = 81920KB , FILEGROWTH = 65536KB )' as AddNewFile
456471
457-
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
458473
459474
END
460475
@@ -465,14 +480,14 @@ ELSE
465480
SELECT 'Found no files that have reached max log file size' as Findings
466481
```
467482

468-
### Increase log file size
483+
### Increase log file size or enable Autogrow
469484

470485
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.
471486

472487
If autogrow is disabled, the database is online, and sufficient space is available on the disk, do either of these:
473488

474489
- 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)
476491

477492
> [!NOTE]
478493
> 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
483498
[Manage the Size of the Transaction Log File](../../relational-databases/logs/manage-the-size-of-the-transaction-log-file.md)
484499
[Transaction Log Backups &#40;SQL Server&#41;](../../relational-databases/backup-restore/transaction-log-backups-sql-server.md)
485500
[sp_add_log_file_recover_suspect_db &#40;Transact-SQL&#41;](../../relational-databases/system-stored-procedures/sp-add-log-file-recover-suspect-db-transact-sql.md)
501+
[Manage the size of the transaction log file](manage-the-size-of-the-transaction-log-file.md)

0 commit comments

Comments
 (0)