auto undo management

auto undo management是oracle推出代替manual rollback segment,oracle官方的文檔上有介紹AUM的原理和實現方法,今天我們不再重複這些內容,我們來深入一些官方文檔中點到爲止的東西。

undo tablespace size

當創建undo tablespace時最少包含一個初始segment,最多包含十個初始segment,每個初始segment包括2個extent,在9i中的話第一個extent被保留了一個塊,只有7個block可用,10g的話8個塊都可以用。

SQL 9I>select segment_name, blocks, bytes/1024, status,BLOCK_ID,BLOCK_ID+blocks-1 from dba_undo_extents where SEGMENT_NAME=’_SYSSMU1$’;

SEGMENT_NAME BLOCKS BYTES/1024 STATUS BLOCK_ID BLOCK_ID+BLOCKS-1
—————————— ———- ———- ——— ———- —————–
_SYSSMU1$ 7 56 EXPIRED 10 16
_SYSSMU1$ 8 64 EXPIRED 17 24

SQL 10G>select segment_name, blocks, bytes/1024, status,BLOCK_ID,BLOCK_ID+blocks-1 from dba_undo_extents where SEGMENT_NAME=’_SYSSMU1$’;

SEGMENT_NAME BLOCKS BYTES/1024 STATUS BLOCK_ID BLOCK_ID+BLOCKS-1
—————————— ———- ———- ——— ———- —————–
_SYSSMU1$ 8 64 EXPIRED 9 16
_SYSSMU1$ 8 64 EXPIRED 249 256

undo space reuse

每個undo extent可以有三種狀態:

active:有活動事務在此extent上

expired:已結束的事務,undo 信息超過undo_retention時間限制

unexpired:已經結束的事務,undo 信息未達到undo_retention時間限制

當一個事務開始它將會去尋找可用的undo block來存放undo信息,它將按照以下順序請求undo space.

1.先去搜索擁有非active extent的undo segment,如果沒有發現,那麼會去創建新的undo segment,如果空間不夠不能創建,將返回錯誤。過程如下

session 1:

SQL 10G>update testundo set owner=’wanghai’ where rownum<2;

1 row updated.

SQL 10G>select segment_name, blocks, bytes/1024, status,BLOCK_ID,BLOCK_ID+blocks-1
2 from dba_undo_extents
3 where tablespace_name = ‘UNDO_THREE’;

SEGMENT_NAME BLOCKS BYTES/1024 STATUS BLOCK_ID BLOCK_ID+BLOCKS-1
—————————— ———- ———- ——— ———- —————–
_SYSSMU31$ 8 64 UNEXPIRED 9 16
_SYSSMU31$ 8 64 ACTIVE 17 24

session 2:

SQL 10G>update test1 set object_id=object_id where rownum<2;

1 row updated.

SQL 10G>select segment_name, blocks, bytes/1024, status,BLOCK_ID,BLOCK_ID+blocks-1
2 from dba_undo_extents
3 where tablespace_name = ‘UNDO_THREE’;

SEGMENT_NAME BLOCKS BYTES/1024 STATUS BLOCK_ID BLOCK_ID+BLOCKS-1
—————————— ———- ———- ——— ———- —————–
_SYSSMU31$ 8 64 ACTIVE 9 16
_SYSSMU31$ 8 64 ACTIVE 17 24

session 3:

SQL 10G>update test01 set id=id where rownum<2;
update test01 set id=id where rownum<2
*
ERROR at line 1:
ORA-01552: cannot use system rollback segment for non-system tablespace ‘USERS’

擴展數據文件:

SQL 10G>alter database datafile ‘/opt/oracle/oradata/dbtest/undo03f.dbf’ resize 320k;

Database altered.

SQL 10G>select segment_name, blocks, bytes/1024, status,BLOCK_ID,BLOCK_ID+blocks-1
2 from dba_undo_extents
3 where tablespace_name = ‘UNDO_THREE’;

SEGMENT_NAME BLOCKS BYTES/1024 STATUS BLOCK_ID BLOCK_ID+BLOCKS-1
—————————— ———- ———- ——— ———- —————–
_SYSSMU32$ 8 64 EXPIRED 25 32
_SYSSMU32$ 8 64 EXPIRED 33 40
_SYSSMU31$ 8 64 ACTIVE 9 16
_SYSSMU31$ 8 64 ACTIVE 17 24

SQL 10G>update test01 set id=id where rownum<2;

1 row updated.

SQL 10G>select segment_name, blocks, bytes/1024, status,BLOCK_ID,BLOCK_ID+blocks-1
2 from dba_undo_extents
3 where tablespace_name = ‘UNDO_THREE’;

SEGMENT_NAME BLOCKS BYTES/1024 STATUS BLOCK_ID BLOCK_ID+BLOCKS-1
—————————— ———- ———- ——— ———- —————–
_SYSSMU32$ 8 64 ACTIVE 25 32
_SYSSMU32$ 8 64 EXPIRED 33 40
_SYSSMU31$ 8 64 ACTIVE 9 16
_SYSSMU31$ 8 64 ACTIVE 17 24

2.如果有一個undo segment被選中,但是其中free的undo block並不足以存儲該事務的undo 信息,那麼它將嘗試創建extent,如果沒有空間,那麼將會進入下一步。

3.如果創建新extent失敗,它將會搜索其他undo segment中expired extent並重用。

4.如果其他undo segment中沒有expired extent可使用,那麼它會繼續搜索其他undo segment中unexpired extent並重用。

5.如果經過以上嘗試還沒有可用空間,將會返回錯誤。

SQL 10G>update testundo set owner=’wanghai’;
501 rows updated.

SQL 10G>

select UNDOTSN,UNDOBLKS,UNXPSTEALCNT,UNXPBLKRELCNT,UNXPBLKREUCNT,

EXPSTEALCNT,EXPBLKRELCNT,EXPBLKREUCNT,ACTIVEBLKS,UNEXPIREDBLKS,EXPIREDBLKS,

TUNED_UNDORETENTION from V$UNDOSTAT where rownum<2;

UNDOTSN UNDOBLKS UNXPSTEALCNT UNXPBLKRELCNT UNXPBLKREUCNT EXPSTEALCNT EXPBLKRELCNT EXPBLKREUCNT ACTIVEBLKS UNEXPIREDBLKS EXPIREDBLKS TUNED_UNDORETENTION
———- ———- ———— ————- ————- ———– ———— ———— ———- ————- ———– ——————-
10 22 9 0 15 9 0 0 32 0 0 24

出現了steal,也就是reuse了unexpired extent

EXPSTEALCNT:嘗試請求expired extent的次數
EXPBLKREUCNT:實際使用expired block數
UNXPSTEALCNT:嘗試請求unexpired extent的次數
UNXPBLKREUCNT:實際使用unexpired block數

如果沒有expired or unexpired extent可以steal,那麼將會

SQL 10G>update testundo set owner=’wanghai’;
update testundo set owner=’wanghai’
*
ERROR at line 1:
ORA-30036: unable to extend segment by 8 in undo tablespace ‘UNDO_THREE’

從上面這些實驗中我們也可以看出9i的undo_retention並不保證undo block一直會被保持到retention過期,不過10g已經提供了這種保證。

SQL 10G>create undo tablespace UNDO_FOUR
2 datafile ‘/opt/oracle/oradata/dbtest/undo04.dbf’ size 320K reuse RETENTION GUARANTEE;

Tablespace created.

注:以上實驗blocksize全爲8k
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章