You are on page 1of 32

By:

--Srikanth
What is Transaction Log File
About TLF, Architecture, Properties
Operations Supported by TLF
Logical Log File
Virtual Log File – States, Terms in VLF

Why – Transaction Log File


Need of TLF in Database

When – Transaction Log File


Usage of TLF

Where – Transaction Log File


Location of TLF

How – Transaction Log File


Creation of TLF
Maintenance of TLF – reuse, auto growth
“ It is reusable circular file which captures transaction details in
the form of log records during logging process to recover database
in disaster ”

 Contains - Log Records

 Stores – Every Transaction made to DB except minimally logged (i.e.


Bulk import, SELECT INTO,….)

 Must be – uncorrupted

 Its Importance in - Disaster recovery


VLF-1 VLF-2 VLF-3 VLF-4 VLF-5

Inactive Log File Active Log File Inactive Log File

VLF-1 VLF-2 VLF-3 VLF-4 VLF-5

Truncated Files Min LSN Unused Space


Start of Logical Log File Last
End of Logical Log File
Checked Point
 Logical Name - Name used to identify or differ from other type of files.

 File Type – ‘ .ldf ‘.

 File Group – NOT APPLICABLE.

 Initial Size - Based on Model DB. Min and default is 1MB, can give > value in Model DB.

 Auto Growth – Based on Model DB. Default is 10%, min is 1%.

 Max Size – Based on Model DB. Default is unlimited (but limited to 2097152MB).

 Max Size – Based on Model DB.

 Path / Location - C:\Program Files\Microsoft SQL Server\ MSSQL11.ANI12\ MSSQL\DATA\

 File Name – Name in about path with its extension.


 Individual Transaction Recovery
At time of loss of communication with client, to roll back the modification made by an
incomplete transaction.

 All incomplete Transactions recovery when SQL Server is started


If the Server running SS fails and database may be left in state where some
modifications are never been written from buffer cache to data file. When instance is started it runs
a recovery of each database. Data integrity is preserved.

 Rolling a restored database, file, file group, or page forward to point of failure
After hardware loss or hard disk failure affecting database fails.

 Transaction Replication
Log Reader Agent monitors the transaction log and copies the transactions marked for
replication from transaction into distribution database.

 Standby-Server Solutions
Database mirroring, Log Shipping.
“ It is internal division of Transaction log comprised of 1 or
more log blocks ”
• Has no fixed size.
• Cannot configure size and number of virtual log files.

• Too many VLF’s can slow down database startup and also log backup and restore
operations.
 Active State
• Log records are currently using by Database engine.
• Records may be a part of active transactions, replication, mirroring , or log shipping.
• Log may be a part of transaction which is already completed but not yet written back to data file.

 Inactive State
• Log whose records are written back to data file and also not using by Database Engine.
• Records may be not part of active transaction, replication, mirroring, or log shipping.
• When ever log backed up is full and bulk logged recovery model, VLF becomes inactive.
“ It is an active part of transaction part “

VLF-1 VLF-2 VLF-3 VLF-4 VLF-5


 Log Sequence Number (LSN)
Sequence number used to maintain order and track operations in database. Can be
either Decimal or Hexadecimal. It is concatenation of 3 pieces of information(from Left to Right).

VLF Sequence Number – 4bytes, up to 10 digits without leading zeros.

 Log block Number – 4bytes, 10 digital decimal number with leading zeros.

 Log Record Number – 2bytes, 5 digital decimal number with leading zeros.

Example: In decimal form – 34-0000000156-00065


In hexadecimal form - 22:9C:41
 Log Block
- It is collection of log records that are written to disk together.
- It is comprised of 1 or more log records.
- It can be compared with Data Page within Data File.

“Each block size is integral multiple of 512 bytes, but not more than 64KB total in size”

 Log Record
It is smallest unit within TLF that contains information recorded for activity within
database.

 Min LSN / Start of Logical Log


It is log sequence number of oldest log record that is required for successful database-wide
rollback.

 Truncated VLF
Space occupied by VLF for reuse.

Check point

-- To see how many VLF’s are existing and LSN numbers – “ dbcc loginfo ” --
“ Database cannot work if there is no transaction log; due to SQL Server Design and ACID
properties compliance ”
All Transactions must full fill these characteristics:

Atomicity -
Transaction must be either fully completed or not begun at all.

 Consistency -
Ensuring that the start or end of any transaction the system is in valid state.

 Isolation -
It appears to be the only action that the system is carrying out at a time. No issues in
concurrent access.

 Durability –
Means it has been successfully completed, all the changes it made to the system are
permanent. Recoverability of data even after crash.
“ In Disaster Recovery , Database Restore, Transaction Replication, Standby-Server
Solutions”

Every Transaction is followed by logging into the online transaction log.

Recovery Models:-

Simple – Transaction log backups are not supported, data loss risk, truncation process is automatic,
space is reclaimed for reuse.

Bulk-logged – TL backups are supported and needed on regular basis, No automated truncation,
reduces transaction log space usage.

Fully - TL backups are supported and needed on regular basis, No automated truncation, biggest
chance of transaction log growing because all transactions are logged.
Do we need SSTL backup ?

Ans: YES !

 Important resource when it comes to disaster recovery.

 Mark INACTIVE VLF’s

Backup is not needed only when using simple recovery model – but there is data loss
exposure

Majority of DBA’s uses 15 min’s interval or even less for high traffic SSDB.
“ Database Properties  Files  “
To see ‘ Current size allocated Transaction Log ‘ and ‘ Log space used ’ by all databases
of instance.

