Oracle Block scn/commit scn/cleanout scn 说明

一. 说明

Oracle的一个数据块里的SCN有三种,分别是块头的SCN, CSC (cleanout SCN)和ITL中的commit SCN。 关于block 里的具体信息,可以把block dump 出来,进行查看。 这个在我之前的blog里有整理:

Oracle datafile block 格式 说明

http://blog.csdn.net/tianlesoftware/article/details/6654786

OraceITL(InterestedTransaction List) 说明

http://blog.csdn.net/tianlesoftware/article/details/6573988

Block的cache header部分,记录着一个block scn,它是当前块最后一次变更的时间戳,确切说,这个更新并不是指itl上的scn的最新更新,在接下来delayed block cleanout下的slot重用情况下,可以看到blockscn并不等于itl上的最后一次更新的scn。可以通过dump获得block scn/last itl scn 和发布ora_rowscn语句获得last itl scn。

Oracle ORA_ROWSCN 伪列 说明

http://blog.csdn.net/tianlesoftware/article/details/6658529

Eachdata block in a datafile contains an SCN, which is the SCN at which the lastchange was made to the block. During an incremental backup, RMAN reads the SCNof each data block in the input file and compares it to the checkpoint SCN ofthe parent incremental backup. RMAN reads the entire file every time whether ornot the blocks have been used.

发布transaction后,未提交之前,block scn是不会改变的,对应的itl中也并不做scn记录。Block scn的改变,确切的说不是在发布commit之时(因为有delayed block cleanout的情况存在),而是在transaction对应的itl获得commit scn之时。

cleanout分为2钟,一种是fast commit cleanout,另一种是delayed block cleanout.

oracle有一个modified block list结构,用来记录每个transaction更改过的block,每个transaction大约可以记录10% buffer cache这多的modified block。这部分block就是当发生commit的时候,oracle可以根据modified block list定位到那些块并做fast commit cleanout。如果一个transaction修改的块超过10% buffer cache,那么超过的块就执行delayed block cleanout。

当做fast commit cleanout时,oracle不会清理 Row locks lb标志位,ITL lck标志位。

另一种情况是delayed block cleanout,当transaction还未commit或rollback时modified block已经被写回磁盘,当发生commit时oracle并不会把block重新读入做cleanout,这样成本太高,而是把cleanout留到下一次对此块的dml时来完成。当delayed cleanout时候如果undo segment header的transaction table slot还没有被覆盖,那么可以找回该事务递交的exact scn,如果slot已经被覆盖,那么将会使用undo segment header中的control scn来做为upper bound scn。

当发生fast commit cleanout,系统将transaction提交时刻的scn作为commit scn,更新block上 itl和undo segment header的Transaction table的slot上的 scn,并修改block scn,三者是一致的。

发生delayed block cleanout的时候,之前的transaction commit更新的只是Transaction table,而并未做block上的处理,等待下次使用此block的时候,更新block scn和itl状态。block scn和itl的更新又分2种情况:

(1)当不产生slot重用的时候(ITL不重用), delayed block cleanout时,根据Transaction table里面的信息,更新block scn和itl上的Scn/Fsc为transaction曾经提交时候的scn。
(2)当产生slot重用的时候(重用ITL),更新对应itl上scn为control scn,而block scn 为delayed block cleanout发生时刻的scn。

说明:ITL 中SCN 和 FSC 的区别

dump block的ITL 的信息如下:

Itl Xid Uba Flag Lck Scn/Fsc

0x01 0x000e.007.00000236 0x00000000.0000.00C-U- 0 scn 0x0000.005b1f7f

0x02 0x000c.005.000003b4 0x01401727.0144.13C--- 0 scn 0x0000.005bbf0b

0x03 0x0011.007.00000406 0x0140015b.00c7.57--U- 483 fsc 0x0000.005bdee1

这里的SCN和FSC其实就是这个ITL对应的事务提交时候的SCN,那么这里所有槽位上的最大的一个SCN号就表示这个BLOCK最后被更新的时候的SCN。每一个事务对应一个itl 记录。如果该事务没有涉及延时块清除,那么显示的FSC。 如果是延时块清除(delayed block cleanout),那么显示的就是SCN。

在ITL信息中有一个显示的Flag的状态,FLAG在block中占用1个字节大小。 不同flag 标记的意义如下:

