Oracle Database uses undo data to do the following:

Roll back an active transaction

Recover a terminated transaction

Provide read consistency

Perform some logical flashback operations

Oracle事务回滚闪回等操作是通过UNDO SEGMENT完成的而不是REDO LOG,更高率. 而只在恢复时使用redo log(Oracle Database uses the online redo log only for recovery). 恢复时也是通过redo log来前滚,通过undo来回滚,redo中记录的undo信息用于前滚undo数据

Oracle Database stores undo data inside the databaserather than in external logs. Undo data is stored in blocks that are updated just like data blocks, with changes to these blocks generating redo records. In this way, Oracle Database can efficiently access undo data without needing to read external logs.

Undo data存于Undo tablespace, 并自动管理

Undo Segments and Transactions

1. Beginning of a Transaction

An executable SQL statement is a SQL statement that generates calls to a database instance, including DML and DDL statements and the SET TRANSACTION statement.

事务开始会先分配undo segment, 并把相关信息绑定到undo segment中的transaction table中,最后才生成transaction id

When a transaction begins, Oracle Database assigns the transaction to an available undo datasegment to record the undo entries for the new transaction. A transaction ID is not allocated until an undo segment andtransaction table(The data structure within an undo segment that holds the transaction identifiers of the transactions using the undo segment.The transaction table entry for every active transaction contains a pointer to all the undo data for the transaction)slot are allocated, which occurs during the first DML statement. A transaction ID is unique to a transaction and represents the undo segment number, slot, and sequence number.

transacation table还会记录事务的开始和commit SCN

Transaction id通过v$transaction的XID查看:

SQL> SELECT XID AS "txn id", XIDUSN AS "undo seg", XIDSLOT AS "slot", XIDSQN AS "seq", STATUS AS "txn_status" FROM V$TRANSACTION;

txn id             undo seg       slot        seq    txn_status

---------------- ---------- ---------- ---------- ----------------

0600060037000000      6          6         55     ACTIVE

  1. Rollback of a Transaction

In rolling back an entire transaction Oracle Database performs the following actions:

  1. Undoes all changes made by all the SQL statements in the transaction by using the corresponding undo segments

Thetransaction table entry for every active transaction contains a pointer to all the undo data (in reverse order of application) for the transaction. The database reads the data from the undo segment, reverses the operation, and then marks the undo entry as applied. Thus, if a transaction inserts a row, then a rollback deletes it. If a transaction updates a row, then a rollback reverses the update. If a transaction deletes a row, then a rollback reinserts it.

  1. Releases all the locks of data held by the transaction
  2. Erases all savepoints in the transaction
  3. Ends the transaction
  1. Commit of Transaction

When a transaction commits, the following actions occur:

  1. The database generates an SCN for the COMMIT.
  2. The internal transaction table for the associated undo tablespace records that the transaction has committed.The corresponding unique SCN of the transaction is assigned and recorded in the transaction table.
  3. The log writer process (LGWR) process writes remaining redo log entries in the redo log buffers to the online redo logand writes the transaction SCN to the online redo log. This atomic event constitutes the commit of the transaction.
  4. Oracle Database releases locks held on rows and tables.
  5. Users who were enqueued waiting on locks held by the uncommitted transaction are allowed to proceed with their work.
  6. Oracle Database deletes savepoints.
  7. Oracle Database performs acommit cleanout.

Commit cleanout是指在提交后清空块中的ITL记录(锁相关的事务信息),只在满足以下两个情况时进行commit cleanout:

  1. Block还在SGA中
  2. 没有其实会话在更改此block的数据

The automatic removal of lock-related transaction information (ITL entry) from the blocks after a commit. The database removes the ITL entry only if modified blocks containing data from the committed transaction are still in the SGA, and if no other session is modifying them.

BLOCK的ITL没有记录表示块内数据没被lock,反之可能有锁也可能没有

如果还有事务在更改此block,则在之后的SELECT中会通过SGA BLOCK的ITL entry找到transaction table查看事务是否已提交,如果已提交则会清除block的ITL,这个过程会记录在redo中. 这个过程也就是SELECT读取SGA block时如何判断是否需要读取前镜像

