oracle執行DML(事物過程)的深入研究(二)

接上一節的 oracle執行DML(事物過程)的深入研究(一)

如果要了解事務的等待現象,就需要了解事務發生時,在什麼階段、 對哪些數據、 變更爲什麼形式。 尤其需要了解數據塊和撤銷區域中的數據修改。若利用 Oracle 提供的轉儲功能,就能從物理角度上觀察被事務所修改的數據信息。接下來通過一個簡單的例子進行討論。

我們實驗如下:

 1.修改數據如下:


SQL> update test set owner='HELLO' WHERE ROWNUM=1;  
 
已更新 1 行。  
 
SQL> SELECT rowid,dbms_rowid.rowid_relative_fno(rowid) as fno,dbms_rowid.rowid_block_number(rowid) a  
s blkno from  test where rownum=1;  
 
ROWID                     FNO      BLKNO  
------------------ ---------- ----------  
AAAMuBAAEAAAHMRAAp          4      29457 

 

可以看到修改該的行數據在第4號文件第29457塊上,我們dump其文件內容

SQL> alter system dump datafile 4 block 29457;

系統已更改。

Start dump data blocks tsn: 4 file#: 4 minblk 29457 maxblk 29457
buffer tsn: 4 rdba: 0x01007311 (4/29457)
scn: 0x0000.0045b9bb seq: 0x01 flg: 0x00 tail: 0xb9bb0601
frmt: 0x02 chkval: 0x0000 type: 0x06=trans data
Hex dump of block: st=0, typ_found=1
Dump of memory from 0x07AB2200 to 0x07AB4200
...
Block header dump:  0x01007311
 Object id on Block? Y
 seg/obj: 0xcb81  csc: 0x00.1223d9  itc: 3  flg: E  typ: 1 - DATA
     brn: 0  bdba: 0x1007309 ver: 0x01 opc: 0
     inc: 0  exflg: 0
 
 Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x000a.026.0000023a  0x00000000.0000.00  ----    0  fsc 0x0000.00000000
0x02   0x0001.00d.00000396  0x0080072c.0119.07  --U-   41  fsc 0x1027.001226bd
0x03   0x0006.00b.00000bc6  0x0080005a.04c8.17  ----    1  fsc 0x0001.00000000
 data_block_dump,data header at 0x7ab227c
===============
tsiz: 0x1f80
hsiz: 0x9e
pbl: 0x07ab227c
bdba: 0x01007311
     76543210
flag=--------
ntab=1
nrow=70
frre=-1
fsbo=0x9e
fseo=0x37f
avsp=0x341
tosp=0x13bb
0xe:pti[0] nrow=70 offs=0
...
tab 0, row 41, @0x37f
tl: 96 fb: --H-FL-- lb: 0x3  cc: 13
col  0: [ 5]  48 45 4c 4c 4f
col  1: [23]
 2f 33 64 37 30 62 62 33 36 5f 50 6f 6c 69 63 79 43 68 65 63 6b 65 72
col  2: *NULL*
col  3: [ 4]  c3 02 04 54
col  4: *NULL*
col  5: [ 7]  53 59 4e 4f 4e 59 4d
col  6: [ 7]  78 69 08 1e 0f 11 05
col  7: [ 7]  78 69 08 1e 0f 11 05
col  8: [19]  32 30 30 35 2d 30 38 2d 33 30 3a 31 34 3a 31 36 3a 30 34
col  9: [ 5]  56 41 4c 49 44
col 10: [ 1]  4e
col 11: [ 1]  4e
col 12: [ 1]  4e
tab 0, row 42, @0xe44
tl: 98 fb: --H-FL-- lb: 0x0  cc: 13
col  0: [ 3]  53 59 53
col  1: [24]
 2f 37 36 38 65 65 38 61 30 5f 50 6f 6c 69 63 79 4e 6f 64 65 49 6d 70 6c
