How Crash Recovery Works? : Part 2

Continuing where we left off in the last post, we saw why database systems use logs for recovery. Besides logs there are other supporting data structures that are needed for recovery.

Broadly speaking there are three data structures that are part of a recovery mechanism (We’ll discuss the ARIES mechanism here)

  • Log
  • Transaction Table
  • Dirty Page Table

What exactly is a log record?

As we discussed in the post before  log is nothing but a history of actions executed by the database system. It is divided into two parts log tail and log file. Log tail is most recent portion of the log that is kept in main memory and is periodically forced to stable storage (for example, when a transaction commits) Log file is a file stored on stable storage.

The structure of a log can be summarized as shown in the table below

LSN

prevLSN

transID

type

pageID

length

offset

Before-

image

After-

image

 

LSN is log sequence number, generally it is a monotonically increasing unique identifier. prevLSN is the LSN of the previous log record in the transaction, implementing the sequence of logs in a transactions as a linked list. It contains the transaction ID of the transaction and pageID of the page being changed, along with the before and after values.

Based on different type of log records, that we will discuss further certain fields may be missing but typically any log record written on an update should have this format.

Transaction table 

A transaction table can be conceptually depicted as –

TransIDLastLSNStatus

It contains a record for all the active transactions in the system. It contains the transaction id, status of the transaction (in progress, committed or aborted) and LastLSN the LSN of the most recent log record belonging to this transaction.

Dirty Page table

A dirty page is, a page with changes that are not yet written to disk. Dirty Page Table contains one entry for each dirty page and recLSN which is the LSN of the first log record that caused the page to become dirty.

A dirty page table can be conceptually depicted as –

pageIDrecLSNStatus

What do logs look like when a transaction happens in the database?

Consider updating a value in database – 

Transaction T1 changes value of X from ABC to DEF. Suppose the data lives on page P500 at offset 21-23. The system will make an entry into the log, transaction table, and dirty page table.

Log

LSNprevLSNtransIDtypepageIDLengthoffsetBeforeAfter
1T1updateP500
21ABCDEF

Transaction Table

transIDlastLSN
T11

Dirty Page Table

pageIDrecLSN
P5001

Transaction T2 changes ‘HIJ’ to ‘KLM’ on page P600

Log

LSNprevLSNtransIDtypepageIDLengthoffsetBeforeAfter
1T1updateP500
21ABCDEF
2T2updateP600

HIJKLM

Transaction Table

transIDlastLSN
T11
T22

Dirty Page Table

pageIDrecLSN
P5001
P6002

Transaction T2 changes bytes 20 through 22 from GDE to QRS on page P500 and Transaction T1 changes ‘TUV’ to ‘WXY’ on P505

Log

LSNprevLSNtransIDtypepageIDLengthoffsetBeforeAfter
1T1updateP500321ABCDEF
2T2updateP6003
HIJKLM
32T2updateP500320GDEQRS
41T1updateP5053
TUVWXY

Transaction Table

transIDlastLSN
T11
T24

Dirty Page Table

pageIDrecLSN
P5001
P6002
P5054

What happens when you commit a transaction?

If a system issues a  T2 commit, a commit type log record is written to the log. After this the log tail is flushed to disk and the transaction is considered committed. After this additional actions need to be taken like removing entry for T2 from transaction tables, when these actions are complete an end type log record is written to the log.
Log

LSNprevLSNtransIDtypepageIDLengthoffsetBeforeAfter
1T1updateP500321ABCDEF
2T2updateP6003
HIJKLM
32T2updateP500320GDEQRS
41T1updateP5053
TUVWXY
53T2commit
..







75T2end

What happens if you rollback or abort a transaction?

If a system issues a  T1 rollback or if T1 is aborted, the system would read the last log record belonging to T1, that is log record 4, it would then undo the changes made by T1, and write a log record of type “compensation log record” (CLR). CLR in addition to information fields of update records contain another field called undoNextLSN which points to the next record that has to be reversed as a part of the undo process.

LSNprevLSNtransIDtypepageIDLengthoffsetBeforeAfterUndoNextLSN
1T1updateP500321ABCDEF
2T2updateP6003
HIJKLM
32T2updateP500320GDEQRS
41T1updateP5053
TUVWXY
54T1abort
65T1CLRP5053
WXYTUV1
76T1CLRP5003
DEFABCNULL
87T1end

Compensation log records are an integral part of a crash recovery mechanism because with write-ahead logging, it is very much possible that the log undoing the transaction that was aborted is written to stable storage but the actual pages have not been flushed to disk. It is also possible that a system crashes in the middle of recovery, when it has undone few changes of a transaction but not all. In such cases,  during crash recovery CLR contains the information needed to redo the rollback / undo. We will see more about this during the overview of the recovery process.  
In summary a log record is written whenever a page is updated or transaction is committed or aborted. Once a transaction is aborted or rolled back the updates are undone and the undo is written to the log in the form of a CLR. In the next post, I’ll try to unravel the crash recovery process. Till then, So long!

How Crash Recovery Works? : Part 3

How Crash Recovery Works? : Part 1

2 thoughts on “How Crash Recovery Works? : Part 2

Add yours

Leave a comment

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started