为了保证事务的回退和满足多用户的CR,oracle引入了undo机制,由于undo是循环使用的,在一个事务完成过程中,它与redo相互配合,其中undo在一次事务中需要完成以下工作:
(1)Transaction开始前回滚段获取一个ITL(事务槽),分配空间,记录事务信息
(2)Transaction提交后,redo完成记录,同时还清除回滚段的事务信息包括行级锁,ITL信息(commit标志,SCN等)
清除这些事务段的信息的过程就叫做块清除,在完成块清除时,我们本事务修改的数据块就会存在两种可能(1)所有的数据块还保存在buffer cache中,(2)部分数据块或者是全部数据块由于LRU管理已经被刷出了buffer cache。oracle为了考虑到块清除的成本,以及性能,会作以下两种方式的块清除处理:
(1)快速块清除(fast blockcleanout), 当事务修改的数据库全部保存在buffer cache并且修改数据块的数据量没有超过cache buffer 的10%,快速清除事务信息。
(2)延迟块清除(delayed block cleanout)当修改的数据块的阀值超过10% 或者本次事务相关的数据块已经被刷出了buffer cache,oracle会下次访问此block时再清除事务信息。
下面通过一个实验测试,来熟悉一下delayed block cleanout的处理
SQL> select * from v$version ;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 – Production
SQL> show parameter undo;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
SQL> conn gabriel/gabriel
Connected.
SQL> create table test_delayed as select * from user_objects;
Table created.
SQL> select count(1) from test_delayed;
COUNT(1)
----------
3
SQL> update test_delayed set object_id=1 where object_name='TEST_REDO';
1 row updated.
SQL> update test_delayed set object_id=2 where object_name='TEST_DELAYED';
1 row updated.
SQL> -----不提交
查询回滚段信息:
SQL> col segment_name for a20;
select owner,segment_name,SEGMENT_ID,FILE_ID,BLOCK_ID,STATUS
SQL> tablespace_name from dba_rollback_segs;
2
OWNER SEGMENT_NAME SEGMENT_ID FILE_ID BLOCK_ID TABLESPACE_NAME
------ -------------------- ---------- ---------- ---------- ----------------
SYS SYSTEM 0 1 9 ONLINE
PUBLIC _SYSSMU1$ 1 2 9 ONLINE
PUBLIC _SYSSMU2$ 2 2 25 ONLINE
PUBLIC _SYSSMU3$ 3 2 41 ONLINE
PUBLIC _SYSSMU4$ 4 2 57 ONLINE
PUBLIC _SYSSMU5$ 5 2 73 ONLINE
PUBLIC _SYSSMU6$ 6 2 89 ONLINE
PUBLIC _SYSSMU7$ 7 2 105 ONLINE
PUBLIC _SYSSMU8$ 8 2 121 ONLINE
PUBLIC _SYSSMU9$ 9 2 137 ONLINE
PUBLIC _SYSSMU10$ 10 2 153 ONLINE
11 rows selected.
查询事务信息
SQL> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec from v$transaction;
XIDUSN XIDSLOT XIDSQN UBABLK UBAFIL UBAREC
---------- ---------- ---------- ---------- - --------- ----------
6 40 427 99 2 7
查询该活动事务所在的回滚段
SQL> select * from v$rollname where usn = &usn;
Enter value for usn: 6
old 1: select * from v$rollname where usn = &usn
new 1: select * from v$rollname where usn = 6
USN NAME
---------- ------------------------------
6_SYSSMU6$
查询test_delayed对象所在的fileid 和blockid由于数据对象还存在buffer
SQL> select b.segment_name,a.file#,a.dbarfil,a.dbablk,a.class,
2 a.state,decode(bitand(flag,1), 0, 'N', 'Y') DIRTY
3 from x$bh a,dba_extents b
4 where b.RELATIVE_FNO = a.dbarfil
5 and b.BLOCK_ID <= a.dbablk and b.block_id + b.blocks > a.dbablk
6 and b.owner='GABRIEL' and b.segment_name='TEST_DELAYED';
SEGMENT_NAME FILE# DBARFIL DBABLK CLASS STATE D
-------------------- ---------- ---------- ---------- ---------- ---------- -
TEST_DELAYED 8 8 28 1 1 N
TEST_DELAYED 8 8 28 1 3 N
TEST_DELAYED 8 8 28 1 3 N
TEST_DELAYED 8 8 27 4 1 N
TEST_DELAYED 8 8 27 4 3 N
由上可知:x$bh.class= 4 表示为segment header x$bh.state =3为前镜像块,因此file#=8
Dbablk=28为数据块
SQL> alter system dump datafile 8 block 28;
System altered.
SQL> alter system dump undo header '_SYSSMU6$';
System altered.
SQL> alter system dump datafile 2 block 99;
System altered.
SQL> @gettrname.sql
TRACE_FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/admin/gabriel/udump/gabriel_ora_4756.trc
以下为trace文件中的截图部分
Block header dump: 0x0200001c
Object id on Block? Y
seg/obj: 0xcf1a csc: 0x00.f4707 itc: 3 flg: E typ: 1 - DATA
brn: 0 bdba: 0x2000019 ver: 0x01 opc: 0
inc: 0 exflg: 0
Itl Xid Uba Flag Lck Scn/Fsc
0x01 0xffff.000.00000000 0x00000000.0000.00 C--- 0 scn 0x0000.000f4707
0x02 0x0006.028.000001ab 0x00800063.0156.07 ---- 2 fsc 0x0004.00000000
0x03 0x0000.000.00000000 0x00000000.0000.00 ---- 0 fsc 0x0000.00000000
---事务信息存在
tab 0, row 1, @0x1cf8
tl: 76 fb: --H-FL--lb: 0x2 cc: 12
col 0: [ 9] 54 45 53 54 5f 52 45 44 4f
col 1: *NULL*
col 2: [ 2] c1 02
col 3: [ 4] c3 06 1d 2d
col 4: [ 5] 54 41 42 4c 45
col 5: [ 7] 78 6f 06 17 0d 0c 23
col 6: [ 7] 78 6f 06 17 0d 0c 23
col 7: [19] 32 30 31 31 2d 30 36 2d 32 33 3a 31 32 3a 31 31 3a 33 34
col 8: [ 5] 56 41 4c 49 44
col 9: [ 1] 4e
col 10: [ 1] 4e
col 11: [ 1] 4e
tab 0, row 2, @0x1ca9
tl: 79 fb: --H-FL--lb: 0x2 cc: 12
col 0: [12] 54 45 53 54 5f 44 45 4c 41 59 45 44
col 1: *NULL*
col 2: [ 2] c1 03
col 3: [ 4] c3 06 1f 13
col 4: [ 5] 54 41 42 4c 45
col 5: [ 7] 78 6f 08 14 16 02 35
col 6: [ 7] 78 6f 08 14 16 02 35
col 7: [19] 32 30 31 31 2d 30 38 2d 32 30 3a 32 31 3a 30 31 3a 35 32
col 8: [ 5] 56 41 4c 49 44
col 9: [ 1] 4e
col 10: [ 1] 4e
col 11: [ 1] 4e
end_of_block_dump
End dump data blocks tsn: 9 file#: 8 minblk 28 maxblk 28
*** 2011-08-20 23:25:38.403