col  2: *NULL*
col  3: [ 4]  c3 02 04 55
col  4: *NULL*
col  5: [10]  4a 41 56 41 20 43 4c 41 53 53
col  6: [ 7]  78 69 08 1e 0f 11 05
col  7: [ 7]  78 69 08 1e 0f 11 05
col  8: [19]  32 30 30 35 2d 30 38 2d 33 30 3a 31 34 3a 31 36 3a 30 34
col  9: [ 5]  56 41 4c 49 44
col 10: [ 1]  4e
col 11: [ 1]  4e
col 12: [ 1]  4e
...
end_of_block_dump
End dump data blocks tsn: 4 file#: 4 minblk 29457 maxblk 29457

通過塊轉儲文件的內容,可確認如下事實:

1 ITL 上,已經執行修改的事務在 ITL 上以 itl=0x03 登記。當前狀態是尚未提交的活動的狀態, 因此正在獲得 TX 鎖。將 Xid=0x0006.00b.00000bc6變換爲十 進制就 是USN=6 SLOT=11 SQN=3014 。從 V$TRANSACTION 視圖上確認當前會話相應的信息, 可以發現是完全一致的。

SQL>  select sid from v$mystat where rownum=1;  
 
       SID  
----------  
       137  
 
SQL> select xidusn,xidslot,xidsqn from v$transaction where addr=(select taddr from v$session where s   id=137);  
 
    XIDUSN    XIDSLOT     XIDSQN  
---------- ---------- ----------  
         6         11       3014                        XID=USN + SLOT + SQN  

 

 2 ITL 信息上 Lock=1 ,這代表被事務修改的行數。而且,因爲尚未提交,所以 Flag 上沒有設定任何值。與此相同, SCN 也未被分配。 Flag 值爲 C ”或“ U ”時,就意味着已執行提交。

3 )利用 ITL UBA Undo Block Address )信息,可以掌握當前事務最近使用的撤銷塊和撤銷記錄( Record )的位置。 UBA [Undo Block DBA+Seq#+Record#] 組成,執行回滾時就要利用這些信息。

4 block_row_dump 區域上的第一行的 lb 值被設定爲 0x1 lb Lock Byte 的縮寫,表示當前行掛起鎖的 ITL 編號。也就是說, 1 ITL 修改當前行。 Lock byte 值實際發生提交也不會被清除,最終發生 Delayed block cleanout 或因另外進程修改塊時纔會被清除。

 

事務和撤銷塊轉儲

在上面例子中, 若能轉儲已修改數據塊的事務所使用的回滾段( USN=6 )的頭塊( header block ), 就可以確認事務相應信息是怎樣被管理的。

SQL> select xidusn,xidslot,xidsqn from v$transaction where addr=(select taddr from v$session where s  
id=137);  
 
    XIDUSN    XIDSLOT     XIDSQN  
---------- ---------- ----------  
         6         11       3014                        USN=6,Slot=11  
SQL> select file_id,block_id from dba_rollback_segs where segment_id=6;  
 
   FILE_ID   BLOCK_ID  
---------- ----------  
         2         89

 

dump 2號文件89號數據塊內容
SQL> alter system dump datafile 2 block 89;

系統已更改。

*** 2010-12-14 14:14:11.171
Start dump data blocks tsn: 1 file#: 2 minblk 89 maxblk 89
buffer tsn: 1 rdba: 0x00800059 (2/89)
scn: 0x0000.0045b9bb seq: 0x01 flg: 0x04 tail: 0xb9bb2601
frmt: 0x02 chkval: 0x8562 type: 0x26=KTU SMU HEADER BLOCK
...
  Extent Control Header
  -----------------------------------------------------------------
  Extent Header:: spare1: 0      spare2: 0      #extents: 3      #blocks: 23   
                  last map  0x00000000  #maps: 0      offset: 4080 
      Highwater::  0x0080005a  ext#: 0      blk#: 0      ext size: 7    
  #blocks in seg. hdr's freelists: 0    
  #blocks below: 0    
  mapblk  0x00000000  offset: 0    
                   Unlocked
     Map Header:: next  0x00000000  #extents: 3    obj#: 0      flag: 0x40000000
  Extent Map
  -----------------------------------------------------------------
   0x0080005a  length: 7    
   0x00800121  length: 8    
   0x00800041  length: 8    
 
 Retention Table  和flashback 功能 相關
  -----------------------------------------------------------
 Extent Number:0  Commit Time: 1292304533
 Extent Number:1  Commit Time: 1292302823
 Extent Number:2  Commit Time: 1292304533
 
  TRN CTL:: seq: 0x04c8 chd: 0x0009 ctl: 0x0023 inc: 0x00000000 nfb: 0x0000
            mgc: 0x8201 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)
            uba: 0x0080005a.04c8.17 scn: 0x0000.0045b3cc
