Undo Segments

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 database rather 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 data segment to record the undo entries for the new transaction. A transaction ID is not allocated until an undo segment and transaction 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

The transaction 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 log and 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 commit 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. If the 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 next checkpoint.

  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 generated in temporary undo segments.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值