Oracle Database creates and manages information that is used to roll back, or undo, changes to the database. Such information consists of records of the actions of transactions, primarily before they are committed. These records are collectively referred to as undo.
Undo records are used to:
Roll back transactions when a ROLLBACK
statement is issued
Recover the database
Provide read consistency
Analyze data as of an earlier point in time by using Oracle Flashback Query
Recover from logical corruptions using Oracle Flashback feature
Undo 记录通常被使用在:
When a ROLLBACK
statement is issued, undo records are used to undo changes that were made to the database by the uncommitted transaction. During database recovery, undo records are used to undo any uncommitted changes applied from the redo log to the data files. Undo records provide read consistency by maintaining the before image of the data for users who are accessing the data at the same time that another user is changing it.
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 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.
Oracle Database automatically tunes the undo retention period based on undo tablespace size and system activity. You can optionally specify a minimum undo retention period (in seconds) by setting the UNDO_RETENTION
initialization parameter. The exact impact this parameter on undo retention is as follows:
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.
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.
当事务提交后, undo 数据将不需要回滚或者事务恢复. 然而为了数据库一致性读, 一些查询时间长的查询语句可能需要一些旧的 undo 数据来获取 block 的前镜像. 此外, oracle flashback 特性也将依靠这些可用的旧的 undo 数据. 因此, 尽可能长的保存这些旧的 undo 数据将是非常有用的.
当启动了自动 undo 管理时, 通常就会涉及到 undo retention period(撤销数据保存期), undo retention period 指的是 undo 已经提交过的数据过期时间.
The following is a summary of the initialization parameters for undo management:
Initialization Parameter | Description |
---|---|
UNDO_MANAGEMENT |
If AUTO or null, enables automatic undo management. If MANUAL , sets manual undo management mode. The default is AUTO . |
UNDO_TABLESPACE |
Optional, and valid only in automatic undo management mode. Specifies the name of an undo tablespace. Use only when the database has multiple undo tablespaces and you want to direct the database instance to use a particular undo tablespace. |
You specify the minimum undo retention period (in seconds) by setting the UNDO_RETENTION
initialization parameter. As described in "About the Undo Retention Period", the current undo retention period may be automatically tuned to be greater than UNDO_RETENTION
, or, unless retention guarantee is enabled, less than UNDO_RETENTION
if space in the undo tablespace is low.
To set the minimum undo retention period:
Do one of the following:
Set UNDO_RETENTION
in the initialization parameter file.
UNDO_RETENTION = 1800
Change UNDO_RETENTION
at any time using the ALTER SYSTEM
statement:
ALTER SYSTEM SET UNDO_RETENTION = 2400;
The effect of an UNDO_RETENTION
parameter change is immediate, but it can only be honored if the current undo tablespace has enough space.
Although Database Configuration Assistant (DBCA) automatically creates an undo tablespace for new installations of Oracle Database Release 11g, there may be occasions when you want to manually create an undo tablespace.
There are two methods of creating an undo tablespace. The first method creates the undo tablespace when the CREATE DATABASE
statement is issued. This occurs when you are creating a new database, and the instance is started in automatic undo management mode (UNDO_MANAGEMENT = AUTO
). The second method is used with an existing database. It uses the CREATE UNDO TABLESPACE
statement.
You cannot create database objects in an undo tablespace. It is reserved for system-managed undo data.
You can create a specific undo tablespace using the UNDO TABLESPACE
clause of the CREATE DATABASE
statement.
The following statement illustrates using the UNDO TABLESPACE
clause in a CREATE DATABASE
statement. The undo tablespace is named undotbs_01
and one data file, /u01/oracle/rbdb1/undo0101.dbf
, is allocated for it.
CREATE DATABASE rbdb1 CONTROLFILE REUSE . . . UNDO TABLESPACE undotbs_01 DATAFILE '/u01/oracle/rbdb1/undo0101.dbf';
If the undo tablespace cannot be created successfully during CREATE DATABASE
, the entire CREATE DATABASE
operation fails. You must clean up the database files, correct the error and retry the CREATE DATABASE
operation.
The CREATE UNDO TABLESPACE
statement is the same as the CREATE TABLESPACE
statement, but the UNDO
keyword is specified. The database determines most of the attributes of the undo tablespace, but you can specify the DATAFILE
clause.
This example creates the undotbs_02
undo tablespace with the AUTOEXTEND
option:
CREATE UNDO TABLESPACE undotbs_02 DATAFILE '/u01/oracle/rbdb1/undo0201.dbf' SIZE 2M REUSE AUTOEXTEND ON;
You can create multiple undo tablespaces, but only one of them can be active at any one time.
ALTER TABLESPACE
statement. However, since most aspects of undo tablespaces are system managed, you need only be concerned with the following actions:
Adding a data file
Renaming a data file
Bringing a data file online or taking it offline
Beginning or ending an open backup on a data file
Enabling and disabling undo retention guarantee
These are also the only attributes you are permitted to alter.
If an undo tablespace runs out of space, or you want to prevent it from doing so, you can add more files to it or resize existing data files.
The following example adds another data file to undo tablespace undotbs_01:
ALTER TABLESPACE undotbs_01 ADD DATAFILE '/u01/oracle/rbdb1/undo0102.dbf' AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED;
You can use the ALTER DATABASE...DATAFILE
statement to resize or extend a data file.
Use the DROP TABLESPACE
statement to drop an undo tablespace. The following example drops the undo tablespace undotbs_01
:
DROP TABLESPACE undotbs_01;
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.
DROP TABLESPACE
for undo tablespaces behaves like DROP TABLESPACE...INCLUDING CONTENTS
. All contents of the undo tablespace are removed.
You can switch from using one undo tablespace to another. Because the UNDO_TABLESPACE
initialization parameter is a dynamic parameter, the ALTER SYSTEM SET
statement can be used to assign a new undo tablespace.
The following statement switches to a new undo tablespace:
ALTER SYSTEM SET UNDO_TABLESPACE = undotbs_02;
Assuming undotbs_01
is the current undo tablespace, after this command successfully executes, the instance uses undotbs_02
in place of undotbs_01
as its undo tablespace.
This section lists views that are useful for viewing information about undo space in the automatic undo management mode and provides some examples. In addition to views listed here, you can obtain information from the views available for viewing tablespace and data file information. See "Data Files Data Dictionary Views" for information on getting information about those 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$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. See Oracle Database 2 Day DBA for more information. |