Version: 0x01
  FREE BLOCK POOL::
    uba: 0x00000000.04c8.16 ext: 0x0  spc: 0x157e 
    uba: 0x00000000.04c8.03 ext: 0x0  spc: 0x1eb8 
    uba: 0x00000000.04bc.14 ext: 0x2  spc: 0x5c2  
    uba: 0x00000000.0473.20 ext: 0x1  spc: 0x1012 
    uba: 0x00000000.0000.00 ext: 0x0  spc: 0x0    
  TRN TBL::   事物表
 
  index  state cflags  wrap#    uel         scn            dba            parent-xid    nub     stmt_num    cmt
  ------------------------------------------------------------------------------------------------
   ...
   0x0a    9    0x00  0x0bc7  0x001a  0x0000.0045b492  0x00800045  0x0000.000.00000000  0x00000001   0x00000000  1292303166

   0x0b   10    0x80  0x0bc6  0x0000  0x0000.0045b96e  0x0080005a  0x0000.000.00000000  0x00000001   0x00000000  0

   0x0c    9    0x00  0x0bc5  0x0029  0x0000.0045b55c  0x00800045  0x0000.000.00000000  0x00000001   0x00000000  1292303295
   ...
End dump data blocks tsn: 1 file#: 2 minblk 89 maxblk 89

 

然後我們執行commit操作。

SQL> commit;

提交完成。

從新dump 數據塊內容。
SQL> alter system dump datafile 4 block 29457;

系統已更改。

Start dump data blocks tsn: 4 file#: 4 minblk 29457 maxblk 29457
buffer tsn: 4 rdba: 0x01007311 (4/29457)
scn: 0x0000.0045c06a seq: 0x01 flg: 0x02 tail: 0xc06a0601
frmt: 0x02 chkval: 0x0000 type: 0x06=trans data
...
Block header dump:  0x01007311
 Object id on Block? Y
 seg/obj: 0xcb81  csc: 0x00.1223d9  itc: 3  flg: E  typ: 1 - DATA
     brn: 0  bdba: 0x1007309 ver: 0x01 opc: 0
     inc: 0  exflg: 0
 
 Itl           Xid                  Uba         Flag  Lck        Scn/Fsc
0x01   0x000a.026.0000023a  0x00000000.0000.00  ----    0  fsc 0x0000.00000000
0x02   0x0001.00d.00000396  0x0080072c.0119.07  --U-   41  fsc 0x1027.001226bd
0x03   0x0006.00b.00000bc6  0x0080005a.04c8.17  --U-    1  fsc 0x0001.0045c06a    -FLAG 變爲 U(upper bound commit)提交狀態
 
data_block_dump,data header at 0x7ab227c
===============
tsiz: 0x1f80
hsiz: 0x9e
pbl: 0x07ab227c
bdba: 0x01007311
     76543210
flag=--------
ntab=1
nrow=70
frre=-1
fsbo=0x9e
fseo=0x37f
avsp=0x341
tosp=0x13bb

tab 0, row 41, @0x37f
tl: 96 fb: --H-FL-- lb: 0x3  cc: 13
col  0: [ 5]  48 45 4c 4c 4f
col  1: [23]
 2f 33 64 37 30 62 62 33 36 5f 50 6f 6c 69 63 79 43 68 65 63 6b 65 72