---- = transaction is active, or committedpending cleanout

C--- = transaction has been committed andlocks cleaned out

-B-- = this undo record contains the undofor this ITL entry

--U- = transaction committed (maybe longago); SCN is an upper bound

---T = transaction was still active atblock cleanout SCN

二. 测试

2.1 fast commit cleanout

SYS@anqing2(rac2)> create table fcc(idnumber);

Table created.

SYS@anqing2(rac2)> insert into fccvalues(1);

1 row created.

SYS@anqing2(rac2)> insert into fccvalues(2);

1 row created.

SYS@anqing2(rac2)> commit;

Commit complete.

SYS@anqing2(rac2)> selectdbms_rowid.rowid_block_number(rowid),ora_rowscn from fcc;

DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)ORA_ROWSCN

----------------------------------------------

305906 7262675

305906 7262675

--刚才插入的2条记录都存放在block305906里,并且ora_rowscn也一样。

对fcc 表进行update:

SYS@anqing2(rac2)> update fcc set id=3where id=1;

1 row updated.

SYS@anqing2(rac2)> update fcc set id=4where id=2;

1 row updated.

SYS@anqing2(rac2)> selectdbms_rowid.rowid_block_number(rowid),ora_rowscn from fcc;

DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)ORA_ROWSCN

----------------------------------------------

305906 7262675

305906 7262675

--ora_rowscn 没有变化

SYS@anqing2(rac2)> commit;

Commit complete.

SYS@anqing2(rac2)> selectdbms_rowid.rowid_block_number(rowid),ora_rowscn from fcc;

DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)ORA_ROWSCN

----------------------------------------------

305906 7262794

305906 7262794

--ora_rowscn 已经改变,这个在ora_rowscn 那篇blog里有说明,这个ora_rowscn是从block header SCN 里读取的。

2.2 delayed block cleanout

--.创建一个小undo表空间.

SYS@dave2(db2)> create undo tablespaceundotbs2 datafile '/u01/app/oracle/oradata/dave2/undotbs02.dbf' size 1M;

Tablespace created.

SYS@dave2(db2)> alter system setundo_tablespace='undotbs2';

System altered.

--创建测试表并insertdata

SYS@dave2(db2)> create table dbc(idnumber);

Table created.

SYS@dave2(db2)> insert into dbcvalues(1);

1 row created.

SYS@dave2(db2)> insert into dbc values(2);

1 row created.

SYS@dave2(db2)> commit;

Commit complete.

SYS@dave2(db2)> selectdbms_rowid.rowid_block_number(rowid) block,dbms_rowid.rowid_relative_fno(rowid) fileno, ora_rowscn from dbc;

BLOCKFILENO ORA_ROWSCN

---------- ---------- ----------

115346 1 2147768913

115346 1 2147768913

--update table

SYS@dave2(db2)> update dbc set id=8where id=1;

1 row updated.

SYS@dave2(db2)> update dbc set id=9where id=2;

1 row updated.

--得到XIDUSN,XIDSLOT的使用情况,后续发布重用脚本

SYS@dave2(db2)> selectxidusn,xidslot,xidsqn from v$transaction;

XIDUSN XIDSLOT XIDSQN

---------- ---------- ----------

16 18 5

--刷新buffer cache,如果出现事务递交前modified block就被flush回硬盘,那么将发生delayed block cleanout。

SYS@dave2(db2)> alter system flush buffer_cache;

System altered.
SQL> commit;
Commit complete


--大概的commit scn

SYS@dave2(db2)> selecttimestamp_to_scn(systimestamp) from dual;

TIMESTAMP_TO_SCN(SYSTIMESTAMP)

------------------------------

2147770572

--使用如下脚本,重用XIDUSN 16 XIDLOT 18

/* Formatted on 2011/8/4 15:47:15(QP5 v5.163.1008.3004) */

CREATE TABLE goon

AS

SELECT *

FROMdba_objects

WHERE 1 = 2;

/* Formatted on 2011/8/4 15:45:12(QP5 v5.163.1008.3004) */

CREATE OR REPLACE PROCEDUREproc_go_break_reuse (v_XIDUSN NUMBER,

v_XIDSLOT NUMBER,

v_XIDSQN NUMBER)

