資料庫控制檔案中的SCN詳解

DB_SUN發表於2014-03-03

                          

在的啟動和關閉資料庫的時候控制檔案起著重要的作用,從官方文件中我們可以知道控制 檔案中儲存著下列的資訊:

資料庫的名稱及資料庫建立時間等

所有的資料檔案、重做日誌檔案的名稱和位置資訊。

表空間的資訊。

Offline資料檔案的資訊。

重做日誌及歸檔日誌的資訊。

備份集及備份檔案的資訊

檢查點及SCN資訊等。

 

正常關閉資料庫時,控制檔案中的SCN

  在控制檔案的資料檔案資訊部分,對於每一個資料檔案都有一個Checkpoint SCN 和 Stop SCN,在資料庫啟動時要對其進行校驗。

我們知道資料庫在一致性關閉的時候控制檔案中的SCN是怎樣的呢?

1.一致性關閉資料庫。Dump控制檔案

SYS@ENMOEDU > shutdown immediate

SYS@ENMOEDU > startup mount

Database mounted.

SYS@ENMOEDU >  oradebug setmypid;

Statement processed.

SYS@ENMOEDU > oradebug dump controlf 3;

Statement processed.

 

2.資料庫的相關資訊,上次資料庫成功完成檢查點的資訊

***************************************************************************

DATABASE ENTRY

***************************************************************************

 (size = 316, compat size = 316, section max = 1, section in-use = 1,

  last-recid= 0, old-recno = 0, last-recno = 0)

 (extent = 1, blkno = 1, numrecs = 1)

 01/13/2014 16:13:27

 DB Name "ENMOEDU"

 Database flags = 0x00404001 0x00001000

 Controlfile Creation Timestamp  01/13/2014 16:13:28

 Incmplt recovery scn: 0x0000.00000000

 Resetlogs scn: 0x0000.000fc451 Resetlogs Timestamp  02/05/2014 15:44:46

 Prior resetlogs scn: 0x0000.000fa657 Prior resetlogs Timestamp  02/05/2014 14:15:27

 Redo Version: compatible=0xb200000

 #Data files = 6, #Online files = 6

 Database checkpoint: Thread=1 scn: 0x0000.0011b73b

 Threads: #Enabled=1, #Open=0, Head=0, Tail=0

 

 

3.檢查點程式記錄。Low cache rba是恢復的起點,此時是fffffff說明資料庫不需要恢復。On disk rba是恢復的終點。

***************************************************************************

CHECKPOINT PROGRESS RECORDS

***************************************************************************

 (size = 8180, compat size = 8180, section max = 11, section in-use = 0,

  last-recid= 0, old-recno = 0, last-recno = 0)

 (extent = 1, blkno = 2, numrecs = 11)

THREAD #1 - status:0x1 flags:0x0 dirty:0

low cache rba:(0xffffffff.ffffffff.ffff) on disk rba:(0x6.6f7.0)

on disk scn: 0x0000.0011b58f 02/19/2014 21:58:48

resetlogs scn: 0x0000.000fc451 02/05/2014 15:44:46

heartbeat: 839919586 mount id: 99161229

Flashback log tail log# 2 thread# 1 seq 2 block 5337 byte 0

 

4.Redo執行緒記錄中記錄了檢查點的資訊,與資料庫的檢查點一樣

***************************************************************************

REDO THREAD RECORDS

***************************************************************************

 (size = 256, compat size = 256, section max = 8, section in-use = 1,

  last-recid= 0, old-recno = 0, last-recno = 0)

 (extent = 1, blkno = 9, numrecs = 8)

THREAD #1 - status:0xbe thread links forward:0 back:0

 #logs:3 first:1 last:3 current:3 last used seq#:0x6

 enabled at scn: 0x0000.000fc451 02/05/2014 15:44:46

 disabled at scn: 0x0000.00000000 01/01/1988 00:00:00

 opened at 02/19/2014 21:57:43 by instance ENMOEDU

