Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Understand ORACLE REDO and UNDO

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

redo--> undo-->datafile

Insert a record, table and undo information will be put into redo, redo information will be put into hard disk before commit or. In the event of a failure, redo restores committed data.

redo-> Each operation is recorded in redo log first. When instance failure occurs (such as power failure), resulting in data not being updated to data file, redo is required when database is restarted to update data to data file again.

undo-> Record a copy before the change, but when your system rolls back, overwrite this copy to the original data

redo-> redo records all the database transactions used for recovery

undo-> Record all previous impressions, used for rollback

redo-> Submitted transactions, instances to be written to the data file during recovery

undo-> Unsubmitted transactions.

The reason for redo is that every time you commit, changes to the data are immediately written to the online redo, but not necessarily to the data file at the same time. Because the data has been submitted, but only exists in the online log file, so in the recovery time needs to find the data from the online log file, re-apply it, so that the changed data in the data file also changed!

The reason for undo is that in order to improve efficiency when oracle is running normally, if the user has not committed, but there is not much free memory, the DBWR process will write dirty blocks to the data file in order to free up valuable memory for other processes. This is why UNDO is needed. Because the commit statement has not yet been issued, oracle's dbwr process has written the uncommitted data to the data file.

undo is also a datafile, maybe the dirty buffer is not written back to disk.

Only when redo apply succeeds can we ensure that everything in undo datafile is correct, and then we can rollback.

The purpose of undo is to restore the system to the state before the system crash (before shutdown), and then redo is to ensure system consistency.

If you don't do undo, the system doesn't know the previous state, and redo is impossible to talk about.

Therefore, when an instance crash recovery occurs, it always rolls forward first and then rolls back.

undo

The data in the fallback segment is stored as a "fallback entry."

Fallback entry = block information (number of block changed in transaction)+ data stored in block before transaction commit

Oracle maintains a "transaction table" for each fallback segment

The transaction table records the transaction numbers associated with all rollback entries in the rollback segment (Transaction SCN& Rollback Entries).

redo

A redo record consists of a set of "change vectors."

Each change variable records the changes made by the transaction to a block in the database.

When a user commits a commit statement, the LGWR process immediately writes a commit record to the redo log file and then starts writing redo information about the transaction.

#When a transaction commits successfully, Oracle generates a system change code (SCN) for the transaction. The SCN for a transaction is recorded in both its commit record and redo record.

commit

Work done prior to submission:

·Generate a fallback entry for the transaction in the fallback cache of the SGA area. The fallback entry holds the original version of the data modified by the transaction.

·Generate a redo record for the transaction in the redo log cache in the SGA area. The redo record records the modifications made to the data block by the transaction and also records the modifications made to the data block in the fallback segment. Redo records in the cache may be written to hard disk before the transaction commits.

·Changes made to the database by transactions are recorded in the database buffer in the SGA area. These modifications may also be written to hard disk before the transaction commits.

Work done at the time of transaction submission:

Record that the transaction has committed in the internal transaction table in the fallback segment specified for the transaction and generate a unique SCN record in the internal transaction table that uniquely identifies the transaction.

LGWR latecomers write redo records from the SGA area redo log cache to online redo log files. The SCN for that transaction is written at the same time as the redo log is written.

Oracle service processes release all record locks and table locks used by transactions.

Oracle notifies users that transaction commit is complete.

Oracle marks the transaction as completed.

rollback

Work done to roll back transactions:

Oracle reverses database modifications made by all SQL statements in a transaction by using fallback entries in fallback segments.

·Oracle service processes release all locks used by transactions

·Oracle notifies transaction rollback success.

Oracle marks the transaction as completed

For example:

insert into a(id) values(1);(redo)

This record needs to be rolled back.

delete from a where id = 1;(undo)

Think about it. insert into a(id) values(1);(redo)

delete from a where id = 1;(undo) is meaningless.

Now look at the correct recovery:

insert first into a(id) values(1);(redo)

then delete from a where id = 1;(undo)

The system goes back to the original state, there is no record of this

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report