ITPub博客

首页 > 数据库 > Oracle > UNDO研究

UNDO研究

Oracle 作者:stockDB 时间:2015-07-06 22:53:41 0 删除 编辑
undo主要提供以下三个作用:
1.提供数据库一致性查询
2.提供数据库恢复
3.rollback事物
SQL> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec from v$transaction;

    XIDUSN    XIDSLOT     XIDSQN     UBABLK     UBAFIL     UBAREC
---------- ---------- ---------- ---------- ---------- ----------
         1         15        889       1105          3         22
可以看到目前存在一个活动事物
现在解释以下v$transaction视图:
V$TRANSACTION lists the active transactions in the system. When the transaction is complete, (either COMMIT or ROLLBACK), the entry should go away.
这个视图显示当前活动的事物,如果这个事物已经提交或者回滚则记录就会消失
XIDUSN   回滚段号
XIDSLOT 就是ITL列表中,slot的号
XIDSQN 就是这个slot使用的次数,因为slot是重复使用的。而这个次数是有使用限制的最大使用次数是2^32 - 1,超过这个次数数据库就会报错
UBABLK    UBA block number
从v$rollstat视图中也可获得事务信息,xacts字段代表的是活动事务的数量,同样看到该事务
SQL> select usn,writes,rssize,xacts,hwmsize,shrinks,wraps from v$rollstat;

       USN     WRITES     RSSIZE      XACTS    HWMSIZE    SHRINKS      WRAPS
---------- ---------- ---------- ---------- ---------- ---------- ----------
         0       5956     385024          0     385024          0          0
         1   11809336    1171456          1   11657216          7         24
         2    6433486    1171456          0    3268608          2         17
         3    6627558    1171456          0   15851520          4         18
         4    5632634    1171456          0    4317184          2         15
         5    5882996    1171456          0    3268608          3         16
         6    8318740    2220032          0   15851520          4         17
         7    6734046    2220032          0    4317184          2         18
         8   10391876    1171456          0   15851520          5         19
         9    5994656    2220032          0    3268608          3         15
        10    5729288    1171456          0    3268608          2         15
        11    6654782    1171456          0    3334144          2         17
查看v$rollname显示出回滚段的名字:
SQL> select * from v$rollname;

       USN NAME
---------- ------------------------------
         0 SYSTEM
         1 _SYSSMU1_1240252155$
         2 _SYSSMU2_111974964$
         3 _SYSSMU3_4004931649$
         4 _SYSSMU4_1126976075$
         5 _SYSSMU5_4011504098$
         6 _SYSSMU6_3654194381$
         7 _SYSSMU7_4222772309$
         8 _SYSSMU8_3612859353$
         9 _SYSSMU9_3945653786$
        10 _SYSSMU10_3271578125$
        11 _SYSSMU11_2740461534$
