B Esakkiappan's SQL Thoughts

To learn by sharing all about SQL Server 2005

Know the Transaction LOG - Part - 1

After writing about the LOG shipping in my previous post, many questions raised that urged me to share the knowledge about the database logs in SQL Server 2005. As it is a huge concept to discuss, I planned to write a series of posts in Database Transaction Logs.

What is Database LOG?

A Database log always called as Transaction Log, is a critical component of a database. It is in a format of one or more disk files, created with 'Create Database' or 'Alter Database' command. The Transaction Log is required to bring back the last working state of your database when a failure occurs to your database.

Based on the 'RECOVERY' option of the database, Transaction Log records every database modification including the information about the pages which are being modified, the data values added or modified, start and end time of modification occurs to a series of LOG records. So that, whenever a 'undoing' or 'redoing' is required, SQL Server 2005 can do that to your database with the help of Transaction Log.

What is Undo or  Redo ?

Before  knowing these, it is necessary to know how a transaction  work with the modification or how SQL Server handles a transaction.

Whenever a data modification request is received by the SQL Server, regardless of explicit or implicit transaction, all the underlying pages are loaded into the buffer cache. A series of log records are created for this transaction including page numbers for which the modifications are to be carried out, before stage and after stage of the modification. All these logs records are internally linked together. Then the modifications take effect in the pages loaded in cache.

After modifications are done in cache, if a Rollback request for this particular transaction is received then all the undo operations for this transaction are carried out from the Transaction Log records. This Roll Backward operation is called 'Undoing'.

Suppose a Commit request for this transaction is received, then first log records are written to log disk files, prior to the data pages that are modified in cache are written to the data disk files. The buffer manager ensures this. Write LOG Records first and then DATA Records into Disk Files. This mechanism is called as 'Write-Ahead-Log'(WAL).

After writing log records into the log file or files and before writing the modified data records into the data files, say suppose, SQL Server stopped due to some resource problems, then, SQL Server uses these log records to recover all the transactions that are marked as committed  and not reflected in data,  during the restart of SQL Server. This recovery is called Restart Recovery. This restart recovery is always done for all the databases of an instance when that instance of SQL Server is restarted. This restart recovery, which is doing Roll Forward of all transactions to the data files is called 'Re-doing'.

LOG Sequence Number (LSN)

Every Log Record in Transaction log is associated with a LSN that is Log Sequence Number. As every transaction is associated with a series of log records, and every log records having LSN within it and all related log records are linked backward for Rollback operations. LSN of a log record is unique and it is a sequence number greater than the previous LSN associated with old transactions.

Every Data Page has a LSN number of the Log record which modified this page earlier, recorded in its header. Every LOG Record associated with a page for which the modification is being carried out, is also having the previous LSN stored in the Page’s header, and the new LSN number for this current modification. When a Redo operation is carried out by the SQL Server, it checks these two LSN numbers. If the LSN of the page is high then REDO skip for this page.

Active LOG Records and Inactive Log Records

The Records in LOG files are marked as two types, Active and Inactive. All the Log Records that are the part of live transactions which are not yet either committed or rolled back are called as Active Log Records. The Log Records related with earlier committed or rolled backed transactions are called as Inactive Records. The redoing or undoing operation carried out by the SQL Server, only deals with Inactive Log Records.

Virtual Log Files

SQL Server Database Engine, divide the physical Log disk file into Virtual log files internally. The number and size of the virtual log files cannot be configured explicitly by any DBA. It is based on the auto growth specification in Database for its log file. Database engine tries to have a minimum number of virtual log files for a physical log file. When a log file is created first, the number of Virtual log files may be 4 to 16. And it will go higher when the physical LOG file is enlarged. The performance will be slower, if number of virtual log files are high. SQL Server will automatically create VLFs during the expansion of Physical LOG File, so creating the LOG file with considerable size and file growth percentage should be adequately specified will reduce the number of Virtual Log Files in LOG. To view the Virtual log file use undocumented DBCC Command DBCC LOGINFO. The following is DBCC LOG Info of one of my active SQL Server Database in my development server. This Log file is having 16 Virtual Log files.

 

FieldID

 

 

 

 

 

FileSize

 

 

 

 

 

StartOffset

 

 

 

 

 

FSEQNO

 

 

 

 

 

STATUS

 

 

 

 

 

PARITY

 

 

 

 

 

CREATELSN

 

 

 

 

 

2

 

 

 

253952

 

 

 

8192

 

 

 

24

 

 

 

0

 

 

 

64

 

 

 

0

 

 

 

2

 

 

 

253952

 

 

 

262144

 

 

 

27

 

 

 

0

 

 

 

64

 

 

 

0

 

 

 

2

 

 

 

253952

 

 

 

