Managing Undo

Automatic Undo Management

For a default installation, Oracle Database automatically manages undo. There is typically no need for DBA intervention. However, if your installation uses Oracle Flashback operations, you may need to perform some undo management tasks to ensure the success of these operations.

You cannot create database objects in an undo tablespace. It is reserved for system-managed undo data.

手动Undo Management已不用了过于复杂

The following is a summary of the initialization parameters for undo management:

  1. UNDO_MANAGEMENT

If AUTO or null, enables automatic undo management. If MANUAL, sets manual undo management mode. The default is AUTO.

  1. UNDO_TABLESPACE

Optional, and valid only in automatic undo management mode. Specifies the name of an undo tablespace.

  1. 不设置UNDO_TABLESPACE:

When the database instance starts, the database automatically selects the first available undo tablespace. If no undo tablespace is available, then the instance starts without an undo tablespace and stores undo records in the SYSTEM tablespace. This is not recommended, and an alert message is written to the alert log file to warn that the system is running without an undo tablespace.

  1. 设置UNDO_TABLESPACE参数:

If the database contains multiple undo tablespaces, then you can optionally specify at startup that you want to use a specific undo tablespace: UNDO_TABLESPACE = undotbs_01

If the tablespace specified in the initialization parameter does not exist, the STARTUP command fails. The UNDO_TABLESPACE parameter can be used to assign a specific undo tablespace to an instance in an Oracle Real Application Clusters environment.

Note: A null UNDO_MANAGEMENT initialization parameter defaults to automatic undo management mode in Oracle Database 11g and later, but defaults to manual undo management mode in earlier releases. To change to automatic undo management, you must first create an undo tablespace and then change the UNDO_MANAGEMENT initialization parameter to AUTO.

The Undo Retention Period

提交事务的UNDO DATA还是有用的,它仍然用于一致读以闪回特性

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.

已提交事务的UNDO DATA如果超过undo retention period则称为过期,过期数据可能被重写

Old (committed) undo information that is older than the current undo retention period is said to be expired and its space is available to be overwritten by new transactions. Old undo information with an age that is less than the current undo retention period is said to be unexpired and is retained for consistent read and Oracle Flashback operations.

  1. Automatic Tuning of Undo Retention

Oracle是自动根据longest-running active query调整当前实际的undo retention period,如果undo tablespace为autoextend你还可以使用UNDO_RETENTION指定最小保留时间,但如果undo tablespace为固定大小将忽略UNDO_RETENTION设置

  1. If the undo tablespace is configured with the AUTOEXTEND option, the database dynamically tunes the undo retention period to be somewhat longer than the longest-running active query on the system. However, this retention period may be insufficient to accommodate Oracle Flashback operations. Oracle Flashback operations resulting in snapshot too old errors are the indicator that you must intervene to ensure that sufficient undo data is retained to support these operations.

Oracle会为满足undo_retention而优先选择扩展UNDO表空间大小,在达到MAXSIZE后才选择覆盖小于undo_retention的undo data

  1. The UNDO_RETENTION parameter is ignored for a fixed size undo tablespace. The database always tunes the undo retention period for the best possible retention, based on system activity and undo tablespace size.
  2. 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. The UNDOTBS1 tablespace that is automatically created by DBCA is auto-extending.
  1. If the undo tablespace is fixed size, the database dynamically tunes the undo retention period for the best possible retention for that tablespace size and the current system load. This best possible retention time is typically significantly greater than the duration of the longest-running active query.

If you decide to change the undo tablespace to fixed-size, you must choose a tablespace size that is sufficiently large. If you choose an undo tablespace size that is too small, the following two errors could occur:

  1. DML could fail because there is not enough space to accommodate undo for new transactions.
  2. Long-running queries could fail with a snapshot too old error, which means that there was insufficient undo data for read consistency.

对于固定UNDO TABLESPACE,UNDO表空间的预警设置也会影响实际的undo retention period

For a fixed-size undo tablespace, the database calculates the best possible retention based on database statistics and on the size of the undo tablespace.

For optimal undo management, rather than tuning based on 100% of the tablespace size, the database tunes the undo retention period based on 70% of the tablespace size, or on the warning alert threshold percentage for space used, whichever is lower. (The warning alert threshold defaults to 70%, but can be changed.) Therefore, if you set the warning alert threshold of the undo tablespace below 70%, this may reduce the tuned size of the undo 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.

  1. Retention Guarantee