显示1号回滚段为_SYSSMU1_1240252155$
dump出回滚段的段头信息:
SQL> alter system dump undo header '_SYSSMU1_1240252155$';
SQL> select
  2  d.value||'/'||lower(rtrim(i.instance,
  3  chr(0)))||'_ora_'||p.spid||'.trc' trace_file_name
  4  from ( select p.spid
  5  from sys.v$mystat m,
  6  sys.v$session s,sys.v$process p
  7  where m.statistic# = 1 and s.sid = m.sid and p.addr = s.paddr) p,
  8  ( select t.instance from sys.v$thread  t,sys.v$parameter v
  9  where v.name = 'thread' and
 10  (v.value = 0 or t.thread# = to_number(v.value))) i,
 11  ( select value from sys.v$parameter
 12  where name = 'user_dump_dest') d;

TRACE_FILE_NAME
--------------------------------------------------------------------------------
/oracle/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_9378.trc

查看回滚段的段头信息
[oracle@single ~]$ more /oracle/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_9378.trc
Trace file /oracle/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_9378.trc
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORACLE_HOME = /oracle/app/oracle/product/11.2.0/dbhome_1
System name:    Linux
Node name:      single
Release:        2.6.32-200.13.1.el5uek
Version:        #1 SMP Wed Jul 27 21:02:33 EDT 2011
Machine:        x86_64
VM name:        VMWare Version: 6
Instance name: orcl
Redo thread mounted by this instance: 1
Oracle process number: 23
Unix process pid: 9378, image: oracle@single (TNS V1-V3)


*** 2015-06-14 04:22:20.078
*** SESSION ID:(1251.2197) 2015-06-14 04:22:20.078
*** CLIENT ID:() 2015-06-14 04:22:20.078
*** SERVICE NAME:(SYS$USERS) 2015-06-14 04:22:20.078
*** MODULE NAME:(sqlplus@single (TNS V1-V3)) 2015-06-14 04:22:20.078
*** ACTION NAME:() 2015-06-14 04:22:20.078
 
 
********************************************************************************
Undo Segment:  _SYSSMU1_1240252155$ (1)
********************************************************************************
  Extent Control Header
  -----------------------------------------------------------------
  Extent Header:: spare1: 0      spare2: 0      #extents: 3      #blocks: 143   
                  last map  0x00000000  #maps: 0      offset: 4080  
      Highwater::  0x00c00451  ext#: 2      blk#: 81     ext size: 128   
  #blocks in seg. hdr's freelists: 0     
  #blocks below: 0     
  mapblk  0x00000000  offset: 2     
                   Unlocked
     Map Header:: next  0x00000000  #extents: 3    obj#: 0      flag: 0x40000000
  Extent Map
  -----------------------------------------------------------------
   0x00c00081  length: 7     
   0x00c000e8  length: 8     
   0x00c00400  length: 128   
 
 Retention Table
  -----------------------------------------------------------
 Extent Number:0  Commit Time: 1434190198
 Extent Number:1  Commit Time: 1434191396
 Extent Number:2  Commit Time: 0     
 
  TRN CTL:: seq: 0x00fd chd: 0x0007 ctl: 0x0010 inc: 0x00000000 nfb: 0x0000
            mgc: 0xb000 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)
            uba: 0x00c00451.00fd.16 scn: 0x0000.0015cedb
Version: 0x01
  FREE BLOCK POOL::
    uba: 0x00000000.00fd.15 ext: 0x2  spc: 0x1568  
    uba: 0x00000000.00fd.02 ext: 0x2  spc: 0x1f06  
    uba: 0x00000000.00fd.0b ext: 0x2  spc: 0x17e0  
    uba: 0x00000000.00cf.35 ext: 0x6  spc: 0x1158  
    uba: 0x00000000.0000.00 ext: 0x0  spc: 0x0     
  TRN TBL::
 
  index  state cflags  wrap#    uel         scn            dba            parent-xid    nub     stmt_num    cmt
  ------------------------------------------------------------------------------------------------
   0x00    9    0x00  0x0379  0x0020  0x0000.0015d22d  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223197
   0x01    9    0x00  0x0379  0x0013  0x0000.0015d2f0  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223486
   0x02    9    0x00  0x0379  0x0019  0x0000.0015d0c2  0x00c0044f  0x0000.000.00000000  0x00000001   0x00000000  1434222596
   0x03    9    0x00  0x0377  0x0012  0x0000.0015d3be  0x00c00451  0x0000.000.00000000  0x00000001   0x00000000  1434223798
   0x04    9    0x00  0x0376  0x001b  0x0000.0015cf9d  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222334
   0x05    9    0x00  0x0378  0x000a  0x0000.0015cff2  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222335
   0x06    9    0x00  0x0378  0x0005  0x0000.0015cfe7  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222335
   0x07    9    0x00  0x0378  0x0016  0x0000.0015cf05  0x00000000  0x0000.000.00000000  0x00000000   0x00000000  1434222115
   0x08    9    0x00  0x0379  0x0018  0x0000.0015d008  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222335
   0x09    9    0x00  0x037a  0x001e  0x0000.0015d372  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223738
   0x0a    9    0x00  0x0377  0x0008  0x0000.0015cffd  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222335
   0x0b    9    0x00  0x0378  0x0006  0x0000.0015cfda  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222335
   0x0c    9    0x00  0x0377  0x001c  0x0000.0015cf7a  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222334
   0x0d    9    0x00  0x0378  0x0000  0x0000.0015d1ff  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223137
   0x0e    9    0x00  0x0379  0x0001  0x0000.0015d2da  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223486
   0x0f   10    0x80  0x0379  0x0002  0x0000.0015d469  0x00c00451  0x0000.000.00000000  0x00000001   0x00000000  0
   0x10    9    0x00  0x0379  0xffff  0x0000.0015d435  0x00c00451  0x0000.000.00000000  0x00000001   0x00000000  1434223979
   0x11    9    0x00  0x0379  0x000e  0x0000.0015d2a2  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223378
   0x12    9    0x00  0x0378  0x0010  0x0000.0015d3fc  0x00c00451  0x0000.000.00000000  0x00000001   0x00000000  1434223878
   0x13    9    0x00  0x0379  0x0009  0x0000.0015d321  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223558
   0x14    9    0x00  0x0379  0x0015  0x0000.0015d136  0x00000000  0x0000.000.00000000  0x00000000   0x00000000  1434222776
   0x15    9    0x00  0x0378  0x0017  0x0000.0015d173  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434222896
   0x16    9    0x00  0x0377  0x000c  0x0000.0015cf57  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222295
   0x17    9    0x00  0x0379  0x000d  0x0000.0015d1cb  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223077
   0x18    9    0x00  0x0379  0x001f  0x0000.0015d021  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222356
   0x19    9    0x00  0x0373  0x0014  0x0000.0015d0fb  0x00000000  0x0000.000.00000000  0x00000000   0x00000000  1434222656
   0x1a    9    0x00  0x0378  0x0004  0x0000.0015cf90  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222334
   0x1b    9    0x00  0x0378  0x0021  0x0000.0015cfb1  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222334
   0x1c    9    0x00  0x0377  0x001a  0x0000.0015cf85  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222334
   0x1d    9    0x00  0x0377  0x0002  0x0000.0015d098  0x00c0044f  0x0000.000.00000000  0x00000002   0x00000000  1434222536
   0x1e    9    0x00  0x0378  0x0003  0x0000.0015d394  0x00c00451  0x0000.000.00000000  0x00000002   0x00000000  1434223738
   0x1f    9    0x00  0x0377  0x001d  0x0000.0015d075  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222536
   0x20    9    0x00  0x0378  0x0011  0x0000.0015d265  0x00c00450  0x0000.000.00000000  0x00000001   0x00000000  1434223257
   0x21    9    0x00  0x0378  0x000b  0x0000.0015cfc4  0x00c0044b  0x0000.000.00000000  0x00000001   0x00000000  1434222334
  EXT TRN CTL::
  usn: 1
  sp1:0x00000000 sp2:0x00000000 sp3:0x00000000 sp4:0x00000000
  sp5:0x00000000 sp6:0x7fff00000000 sp7:0x00000000 sp8:0x00000000
  EXT TRN TBL::
  index  extflag    extHash    extSpare1   extSpare2
  ---------------------------------------------------
   0x00  0x00000000 0x00000000 0x00000000  0x00000000
   0x01  0x00000000 0x00000000 0x00000000  0x00000000
   0x02  0x00000000 0x00000000 0x00000000  0x00000000
   0x03  0x00000000 0x00000000 0x00000000  0x00000000
   0x04  0x00000000 0x00000000 0x00000000  0x00000000
   0x05  0x00000000 0x00000000 0x00000000  0x00000000
   0x06  0x00000000 0x00000000 0x00000000  0x00000000
   0x07  0x00000000 0x00000000 0x00000000  0x00000000
   0x08  0x00000000 0x00000000 0x00000000  0x00000000
   0x09  0x00000000 0x00000000 0x00000000  0x00000000
   0x0a  0x00000000 0x00000000 0x00000000  0x00000000
   0x0b  0x00000000 0x00000000 0x00000000  0x00000000
   0x0c  0x00000000 0x00000000 0x00000000  0x00000000
   0x0d  0x00000000 0x00000000 0x00000000  0x00000000
   0x0e  0x00000000 0x00000000 0x00000000  0x00000000
   0x0f  0x00000000 0x00000000 0x00000000  0x00000000
   0x10  0x00000000 0x00000000 0x00000000  0x00000000
   0x11  0x00000000 0x00000000 0x00000000  0x00000000
   0x12  0x00000000 0x00000000 0x00000000  0x00000000
   0x13  0x00000000 0x00000000 0x00000000  0x00000000
   0x14  0x00000000 0x00000000 0x00000000  0x00000000
   0x15  0x00000000 0x00000000 0x00000000  0x00000000
   0x16  0x00000000 0x00000000 0x00000000  0x00000000
   0x17  0x00000000 0x00000000 0x00000000  0x00000000
   0x18  0x00000000 0x00000000 0x00000000  0x00000000
   0x19  0x00000000 0x00000000 0x00000000  0x00000000
   0x1a  0x00000000 0x00000000 0x00000000  0x00000000
   0x1b  0x00000000 0x00000000 0x00000000  0x00000000
   0x1c  0x00000000 0x00000000 0x00000000  0x00000000
   0x1d  0x00000000 0x00000000 0x00000000  0x00000000
   0x1e  0x00000000 0x00000000 0x00000000  0x00000000
   0x1f  0x00000000 0x00000000 0x00000000  0x00000000
   0x20  0x00000000 0x00000000 0x00000000  0x00000000
   0x21  0x00000000 0x00000000 0x00000000  0x00000000
state 为10的就是意味着此为活动事物
   0x0f   10    0x80  0x0379  0x0002  0x0000.0015d469  0x00c00451  0x0000.000.00000000  0x00000001   0x00000000  0

SQL> select to_number('0f','xx') from dual  ;

TO_NUMBER('0F','XX')
--------------------
                  15
正好是占用15号slot
再看这个slot对应的dba(数据块地址)0x00c00451 
DBA代表数据块的存储地址,通过转换DBA计算出文件号和数据块号
SQL> SELECT DBMS_UTILITY.data_block_address_file (
  2  TO_NUMBER (LTRIM ('0x00c00451', '0x'), 'xxxxxxxx'))
  3  AS file_no,
  4  DBMS_UTILITY.data_block_address_block (
  5  TO_NUMBER (LTRIM ('0x00c00451', '0x'), 'xxxxxxxx'))
  6  AS block_no
  7  FROM DUAL;

   FILE_NO   BLOCK_NO
---------- ----------
         3       1105
经过转换后,该前镜像信息位于file 3, block 1105,这和从事务表中查询得的数据完全一致:
SQL> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec from v$transaction;

    XIDUSN    XIDSLOT     XIDSQN     UBABLK     UBAFIL     UBAREC
---------- ---------- ---------- ---------- ---------- ----------
         1         15        889       1105          3         22
和上面的信息正好对应上

在数据块中有指定的事物undo地址,类似于:
 Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x0009.01d.00000181  0x00800546.0129.18  ----    3  fsc 0x0002.00000000
0x02   0x0002.010.00000158  0x00800598.013f.26  C---    0  scn 0x0000.000d4495
其中scn表示提交commit scn,fsc表示快速提交scn
这里存在ITL事务槽信息,ITL事务槽指Interested Transaction List(ITL),事务必须
获得一个ITL事务槽才能进行数据修改,ITL内容主要包括:
Xid---------Transaction ID;
Uba---------Undo Block Address
Lck---------Lock Status
注意:Xid=Undo.segment.number+transaction.table.slot.number+wrap
当select语句要查找的row正好有活动的事物存在,就会根据Uba去查找undo块地址,去构建前镜像
在上面的输出,看到itl1(0x01)上存在活动事务,将xid=0x0009.01d.00000181分解一下
该事务指向的回滚段号是9
SQL> select xidusn,xidslot,xidsqn,ubablk,ubafil,ubarec from v$transaction;

    XIDUSN    XIDSLOT     XIDSQN     UBABLK     UBAFIL     UBAREC
---------- ---------- ---------- ---------- ---------- ----------
         9         29        385       1350          2         22
可以看到,在数据块上同样存在指向回滚段的事务信息.
uba代表的是undo block address,指向具体的回滚段.可以看到ITL上uba=0x00800546.0129.18
将这个UBA进行分解:
0x00800546正是前镜像的数据块地址
seq:0129是顺序号
18是undo记录的开始地址(irb信息)
uba的内容和undo中的信息完全相符:

事物流程如下:
(1)当一个事务开始时,需要在回滚段事务表上分配一个事务槽
(2)在数据块头部获得一个ITL事务槽,该事务槽指向回滚段头的事务槽
(3)在修改数据之前,需要记录前镜像信息,这个信息以undo record的形式存储在回滚段中,
回滚段头事务槽指向该记录.
(4)锁定修改的行,修改行锁定位(lb-lock byte)指向ITL事务槽
(5)数据修改可以进行
这是一个事务的基本过程

块清除(Block Cleanouts)
当用户发出提交(commit)之后,oracle怎样来处理的.oracle是需要写出redo来保证故障时数据可以被
恢复,我们知道oracle并不需要在提交时就写出变更的数据块.那么在提交时,oracle会对数据块进行什么操作?

在事务需要修改数据时,必须分配ITL事务槽,必须锁定行,必须分配回滚段事务槽和回滚表空间来记录要修改
的数据的前镜像.当事务提交时,oracle需要将回滚段上的事务表信息标记为非活动,以便空间可以被重用
那么还有ITL事务信息和锁定信息需要清除,以记录提交.

由于oracle在数据块上存储了ITL和锁定等事务信息,所以oracle必须在事务提交之后清除这些事务数据,
这就是块清除.块清除主要清除的数据有行级锁,ITL信息(包括提交标志,scn等).

如果提交时修改过的数据块仍然在buffer cache中,那么oracle可以清除ITL信息,这种清除叫做快速块清除
(fast block cleanout),快速块清除还有一个限制,当修改的块数量超过buffer cache的10%,则对超出的部
分不再进行快速块清除.

如果提交事务时,修改过的数据块已经被写回到数据文件上(或大量修改超出10%的部分),再次读出该数据块
进行修改,显然成本过于高昂,对于这种情况,oracle选择延迟块清除(delayed block cleanout),等到下一次
访问该block时再来清除ITL锁定信息,这就是延迟块清除.oracle通过延迟块清除来提高数据库的性能,加快
提交操作.快速提交是最普遍的情况.来看一下延迟块清除的处理.
转载地址:http://blog.itpub.net/26015009/viewspace-752342/

UNDO自我机制自我理解:



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30291162/viewspace-1725029/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: UNDO介绍
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2015-06-15

  • 博文量
    35
  • 访问量
    24297