516096

 

 

 

22

 

 

 

0

 

 

 

128

 

 

 

0

 

 

 

2

 

 

 

278528

 

 

 

770048

 

 

 

23

 

 

 

0

 

 

 

128

 

 

 

0

 

 

 

2

 

 

 

262144

 

 

 

1048576

 

 

 

25

 

 

 

0

 

 

 

64

 

 

 

24000000034800494

 

 

 

2

 

 

 

262144

 

 

 

1310720

 

 

 

26

 

 

 

0

 

 

 

64

 

 

 

25000000049500003

 

 

 

2

 

 

 

262144

 

 

 

1572864

 

 

 

28

 

 

 

0

 

 

 

64

 

 

 

27000000020000052

 

 

 

2

 

 

 

262144

 

 

 

1835008

 

 

 

29

 

 

 

0

 

 

 

64

 

 

 

28000000019100003

 

 

 

2

 

 

 

262144

 

 

 

2097152

 

 

 

30

 

 

 

0

 

 

 

64

 

 

 

29000000007300459

 

 

 

2

 

 

 

262144

 

 

 

2359296

 

 

 

31

 

 

 

0

 

 

 

64

 

 

 

29000000050700023

 

 

 

2

 

 

 

262144

 

 

 

2621440

 

 

 

32

 

 

 

0

 

 

 

64

 

 

 

31000000019800006

 

 

 

2

 

 

 

327680

 

 

 

2883584

 

 

 

33

 

 

 

0

 

 

 

64

 

 

 

32000000004600469

 

 

 

2

 

 

 

327680

 

 

 

3211264

 

 

 

34

 

 

 

0

 

 

 

64

 

 

 

33000000024100176

 

 

 

2

 

 

 

393216

 

 

 

3538944

 

 

 

35

 

 

 

0

 

 

 

64

 

 

 

34000000035400136

 

 

 

2

 

 

 

393216

 

 

 

3932160

 

 

 

36

 

 

 

0

 

 

 

64

 

 

 

35000000034400087

 

 

 

2

 

 

 

458752

 

 

 

4325376

 

 

 

37

 

 

 

0

 

 

 

64

 

 

 

36000000044200003

 

 

 

Checkpoints in Transaction Logs

Within a start and end of a complete transaction, we may use checkpoints or save points with the help of CHECKPOINT and SAVE TRANSACTION T-SQL statements, to store partially done transactions to write in disk files. These checkpoints may also internally triggered by SQL Server itself too. What a checkpoint does within the transaction?

Checkpoint is a SQL Server process that writing all modified data pages I buffer cache into disk files.It is also forces any pending transaction log records into log file. Performing Checkpoints reduces the recovery time of restart recovery, as it forced the transactions to written to log files and also writes the dirty pages into disk files. This process of Checkpoints minimize the Roll forward operations of Restore Recovery.

The Checkpoint Operation involves following steps.

  • Writing out all dirty pages into Data disk files.
  • Writing a list of active transactions to Transaction log.
  • Storing check point log records to Transaction Log.

Scope of the Checkpoints is the Database level. So the Checkpoint operation run only for the current database only.

Checkpoint occurs in the following cases.

  1. Whenever we issue CHECKPOINT T-SQL Command for the current database.
  2. Whenever SQL Server shuts down without option WITH NOWAIT. This checkpoint works for all the databases in that instance. WITH NOWAIT option skips the checkpoint.
  3. Whenever a Data Files are added to or removed from a Database using ALTER DATABASE Command.
  4. When Bulk copy operation or Select Into operation performed in a database for which ‘Bulk-logged’ Recovery model is set.
  5. When a database’s recovery model is changed from Bulk-logged or FULL to SIMPLE.
  6. For a Simple Recovery Model Database, if the size of the Transaction Log exceeds 70%.
  7. When number of log entries exceeds the estimated amount of work required by the SQL Server's 'Recovery Interval' configuration.

I think, I covered utmost every aspect of Restart Recovery often called as Crash Recovery.In my next part of this post, I will write about the another type of Recovery - Restore Recovery

 

This post is originaly published in my SQLThoughts Blogger blog

Comments

sql server 2005 transactions said:

Pingback from  sql server 2005 transactions

# May 15, 2008 7:05 PM

Uncommited Transactions - Forum Fachinformatiker.de said:

Pingback from  Uncommited Transactions - Forum Fachinformatiker.de

# September 29, 2008 2:36 PM

Transaction Log and Rollback | keyongtech said:

Pingback from  Transaction Log and Rollback | keyongtech

# January 18, 2009 5:40 PM

Transaction Log and Rollback | keyongtech said:

Pingback from  Transaction Log and Rollback | keyongtech

# January 18, 2009 5:40 PM