05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 ■ REDO AND UNDO 285<br />

■Note “Rollback segment” <strong>and</strong> “undo segment” are considered synonymous terms. Using manual undo<br />

management, the DBA will create “rollback segments.” Using automatic undo management, the system will<br />

automatically create <strong>and</strong> destroy “undo segments” as necessary. These terms should be considered the<br />

same for all intents <strong>and</strong> purposes in this discussion.<br />

It is a common misconception that undo is used to restore the database physically to the<br />

way it was before the statement or transaction executed, but this is not so. The database is<br />

logically restored to the way it was—any changes are logically undone—but the data structures,<br />

the database blocks themselves, may well be different after a rollback. The reason for<br />

this lies in the fact that, in any multiuser system, there will be tens or hundreds or thous<strong>and</strong>s<br />

of concurrent transactions. One of the primary functions of a database is to mediate concurrent<br />

access to its data. The blocks that our transaction modifies are, in general, being modified<br />

by many other transactions as well. Therefore, we cannot just put a block back exactly the way<br />

it was at the start of our transaction—that could undo someone else’s work!<br />

For example, suppose our transaction executed an INSERT statement that caused the allocation<br />

of a new extent (i.e., it caused the table to grow). Our INSERT would cause us to get a<br />

new block, format it for use, <strong>and</strong> put some data on it. At that point, some other transaction<br />

might come along <strong>and</strong> insert data into this block. If we were to roll back our transaction, obviously<br />

we cannot unformat <strong>and</strong> unallocate this block. Therefore, when <strong>Oracle</strong> rolls back, it is<br />

really doing the logical equivalent of the opposite of what we did in the first place. For every<br />

INSERT, <strong>Oracle</strong> will do a DELETE. For every DELETE, <strong>Oracle</strong> will do an INSERT. For every UPDATE,<br />

<strong>Oracle</strong> will do an “anti-UPDATE,” or an UPDATE that puts the row back the way it was prior to our<br />

modification.<br />

■Note This undo generation is not true for direct path operations, which have the ability to bypass undo<br />

generation on the table. We’ll discuss these in more detail shortly.<br />

How can we see this in action? Perhaps the easiest way is to follow these steps:<br />

1. Create an empty table.<br />

2. Full scan the table <strong>and</strong> observe the amount of I/O performed to read it.<br />

3. Fill the table with many rows (no commit).<br />

4. Roll back that work <strong>and</strong> undo it.<br />

5. Full scan the table a second time <strong>and</strong> observe the amount of I/O performed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!