2. The Recovery Process
When SQL Server is started, it verifies that
completed transactions recorded in the log are reflected in the data and
that incomplete transactions whose changes are reflected in the data
are rolled back out of the database. This is the recovery process.
Recovery is an automatic process performed on each database during SQL
Server startup. Recovery must be completed before the database is made
available for use.
The recovery process guarantees that all completed
transactions recorded in the transaction log are reflected in the data
and all incomplete transactions reflected in the data are rolled back.
During recovery, SQL Server looks for the last checkpoint record in the
log. Only
the changes that occurred or were still open since the last checkpoint
need to be examined to determine the need for any transactions to be
redone (that is, rolled forward) or undone (that is, rolled back). After
all the changes are rolled forward or rolled back, as necessary, the
database is checkpointed, and recovery is complete.
The recovery algorithm has three phases centered around the last checkpoint record in the transaction log, as shown in Figure 4.
These phases are as follows:
Analysis phase—
SQL Server reads forward from the last checkpoint record in the
transaction log. This pass identifies a list of pages (the dirty page
table [DPT]) that might have been dirty at the time of the system crash
or when SQL Server was shut down, as well as a list of the uncommitted
transactions at the time of the crash.
Redo (roll-forward) phase—
During this phase, SQL Server rolls forward all the committed
transactions recorded in the log since the last checkpoint. This phase
returns the database to the state it was in at the time of the crash.
The starting point for the redo pass is the LSN of the oldest committed
transaction within the DPT, so that only changes not previously
checkpointed (only the committed dirty pages) are reapplied.
Undo (rollback) phase—
This phase moves backward from the end of the log to the oldest active
transaction at the time of the system crash or shutdown. All
transactions that were not committed at the time of the crash but had
pages written to the database are undone so that none of their changes
are actually reflected in the database.
Now let’s examine the transactions in the log in Figure 31.4 and determine how they will be handled during the recovery process:
Transaction T1 is started and committed prior to the last checkpoint. No recovery is necessary.
Transaction
T2 started before the last checkpoint but had not completed at the time
of the system crash. The changes written out by the checkpoint process
for this transaction have to be rolled back.
Transaction
T3 started before the last checkpoint was issued and committed after
that checkpoint but prior to the system crash. The changes made to the
data after the checkpoint need to be rolled forward.
Transaction T4 started and committed after the last checkpoint. This entire transaction needs to be rolled forward.
Transaction
T5 started after the last checkpoint, but no changes to the data were
recorded in the log, so no data changes were written to the data.
(Remember that changes must be written to the log before they can be
written to the data.) No undo action is required for this transaction.
In a nutshell, this type of analysis is pretty much
the same analysis the recovery process would do. To identify the number
of transactions rolled forward or rolled back during recovery, you can
examine the SQL Server error log and look at the recovery startup
messages for each database. Following is a sample fragment of the
recovery messages you might see in the SQL Server error log:
2009-05-22 10:37:04.440 spid15s Starting up database 'tempdb'.
2009-05-22 10:37:04.630 spid18s Starting up database 'msdb'.
2009-05-22 10:37:04.640 spid19s Starting up database 'sample'.
2009-05-22 10:37:04.660 spid20s Starting up database 'AdventureWorks'.
2009-05-22 10:37:04.670 spid21s Starting up database 'bigpubs2008'.
2009-05-22 10:37:05.140 spid20s 1 transactions rolled forward in database
'AdventureWorks' (6). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.150 spid19s 1 transactions rolled forward in database
'sample' (5). This is an informational message only. No user action is required.
2009-05-22 10:37:05.240 spid19s 0 transactions rolled back in database
'AdventureWorks' (6). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.260 spid20s 0 transactions rolled back in database
'sample' (5). This is an informational message only. No user action is required.
2009-05-22 10:37:05.260 spid19s Recovery is writing a checkpoint in database
'AdventureWorks' (6). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.300 spid20s Recovery is writing a checkpoint in database
'sample' (5). This is an informational message only. No user action is required.
2009-05-22 10:37:05.340 spid18s 50 transactions rolled forward in database
'msdb' (4). This is an informational message only. No user action is required.
2009-05-22 10:37:05.350 spid19s Starting up database 'AdventureWorksDW'.
2009-05-22 10:37:05.460 spid20s 0 transactions rolled back in database 'msdb'
(4). This is an informational message only. No user action is required.
2009-05-22 10:37:05.470 spid20s Recovery is writing a checkpoint in database
'msdb' (4). This is an informational message only. No user action is required.
2009-05-22 10:37:05.480 spid20s Starting up database 'AdventureWorksLT'.
2009-05-22 10:37:05.520 spid21s 1 transactions rolled forward in database
'bigpubs2008' (7). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.610 spid7s 1 transactions rolled back in database
'bigpubs2008' (7). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.610 spid7s Recovery is writing a checkpoint in database
'bigpubs2008' (7). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.630 spid19s 1 transactions rolled forward in database
'AdventureWorksDW' (8). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.680 spid18s 0 transactions rolled back in database
'AdventureWorksDW' (8). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.700 spid18s Recovery is writing a checkpoint in database
'AdventureWorksDW' (8). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.740 spid20s 1 transactions rolled forward in database
'AdventureWorksLT' (9). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.760 spid20s 0 transactions rolled back in database
'AdventureWorksLT' (9). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.770 spid20s Recovery is writing a checkpoint in database
'AdventureWorksLT' (9). This is an informational message only. No user action is
required.
2009-05-22 10:37:05.770 spid18s Starting up database 'AdventureWorks2008'.
2009-05-22 10:37:05.780 spid19s Starting up database 'AdventureWorksDW2008'.
2009-05-22 10:37:05.790 spid21s Starting up database 'AdventureWorksLT2008'.
2009-05-22 10:37:05.900 spid19s 1 transactions rolled forward in database
'AdventureWorksDW2008' (11). This is an informational message only. No user action
is required.
2009-05-22 10:37:05.910 spid21s 1 transactions rolled forward in database
'AdventureWorksLT2008' (12). This is an informational message only. No user action
is required.
2009-05-22 10:37:05.940 spid18s 9 transactions rolled forward in database
'AdventureWorks2008' (10). This is an informational message only. No user action
is required.
2009-05-22 10:37:05.950 spid7s 0 transactions rolled back in database
'AdventureWorksDW2008' (11). This is an informational message only. No user action
is required.
2009-05-22 10:37:05.950 spid19s 0 transactions rolled back in database
'AdventureWorksLT2008' (12). This is an informational message only. No user action
is required.
2009-05-22 10:37:05.960 spid7s Recovery is writing a checkpoint in database
'AdventureWorksDW2008' (11). This is an informational message only. No user action
is required.
2009-05-22 10:37:05.970 spid19s Recovery is writing a checkpoint in database
'AdventureWorksLT2008' (12). This is an informational message only. No user action
is required.
2009-05-22 10:37:06.530 spid19s 0 transactions rolled back in database
'AdventureWorks2008' (10). This is an informational message only. No user action
is required.
2009-05-22 10:37:06.630 spid19s Recovery is writing a checkpoint in database
'AdventureWorks2008' (10). This is an informational message only. No user action
is required.
2009-05-22 10:37:08.220 spid19s Recovery completed for database
AdventureWorks2008 (database ID 10) in 1 second(s) (analysis 73 ms, redo 1 ms, undo
572 ms.) This is an informational message only. No user action is required.
2009-05-22 10:37:08.250 spid7s Recovery is complete. This is an informational
message only. No user action is required.