首页 > 代码库 > UNDO segment深入解析
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 images 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 Retention Oracle 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_undoretention from 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 86700 07-FEB-05 23:31 07-FEB-05 23:41 86700 07-FEB-05 23:41 07-FEB-05 23:51 86700 07-FEB-05 23:51 07-FEB-05 23:52 86700 576 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@rh6 ~]$ oerr ora 30036 30036, 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 PERMANENT SYSAUX PERMANENT TEMP TEMPORARY USERS PERMANENT UNDOTBS2 UNDO EXAMPLE PERMANENT TBS1 PERMANENT 7 rows selected. Elapsed: 00:00:00.03 10:34:56 SYS@ prod>create undo tablespace undotbs1 10:35:15 2 datafile ‘/u01/app/oracle/oradata/prod/undotbs1.dbf‘ size 1m; Tablespace created. Elapsed: 00:00:01.38 10:35:40 SYS@ prod>show parameter undo NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ undo_management string AUTO undo_retention integer 900 undo_tablespace string UNDOTBS2 10:35:49 SYS@ prod>alter system set undo_tablespace=undotbs1; System altered. Elapsed: 00:00:00.13 10: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.program 10:38:00 3 from v$session s,v$transaction t,dba_undo_extents u 10: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.program 10: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/tiger Connected. 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.20 10:45:01 SCOTT@ prod>insert into t1 select * from t1 where rownum <20000; 19999 rows created. Elapsed: 00:00:00.07 10: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.01 10: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 b 10: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_extents SEGMENT_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 UNEXPIRED SEGMENT_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 EXPIRED SEGMENT_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 EXPIRED SEGMENT_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 EXPIRED SEGMENT_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 EXPIRED SEGMENT_NAME TABLESPACE_NAME EXTENT_ID STATUS ------------------------- ------------------------------ ---------- --------- _SYSSMU21_2312338076$ UNDOTBS2 2 UNEXPIRED 86 rows selected. Elapsed: 00:00:00.06 10:52:33 SYS@ prod>alter session set nls_date_format=‘yyyy-mm-dd hh24:mi:ss‘; Session altered. Elapsed: 00:00:00.02 10: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 921 2014-07-01 10:42:51 2014-07-01 10:52:51 2 74 921 2014-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 UPS 10:58:39 4 FROM v$undostat 10: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_tablespaces 11:00:01 8 where tablespace_name=(select upper(value) from v$parameter where name=‘undo_tablespace‘)); BYTES ---------- 87293952 Elapsed: 00:00:00.07 11: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 UPS 11:01:42 4 FROM v$undostat 11: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_tablespaces 11:01:42 8 where tablespace_name=(select upper(value) from v$parameter where name=‘undo_tablespace‘)); BYTES ---------- 83.25 Elapsed: 00:00:00.10 11:01:44 SYS@ prod>
以上部分内容参考了《Oracle DBA实战攻略》书中的内容,这里表示感谢!
本文出自 “天涯客的blog” 博客,请务必保留此出处http://tiany.blog.51cto.com/513694/1432894