Oracle資料庫啟動過程驗證檢查點SCN
SCN(System Change Number,系統改變號)是Oracle資料庫非常重要的機制。SCN在資料庫啟動,讀一致性,資料的恢複,FLASHBACK操作等情況下都會用到。
1.SCN號是重要的時間機制,兩者之間可以相互的轉換。
1).將時間轉換為SCN號:
SQL> select timestamp_to_scn(sysdate) from dual;
TIMESTAMP_TO_SCN(SYSDATE)
-------------------------
920939
2).將SCN號轉換為具體時間:
SQL> select scn_to_timestamp(920000) from dual;
SCN_TO_TIMESTAMP(920000)
---------------------------------------------------------------------------
07-FEB-10 01.06.11.000000000 AM
3).查看當前資料庫最新的SCN號:
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
920782
當前最新的SCN號,該SCN號會不停的在記憶體中被重新整理,Oracle 10g每隔3秒就會被重新整理。
2.資料庫啟動過程對SCN號的驗證。
下面我們先來瞭解4種類型的SCN,這4種類型的SCN實際都是檢查點SCN,都是由CKPT進程更新,儲存在控制檔案和資料檔案頭中,不同類型的SCN有不同的作用。
1).資料庫檢查點SCN:
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
920459
資料庫檢查點SCN,每執行一次檢查點,CKPT進程會更新控制檔案中的資料庫檢查點SCN。
2).資料檔案SCN:
SQL> select checkpoint_change# from v$datafile;
CHECKPOINT_CHANGE#
------------------
920459
920459
920459
920459
920459
920459
920459
920459
920459
920459
920459
36 rows selected.
資料檔案SCN,每個資料檔案都有一個資料檔案SCN,每執行一次檢查點,由CKPT進程都將更新位於控制檔案中的資料檔案SCN。
3).資料檔案啟動SCN:
SQL> select checkpoint_change# from v$datafile_header;
CHECKPOINT_CHANGE#
------------------
920459
920459
920459
920459
920459
920459
920459
920459
920459
920459
920459
36 rows selected.
啟動SCN,每個資料檔案都有一個啟動SCN,每執行一次檢查點,CKPT進程都將更新資料檔案頭中的檢查點SCN,這被叫做啟動SCN。
4).資料檔案終止SCN:
SQL> select last_change# from v$datafile;
LAST_CHANGE#
------------
36 rows selected.
終止SCN,每個資料檔案都有一個終止SCN,該SCN儲存在控制檔案中,當資料庫被OPEN之後,該值即變成#ffffff無窮大。
資料庫在啟動過程中對SCN的驗證分為兩個階段:
第一階段,檢查控制檔案和資料檔案中資料檔案的檢查點次數是否一致(不同的資料檔案檢查點次數可能是不同的),如果不一致說明要麼控制檔案過舊,要麼資料檔案過舊,需要執行相應的恢複操作;如果一致繼續執行下一步檢查。
第二階段,檢查資料檔案的啟動SCN和終止SCN是否一致,如果資料庫關閉是非正常關閉,那麼終止SCN肯定是無窮大,這個時候需要完成一個執行個體恢複的過程;如果啟動SCN和終止SCN相同,那麼資料庫就可以OPEN了。
另外,儲存在控制檔案中的資料庫SCN、資料檔案SCN和儲存在資料檔案頭中的啟動SCN實際都是在執行檢查點時由CKPT進程對控制檔案和資料檔案頭的更新,3者必定是相同的,在執行SHUTDOWN IMMEDIATE|NORMAL|TRANSANTIONAL關閉資料庫時會再次觸發檢查點,這時前面3種類型的SCN和儲存在控制檔案中的終止SCN必定是相同的。
下面我們通過對控制檔案和資料檔案頭的轉儲來驗證上面的說法:
1).正常關閉資料庫的狀態。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 599785472 bytes
Fixed Size 2022600 bytes
Variable Size 230687544 bytes
Database Buffers 360710144 bytes
Redo Buffers 6365184 bytes
Database mounted.
SQL> @?/rdbms/admin/gettrace
TRACE_FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle1/admin/orcl/udump/orcl_ora_4132.trc
SQL> alter session set events 'immediate trace name controlf level 8';
Session altered.
完成對控制檔案的轉儲,下面來查看一下orcl_ora_4132.trc追蹤檔案內容:
a).儲存在控制檔案中的資料庫內容:
***************************************************************************
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)
02/19/2014 14:20:20
DB Name "ORCL"
Database flags = 0x50404001 0x00001000
Controlfile Creation Timestamp 02/19/2014 14:20:21
Incmplt recovery scn: 0x0000.00000000
Resetlogs scn: 0x0000.00080634 Resetlogs Timestamp 02/19/2014 14:20:23
Prior resetlogs scn: 0x0000.00000001 Prior resetlogs Timestamp 10/22/2005 21:44:08
Redo Version: compatible=0xa200100
#Data files = 6, #Online files = 6
Database checkpoint: Thread=1 scn: 0x0000.00091eb4
Threads: #Enabled=1, #Open=0, Head=0, Tail=0
......
Max log members = 3, Max data members = 1
Arch list: Head=2, Tail=2, Force scn: 0x0000.00091e72scn: 0x0000.00091e88
Activation ID: 1367751332
Controlfile Checkpointed at scn: 0x0000.00091e88 04/16/2014 13:54:24
thread:0 rba:(0x0.0.0)
......
粗體加紅表示的資料庫檢查點SCN號。
b).儲存在控制檔案中的資料檔案檢查點次數、SCN號和停止SCN號:
***************************************************************************
DATA FILE RECORDS
***************************************************************************
(size = 428, compat size = 428, section max = 100, section in-use = 6,
last-recid= 9, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 11, numrecs = 100)
DATA FILE #1:
(name #7) /u01/app/oracle1/oradata/orcl/system01.dbf
creation size=0 block size=8192 status=0xe head=7 tail=7 dup=1
tablespace 0, index=1 krfil=1 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:183 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.00000008 10/22/2005 21:44:31
thread:0 rba:(0x0.0.0)
cnt表示的是檢查點次數,scn表示的是資料檔案檢查點SCN,Stop scn表示的是資料檔案停止SCN。
DATA FILE #2:
(name #6) /u01/app/oracle1/oradata/orcl/undotbs01.dbf
creation size=0 block size=8192 status=0xe head=6 tail=6 dup=1
tablespace 1, index=2 krfil=2 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:145 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.0007fd42 10/22/2005 22:24:27
thread:0 rba:(0x0.0.0)
DATA FILE #3:
(name #5) /u01/app/oracle1/oradata/orcl/sysaux01.dbf
creation size=0 block size=8192 status=0xe head=5 tail=5 dup=1
tablespace 2, index=3 krfil=3 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:183 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.00001896 10/22/2005 21:44:46
thread:0 rba:(0x0.0.0)
DATA FILE #4:
(name #4) /u01/app/oracle1/oradata/orcl/users01.dbf
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:182 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.000027b9 10/22/2005 21:45:00
thread:0 rba:(0x0.0.0)
DATA FILE #5:
(name #9) /u01/app/oracle1/oradata/orcl/gguser.dbf
creation size=128000 block size=8192 status=0xe head=9 tail=9 dup=1
tablespace 6, index=6 krfil=5 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:135 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.00083314 02/19/2014 14:33:57
thread:1 rba:(0x2.c04b.10)
DATA FILE #6:
(name #10) /u01/app/oracle1/oradata/orcl/ogg2.dbf
creation size=640 block size=8192 status=0x86 head=10 tail=10 dup=1
tablespace 7, index=7 krfil=6 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:3 scn: 0x0000.00089cfb 03/18/2014 13:32:52
Stop scn: 0x0000.00089cfb 03/18/2014 13:32:52
Creation Checkpointed at scn: 0x0000.00089bd3 03/18/2014 13:29:26
thread:1 rba:(0x3.e2.10)
從上面的內容可以看出,每個資料檔案的檢查點次數都是不同;除了DATA FILE 6以外的SCN號不同外,其餘資料檔案都是相同的;每個資料檔案的SCN號與Stop SCN號都是一致的,除了DATA FILE 6以外,其餘的資料檔案SCN、STOP SCN都和前面的資料庫SCN一致。
c).轉儲儲存在資料檔案頭中的資料檔案檢查點次數和啟動SCN:
DATA FILE #1:
(name #7) /u01/app/oracle1/oradata/orcl/system01.dbf
creation size=0 block size=8192 status=0xe head=7 tail=7 dup=1
tablespace 0, index=1 krfil=1 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:183 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.00000008 10/22/2005 21:44:31
DATA FILE #2:
(name #6) /u01/app/oracle1/oradata/orcl/undotbs01.dbf
creation size=0 block size=8192 status=0xe head=6 tail=6 dup=1
tablespace 1, index=2 krfil=2 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:145 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.0007fd42 10/22/2005 22:24:27
DATA FILE #3:
(name #5) /u01/app/oracle1/oradata/orcl/sysaux01.dbf
creation size=0 block size=8192 status=0xe head=5 tail=5 dup=1
tablespace 2, index=3 krfil=3 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:183 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.00001896 10/22/2005 21:44:46
thread:0 rba:(0x0.0.0)
DATA FILE #4:
(name #4) /u01/app/oracle1/oradata/orcl/users01.dbf
creation size=0 block size=8192 status=0xe head=4 tail=4 dup=1
tablespace 4, index=4 krfil=4 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:182 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.000027b9 10/22/2005 21:45:00
thread:0 rba:(0x0.0.0)
DATA FILE #5:
(name #9) /u01/app/oracle1/oradata/orcl/gguser.dbf
creation size=128000 block size=8192 status=0xe head=9 tail=9 dup=1
tablespace 6, index=6 krfil=5 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:135 scn: 0x0000.00091eb4 04/16/2014 13:54:27
Stop scn: 0x0000.00091eb4 04/16/2014 13:54:27
Creation Checkpointed at scn: 0x0000.00083314 02/19/2014 14:33:57
thread:1 rba:(0x2.c04b.10)
DATA FILE #6:
(name #10) /u01/app/oracle1/oradata/orcl/ogg2.dbf
creation size=640 block size=8192 status=0x86 head=10 tail=10 dup=1
tablespace 7, index=7 krfil=6 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:3 scn: 0x0000.00089cfb 03/18/2014 13:32:52
Stop scn: 0x0000.00089cfb 03/18/2014 13:32:52
Creation Checkpointed at scn: 0x0000.00089bd3 03/18/2014 13:29:26
thread:1 rba:(0x3.e2.10)
這裡每個資料檔案檢查點次數和前面從控制檔案轉儲出來的資料檔案檢查點次數是一致的;每個資料檔案的SCN號和Stop SCN與從控制檔案查詢的資料檔案SCN號和STOP SCN號也是一致的。
由於儲存在控制檔案中的資料庫SCN號與除了DATA FILE 6以外的所有資料檔案的SCN、STOP SCN都完全一致(包括儲存在控制檔案和資料檔案頭)。
SQL> select tablespace_name,file_id from dba_data_files where file_id=6;
TABLESPACE_NAME FILE_ID
------------------------------ ----------
OGG2 6
SQL> select tablespace_name,status from dba_tablespaces;
TABLESPACE_NAME STATUS
------------------------------ ---------
SYSTEM ONLINE
UNDOTBS1 ONLINE
SYSAUX ONLINE
TEMP ONLINE
USERS ONLINE
TBS_OGG ONLINE
OGG2 READ ONLY
7 rows selected.
DATA FILE 6不同是因為對應的資料表空間是READ ONLY的狀態,所以資料檔案的SCN不會發生變化。
$ORACLE_HOME/rdbms/admin/gettrace.sql指令碼的內容如下:
[oracle1@RedHat5 admin]$ cat gettrace.sql
SELECT d.value || '/' || lower(rtrim(i.instance, chr(0))) || '_ora_' ||
p.spid || '.trc' trace_file_name
from (select p.spid
from sys.v$mystat m, sys.v$session s, sys.v$process p
where m.statistic# = 1
and s.sid = m.sid
and p.addr = s.paddr) p,
(select t.instance
from sys.v$thread t, sys.v$parameter v
where v.name = 'thread'
and (v.value = 0 or t.thread# = to_number(v.value))) i,
(select value from sys.v$parameter where name = 'user_dump_dest') d;
--end--