col  2: *NULL*
col  3: [ 4]  c3 02 04 54
col  4: *NULL*
col  5: [ 7]  53 59 4e 4f 4e 59 4d
col  6: [ 7]  78 69 08 1e 0f 11 05
col  7: [ 7]  78 69 08 1e 0f 11 05
col  8: [19]  32 30 30 35 2d 30 38 2d 33 30 3a 31 34 3a 31 36 3a 30 34
col  9: [ 5]  56 41 4c 49 44
col 10: [ 1]  4e
col 11: [ 1]  4e
col 12: [ 1]  4e
tab 0, row 42, @0xe44
tl: 98 fb: --H-FL-- lb: 0x0  cc: 13
col  0: [ 3]  53 59 53
col  1: [24]
 2f 37 36 38 65 65 38 61 30 5f 50 6f 6c 69 63 79 4e 6f 64 65 49 6d 70 6c
col  2: *NULL*
col  3: [ 4]  c3 02 04 55
col  4: *NULL*
col  5: [10]  4a 41 56 41 20 43 4c 41 53 53
col  6: [ 7]  78 69 08 1e 0f 11 05
col  7: [ 7]  78 69 08 1e 0f 11 05
col  8: [19]  32 30 30 35 2d 30 38 2d 33 30 3a 31 34 3a 31 36 3a 30 34
col  9: [ 5]  56 41 4c 49 44
col 10: [ 1]  4e
col 11: [ 1]  4e
col 12: [ 1]  4e
end_of_block_dump
End dump data blocks tsn: 4 file#: 4 minblk 29457 maxblk 29457

dump undo數據塊的內容
SQL> alter system dump datafile 2 block 89;

系統已更改。

Start dump data blocks tsn: 1 file#: 2 minblk 89 maxblk 89
buffer tsn: 1 rdba: 0x00800059 (2/89)
scn: 0x0000.0045c156 seq: 0x02 flg: 0x00 tail: 0xc1562602
frmt: 0x02 chkval: 0x0000 type: 0x26=KTU SMU HEADER BLOCK

  Extent Control Header
  -----------------------------------------------------------------
  Extent Header:: spare1: 0      spare2: 0      #extents: 3      #blocks: 23   
                  last map  0x00000000  #maps: 0      offset: 4080 
      Highwater::  0x0080005b  ext#: 0      blk#: 1      ext size: 7    
  #blocks in seg. hdr's freelists: 0    
  #blocks below: 0    
  mapblk  0x00000000  offset: 0    
                   Unlocked
     Map Header:: next  0x00000000  #extents: 3    obj#: 0      flag: 0x40000000
  Extent Map
  -----------------------------------------------------------------
   0x0080005a  length: 7    
   0x00800121  length: 8    
   0x00800041  length: 8    
 
 Retention Table
  -----------------------------------------------------------
 Extent Number:0  Commit Time: 1292304533
 Extent Number:1  Commit Time: 1292302823
 Extent Number:2  Commit Time: 1292304533
 
  TRN CTL:: seq: 0x04c8 chd: 0x000a ctl: 0x0011 inc: 0x00000000 nfb: 0x0001
            mgc: 0x8201 xts: 0x0068 flg: 0x0001 opt: 2147483646 (0x7ffffffe)
            uba: 0x0080005b.04c8.0d scn: 0x0000.0045b480
Version: 0x01
  FREE BLOCK POOL::
    uba: 0x0080005b.04c8.0e ext: 0x0  spc: 0x18d2 
    uba: 0x00000000.04c8.03 ext: 0x0  spc: 0x1eb8 
    uba: 0x00000000.04bc.14 ext: 0x2  spc: 0x5c2  
    uba: 0x00000000.0473.20 ext: 0x1  spc: 0x1012 
    uba: 0x00000000.0000.00 ext: 0x0  spc: 0x0    
  TRN TBL::
 
  index  state cflags  wrap#    uel         scn            dba            parent-xid    nub     stmt_num    cmt
  ------------------------------------------------------------------------------------------------
   0x0b    9    0x00  0x0bc6  0x0009  0x0000.0045c06a  0x0080005a  0x0000.000.00000000  0x00000001   0x00000000  1292307799
  
End dump data blocks tsn: 1 file#: 2 minblk 89 maxblk 89

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章