Ideally, the COMMIT cleans the blocks so that a subsequent SELECT does not have to perform this task. If no ITL entry exists for a specific row, then it is not locked. If an ITL entry exists for a specific row, then it is possibly locked, so a session must check the undo segment header to determine whether this interested transaction has committed. Ifthe interested transaction has committed, then the session cleans out the block, which generates redo. However, if the COMMIT cleaned out the ITL previously, then the check and cleanout are unnecessary.

Note: Because a block cleanout generates redo, a query may generate redo and thus cause blocks to be written during the nextcheckpoint.

  1. Oracle Database marks the transaction complete.

(二)Undo segment usage

多个事务可以同时写入同一undo segment的同一extent,但必须为不同block; 同一事务同一时间只能使用同一个extent(current extent)

Multiple active transactions can write concurrently to the same undo segment or to different segments. For example, transactions T1 and T2 can both write to undo segment U1, or T1 can write to U1 while T2 writes to undo segment U2.

At any given time, a transaction writes sequentially to only one extent in an undo segment, known as the current extent for the transaction. Multiple active transactions can write simultaneously to the same current extent or to different current extents. Figure 12-21 shows transactions T1 and T2 writing simultaneously to extent E3.Within an undo extent, a data block contains data for only one transaction.

Undo segment与一般segment不同点在于循环利用:

Conceptually, the extents in an undo segment form a ring. Transactions write to one undo extent, and then to the next extent in the ring, and so on in cyclical fashion.

Figure 12-21 shows two transactions, T1 and T2, which begin writing in the third extent (E3) of an undo segment and continue writing to the fourth extent (E4).

Figure 12-21 Ring of Allocated Extents in an Undo Segment

As the current undo extent fills, the first transaction needing space checks the availability of the next allocated extent in the ring. If the next extent does not contain data from an active transaction, then this extent becomes the current extent. Now all transactions that need space can write to the new current extent. In Figure 12-22, when E4 is full, T1 and T2 continue writing to E1, overwriting the nonactive undo data in E1.

Figure 12-22 Cyclical Use of Allocated Extents in an Undo Segment

If the next extent does contain data from an active transaction, then the database must allocate a new extent. Figure 12-23 shows a scenario in which T1 and T2 are writing to E4. When E4 fills up, the transactions cannot continue writing to E1 because E1 contains active undo entries. Therefore, the database allocates a new extent (E5) for this undo segment. The transactions continue writing to E5.

Figure 12-23 Allocation of a New Extent for an Undo Segment

Temporary Undo Segments

临时对象的更改是肯定会记在undo segment的,但重点在于是否把undo segment的更改记录在redo log中,在12c之前是记日志的,而在12c后多个temporary undo segments概念,即默认是不记日志的,可通过TEMP_UNDO_ENABLED参数设置

The database separates undo data into two streams:

A temporary undo stream encapsulates only undo records generated by changes to temporary objects, whereas a permanent undo stream encapsulates only undo records for permanent objects. The database manages temporary and permanent undo independently.

Undo records for changes to temporary tables are both session-specific and useful only for read consistency and transaction rollback.

Before Oracle Database 12c, the database always stored these records in the online redo log. Because changes to temporary objects are not logged in the online redo log, writing undo for temporary objects into temporary undo segments saves space in the online redo log and archived redo log files.

The database does not log changes to the undo or changes to the temporary table, which improves performance.

You can set the TEMP_UNDO_ENABLED initialization parameter so that temporary tables store undo data in a temporary undo segment. When this parameter is TRUE, the database allocates temporary undo segments from temporary tablespaces.

使用temporary undo segments好处:

  1. Enabling you to configure permanent and undo tablespace sizes that best fit the workloads for permanent and temporary tables
  2. Reducing the size of redo written to the online redo log
  3. Avoiding the need to back up temporary undo data

Note: On an Active Data Guard instance, DML on global temporary tables requires undo to be generatedin temporary undo segments.