/* ————————————————–

Description:It’s used to maketransaction slot reused

—————————————————*/

AS

nsid NUMBER;

TYPEtransaction_record_type IS RECORD

(

XIDUSN NUMBER,

XIDSLOT NUMBER,

XIDSQN NUMBER

);

transaction_recordtransaction_record_type;

BEGIN

SELECT SYS_CONTEXT ('userenv', 'sid') INTO nsid FROM DUAL;

LOOP

INSERT INTO goon

SELECT *

FROMdba_objects

WHERE ROWNUM < 100;

SELECTXIDUSN,XIDSLOT,XIDSQN

INTOtransaction_record

FROMv$transaction a,v$session b

WHERE a.ADDR = b.TADDR AND b.SID = nsid;

IF ( transaction_record.XIDUSN=v_XIDUSN

ANDtransaction_record.XIDSLOT =v_XIDSLOT

ANDtransaction_record.XIDSQN >v_XIDSQN)

THEN

GOTOresue_end;

END IF;

COMMIT;

DELETE FROM goon;

SELECTXIDUSN,XIDSLOT,XIDSQN

INTOtransaction_record

FROMv$transaction a,v$session b

WHERE a.ADDR = b.TADDR AND b.SID = nsid;

IF ( transaction_record.XIDUSN=v_XIDUSN

AND transaction_record.XIDSLOT=v_XIDSLOT

ANDtransaction_record.XIDSQN >v_XIDSQN)

THEN

GOTOresue_end;

END IF;

COMMIT;

END LOOP;

<<resue_end>>

COMMIT;

END;

--调用脚本

SYS@dave2(db2)> execproc_go_break_reuse(16,18,5);

PL/SQL procedure successfully completed.

--产生延时块清除,记录相应scn号

SYS@dave2(db2)> select * from dbc;

ID

----------

8

9

--发生延时块清除时候的大概scn

SYS@dave2(db2)> selecttimestamp_to_scn(systimestamp) from dual;

TIMESTAMP_TO_SCN(SYSTIMESTAMP)

------------------------------

2147771961

--最后一次itl 上的 commit scn

SYS@dave2(db2)> selectdbms_rowid.rowid_block_number(rowid) block,dbms_rowid.rowid_relative_fno(rowid) fileno, ora_rowscn from dbc;

BLOCK FILENO ORA_ROWSCN

---------- ---------- ----------

115346 1 2147771334

115346 1 2147771334

--dump undo header

--查看正在使用的回滚段

SYS@dave2(db2)> selectxidusn,xidslot,xidsqn,ubablk,ubafil,ubarec from v$transaction;

XIDUSN XIDSLOT XIDSQNUBABLK UBAFIL UBAREC

---------- ---------- ---------- -------------------- ----------

13 41 7 42 7 6

XIDUSN: rollback ID

UBABLK: datafile ID

但是这里我们要使用我们当时的undo块,即我们之前查询的XIDUSN=16

SYS@dave2(db2)> select usn,name fromv$rollname where usn=16;

USNNAME

---------- ------------------------------

16 _SYSSMU16$

alter system dump undo header '_SYSSMU13$';

SYS@dave2(db2)> alter system dump undoheader '_SYSSMU16$';

System altered.

SYS@dave2(db2)> oradebug setmypid

Statement processed.

SYS@dave2(db2)> oradebug tracefile_name

/u01/app/oracle/admin/dave2/udump/dave2_ora_11079.trc

TRNCTL:: seq: 0x0003 chd: 0x002a ctl: 0x0029 inc: 0x00000000 nfb: 0x0001

mgc: 0x8201 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)

uba: 0x01c0007a.0003.30 scn: 0x0000.800464f4 --control SCN

SYS@dave2(db2)> select to_number('800464f4','xxxxxxxxxxx')from dual;

TO_NUMBER('800464F4','XXXXXXXXXXX')

-----------------------------------

2147771636

Version: 0x01

FREE BLOCK POOL::

uba: 0x01c0007a.0003.30 ext: 0x2spc: 0x572

uba: 0x00000000.0000.00 ext: 0x0spc: 0x0

uba: 0x00000000.0000.00 ext: 0x0spc: 0x0

uba: 0x00000000.0000.00 ext: 0x0spc: 0x0

