zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Undo详解程序员

程序员 详解 undo
2023-06-13 09:20:21 时间
What Is Undo?



The purpose of redo is recover your operation,on the contrary,the purpose of undo is undo your operation.For example,your TRANSACTION is failed or you regret for the operation,at this time,you need ROLLBACK command backup to before the series of operation.ROLLBACK is logical realization instead of physics relization.Because in multi-users system,data structure,blocks and so on is changing at any time.For example,we INSERT data,tablespace in not enough,extend one new EXTENT,our new data store in it,then other users also INSERT data into new EXTENT,but now,l want to excute ROLLBACK operation,physically,lt is impossible to undo abviously,because the action will impact other users operation.So ROLLBACK is ROLLBACK logically.For example,For insert operation,oracle excute DELETE operation actually when we issue ROLLBACK.

什么是UNDO 
REDO是为了重新实现你的操作,而UNDO相反,是为了撤销你做的操作,比如你得一个TRANSACTION执行失败了或你自己后悔了,则需要用ROLLBACK命令回退到操作之前。回滚是在逻辑层面实现而不是物理层面,因为在一个多用户系统中,数据结构,blocks等都在时时变化,比如我们INSERT一个数据,表的空间不够,扩展了一个新的EXTENT,我们的数据保存在这新的EXTENT里,其它用户随后也在这EXTENT里插入了数据,而此时我想ROLLBACK,那么显然物理上讲这EXTENT撤销是不可能的,因为这么做会影响其他用户的操作。所以,ROLLBACK是逻辑上回滚,比如对INSERT来说,那么ROLLBACK就是DELETE了。

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:


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.

About the Undo Retention Period

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 已经提交过的数据过期时间.

对于固定大小的 undo 表空间, undo_retention 参数将被忽略. 数据库会基于系统的性能以及 undo 表空间的大小, 自动调整 undo_retention 的值以获取最佳的保存时间(过期时间) 对于启用了自动扩展的 undo 表空间, 当undo 表空间可用空间越来越小时, 表空间会自动扩展大小而不是去覆盖已经过期的undo数据. 如果自动扩展的 undo 表空间指定了 maxsize 时, 当undo 表空间大小到达最大值时, 数据库就会去覆盖已经过期的 undo 数据

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


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:


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.

Managing Undo Tablespaces Creating an Undo Tablespace

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.

Using CREATE DATABASE to Create an Undo Tablespace

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.

Using the CREATE UNDO TABLESPACE Statement

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.

Altering an Undo Tablespace

Undo tablespaces are altered using the
ALTER TABLESPACE statement. However, since most aspects of undo tablespaces are system managed, you need only be concerned with the following actions:


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.

Dropping an Undo Tablespace

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.

Switching Undo Tablespaces

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.

Undo Space Data Dictionary Views

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:


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
DBA_HIST_UNDOSTAT Contains statistical snapshots of V$UNDOSTAT information. See Oracle Database 2 Day DBA for more information.