oracle回滚机制深入研究
这篇文章主要描述oracle的回滚机制,篇幅可能较长,因为对于oracle的回滚机制来说,要讨论和描述的实在太多,只能刷选自己认为最有意义的一部分进行深入研究和分享
一、我们来看一个dml语句的处理过程描述
update undotest set object_type='view' where object_type='procedure';
1.检查shared pool中是否存在相同的语句,如果存在,重用执行计划,执行扫描运算,如果不存在,执行硬解析生成执行计划
2.根据执行计划中的扫描运算,检查undotest表中的相关数据块是否存在buffer cache中,如果不存在则读取到内存中
3.检查数据块中符合object_type='procedure'条件的记录,如果没有符合条件的行记录,则结束语句,如果存在则进入下一步
4.以当前模式(current)获取符合object_type='procedure'条件的数据块,准备进行更新
5.在回滚表空间的相应回滚段头的事务表上分配事务槽,这个动作需要记录redo日志
6.从回滚段数据块上创建object_type='procedure'的前映像数据,这个动作也要记录redo日志
7.修改object_type='view' ,这是dml操作的数据变更,而需要记录redo日志
8.用户提交时,在redo日志中记录提交信息,将回滚段头上的事务表和回滚段数据块标记为非活动,清除修改数据块上的事务信息(也可能延迟清除)。同时必须确保整个事务的redp日志写到磁盘上的日志文件
注意:如果最后用户回滚了事务,oracle从回滚段中将前映像数据提取出来,覆盖被更新的数据块。这个回滚动作本身也需要产生redo日志,因此,我们要知道回滚的代价非常昂贵。
二、不同的dml操作,undo block中保存的前映像内容
•insert操作,undo中只需要保存插入记录的rowid,如果需要回退,通过保存的rowid进行删除即可(后面有案例)
•update操作,undo中只需要记录被更新字段的旧值,如果需要回退,只需要通过旧值覆盖更新后的值即可。
•delete操作,undo中必须记录整行的数据,如果需要回退,只需要将这整行的数据重新插入至表中即可。
备注:本文章只对insert操作,undo中保存的内容进行测试,至于update、delete操作,实验方法基本一致,这里就不再累赘
示例
1.开启事务
sql> create table tt as select object_name,object_id from dba_objects;
table created.
sql> insert into tt values('dddddd',1233456); --执行insert操作
1 row created.
2.检查事务信息和其他一些事务信息
sql> select header_file,header_block from dba_segments a where segment_name='tt';
header_file header_block
----------- ------------
4 810
sql> select
2 dbms_rowid.rowid_object(rowid) object_id,
3 dbms_rowid.rowid_relative_fno(rowid) rel_fno,
4 dbms_rowid.rowid_block_number(rowid) blockno,
5 dbms_rowid.rowid_row_number(rowid) rowno,rowid,object_name
6 from tt where object_name='dddddd';
object_id rel_fno blockno rowno rowid
---------- ---------- ---------- ---------- ------------------
object_name
--------------------------------------------------------------------------------
73430 4 1150 0 aaar7waaeaaaar+aaa
dddddd
sql> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec,to_char(start_scnw,'xxxxxxxx') start_scnw,
2 to_char(start_scnb,'xxxxxxxx') start_scnb, start_scnb+start_scnw*power(2,32) start_scn
3 from v$transaction;
xidusn xidslot xidsqn ubablk ubafil ubarec start_scnw start_scnb start_scn
---------- ---------- ---------- ---------- ---------- ---------- -------------------- -------------------- ----------
5 21 960 1466 3 7 0 f5520 1004832
根据上面查询获取的事务信息,dump insert事务undo 段头和undo 块
sql> select name from v$rollname where usn=5;
name
------------------------------
_syssmu5_1527469038$
3.dump undo 段头和undo 块
sql> oradebug setmypid;
statement processed.
sql> oradebug tracefile_name
/u01/oracle/diag/rdbms/test/test/trace/test_ora_604.trc
sql> alter system dump undo header '_syssmu5_1527469038$';
system altered.
sql> alter system dump datafile 3 block 1466;
system altered.
undo blk:
xid: 0x0005.015.000003c0 seq: 0x12c cnt: 0x7 irb: 0x7 icl: 0x0 flg: 0x0000
rec offset rec offset rec offset rec offset rec offset
---------------------------------------------------------------------------
0x01 0x1fa4 0x02 0x1f48 0x03 0x1ecc 0x04 0x1e70 0x05 0x1df4
0x06 0x1d90 0x07 0x1d20
*-----------------------------
* rec #0x7 slt: 0x15 objn: 73430(0x00011ed6) objd: 73430 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x00
undo type: regular undo begin trans last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000ext idx: 0
flg2: 0
*-----------------------------
uba: 0x00c005b9.012c.2d ctl max scn: 0x0000.000f4ed4 prv tx scn: 0x0000.000f4ee0
txn start scn: scn: 0x0000.000f53a9 logon user: 85
prev brb: 12584373 prev bcl: 0
kdo undo record:
ktb redo
op: 0x03 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: z
kdo op code: drp row dependencies disabled --drp操作完成回滚
xtype: xa flags: 0x00000000 bdba: 0x0100047e hdba: 0x0100032a --bdba表示block address hdba 代表sgment header address
itli: 1 ispac: 0 maxfr: 4858
tabn: 0 slot: 0(0x0) ---------这个slot表示这一行数据在数据块中的行地址(行序列)
其中bdba表示file 4,block 1150 ,hdba表示file 4,block 810 与我们上面查询出来的地址相符。
我们可以看到,undo块中并没有直接保存rowid信息,但是oracle完全可以根据上面的几个信息(bdba,slot,objd)定位回滚时需要删除的具体数据,因此对于insert操作,oracle只需保留上述信息,即可完成回滚操作
三、基本概念和名词解释
•uba: undo block address
•rba: redo block address
•dba: data block address
•rdba: root dba
•xid: transaction id
•itl: interested transaction list 保存在数据块的头部(事务信息部分),包含xid,uba,lck,flg等重要信息
transaction identifiers
transaction identifiers (xid) uniquely identify a transaction within the system; they are used within the interested transaction list (itl) of the data block.
a transaction identifier consists of:
•undo segment number 即v$rollname中的usn
•transaction table slot number 对应回滚段头中回滚事务表的第几条记录
•sequence number or wrap#
xid = usn# . slot# . wrap#
undo block address
the undo block address (uba) uniquely identifies the undo block for a given transaction; it is found within the itl of the data block.
a uba consists of:
•data block address (dba) of the block 前映像undo 块地址
•the sequence number of the block 序列号
•the record number within the block undo记录的开始地址(针对该块)
uba = dba. seq#. re
四、从一个dml语句开始研究,session a执行下面的语句,更新完成后先不提交
(1)session a执行更新,更新两条数据,如下
sql> select * from undotest where object_type='procedure' and id in (4703,4704);
id object_id object_name object_type
---------- ---------- -------------------- ---------------
4703 4771 subptxt2 procedure
4704 4772 subptxt procedure
sql> update undotest set object_type='view' where object_type='procedure' and id in (4703,4704);
2 rows updated.
sql> select * from undotest where id in (4703,4704);
id object_id object_name object_type
---------- ---------- -------------------- ---------------
4703 4771 subptxt2 view
4704 4772 subptxt view
(2)查询更新数据块的信息和事务信息
sql> select
2 dbms_rowid.rowid_object(rowid) object_id,
3 dbms_rowid.rowid_relative_fno(rowid) rel_fno,
4 dbms_rowid.rowid_block_number(rowid) blockno,
5 dbms_rowid.rowid_row_number(rowid) rowno,rowid,id,object_name,object_type
6 from dh.undotest where id in (4703,4704);
object_id rel_fno blockno rowno rowid id object_name object_type
---------- ---------- ---------- ---------- ------------------ ---------- -------------------- ---------------
73424 4 195 70 aaar7qaaeaaaaddabg 4703 subptxt2 view
73424 4 195 71 aaar7qaaeaaaaddabh 4704 subptxt view
sql> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec,to_char(start_scnw,'xxxxxxxx') start_scnw,
2 to_char(start_scnb,'xxxxxxxx') start_scnb, start_scnb+start_scnw*power(2,32) start_scn
3 from v$transaction;
xidusn xidslot xidsqn ubablk ubafil ubarec start_scnw start_scnb start_scn
---------- ---------- ---------- ---------- ---------- ---------- -------------------- -------------------- ----------
19 0 4 258 5 2 0 0 0
sql> select name from v$rollname where usn=19;
name
------------------------------
_syssmu19$
(3)刷新内存,dump数据块,undo 段头,undo block,如下
sql> alter system flush buffer_cache;
system altered.
sql> oradebug
sql> oradebug setmypid
statement processed.
sql> oradebug tracefile_name
/u01/oracle/diag/rdbms/test/test/trace/test_ora_902.trc
sql> alter system dump datafile 4 block 195;
system altered.
sql> alter system dump undo header '_syssmu19$';
system altered.
sql> alter system dump datafile 5 block 258;
system altered.
(4)分析数据块dump文件
block header dump: 0x010000c3
object id on block? y
seg/obj: 0x11ed0 csc: 0x00.f2e95 itc: 3 flg: e typ: 1 - data
brn: 1 bdba: 0x10000b8 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.000f2e95
0x02 0x0013.000.00000004 0x01400102.0001.02 ---- 2 fsc 0x000a.00000000
0x03 0x0000.000.00000000 0x00000000.0000.00 ---- 0 fsc 0x0000.00000000
uba中undo record参数为2,即这个事务在这个数据块上的变更(修改)产生的undo记录(变更旧值记录)在对应的undo数据块中的开始地址(开始偏移量)
bdba: 0x010000c3
data_block_dump,data header at 0x2b0a86e2da7c
.....省略一部分..........
tab 0, row 70, @0x45f
tl: 25 fb: --h-fl-- lb: 0x2 cc: 4 ---这一行数据被锁定,锁定该行数据的事务为itl事务槽中的第二个事务。
col 0: [ 3] c2 30 04
col 1: [ 3] c2 30 48
col 2: [ 8] 53 55 42 50 54 58 54 32
col 3: [ 4] 56 49 45 57
tab 0, row 71, @0x447
tl: 24 fb: --h-fl-- lb: 0x2 cc: 4 ---这一行数据被锁定,锁定该行数据的事务为itl事务槽中的第二个事务。
col 0: [ 3] c2 30 05
col 1: [ 3] c2 30 49
col 2: [ 7] 53 55 42 50 54 58 54
col 3: [ 4] 56 49 45 57
.........省略trace 文件中后面的输出.............
(5)分析undo 段头dump文件
********************************************************************************
undo segment: _syssmu19$ (19)
********************************************************************************
extent control header
-----------------------------------------------------------------
............省略中间一部分................
retention table
-----------------------------------------------------------
extent number:0 commit time: 0
extent number:1 commit time: 0
trn ctl:: seq: 0x0001 chd: 0x0001 ctl: 0x0021 inc: 0x00000000 nfb: 0x0000
mgc: 0xb000 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)
uba: 0x01400102.0001.01 scn: 0x0000.000e61e6
这个uba经过测试验证,为事务表中最后(最新)一个事务的对应的undo block上的第一个undo record,即当这个回滚段头存在多个事务时,这个uba表示最新的事务的uba地址,这个地址在整个事务的周期内不发生改变(除非这个回滚段头上又分配了一个新的事务表),因此,这个地址指向的是事务使用的第一个undo block
version: 0x01
free block pool::
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
trn tbl::
index state cflags wrap# uel scn dba parent-xid nub stmt_num cmt
------------------------------------------------------------------------------------------------
0x00 10 0x80 0x0004 0x0000 0x0000.00000000 0x01400102 0x0000.000.00000000 0x00000001 0x00000000 0
0x01 9 0x00 0x0003 0x0002 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x02 9 0x00 0x0003 0x0003 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x03 9 0x00 0x0003 0x0004 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
............省略中间一部分................
0x1f 9 0x00 0x0003 0x0020 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x20 9 0x00 0x0003 0x0021 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x21 9 0x00 0x0003 0xffff 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
回顾我们开始时查询的事务xid信息:xidslot=0(这个事务占用undo事务表中第几号事务槽) and xidseq=4 与上面标红的0x00、state=10(代表事务状态为活动)、wrap#=4 完全相符合。
其中dba列值表示包含这个事务的前映像的undo块地址(事务最后更新的前映像undo地址),与itl事务槽中的uba地址一致 ,即file 5,block 258(可使用dbms_utility.data_block_address_file函数转换)
(6)分析undo block dump文件
undo blk:
xid: 0x0013.000.00000004 seq: 0x1 cnt: 0x2 irb: 0x2 icl: 0x0 flg: 0x0000
rec offset rec offset rec offset rec offset rec offset --undo信息偏移量,在undo块中偏移
---------------------------------------------------------------------------
0x01 0x1f58 0x02 0x1ef4 --当前undo块中的最后一个偏移地址即ox02 与irb一致
其中irb:0x2 表示未提交事务最新(最后执行的)变更对应的undo数据块中的undo record地址,如果开始rollback,这是回滚的起点。
*-----------------------------
* rec #0x1 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004) --根据rec #0x2 中的rci记录找到#0x1
* layer: 11 (row) opc: 1 rci 0x00 --rci指针为0,表示这是事务回滚过程中的最后一条记录
undo type: regular undo begin trans last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000ext idx: 0
flg2: 0
*-----------------------------
uba: 0x00000000.0000.00 ctl max scn: 0x0000.000e61e6 prv tx scn: 0x0000.000e61e6
txn start scn: scn: 0x0000.00000000 logon user: 85
prev brb: 0 prev bcl: 0
kdo undo record:
ktb redo
op: 0x03 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: z
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000c3 hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 70(0x46) flag: 0x2c lock: 0 ckix: 80 --slot表示更新数据的行序列值(rowno),用于定位具体的数据,与我们查询的一致
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45 ---转换为字符为‘procedure’,与我们更新前数据一致
转换方法:
select utl_raw.cast_to_varchar2(replace('50 52 4f 43 45 44 55 52 45',' ')) value from dual;
*-----------------------------
* rec #0x2 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x01 --指向rec# 0x01,即上一个偏移量
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400102.0001.01 --这个uba表示undo chain tx table,即同一update语句中的undo chain地址,这里为1,指向第一个偏移量。一个事务中的不同dml语句,无法通过该参数连接起来(形成undo chain),因为该参数,不能跨越dml语句。
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000c3 hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 71(0x47) flag: 0x2c lock: 0 ckix: 80 --slot表示更新数据的行序列值(rowno),用于定位具体的数据,与我们查询的一致
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45 --与上面描述的一样,转换为字符为‘procedure’,与我们更新前数据一致
rec #0x2 这条undo record中还记录了一个rci参数,表示undo chain,同一个事务中的多次修改,根据这个chain连接起来。
当前rci参数值为0x01,即指向该undo块中的第一个undo record,找到rec #0x01 undo record后,检查发现确实是我们这个update语句中的被更新的另一条数据。
五、在session a中继续进行更新操作,进一步研究
(1)更新两条数据,同时查询基本的事务信息和数据块信息
sql> update undotest set object_type='view' where object_type='procedure' and id in (11504,11505);
2 rows updated.
sql> select
2 dbms_rowid.rowid_object(rowid) object_id,
3 dbms_rowid.rowid_relative_fno(rowid) rel_fno,
4 dbms_rowid.rowid_block_number(rowid) blockno,
5 dbms_rowid.rowid_row_number(rowid) rowno,rowid,id,object_name,object_type
6 from undotest where id in (11504,11505);
object_id rel_fno blockno rowno rowid id object_name object_type
---------- ---------- ---------- ---------- ------------------ ---------- -------------------- ---------------
73424 4 236 41 aaar7qaaeaaaadsaap 11504 dbms_feature_rman_bz procedure
ip2
73424 4 236 42 aaar7qaaeaaaadsaaq 11505 dbms_feature_rman_ba procedure
sic
sql> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec,to_char(start_scnw,'xxxxxxxx') start_scnw,
2 to_char(start_scnb,'xxxxxxxx') start_scnb, start_scnb+start_scnw*power(2,32) start_scn
3 from v$transaction;
xidusn xidslot xidsqn ubablk ubafil ubarec start_scnw start_scnb start_scn
---------- ---------- ---------- ---------- ---------- ---------- -------------------- -------------------- ----------
19 0 4 258 5 4 0 0 0
sql> alter system flush buffer_cache;
system altered.
sql> alter system dump datafile 4 block 195;
system altered.
sql> alter system dump datafile 4 block 236;
system altered.
sql> alter system dump undo header '_syssmu19$';
system altered.
sql> alter system dump datafile 5 block 258;
system altered.
(2)分析数据块dump文件
可以看到,session a事务中第一个update语句修改的数据块(datafile 4 block 195)没有任何改变
block header dump: 0x010000c3
object id on block? y
seg/obj: 0x11ed0 csc: 0x00.f2e95 itc: 3 flg: e typ: 1 - data
brn: 1 bdba: 0x10000b8 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.000f2e95
0x02 0x0013.000.00000004 0x01400102.0001.02 ---- 2 fsc 0x000a.00000000
0x03 0x0000.000.00000000 0x00000000.0000.00 ---- 0 fsc 0x0000.00000000
bdba: 0x010000c3
data_block_dump,data header at 0x2b5f5d4e6a7c
.........省略trace 文件中后面的输出.............
事务中第二个update语句修改的数据块(4,236)上的事务信息与我们查询的一致,如下
block header dump: 0x010000ec
object id on block? y
seg/obj: 0x11ed0 csc: 0x00.f2e95 itc: 3 flg: e typ: 1 - data
brn: 0 bdba: 0x10000e8 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.000f2e95
0x02 0x0013.000.00000004 0x01400102.0001.04 ---- 2 fsc 0x000a.00000000
0x03 0x0000.000.00000000 0x00000000.0000.00 ---- 0 fsc 0x0000.00000000
bdba: 0x010000ec
data_block_dump,data header at 0x2b5f5d4e6a7c
..........省略一部分..........
tab 0, row 41, @0x38f
tl: 42 fb: --h-fl-- lb: 0x2 cc: 4
col 0: [ 4] c3 02 10 05
col 1: [ 4] c3 02 13 03
col 2: [23]
44 42 4d 53 5f 46 45 41 54 55 52 45 5f 52 4d 41 4e 5f 42 5a 49 50 32
col 3: [ 4] 56 49 45 57
tab 0, row 42, @0x365
tl: 42 fb: --h-fl-- lb: 0x2 cc: 4
col 0: [ 4] c3 02 10 06
col 1: [ 4] c3 02 13 04
col 2: [23]
.........省略trace 文件中后面的输出.............
(3)分析undo 段头dump文件
undo segment: _syssmu19$ (19)
********************************************************************************
...............省略一部分..............
trn ctl:: seq: 0x0001 chd: 0x0001 ctl: 0x0021 inc: 0x00000000 nfb: 0x0000
mgc: 0xb000 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)
uba: 0x01400102.0001.01 scn: 0x0000.000e61e6
version: 0x01
free block pool::
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
trn tbl::
index state cflags wrap# uel scn dba parent-xid nub stmt_num cmt
------------------------------------------------------------------------------------------------
0x00 10 0x80 0x0004 0x0000 0x0000.00000000 0x01400102 0x0000.000.00000000 0x00000001 0x00000000 0
0x01 9 0x00 0x0003 0x0002 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x02 9 0x00 0x0003 0x0003 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x03 9 0x00 0x0003 0x0004 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x04 9 0x00 0x0003 0x0005 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x05 9 0x00 0x0003 0x0006 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
.........省略trace 文件中后面的输出.............
undo段头上的事务表没有发生改变
(4)分析undo block dump文件
undo blk:
xid: 0x0013.000.00000004 seq: 0x1 cnt: 0x4 irb: 0x4 icl: 0x0 flg: 0x0000
rec offset rec offset rec offset rec offset rec offset
---------------------------------------------------------------------------
0x01 0x1f58 0x02 0x1ef4 0x03 0x1e98 0x04 0x1e34
*-----------------------------
* rec #0x1 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x00
undo type: regular undo begin trans last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000ext idx: 0
flg2: 0
*-----------------------------
uba: 0x00000000.0000.00 ctl max scn: 0x0000.000e61e6 prv tx scn: 0x0000.000e61e6
txn start scn: scn: 0x0000.00000000 logon user: 85
prev brb: 0 prev bcl: 0
kdo undo record:
ktb redo
op: 0x03 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: z
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000c3 hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 70(0x46) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45
*-----------------------------
* rec #0x2 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x01
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400102.0001.01
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000c3 hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 71(0x47) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45
*-----------------------------
* rec #0x3 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x02
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x03 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: z
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ec hdba: 0x010000aa --bdba:4,236;即我们update语句修改的数据块,与查询出来的信息一致
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 41(0x29) flag: 0x2c lock: 0 ckix: 0
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45 ---转换为字符为‘procedure’,与我们更新前数据一致
*-----------------------------
* rec #0x4 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x03
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400102.0001.03 --新发起的update语句,uba参数值与第一条不同(rec #0x2 undo record记录),事务中的每个dml语句保存自己的uba,用于语句回滚
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ec hdba: 0x010000aa --bdba:4,236;即我们update语句修改的数据块,与查询出来的信息一致
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 42(0x2a) flag: 0x2c lock: 0 ckix: 0
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45 ---转换为字符为‘procedure’,与我们更新前数据一致
end dump data blocks tsn: 6 file#: 5 minblk 258 maxblk 258
六、再次在session a中进行更新,当事务使用的undo block超过一个1个时,分析整个事务的undo record是怎么串联起来。
(1)在session a 中继续更新undotest,并查看事务信息
sql> update undotest set object_type='view' where object_type='table' and rownum<99;
98 rows updated.
sql> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec,to_char(start_scnw,'xxxxxxxx') start_scnw,
2 to_char(start_scnb,'xxxxxxxx') start_scnb, start_scnb+start_scnw*power(2,32) start_scn
3 from v$transaction;
xidusn xidslot xidsqn ubablk ubafil ubarec start_scnw start_scnb start_scn
---------- ---------- ---------- ---------- ---------- ---------- -------------------- -------------------- ----------
19 0 4 259 5 20 0 0 0
ubablk之前为258,现在查询出来为259,因此可以肯定目前事务使用已经超过一个undo block。
(2)dump 段头,undo 块,data block,这里就不再列举出来
sql> alter system flush buffer_cache;
system altered.
sql> alter system dump datafile 5 block 259;
system altered.
sql> alter system dump undo header '_syssmu19$';
system altered.
(3)分析dump 出来的trace 文件
********************************************************************************
undo segment: _syssmu19$ (19)
********************************************************************************
extent control header
-----------------------------------------------------------------
extent header:: spare1: 0 spare2: 0 #extents: 2 #blocks: 15
last map 0x00000000 #maps: 0 offset: 4080
highwater:: 0x01400103 ext#: 0 blk#: 2 ext size: 7
#blocks in seg. hdr's freelists: 0
#blocks below: 0
mapblk 0x00000000 offset: 0
unlocked
map header:: next 0x00000000 #extents: 2 obj#: 0 flag: 0x40000000
extent map
-----------------------------------------------------------------
0x01400101 length: 7
0x01400108 length: 8
retention table
-----------------------------------------------------------
extent number:0 commit time: 0
extent number:1 commit time: 0
trn ctl:: seq: 0x0001 chd: 0x0001 ctl: 0x0021 inc: 0x00000000 nfb: 0x0000
mgc: 0xb000 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)
uba: 0x01400102.0001.01 scn: 0x0000.000e61e6
version: 0x01
free block pool::
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
uba: 0x00000000.0000.00 ext: 0x0 spc: 0x0
trn tbl::
index state cflags wrap# uel scn dba parent-xid nub stmt_num cmt
------------------------------------------------------------------------------------------------
0x00 10 0x80 0x0004 0x0000 0x0000.00000000 0x01400103 0x0000.000.00000000 0x00000002 0x00000000 0
0x01 9 0x00 0x0003 0x0002 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x02 9 0x00 0x0003 0x0003 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
0x03 9 0x00 0x0003 0x0004 0x0000.000e61e6 0x00000000 0x0000.000.00000000 0x00000000 0x00000000 0
.........省略trace 文件中后面的输出.............
可以看到dba的地址已经发生改变,指向最新使用的undo block地址,即datafile 5,block 259
(4)分析undo block trace
1.下面是undo block datafile 5 block 259的dump trace文件
undo blk:
xid: 0x0013.000.00000004 seq: 0x1 cnt: 0x14 irb: 0x14 icl: 0x0 flg: 0x0000
rec offset rec offset rec offset rec offset rec offset
---------------------------------------------------------------------------
0x01 0x1f88 0x02 0x1f28 0x03 0x1ec8 0x04 0x1e68 0x05 0x1e08
0x06 0x1da8 0x07 0x1d48 0x08 0x1ce8 0x09 0x1c88 0x0a 0x1c28
0x0b 0x1bc8 0x0c 0x1b68 0x0d 0x1b08 0x0e 0x1aa8 0x0f 0x1a48
0x10 0x19e8 0x11 0x1988 0x12 0x1928 0x13 0x18c8 0x14 0x1868
*-----------------------------
* rec #0x1 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x00
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x01400102
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400102.0001.52 --当一个事务使用多个undo block时,事务使用的undo块顺序,是根据这个参数串联起来,即undo chain
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ab hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 201(0xc9) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 1
col 3: [ 5] 54 41 42 4c 45
*-----------------------------
* rec #0x2 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x01
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400103.0001.01
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ab hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 205(0xcd) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 1
col 3: [ 5] 54 41 42 4c 45
*-----------------------------
.................省略一部分................
*-----------------------------
* rec #0x13 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x12
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400103.0001.12
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ab hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 239(0xef) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 1
col 3: [ 5] 54 41 42 4c 45
*-----------------------------
* rec #0x14 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004) --这个对应irb参数:0x14,也对应itl中的uba第三个参数:undo record的开始地址。
* layer: 11 (row) opc: 1 rci 0x13
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400103.0001.13
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ab hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 241(0xf1) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 1
col 3: [ 5] 54 41 42 4c 45 ---转化为字符,可以发现与我们更新的旧值相同。符合我们的语气
end dump data blocks tsn: 6 file#: 5 minblk 259 maxblk 259
2.下面是undo block datafile 5 block 258(事务使用的第一个undo block)的dump trace文件
我们可以看到如下内容(省略无用信息),注意undo record中的uba参数
*-----------------------------
* rec #0x4 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x03
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400102.0001.03
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ec hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 42(0x2a) flag: 0x2c lock: 0 ckix: 0
ncol: 4 nnew: 1 size: 5
col 3: [ 9] 50 52 4f 43 45 44 55 52 45
*-----------------------------
* rec #0x5 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x04
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x03 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: z
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ab hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 0(0x0) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 1
col 3: [ 5] 54 41 42 4c 45
rec #0x5 记录时我们最后一个update语句使用的第一个undo记录,我们可以看到,该记录中并没有保存事务中上一条语句使用的uba,因此我们可以知道,undo record中的uba参数无法跨越语句,只能在单个语句中将undo记录串联起来
*-----------------------------
* rec #0x6 slt: 0x00 objn: 73424(0x00011ed0) objd: 73424 tblspc: 4(0x00000004)
* layer: 11 (row) opc: 1 rci 0x05
undo type: regular undo last buffer split: no
temp object: no
tablespace undo: no
rdba: 0x00000000
*-----------------------------
kdo undo record:
ktb redo
op: 0x02 ver: 0x01
compat bit: 4 (post-11) padding: 1
op: c uba: 0x01400102.0001.05
kdo op code: urp row dependencies disabled
xtype: xa flags: 0x00000000 bdba: 0x010000ab hdba: 0x010000aa
itli: 2 ispac: 0 maxfr: 4858
tabn: 0 slot: 2(0x2) flag: 0x2c lock: 0 ckix: 80
ncol: 4 nnew: 1 size: 1
col 3: [ 5] 54 41 42 4c 45
七、一些结论
1.undo段头中trn ctl的uba参数:undo段头的事务表中最后(最新)一个事务的对应的undo block上的第一个undo record,即当这个回滚段头存在多个事务时,这个uba表示最新的事务的uba地址,这个地址在整个事务的周期内不发生改变(除非这个回滚段头上又分配了一个新的事务表),因此,这个地址指向的是事务使用的第一个undo block;
2.undo block中undo record记录中的uba参数:如果事务中的一个dml语句需要使用多个undo record,则这个参数指向的是前一个undo record的记录,通过这个参数可以形成同一个dml语句的undo chain tx table。但是一个事务中的不同dml语句,无法通过该参数连接起来(形成undo chain),因为该参数,不能跨越dml语句。当一个事务使用多个undo block时,事务使用的undo块顺序,是根据这个参数串联起来;
3.数据块头的itl中的uba参数:指向事务在这个数据块上的变更(修改)所使用的undo块中的最后一个undo record偏移量地址,针对这个数据块修改,这个uba是回滚的起点;
4.undo record中的bdba表示block address ,hdba表示 segment header address
5.undo块中不会直接保存rowid信息,但是oracle完全可以undo record中几个信息(bdba,slot,objd),生成数据变更是的具体地址(rowid);
6.一个事务所使用的undo记录顺序是通过undo record中的uba参数、rci参数,undo块上的irb,itl中的uba共同起作用来串联变更的顺序,事务的回滚也是根据这个undo chain按顺序完成。其中最关键的两个参数:undo record中的uba 形成单个dml语句的undo chain,rci参数可以跨越dml语句,可以指向事务中前一个dml语句使用的undo record;
上一篇: Oracle数据文件管理
下一篇: Ai绘制立体效果的弹簧