Checkpointed at scn:  0x0000.0011b73b 02/19/2014 21:58:52

 

 

 

 5.資料檔案中記錄的檢查點資訊與DATABASE ENTRY記錄的一樣

***************************************************************************

DATA FILE RECORDS

***************************************************************************

DATA FILE #6: 

  name #13: /u01/app/oracle/oradata/ENMOEDU/sample01.dbf

creation size=12800 block size=8192 status=0xe head=13 tail=13 dup=1

 tablespace 7, index=7 krfil=6 prev_file=0

 unrecoverable scn: 0x0000.001143f4 02/10/2014 15:34:23

 Checkpoint cnt:7 scn: 0x0000.0011b73b 02/19/2014 21:58:52

 Stop scn: 0x0000.0011b73b 02/19/2014 21:58:52

 Creation Checkpointed at scn:  0x0000.00111736 02/10/2014 15:28:26

 thread:1 rba:(0x4.1f5.10)

 enabled  threads:  01000000 00000000 00000000 00000000 00000000 00000000 

 

由於資料庫是一致性關閉,資料檔案中的Checkpoint scn Stop scndatabase scnredo中記錄的scn值都一樣,所以在下次啟動資料庫的時候可以直接啟動,不用進行恢復。

 

 

資料庫異常關閉的時候的SCN的值。

 

1.如果資料庫異常關閉,則不會執行任何的檢查點。通過shutdown abort可以模擬一次異常,當使用shutdown abort方式關閉資料庫時,此時的SCN值是怎麼樣的。

SYS@ENMOEDU > shutdown abort

ORACLE instance shut down.

SYS@ENMOEDU > startup mount

Database mounted.

SYS@ENMOEDU >  oradebug setmypid;

Statement processed.

SYS@ENMOEDU > oradebug dump controlf 3;

Statement processed.

 

2.DATABASE ENTRY中我們可以看到資料庫thread checkpoint資訊。

***************************************************************************

DATABASE ENTRY

***************************************************************************

 (size = 316, compat size = 316, section max = 1, section in-use = 1,

  last-recid= 0, old-recno = 0, last-recno = 0)

 (extent = 1, blkno = 1, numrecs = 1)

 01/13/2014 16:13:27

 DB Name "ENMOEDU"

 Database flags = 0x00404001 0x00001200

 Controlfile Creation Timestamp  01/13/2014 16:13:28

 Incmplt recovery scn: 0x0000.00000000

 Resetlogs scn: 0x0000.000fc451 Resetlogs Timestamp  02/05/2014 15:44:46

 Prior resetlogs scn: 0x0000.000fa657 Prior resetlogs Timestamp  02/05/2014 14:15:27

 Redo Version: compatible=0xb200000

 #Data files = 6, #Online files = 6

 Database checkpoint: Thread=1 scn: 0x0000.0011b73e

 Threads: #Enabled=1, #Open=1, Head=1, Tail=1

 

3. 檢查點資訊其中包含 low chache rba on disk ora資訊。low cache rbarecovery block address)是恢復的起點,on disk rba(確定已經寫到磁碟上的重做值)是恢復的終點,

***************************************************************************

CHECKPOINT PROGRESS RECORDS

***************************************************************************

 (size = 8180, compat size = 8180, section max = 11, section in-use = 0,

  last-recid= 0, old-recno = 0, last-recno = 0)

 (extent = 1, blkno = 2, numrecs = 11)

THREAD #1 - status:0x2 flags:0x0 dirty:510

low cache rba:(0x6.18e2.0) on disk rba:(0x6.ca99.0)

on disk scn: 0x0000.0011e785 02/19/2014 22:55:08

resetlogs scn: 0x0000.000fc451 02/05/2014 15:44:46

heartbeat: 839992040 mount id: 99202971

Flashback log tail log# 3 thread# 1 seq 3 block 2244 byte 0

 

4. Redo執行緒的記錄。

***************************************************************************

REDO THREAD RECORDS

