Sub topic 8
What happens inside: undo and redo
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and can connect to an Oracle database. This chapter explains the internal work that makes COMMIT, ROLLBACK, and read consistency possible.
Undo and redo have different jobs
When Oracle changes a row, it creates two kinds of information:
- Undo data stores enough of the old version to reverse the change.
- Redo data records the change so Oracle can reproduce it during recovery.
Undo answers, “What was here before?” Redo answers, “What change must be reapplied?”
Undo is stored in undo segments in an undo tablespace. Redo is first placed in the redo log buffer and is later written to online redo log files. Oracle can write changed table blocks to data files later. This separation lets Oracle commit safely without immediately writing every changed block.
| Information | Main purpose | Used by |
|---|---|---|
| Undo | Reverse changes and construct older row versions | `ROLLBACK`, read consistency |
| Redo | Reapply changes after a failure | Instance and media recovery |
What happens during an update
Consider a transfer between two accounts:
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
COMMIT;For each update, Oracle changes the database block in memory. It also records the previous values in undo and records the change in redo.
The COMMIT does not mean that every modified data block has already reached its data file. Instead, Oracle makes the transaction durable by writing the transaction's redo records to the online redo log. After that write succeeds, a database failure can be repaired by applying the redo to data blocks.
How rollback uses undo
ROLLBACK reads the transaction's undo records and reverses its changes. It does not restore a backup copy of the entire table. It reverses each changed row using the recorded old values.
A rollback also generates redo. This may seem surprising, but the reversal itself must be recoverable if the instance fails while the rollback is running. Therefore, “rollback” does not mean “no redo.”
A savepoint records a position inside the transaction. Rolling back to it uses undo for only the work after that position:
SAVEPOINT before_adjustment;
UPDATE accounts
SET balance = balance - 50
WHERE id = 1;
ROLLBACK TO SAVEPOINT before_adjustment;
COMMIT;The update is undone, but work before the savepoint remains part of the transaction. The final COMMIT is still required.
Read consistency and transaction design
Oracle uses undo to give a query a consistent view of data. If another session has changed a row but has not committed, a reader can use the older committed version from undo instead of waiting for the writer. This is why ordinary reads usually do not block on uncommitted updates.
Keep transactions short enough to release locks and avoid unnecessary undo growth. Group statements that form one business action, such as both sides of a transfer, in one transaction. Do not commit after every row unless each row is genuinely independent.
Exercise
After an update, which structure lets ROLLBACK restore the previous value, and which structure lets recovery reproduce a committed change?
Show answer
Undo lets ROLLBACK restore the previous value. Redo lets recovery reproduce the committed change.