uba: 0x00000000.0000.00 ext: 0x0spc: 0x0

TRNTBL::

index state cflags wrap#uel scn dba parent-xid nubstmt_num cmt

------------------------------------------------------------------------------------------------

0x00 9 0x000x0007 0x0001 0x0000.80046548 0x01c000680x0000.000.000000000x00000001 0x00000000 1312472853

0x01 9 0x000x0007 0x0002 0x0000.80046554 0x01c000680x0000.000.000000000x00000001 0x000000001312472853

0x02 9 0x000x0007 0x0003 0x0000.80046560 0x01c000790x0000.000.000000000x00000001 0x00000000 1312472853

0x03 9 0x000x0007 0x0004 0x0000.8004656c 0x01c000790x0000.000.000000000x00000001

--dump block 115346

SYS@dave2(db2)> alter system dumpdatafile 1 block 115346;

System altered.

SYS@dave2(db2)> oradebug tracefile_name

/u01/app/oracle/admin/dave2/udump/dave2_ora_11079.trc

Start dump data blocks tsn: 0 file#: 1minblk 115346 maxblk 115346

buffer tsn: 0 rdba: 0x0041c292 (1/115346)

scn: 0x0000.80046634 seq:0x01 flg: 0x04 tail: 0x66340601

--这个scn 就是blockscn,将 0x0000.80046634转成数字:

SYS@dave2(db2)> select to_number('80046634','xxxxxxxxx')from dual;

TO_NUMBER('80046634','XXXXXXXXX')

---------------------------------

2147771956

该值与前面延时块清除时的SCN:2147771961差不多,所以这里应该是延时块清除的时候的scn。

frmt: 0x02 chkval: 0x2974 type: 0x06=transdata

.....

Block header dump: 0x0041c292

Object id on Block? Y

seg/obj: 0xdf46 csc: 0x00.80046634 itc: 2flg: O typ: 1 - DATA

fsl: 0 fnx: 0x0 ver: 0x01

ItlXid Uba FlagLck Scn/Fsc

0x010x0010.012.000000050x01c0005e.0001.25 C-U- 0scn 0x0000.800463c6

0x020x000f.008.000000050x01c00051.0002.17 C--- 0scn 0x0000.80045e0b

其中ITL 中的XID 格式为:usn#.slot#.wrap#

SYS@dave2(db2)> selectto_number('10','xxxxxxxxxxx') from dual;

TO_NUMBER('10','XXXXXXXXXXX')

-----------------------------

16

SYS@dave2(db2)> select to_number('12','xxxxxxxxxxx')from dual;

TO_NUMBER('12','XXXXXXXXXXX')

-----------------------------

18

SYS@dave2(db2)> selectto_number('00000005','xxxxxxxxxxx') from dual;

TO_NUMBER('00000005','XXXXXXXXXXX')

-----------------------------------

5

和之前V$TRANSACTION查询的一致。

SYS@dave2(db2)> selectto_number('800463c6','xxxxxxxxxxx') from dual;

TO_NUMBER('800463C6','XXXXXXXXXXX')

-----------------------------------

2147771334

--等于最后一次commit的SCN

SYS@dave2(db2)> select to_number('80045e0b','xxxxxxxxxxx')from dual;

TO_NUMBER('80045E0B','XXXXXXXXXXX')

-----------------------------------

2147769867

说明:

在前面提到,当slot重用时,更新对应itl上scn为control scn。 但是我这里的dump undo的control SCN 为2147771636。但itl 的scn为2147771334,是最后一次commit的SCN. 所以说这个测试并没有证明这个结论。实验步骤还有待重新完善。

题外话一句: 结果有时候并不重要,重要的是分析的过程。

参考:http://www.easyora.net/blog/scn_block_scn.html

-------------------------------------------------------------------------------------------------------

Blog: http://blog.csdn.net/tianlesoftware

Weibo: http://weibo.com/tianlesoftware

Email: [email protected]

DBA1 群:62697716(满); DBA2 群:62697977(满)DBA3 群:62697850(满)

DBA 超级群:63306533(满); DBA4 群: 83829929(满)DBA5群: 142216823(满)

DBA6 群:158654907(满) 聊天 群:40132017(满) 聊天2群:69087192(满)

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

你可能感兴趣的:(oracle)