***************************************************************************

 (size = 256, compat size = 256, section max = 8, section in-use = 1,

  last-recid= 0, old-recno = 0, last-recno = 0)

 (extent = 1, blkno = 9, numrecs = 8)

THREAD #1 - status:0xbf thread links forward:0 back:0

 #logs:3 first:1 last:3 current:3 last used seq#:0x6

 enabled at scn: 0x0000.000fc451 02/05/2014 15:44:46

 disabled at scn: 0x0000.00000000 01/01/1988 00:00:00

 opened at 02/19/2014 22:49:27 by instance ENMOEDU

Checkpointed at scn:  0x0000.0011b73e 02/19/2014 22:49:27

 thread:1 rba:(0x6.9ce.10)

 enabled  threads:  01000000 00000000 00000000 00000000 00000000 00000000

 

5. 資料檔案的檢查點資訊;

DATA FILE #6: 

  name #13: /u01/app/oracle/oradata/ENMOEDU/sample01.dbf

creation size=12800 block size=8192 status=0xe head=13 tail=13 dup=1

 tablespace 7, index=7 krfil=6 prev_file=0

 unrecoverable scn: 0x0000.001143f4 02/10/2014 15:34:23

 Checkpoint cnt:8 scn: 0x0000.0011b73e 02/19/2014 22:49:27

 Stop scn: 0xffff.ffffffff 02/19/2014 21:58:52

 Creation Checkpointed at scn:  0x0000.00111736 02/10/2014 15:28:26

 thread:1 rba:(0x4.1f5.10)

 enabled  threads:  01000000 00000000 00000000 00000000 00000000 00000000

 

可以看出由於是異常關閉資料庫,資料庫沒有完成最後的檢查點,資料庫檔案的stop scn仍然設定為最大的ffffffff。由以上資訊可以看出,各部分的checkpoint scn 都是一樣的,但是資料檔案的stop scn不等於checkpoint scn,意味著資料庫的上一次的關閉沒有執行完全檢查點,是異常關閉資料庫,此時啟動資料庫需要進行完全恢復。

 

在進行恢復的時候,從檢查點資訊中讀取low cache rba的值確定日誌檔案的地址。再讀取日誌,從最後的檢查點開始,應用所有的重做記錄,這個過程叫做前滾。資料庫可以被開啟提供訪問和使用,但是此時資料庫中包含已提交的和沒有提交的事務,使用undo段回滾。

 

再對資料庫進行例項恢復的時候,我們從alert日誌中可以看到的資訊。

alter database open

Beginning crash recovery of 1 threads

 parallel recovery started with 2 processes

Started redo scan

Completed redo scan

 read 22747 KB redo, 510 data blocks need recovery

Started redo application at

 Thread 1: logseq 6, block 6370

Recovery of Online Redo Log: Thread 1 Group 3 Seq 6 Reading mem 0

  Mem# 0: /u01/app/oracle/oradata/ENMOEDU/redo03.log

Completed redo application of 3.07MB

Wed Feb 19 23:13:55 2014

Completed crash recovery at

 Thread 1: logseq 6, block 51865, scn 1193381

 510 data blocks read, 493 data blocks written, 22747 redo k-bytes read

Wed Feb 19 23:13:55 2014

 

在例項恢復的時候。 Alter中的logseq 6, block 6370就是日誌中的檢查點中記錄的low cache rba:(0x6.18e2.0) 。恢復完成時的logseq 6, block 51865就是on disk rba:(0x6.ca99.0)。把十六進位制的數換成十進位制。

 

總結:當一致性關閉資料庫的時候,關閉資料庫之前資料庫會執行一個檢查點動作,每個資料檔案的你Stop SCN會被設定為Checkpoint SCN。資料庫不需要恢復。當資料庫是異常關閉的時候,Stop SCN 來不及設定為Checkpoint SCN會是無窮大,在重啟資料庫的時候需要恢復

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29435844/viewspace-1099534/,如需轉載,請註明出處,否則將追究法律責任。

相關文章