上面说了在UNDO表空间不能扩展时会选择覆盖小于undo_retention的undo data,但设置了RETENTION GUARANTEE后就不会覆盖

If retention guarantee is enabled, then the specified minimum undo retention is guaranteed; the database never overwrites unexpired undo data even if it means that transactions fail due to lack of space in the undo tablespace. If retention guarantee is not enabled, then the database can overwrite unexpired undo when space is low, thus lowering the undo retention for the system. This option is disabled by default.

在CREATE UNDO TABLESPACE或ALTER TALBESPACE中指定RETENTION GUARANTEE

You enable retention guarantee by specifying the RETENTION GUARANTEE clause for the undo tablespace when you create it with either the CREATE DATABASE or CREATE UNDO TABLESPACE statement. Or, you can later specify this clause in an ALTER TABLESPACE statement. You disable retention guarantee with the RETENTION NOGUARANTEE clause.

  1. Tracking the Tuned Undo Retention Period

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;

Sizing a Fixed-Size Undo Tablespace

使用固定大小UNDO表空间会有更好的性能,可通过UNDO ADVISOR来评估表空间设置大小

Automatic tuning of undo retention typically achieves better results with a fixed-size undo tablespace. If you decide to use a fixed-size undo tablespace, then the Undo Advisor can help you estimate needed capacity.

To use the Undo Advisor, you first estimate these two values:

  1. The length of your expected longest running query

After the database has completed a workload cycle, you can view the Longest Running Query field on the System Activity subpage of the Automatic Undo Management page.

  1. The longest interval that you will require for Oracle Flashback operations

For example, if you expect to run Oracle Flashback queries for up to 48 hours in the past, your Oracle Flashback requirement is 48 hours.

Note: The Undo Advisor relies for its analysis on data collected in the Automatic Workload Repository (AWR). It is therefore important that the AWR have adequate workload statistics available so that the Undo Advisor can make accurate recommendations. For newly created databases, adequate statistics may not be available immediately. In such cases, continue to use the default auto-extending undo tablespace until at least one workload cycle completes.

示例:

DECLARE

   tid    NUMBER;

   tname  VARCHAR2(30);

   oid    NUMBER;

BEGIN

   DBMS_ADVISOR.CREATE_TASK('Undo Advisor', tid, tname, 'Undo Advisor Task');

   DBMS_ADVISOR.CREATE_OBJECT(tname, 'UNDO_TBS', null, null, null, 'null', oid);

   DBMS_ADVISOR.SET_TASK_PARAMETER(tname, 'TARGET_OBJECTS', oid);

   DBMS_ADVISOR.SET_TASK_PARAMETER(tname, 'START_SNAPSHOT', 1);

   DBMS_ADVISOR.SET_TASK_PARAMETER(tname, 'END_SNAPSHOT', 2);

   DBMS_ADVISOR.SET_TASK_PARAMETER(tname, 'INSTANCE', 1);

   DBMS_ADVISOR.execute_task(tname);

END;

/

After you have created the advisor task, you can view the  information is also available in the DBA_ADVISOR_* data dictionary views (DBA_ADVISOR_TASKS, DBA_ADVISOR_OBJECTS, DBA_ADVISOR_FINDINGS, DBA_ADVISOR_RECOMMENDATIONS, and so on).

You then take the maximum of these two values and use that value as input to the Undo Advisor.

使用UNDO ADVISOR后通过以下语句调整UNDO TABLESPACE大小

ALTER DATABASE DATAFILE '/oracle/dbs/undotbs.dbf' RESIZE 300M;

ALTER DATABASE DATAFILE '/oracle/dbs/undotbs.dbf' AUTOEXTEND OFF;

Managing Undo Tablespaces

1. Creating an Undo Tablespace

CREATE UNDO TABLESPACE undotbs_02

     DATAFILE '/u01/oracle/rbdb1/undo0201.dbf' SIZE 2M REUSE AUTOEXTEND ON;

  1. Altering an Undo Tablespace

You can alter an undo tablespaces using the ALTER TABLESPACE statement.

