UNDO Segment深入解析
Undo Segment深入解析
在undo自动管理时,设置了undo_retention以后,undo块就存在四种状态。
Active:表示正在使用该undo的事务还没有提交或回滚。
Inactive:表示该undo上没有活动的事务,该状态的undo可以被其他事务覆盖。
Expired:表示该undo持续inactive的时间超过undo_retention所指定的时间。
Freed:表示该undo块内容是空的,从来没有被使用过。
Undo Retention After a transaction is committed, undo data is no longer needed for rollback or transaction recovery purposes. However, for consistent read purposes, long-running queries may require this old undo information for producing older p_w_picpaths of data blocks. Furthermore, the success of several Oracle Flashback features can also depend upon the availability of older undo information. For these reasons, it is desirable to retain the old undo information for as long as possible.When automatic undo management is enabled, there is always a current undo retention period, which is the minimum amount of time that Oracle Database attempts to retain old undo information before overwriting it. Old (committed) undo information that is older than the current undo retention period is said to be expired. Old undo information with an age that is less than the current undo retention period is said to be unexpired.Oracle Database automatically tunes the undo retention period based on undo tablespace size and system activity. You can specify a minimum undo retention period (in seconds) by setting the UNDO_RETENTION initialization parameter. The database makes its best effort to honor the specified minimum undo retention period, provided that the undo tablespace has space available for new transactions. When available space for new transactions becomes short, the database begins to overwrite expired undo. If the undo tablespace has no space for new transactions after all expired undo is overwritten, the database may begin overwriting unexpired undo information. If any of this overwritten undo information is required for consistent read in a current long-running query, the query could fail with the snapshot too old error message.The following points explain the exact impact of the UNDO_RETENTION parameter on undo retention:The UNDO_RETENTION parameter is ignored for a fixed size undo tablespace. The database may overwrite unexpired undo information when tablespace space becomes low.For an undo tablespace with the AUTOEXTEND option enabled, the database attempts to honor the minimum retention period specified by UNDO_RETENTION. When space is low, instead of overwriting unexpired undo information, the tablespace auto-extends. If the MAXSIZE clause is specified for an auto-extending undo tablespace, when the maximum size is reached, the database may begin to overwrite unexpired undo information.Automatic Tuning of Undo RetentionOracle Database automatically tunes the undo retention period based on how the undo tablespace is configured.If the undo tablespace is fixed size, the database tunes the retention period for the best possible undo retention for that tablespace size and the current system load. This tuned retention period can be significantly greater than the specified minimum retention period.If the undo tablespace is configured with the AUTOEXTEND option, the database tunes the undo retention period to be somewhat longer than the longest-running query on the system at that time. Again, this tuned retention period can be greater than the specified minimum retention period.Note:Automatic tuning of undo retention is not supported for LOBs. This is because undo information for LOBs is stored in the segment itself and not in the undo tablespace. For LOBs, the database attempts to honor the minimum undo retention period specified by UNDO_RETENTION. However, if space becomes low, unexpired LOB undo information may be overwritten.You can determine the current retention period by querying the TUNED_UNDORETENTION column of the V$UNDOSTAT view. This view contains one row for each 10-minute statistics collection interval over the last 4 days. (Beyond 4 days, the data is available in the DBA_HIST_UNDOSTAT view.) TUNED_UNDORETENTION is given in seconds.select to_char(begin_time, 'DD-MON-RR HH24:MI') begin_time,to_char(end_time, 'DD-MON-RR HH24:MI') end_time, tuned_undoretentionfrom v$undostat order by end_time;BEGIN_TIME END_TIME TUNED_UNDORETENTION--------------- --------------- -------------------04-FEB-05 00:01 04-FEB-05 00:11 12100 ... 07-FEB-05 23:21 07-FEB-05 23:31 8670007-FEB-05 23:31 07-FEB-05 23:41 8670007-FEB-05 23:41 07-FEB-05 23:51 8670007-FEB-05 23:51 07-FEB-05 23:52 86700576 rows selected.
回滚段自动管理模式下UNDO block分配算法:
1、如果当前extent有空闲的数据块,则使用当前extent
2、如果当前extent的下一个extent已经处于过期(expired)状态,那么环绕(wrap)到一个extent,然后使用该extent的第一个数据块。
3、如果下一个extent没有expired,则从undo tablespace中分配空间。如果有剩余空间则使用新分配extent的第一个数据块。此时undo tablespace的使用率开始增加。
4、如果没有剩余空闲的extent,则从offline状态的回滚段中偷取(steal)过期的extent,加入当前回滚段,并使用第一个数据块。
5、如果offline状态的回滚段中没有expired extent,则从online状态的回滚段中偷取(steal)过期区加入到当前的回滚段中,并使用extent中的第一个数据块。
6、如果undo tablespace可以扩展,则扩展undo tablespace,并将新extent加入到当前的回滚段中,同时使用第一个数据块,此时undo所占的操作系统空间开始增加。
7、如果undo tablespace 不能扩展,则自动调整(下降幅度为10%)回滚段的保留时间,然后偷取在更短保留时间下的未过期的extent,如果还未找到过期的extent,则继续以10%的速度减少回滚段的保留时间,重复几次。
8、随机从其他offline状态的回滚段中偷取未过期(unexpired)的extent。
9、尝试使用当前回滚段中未过期的extent,如果所有的区都为ACTIVE状态,则进入下一步。
10、如果以上所有的尝试都失败,则报ORA-30036的错误。
[oracle@rh7 ~]$ oerr ora 3003630036, 00000, "unable to extend segment by %s in undo tablespace '%s'"// *Cause: the specified undo tablespace has no more space available.// *Action: Add more space to the undo tablespace before retrying// the operation. An alternative is to wait until active// transactions to commit.案例:10:34:45 SYS@ prod>select tablespace_name,contents from dba_tablespaces;TABLESPACE_NAME CONTENTS------------------------------ ---------SYSTEM PERMANENTSYSAUX PERMANENTTEMP TEMPORARYUSERS PERMANENTUNDOTBS2 UNDOEXAMPLE PERMANENTTBS1 PERMANENT7 rows selected.Elapsed: 00:00:00.0310:34:56 SYS@ prod>create undo tablespace undotbs110:35:15 2 datafile '/u01/app/oracle/oradata/prod/undotbs1.dbf' size 1m;Tablespace created.Elapsed: 00:00:01.3810:35:40 SYS@ prod>show parameter undoNAME TYPE VALUE------------------------------------ ----------- ------------------------------undo_management string AUTOundo_retention integer 900undo_tablespace string UNDOTBS210:35:49 SYS@ prod>alter system set undo_tablespace=undotbs1;System altered.Elapsed: 00:00:00.1310:36:03 SYS@ prod>10:36:03 SYS@ prod>select s.sid,s.serial#,s.username,u.segment_name,count(u.extent_id) "Extent count",10:37:37 2 t.used_ublk,t.used_urec,s.program10:38:00 3 from v$session s,v$transaction t,dba_undo_extents u10:38:24 4 where s.taddr=t.addr and u.segment_name like '_SYSSMU'||t.xidusn||'_%$' and u.status='ACTIVE'10:39:50 5 GROUP BY s.sid,s.serial#,s.username,u.segment_name,t.used_ublk,t.used_urec,s.program10:40:37 6 order by t.used_ublk desc,t.used_urec desc,s.sid,s.serial#,s.username,s.program;开启新的session,执行DML:10:41:45 SYS@ prod>conn scott/tigerConnected.10:42:45 SCOTT@ prod>insert into t1 select * from t1 where rownum <1000;999 rows created.查看undo tablespace 使用情况:10:43:29 SYS@ prod> select s.sid,s.serial#,s.username,u.segment_name,count(u.extent_id) "Extent count", 2 t.used_ublk,t.used_urec,s.program 3 from v$session s,v$transaction t,dba_undo_extents u 4 where s.taddr=t.addr and u.segment_name like '_SYSSMU'||t.xidusn||'_%$' and u.status='ACTIVE' 5 GROUP BY s.sid,s.serial#,s.username,u.segment_name,t.used_ublk,t.used_urec,s.program 6* order by t.used_ublk desc,t.used_urec desc,s.sid,s.serial#,s.username,s.program SID SERIAL# USERNAME SEGMENT_NAME Extent count USED_UBLK USED_UREC PROGRAM---------- ---------- ---------- -------------------- ------------ ---------- ---------- ---------- 37 36 SCOTT _SYSSMU12_2144756092 1 1 11 sqlplus@rh $ 6 (TNS V1- V3)Elapsed: 00:00:00.2010:45:01 SCOTT@ prod>insert into t1 select * from t1 where rownum <20000;19999 rows created.Elapsed: 00:00:00.0710:44:52 SYS@ prod>select s.sid,s.serial#,s.username,u.segment_name,count(u.extent_id) "Extent count", 2 t.used_ublk,t.used_urec,s.program 3 from v$session s,v$transaction t,dba_undo_extents u 4 where s.taddr=t.addr and u.segment_name like '_SYSSMU'||t.xidusn||'_%$' and u.status='ACTIVE' 5 GROUP BY s.sid,s.serial#,s.username,u.segment_name,t.used_ublk,t.used_urec,s.program 6* order by t.used_ublk desc,t.used_urec desc,s.sid,s.serial#,s.username,s.program SID SERIAL# USERNAME SEGMENT_NAME Extent count USED_UBLK USED_UREC PROGRAM---------- ---------- ---------- -------------------- ------------ ---------- ---------- ---------- 37 36 SCOTT _SYSSMU16_2726800344 2 7 109 sqlplus@rh $ 6 (TNS V1- V3)Elapsed: 00:00:00.0110:45:13 SYS@ prod>10:48:16 SYS@ prod>select a.usn,a.name,b.xacts,b.extents,b.status from v$rollname a,v$rollstat b10:49:01 2 where a.usn=b.usn; USN NAME XACTS EXTENTS STATUS---------- ------------------------------ ---------- ---------- --------------- 0 SYSTEM 0 6 ONLINE 11 _SYSSMU11_2517864848$ 0 2 ONLINE 12 _SYSSMU12_2144756092$ 0 2 ONLINE 13 _SYSSMU13_527038519$ 0 3 ONLINE 14 _SYSSMU14_2951869305$ 0 2 ONLINE 15 _SYSSMU15_2206823906$ 0 2 ONLINE 16 _SYSSMU16_2726800344$ 1 2 ONLINE 17 _SYSSMU17_2098084560$ 0 2 ONLINE 10:50:45 SYS@ prod>select SEGMENT_NAME,TABLESPACE_NAME,EXTENT_ID,STATUS from dba_undo_extentsSEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS------------------------- ------------------------------ ---------- ---------_SYSSMU17_2098084560$ UNDOTBS1 0 UNEXPIRED_SYSSMU17_2098084560$ UNDOTBS1 1 UNEXPIRED_SYSSMU16_2726800344$ UNDOTBS1 0 ACTIVE_SYSSMU16_2726800344$ UNDOTBS1 1 ACTIVE_SYSSMU15_2206823906$ UNDOTBS1 0 UNEXPIRED_SYSSMU15_2206823906$ UNDOTBS1 1 UNEXPIRED_SYSSMU14_2951869305$ UNDOTBS1 0 UNEXPIRED_SYSSMU14_2951869305$ UNDOTBS1 1 UNEXPIRED_SYSSMU13_527038519$ UNDOTBS1 0 UNEXPIRED_SYSSMU13_527038519$ UNDOTBS1 1 UNEXPIRED_SYSSMU13_527038519$ UNDOTBS1 2 UNEXPIRED_SYSSMU12_2144756092$ UNDOTBS1 0 UNEXPIRED_SYSSMU12_2144756092$ UNDOTBS1 1 UNEXPIRED_SYSSMU11_2517864848$ UNDOTBS1 0 UNEXPIRED_SYSSMU11_2517864848$ UNDOTBS1 1 UNEXPIRED_SYSSMU30_1737877121$ UNDOTBS2 0 EXPIRED_SYSSMU30_1737877121$ UNDOTBS2 1 UNEXPIREDSEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS------------------------- ------------------------------ ---------- ---------_SYSSMU30_1737877121$ UNDOTBS2 2 EXPIRED_SYSSMU29_2754652023$ UNDOTBS2 0 EXPIRED_SYSSMU29_2754652023$ UNDOTBS2 1 EXPIRED_SYSSMU29_2754652023$ UNDOTBS2 2 EXPIRED_SYSSMU29_2754652023$ UNDOTBS2 3 UNEXPIRED_SYSSMU28_707429450$ UNDOTBS2 0 UNEXPIRED_SYSSMU28_707429450$ UNDOTBS2 1 EXPIRED_SYSSMU28_707429450$ UNDOTBS2 2 EXPIRED_SYSSMU27_3269963619$ UNDOTBS2 0 EXPIRED_SYSSMU27_3269963619$ UNDOTBS2 1 EXPIRED_SYSSMU27_3269963619$ UNDOTBS2 2 EXPIRED_SYSSMU27_3269963619$ UNDOTBS2 3 UNEXPIRED_SYSSMU27_3269963619$ UNDOTBS2 4 EXPIRED_SYSSMU27_3269963619$ UNDOTBS2 5 EXPIRED_SYSSMU26_2968904537$ UNDOTBS2 0 EXPIRED_SYSSMU26_2968904537$ UNDOTBS2 1 EXPIRED_SYSSMU26_2968904537$ UNDOTBS2 2 EXPIREDSEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS------------------------- ------------------------------ ---------- ---------_SYSSMU26_2968904537$ UNDOTBS2 3 EXPIRED_SYSSMU26_2968904537$ UNDOTBS2 4 UNEXPIRED_SYSSMU26_2968904537$ UNDOTBS2 5 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 0 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 1 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 2 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 3 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 4 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 5 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 6 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 7 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 8 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 9 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 10 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 11 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 12 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 13 EXPIREDSEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS------------------------- ------------------------------ ---------- ---------_SYSSMU25_2810228709$ UNDOTBS2 14 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 15 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 16 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 17 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 18 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 19 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 20 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 21 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 22 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 23 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 24 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 25 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 26 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 27 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 28 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 29 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 30 EXPIREDSEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS------------------------- ------------------------------ ---------- ---------_SYSSMU25_2810228709$ UNDOTBS2 31 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 32 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 33 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 34 EXPIRED_SYSSMU25_2810228709$ UNDOTBS2 35 UNEXPIRED_SYSSMU24_386518199$ UNDOTBS2 0 EXPIRED_SYSSMU24_386518199$ UNDOTBS2 1 UNEXPIRED_SYSSMU24_386518199$ UNDOTBS2 2 EXPIRED_SYSSMU23_4084707454$ UNDOTBS2 0 EXPIRED_SYSSMU23_4084707454$ UNDOTBS2 1 UNEXPIRED_SYSSMU23_4084707454$ UNDOTBS2 2 EXPIRED_SYSSMU22_3375463809$ UNDOTBS2 0 UNEXPIRED_SYSSMU22_3375463809$ UNDOTBS2 1 UNEXPIRED_SYSSMU22_3375463809$ UNDOTBS2 2 EXPIRED_SYSSMU22_3375463809$ UNDOTBS2 3 EXPIRED_SYSSMU21_2312338076$ UNDOTBS2 0 EXPIRED_SYSSMU21_2312338076$ UNDOTBS2 1 EXPIREDSEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS------------------------- ------------------------------ ---------- ---------_SYSSMU21_2312338076$ UNDOTBS2 2 UNEXPIRED86 rows selected.Elapsed: 00:00:00.0610:52:33 SYS@ prod>alter session set nls_date_format='yyyy-mm-dd hh34:mi:ss';Session altered.Elapsed: 00:00:00.0210:53:03 SYS@ prod>select BEGIN_TIME,END_TIME,UNDOTSN,UNDOBLKS,TUNED_UNDORETENTION from v$undostat;BEGIN_TIME END_TIME UNDOTSN UNDOBLKS TUNED_UNDORETENTION------------------- ------------------- ---------- ---------- -------------------2014-07-01 10:52:51 2014-07-01 10:53:08 2 0 9212014-07-01 10:42:51 2014-07-01 10:52:51 2 74 9212014-07-01 10:32:51 2014-07-01 10:42:51 2 43 1260估算undo tablespace 的大小:UndoTablesapce = UR * (UPS * DBS)UR: undo_retention UPS:在业务高峰期每秒产生的undo blocks的数量DBS:undo tablespace的数据库的大小10:53:08 SYS@ prod>select (UR * (UPS * DBS)) AS "BYTES"10:56:12 2 from (select value AS UR from v$parameter where name='undo_retention'),10:57:04 3 (select undoblks/((end_time-begin_time)*900) as UPS10:58:39 4 FROM v$undostat10:58:50 5 where undoblks=(select max(undoblks) from v$undostat)),10:59:29 6 (select block_size as DBS 10:59:51 7 FROM dba_tablespaces11:00:01 8 where tablespace_name=(select upper(value) from v$parameter where name='undo_tablespace')); BYTES---------- 87293952Elapsed: 00:00:00.0711:00:45 SYS@ prod>11:00:45 SYS@ prod>select (UR * (UPS * DBS))/1024/1024 AS "BYTES"11:01:42 2 from (select value AS UR from v$parameter where name='undo_retention'),11:01:42 3 (select undoblks/((end_time-begin_time)*900) as UPS11:01:42 4 FROM v$undostat11:01:42 5 where undoblks=(select max(undoblks) from v$undostat)),11:01:42 6 (select block_size as DBS 11:01:42 7 FROM dba_tablespaces11:01:42 8 where tablespace_name=(select upper(value) from v$parameter where name='undo_tablespace')); BYTES---------- 83.25Elapsed: 00:00:00.1011:01:44 SYS@ prod>
以上部分内容参考了《Oracle DBA实战攻略》书中的内容,这里表示感谢!