“ dbcc sqlperf(logspace) ”

To see Virtual log file(s) details of :

Particular Database -

use <db_name> --On particular database’s Query window


go ( or ) select * from sys.database_files where file_id=2
dbcc loginfo

select * from sys.master_files where file_id=2

To see all the details of the log record an undocumented function

Select * from sys.fn_dblog( null, null)


Step 1: Right click on the Database you want to create Log File, click
properties
Step 2: Select ‘Files’
Step 3: Click ADD to add the log file and mention file name with ‘.ldf’
Can a database have more than one Transaction Log ?

Ans: YES !
But recommended only in specific situations.

 When the first log file is full


 Disk drive is running out of space

NOTE:

1. Either way the problem should be addressed earlier and handled by


creating log backups and monitoring available space on disk drive .

2. Adding multiple log files will not improve performance in fact llel
I/O operations are not possible.
“ It is important task of SS Admin to maintain the space of TLF ”
Monitoring is recommended on daily basis or even more often if
there is high traffic in SSDB.

TLF space can be monitored by -- dbcc sqlperf(logspace)

Log size: current size allocated to the log. Always smaller than original
bcz.. SSDE reserves small space for internal header information

Log Space Used: % of log file currently occupied with TL information

Status: Status of file always ‘0’


“ Transaction Log is cyclic process of writing log record into VLF ”

 When one VLF is full it starts to write to inactive VLF

 If all are full with active transaction it will grow further to create more VLF

 IF it cannot it will through an error – 9002 saying TL for DB is full and Transaction will fail

To know reason why TL cannot be reuse, post the query

Select log_reuse_wait_desc from sys.databases where name=<DB_NAME>


Implicit Explicit
Example: Example:

Begin tran

Insert into <tab_name> values(1,Iam) Insert into <tab_name> values(1,Iam)


Update <tab_name> set sname=‘you’ Update <tab_name> set sname=‘you’
where sno=1 where sno=1
Delete from <tab_name> where sno=1 Delete from <tab_name> where sno=1

End tran or commit


/* NO rollback bcz.. Auto commit */
rollback
Q 1. Why does it work like that ?

Q 2. Why is not change written to the data file immediately ?

Q 3. What is recorded in the Transaction Log ?

Let’s look into a simplified example - - - - - -


Consider 3 different operations to perform on a table

begin, update, commit

Every transaction / operation / record is given a number

VLF 1 VLF 2 VLF 3 VLF 4


LSN 50 LSN 51 LSN 52
Trn 1 Trn 1 Trn 1
begin Update commit

Active log / Logical log

Each log record is stored in VLF in TLF.


The active portion of the log is the section that contains our
transaction.

When more transactions are created, the active portion of log will grow.

VLF 1 VLF 2 VLF 3 VLF 4


LSN 50 LSN 51 LSN 52 LSN53 LSN 54 LSN 55 LSN LSN 57
Trn 1 Trn 1 Trn 1 Trn 2
Trn 2 Trn 2 56 Trn 2
begin Update commit begin insert insert Trn 3 Commit
begin

Active log / Logical log


What happens if check point occurs now ?

- - Changes are written to the data file.


- - A check point record is created.

VLF 1 VLF 2 VLF 3 VLF 4


LSN 50 LSN 51 LSN 52 LSN53 LSN 54 LSN 55 LSN LSN 57 LSN 58
Trn 1 Trn 1 Trn 1 Trn 2
Trn 2 Trn 2 56 Trn 2 Check
begin Update commit begin insert insert Trn 3 Commit point
begin

Check
Active log / Logical log
Point
Changes made by transaction 1, 2, 3 have been written to the data file.

Active portion of the log now spans from LSN 56 to 58, bcz.. Trn 3 has not
been committed.

LSN 50 to 55 are truncated means SS makes VLF 1 and 2 are reusable.

NOTE: Physical size of log is still same.


VLF 1 VLF 2 VLF 3 VLF 4
LSN LSN 57 LSN 58
56 Trn 2 Check
Trn 3 Commit point
begin

Truncated Active log / Logical log


If the database is running ‘ Simple Recovery Database ’

The truncated VLF’s can be reused, as those records are no longer


required.

If the database is running ‘ Full or Bulk –logged recovery models ’

The truncated VLF’s can only be deleted and space is reused when
the TL is backed - up
VLF 1 VLF 2 VLF 3 VLF 4
LSN LSN 57 LSN 58
56 Trn 2 Check
Trn 3 Commi point
begin t

Circulation – reusing the VLF’s


VLF 1 VLF 2 VLF 3 VLF 4
LSN LSN 57 LSN 58
56 Trn 2 Check
Trn 3 Commi point
begin t

Can be Auto growth if space is available in disk driver

Or

Error No – 9002 : saying TL for DB is full and transaction will fail


NOTE:

If the end of logical log reaches the end of physical log the new record
will wrap around the start of logical log.

If the end of logical log meets start of logical log, means log is full
and error no: 9002 will be thrown.
Lazy Writer
Checkpoint

Dirty Page
Transaction Log

Full Recovery
Simple RecoveryMode
Mode
Data file
References from:

 SS HELP Document.
 A beginner’s guide to SQL Server transaction logs – Ivan Stankovic
 SQLBackupRestore.com – YOHZ Software
 Transaction Log Performance Tunning (PPT ) –Chirag Roy (COEO-sr.DBA)

--Srikanth

You might also like