How does Oracle manage undo data?

How does Oracle manage undo data?

In the world of Oracle Database, Undo Data is the ultimate safety net. It is the "Ctrl+Z" of the database world, ensuring that if you make a mistake—or if the system crashes—the data can always be returned to a consistent state.

While Redo records what you did, Undo records how to reverse it.


1. What exactly is Undo Data?

Whenever a transaction modifies data, Oracle doesn't just overwrite the old value. It captures the "before image" (the data as it looked before the change) and stores it in a special place called Undo Segments, located within an Undo Tablespace.

If you change a salary from 5000 to 6000, Oracle:

  • Writes 5000 to the Undo Segment.

  • Writes 6000 to the Data Block in the Buffer Cache.


2. The Three Critical Roles of Undo

Oracle manages undo data to serve three primary functions:

A. Transaction Rollback

If you realize you made a mistake and type ROLLBACK;, Oracle goes straight to the Undo Segments, finds the "before images" for your session, and puts the old values back into the data blocks.

B. Read Consistency (The "Flashback" Effect)

This is Oracle’s secret weapon. If User A starts a massive report at 9:00 AM, and User B updates the data at 9:05 AM, User A's report must still show the 9:00 AM version of the data.

  • Oracle uses Undo Data to reconstruct the "old" version of the blocks for User A, ensuring they see a consistent "snapshot" in time without blocking User B from working.

C. Database Recovery

As we discussed in Crash Recovery, when an instance restarts after a crash, SMON uses Undo Data to "Roll Back" any transactions that were in progress but never committed.


3. How Undo is Organized

Oracle manages undo automatically using AUM (Automatic Undo Management). Here is the hierarchy:

  1. Undo Tablespace: A dedicated physical file on disk.

  2. Undo Segments: Internal structures that act like circular buffers.

  3. Undo Extents: The actual chunks of space.

The "Circular" Life Cycle

Undo space is recycled. Once a transaction is Committed, that undo data is no longer strictly "needed" for rollback. Oracle marks that space as Unexpired (still needed for read consistency) and eventually Expired (safe to overwrite).


4. Key Parameters: UNDO_RETENTION

How long should Oracle keep the "old" data after you commit? This is controlled by the UNDO_RETENTION parameter (measured in seconds).

  • If you set it to 3600 (1 hour), Oracle tries to keep undo data for at least an hour so that long-running reports don't fail with the infamous ORA-01555: snapshot too old error.

  • Note: If the Undo Tablespace runs out of space, Oracle will overwrite "Unexpired" data unless you have enabled Undo Guarantee.


5. Summary: Undo vs. Redo

FeatureUndo DataRedo Log
PurposeTo Undo a change (Rollback).To Replay a change (Recovery).
AnalogyThe "Eraser."The "Recorder."
Used ForRead Consistency, Rollback.Data Loss Prevention.
StorageUndo Tablespace (Data Files).Online Redo Log Files.

6. Monitoring Undo Health

If your Undo Tablespace is too small, your users will experience errors during large deletes or updates. You can check the health of your undo management with this view:

SQL
SELECT begin_time, end_time, undoblks, transactioncount, maxquerylen 
FROM v$undostat 
ORDER BY begin_time DESC;
  • maxquerylen tells you the duration of the longest query. If this is higher than your UNDO_RETENTION, you risk a "Snapshot too old" error!


Pro-Tip: Flashback Technology

Because Oracle manages Undo so efficiently, you can use features like Flashback Query to see what a table looked like 15 minutes ago without restoring any backups:

SELECT * FROM employees AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '15' MINUTE);

Looking for servers Rental ?

Call Our Expert :


  • (call for rental enquiries)

Email us :