However, since most aspects of undo tablespaces are system managed, you need only be concerned with the following actions:

  1. Adding a data file

ALTER TABLESPACE undotbs_01

     ADD DATAFILE '/u01/oracle/rbdb1/undo0102.dbf' AUTOEXTEND ON NEXT 1M

         MAXSIZE UNLIMITED;

  1. Renaming a data file
  2. Bringing a data file online or taking it offline
  3. Beginning or ending an open backup on a data file
  4. Enabling and disabling undo retention guarantee
  5. You can use the ALTER DATABASE...DATAFILE statement to resize or extend a data file.

  1. Dropping an Undo Tablespace

DROP TABLESPACE undotbs_01 INCLUDING CONTENTS AND DATAFILES;

注意DROP TABLESPACE会DROP包含unexpired undo information的UNDO表空间

An undo tablespace can only be dropped if it is not currently used by any instance.If the undo tablespace contains any outstanding transactions (for example, a transaction died but has not yet been recovered), the DROP TABLESPACE statement fails. However, since DROP TABLESPACE drops an undo tablespace even if it contains unexpired undo information (within retention period), you must be careful not to drop an undo tablespace if undo information is needed by some existing queries.

  1. Switching Undo Tablespaces

ALTER SYSTEM SET UNDO_TABLESPACE = undotbs_02;

切换完成后所有新事务将使用新的UNDO TABLESPACE

The database is online while the switch operation is performed, and user transactions can be executed while this command is being executed. When the switch operation completes successfully, all transactions started after the switch operation began are assigned to transaction tables in the new undo tablespace.

切换前未提交事务将继续使用旧的UNDO表空间,此时旧的UNDO表空间处于PENDING OFFLIE状态,它不能被其它实例使用或被DROP

The switch operation does not wait for transactions in the old undo tablespace to commit. If there are any pending transactions in the old undo tablespace, the old undo tablespace enters into a PENDING OFFLINE mode (status). In this mode, existing transactions can continue to execute, but undo records for new user transactions cannot be stored in this undo tablespace.

An undo tablespace can exist in this PENDING OFFLINE mode, even after the switch operation completes successfully. A PENDING OFFLINE undo tablespace cannot be used by another instance, nor can it be dropped. Eventually, after all active transactions have committed, the undo tablespace automatically goes from the PENDING OFFLINE mode to the OFFLINE mode. From then on, the undo tablespace is available for other instances (in an Oracle Real Application Cluster environment).

UNDO_TABLESPACE设置为空的效果是切换UNDO TALBESPACE

If the parameter value for UNDO TABLESPACE is set to '' (two single quotes), then the current undo tablespace is switched out and the next available undo tablespace is switched in. Use this statement with care because there may be no undo tablespace available.

The following example unassigns the current undo tablespace:

ALTER SYSTEM SET UNDO_TABLESPACE = '';

  1. Establishing User Quotas for Undo Space

You can use the Oracle Database Resource Manager to establish user quotas for undo space. The Database Resource Manager directive UNDO_POOL allows DBAs to limit the amount of undo space consumed by a group of users (resource consumer group).

You can specify an undo pool for each consumer group. An undo pool controls the amount of total undo that can be generated by a consumer group. When the total undo generated by a consumer group exceeds its undo limit, the current UPDATE transaction generating the undo is terminated. No other members of the consumer group can perform further updates until undo space is freed from the pool.

When no UNDO_POOL directive is explicitly defined, users are allowed unlimited undo space.

Migrating to Automatic Undo Management

If you are currently using rollback segments to manage undo space, Oracle strongly recommends that you migrate your database to automatic undo management.

For instructions, see Oracle Database Upgrade Guide.

Managing Temporary Undo

默认临时表的undo record也存于undo表空间,这个操作同样记录于redo

By default, undo records for temporary tables are stored in the undo tablespace and are logged in the redo, which is the same way undo is managed for persistent tables.

如果设置TEMP_UNDO_ENABLED为true,则临时表的undo record称为temporary undo。Temporary undo records会存于临时表空间,所以不会产生redo

However, you can use the TEMP_UNDO_ENABLED initialization parameter to separate undo for temporary tables from undo for persistent tables. When this parameter is set to TRUE, the undo for temporary tables is called temporary undo. Temporary undo records are stored in the database's temporary tablespaces and thus are not logged in the redo log.