Undo Segments相关推荐

  1. oracle 12c undo,Oracle 12C新特性-临时UNDO段(Temporary Undo Segments) | 信春哥,系统稳,闭眼上线不回滚!...

    在12C版本,为了减少UNDO表空间的使用率及减少REDO和归档日志的产生量,ORACLE推出了临时UNDO段(Temporary Undo Segments)新特性.这个新特性把临时表产生的UNDO ...

  2. Script:收集UNDO诊断信息

    以下脚本可以用于收集Automatic Undo Management的必要诊断信息,以sysdba身份运行: spool Undo_Diag.out ttitle off set pages 999 ...

  3. 在Oracle中如何让SELECT查询绕过UNDO

    是否有想过如何在Oracle中实现脏读(dirty read),在Oracle官方文档或者Asktom的时候显然会提到Oracle是不实现脏读的, 总是有undo来提供数据块的前镜像(before i ...

  4. 翻译: Oralce官方文档-- Data Blocks, Extents, and Segments

    Data Blocks, Extents, and Segments                                                                   ...

  5. 手动创建数据库实例全攻略7:UNDO

    手动创建数据库实例全攻略7:UNDO 一.偷来的常识 网络上这个哥们写的太精彩了,直接复制阅读之! 文字来源:http://blog.csdn.net/tianlesoftware/article/d ...

  6. Oracle 中UNDO与REDO的差别具体解释

    一 为了更清楚的看出2者差别,请看下表:                                               UNDO                             ...

  7. 【翻译自mos文章】使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式。...

    [翻译自mos文章]使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式. 使用aum( Automatic Undo Manage ...

  8. Oracle undo 管理

    在开始之前,我们先来思考几个问题? 1.  ora-01555错误的是怎么产生的?有什么办法解决? 该问题,参考我的Blog: Oracle ORA-01555快照过旧 http://blog.csd ...

  9. Oracle undo 介绍

    从 oracle 11g 开始, 如果使用的是默认安装方式, 数据库就无需 DBA 干预而会自动管理 undo; 如果在安装过程中勾选了 oracle flashback 操作选项, 就需要执行一些管 ...

最新文章

  1. Cocos2d-x学习笔记(三十)之 游戏存档
  2. struts2 中 Preparable 接口实现数据准备
  3. 外卖流量红利期已过:正从补贴战进入AI赛道
  4. 【自定义排序规则】剑指 Offer 45. 把数组排成最小的数
  5. 成都Uber优步司机奖励政策(4月2、3日)
  6. 韩顺平php视频笔记45 循环相关语句break,continue常量
  7. 那些除夕夜还在上BOSS直聘的人
  8. Windows判断是否为64位程序(C++)
  9. 为memcached增加缓存依赖的性能测试
  10. 为什么很多人在自媒体转不到钱?
  11. oracle not in 数组,慎用Oracle的not in (轉)
  12. 什么是https,和ssl什么关系,为什么用https
  13. ubuntu-安装qt+gdb-imagewatch
  14. pytorch minist
  15. java编程软件安装
  16. 斯坦福图机器学习CS224W笔记自用:How Expressive are Graph Neural Networks?
  17. Unity同时接入微信和支付宝支付 Android篇(aar包和jar包)
  18. JVM系列之深入理解JVM(三)
  19. 鸿蒙2.0正式开源,华为重磅押注开发者生态
  20. 【教程】Github快速学习

热门文章

  1. python外星人入侵游戏无法开火_《Python编程:从入门到实践》第十二章,外星人入侵实例中无法获取键盘按键和退出游戏...
  2. 我的人生之路记录(更新2020年9月)
  3. 单页Web应用 5 构建Model
  4. LeetCode——1937. 扣分后的最大得分(Maximum Number of Points with Cost)[中等]——分析及代码(Java)
  5. linux tbb 安装_linux安装intel tbb
  6. 华为的Linux系统和win系统,公认最美国产操作系统Deepin已与华为合作能替代windows?...
  7. TDMA、CDMA(这俩同频原因),提到一点FDMA是什么,理清这些之间的关系
  8. 精品微信小程序在线考试系统+后台管理系统|前后分离VUE
  9. C语言在读取txt类型文件中的汉字字符串出现乱码的解决办法
  10. 教程 | 各省电力缺口有多大,看看这张电力大数据地图