When temporary undo is enabled, it might be necessary to increase the size of the temporary tablespaces to account for the undo records.

Enabling temporary undo provides the following benefits:

  1. Temporary undo reduces the amount of undo stored in the undo tablespaces.

Less undo in the undo tablespaces can result in more realistic undo retention period requirements for undo records.

  1. Temporary undo reduces the size of the redo log.

Performance is improved because less data is written to the redo log, and components that parse redo log records, such as LogMiner, perform better because there is less redo data to parse.

  1. Temporary undo enables data manipulation language (DML) operations on temporary tables in a physical standby database with the Oracle Active Data Guard option. However, data definition language (DDL) operations that create temporary tables must be issued on the primary database.

Temporary undo可以在SYSTEM或SESSION级设置

You can enable temporary undo for a specific session or for the whole system. When you enable temporary undo for a session using an ALTER SESSION statement, the session creates temporary undo without affecting other sessions.

When a session uses temporary objects for the first time, the current value of the TEMP_UNDO_ENABLED initialization parameter is set for the rest of the session. Therefore, if temporary undo is enabled for a session and the session uses temporary objects, then temporary undo cannot be disabled for the session. Similarly, if temporary undo is disabled for a session and the session uses temporary objects, then temporary undo cannot be enabled for the session.

Temporary undo is enabled by default for a physical standby database with the Oracle Active Data Guard option. The TEMP_UNDO_ENABLED initialization parameter has no effect on a physical standby database with Active Data Guard option because of the default setting.

Note: Temporary undo can be enabled only if the compatibility level of the database is 12.0.0 or higher.

To enable or disable temporary undo:

  1. In SQL*Plus, connect to the database.

If you are enabling or disabling temporary undo for a session, then start the session in SQL*Plus.

If you are enabling or disabling temporary undo for the system, then connect as an administrative user with the ALTER SYSTEM system privilege in SQL*Plus.

  1. Set the TEMP_UNDO_ENABLED initialization parameter:

ALTER SESSION SET TEMP_UNDO_ENABLED = TRUE;

ALTER SESSION SET TEMP_UNDO_ENABLED = FALSE;

ALTER SYSTEM SET TEMP_UNDO_ENABLED = TRUE;

ALTER SYSTEM SET TEMP_UNDO_ENABLED = FALSE;

Undo Space Data Dictionary Views

The following dynamic performance views are useful for obtaining space information about the undo tablespace:

View

Description

V$UNDOSTAT

Contains statistics for monitoring and tuning undo space. Use this view to help estimate the amount of undo space required for the current workload. The database also uses this information to help tune undo usage in the system. This view is meaningful only in automatic undo management mode.

V$TEMPUNDOSTAT

Contains statistics for monitoring and tuning temporary undo space. Use this view to help estimate the amount of temporary undo space required in the temporary tablespaces for the current workload. The database also uses this information to help tune temporary undo usage in the system. This view is meaningful only when temporary undo is enabled.

V$ROLLSTAT

For automatic undo management mode, information reflects behavior of the undo segments in the undo tablespace

V$TRANSACTION

Contains undo segment information

DBA_UNDO_EXTENTS

Shows the status and size of each extent in the undo tablespace.

DBA_HIST_UNDOSTAT

Contains statistical snapshots of V$UNDOSTAT information.

The V$UNDOSTAT view is useful for monitoring the effects of transaction execution on undo space in the current instance. Statistics are available for undo space consumption, transaction concurrency, the tuning of undo retention, and the length and SQL ID of long-running queries in the instance.

Each row in the view contains statistics collected in the instance for a ten-minute interval. The first row of the view contains statistics for the (partial) current time period. The view contains a total of 576 rows, spanning a 4 day cycle.

The following example shows the results of a query on the V$UNDOSTAT view.

  SELECT TO_CHAR(BEGIN_TIME, 'MM/DD/YYYY HH24:MI:SS') BEGIN_TIME,

         TO_CHAR(END_TIME, 'MM/DD/YYYY HH24:MI:SS') END_TIME,

         UNDOTSN, UNDOBLKS, TXNCOUNT, MAXCONCURRENCY AS "MAXCON"

         FROM v$UNDOSTAT WHERE rownum <= 144;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值