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

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 9 ■ ■ ■ Redo and Undo This chapter describes two of the most important pieces of data in an Oracle database: redo and undo. Redo is the information Oracle records in online (and archived) redo log files in order to “replay” your transaction in the event of a failure. Undo is the information Oracle records in the undo segments in order to reverse, or roll back, your transaction. In this chapter, we will discuss topics such as how redo and undo (rollback) are generated, and how they fit into transactions, recovery, and so on. We’ll start off with a high-level overview of what undo and redo are and how they work together. We’ll then drill down into each topic, covering each in more depth and discussing what you, the developer, need to know about them. The chapter is slanted toward the developer perspective in that we will not cover issues that a DBA should be exclusively in charge of figuring out and tuning. For example, how to find the optimum setting for RECOVERY_PARALLELISM or the FAST_START_MTTR_TARGET parameters are not covered. Nevertheless, redo and undo are topics that bridge the DBA and developer roles. Both need a good, fundamental understanding of the purpose of redo and undo, how they work, and how to avoid potential issues with regard to their use. Knowledge of redo and undo will also help both DBAs and developers better understand how the database operates in general. In this chapter, I will present the pseudo-code for these mechanisms in Oracle and a conceptual explanation of what actually takes place. Every internal detail of what files get updated with what bytes of data will not be covered. What actually takes place is a little more involved, but having a good understanding of the flow of how it works is valuable and will help you to understand the ramifications of your actions. What Is Redo? Redo log files are crucial to the Oracle database. These are the transaction logs for the database. Oracle maintains two types of redo log files: online and archived. They are used for recovery purposes; their purpose in life is to be used in the event of an instance or media failure. If the power goes off on your database machine, causing an instance failure, Oracle will use the online redo logs to restore the system to exactly the point it was at immediately prior to the power outage. If your disk drive fails (a media failure), Oracle will use archived redo logs as well as online redo logs to recover a backup of the data that was on that drive to the correct point in time. Additionally, if you “accidentally” truncate a table or remove some 283

284 CHAPTER 9 ■ REDO AND UNDO critical information and commit the operation, you can restore a backup of the affected data and recover it to the point in time immediately prior to the “accident” using online and archived redo log files. Archived redo log files are simply copies of old, full online redo log files. As the system fills up log files, the ARCH process will make a copy of the online redo log file in another location, and optionally make several other copies into local and remote locations as well. These archived redo log files are used to perform media recovery when a failure is caused by a disk drive going bad or some other physical fault. Oracle can take these archived redo log files and apply them to backups of the data files to catch them up to the rest of the database. They are the transaction history of the database. ■Note With the advent of the Oracle 10g, we now have flashback technology. This allows us to perform flashback queries (i.e., query the data as of some point in time in the past), un-drop a database table, put a table back the way it was some time ago, and so on. As a result, the number of occasions where we need to perform a conventional recovery using backups and archived redo logs has decreased. However, performing a recovery is the DBA’s most important job. Database recovery is the one thing a DBA is not allowed to get wrong. Every Oracle database has at least two online redo log groups with at least a single member (redo log file) in each group. These online redo log groups are used in a circular fashion. Oracle will write to the log files in group 1, and when it gets to the end of the files in group 1, it will switch to log file group 2 and begin writing to that one. When it has filled log file group 2, it will switch back to log file group 1 (assuming you have only two redo log file groups; if you have three, Oracle would, of course, proceed to the third group). Redo logs, or transaction logs, are one of the major features that make a database a database. They are perhaps its most important recovery structure, although without the other pieces such as undo segments, distributed transaction recovery, and so on, nothing works. They are a major component of what sets a database apart from a conventional file system. The online redo logs allow us to effectively recover from a power outage—one that might happen while Oracle is in the middle of a write. The archived redo logs allow us to recover from media failures when, for instance, the hard disk goes bad or human error causes data loss. Without redo logs, the database would not offer any more protection than a file system. What Is Undo? Undo is conceptually the opposite of redo. Undo information is generated by the database as you make modifications to data to put it back the way it was before the modifications, in the event the transaction or statement you are executing fails for any reason or if you request it with a ROLLBACK statement. Whereas redo is used to replay a transaction in the event of failure— to recover the transaction—undo is used to reverse the effects of a statement or set of statements. Undo, unlike redo, is stored internally in the database in a special set of segments known as undo segments.

284<br />

CHAPTER 9 ■ REDO AND UNDO<br />

critical information <strong>and</strong> commit the operation, you can restore a backup of the affected<br />

data <strong>and</strong> recover it to the point in time immediately prior to the “accident” using online <strong>and</strong><br />

archived redo log files.<br />

Archived redo log files are simply copies of old, full online redo log files. As the system<br />

fills up log files, the ARCH process will make a copy of the online redo log file in another location,<br />

<strong>and</strong> optionally make several other copies into local <strong>and</strong> remote locations as well. These<br />

archived redo log files are used to perform media recovery when a failure is caused by a disk<br />

drive going bad or some other physical fault. <strong>Oracle</strong> can take these archived redo log files <strong>and</strong><br />

apply them to backups of the data files to catch them up to the rest of the database. They are<br />

the transaction history of the database.<br />

■Note With the advent of the <strong>Oracle</strong> 10g, we now have flashback technology. This allows us to perform<br />

flashback queries (i.e., query the data as of some point in time in the past), un-drop a database table, put a<br />

table back the way it was some time ago, <strong>and</strong> so on. As a result, the number of occasions where we need to<br />

perform a conventional recovery using backups <strong>and</strong> archived redo logs has decreased. However, performing<br />

a recovery is the DBA’s most important job. <strong>Database</strong> recovery is the one thing a DBA is not allowed to get<br />

wrong.<br />

Every <strong>Oracle</strong> database has at least two online redo log groups with at least a single member<br />

(redo log file) in each group. These online redo log groups are used in a circular fashion.<br />

<strong>Oracle</strong> will write to the log files in group 1, <strong>and</strong> when it gets to the end of the files in group 1, it<br />

will switch to log file group 2 <strong>and</strong> begin writing to that one. When it has filled log file group 2,<br />

it will switch back to log file group 1 (assuming you have only two redo log file groups; if you<br />

have three, <strong>Oracle</strong> would, of course, proceed to the third group).<br />

Redo logs, or transaction logs, are one of the major features that make a database a database.<br />

They are perhaps its most important recovery structure, although without the other<br />

pieces such as undo segments, distributed transaction recovery, <strong>and</strong> so on, nothing works.<br />

They are a major component of what sets a database apart from a conventional file system.<br />

The online redo logs allow us to effectively recover from a power outage—one that might happen<br />

while <strong>Oracle</strong> is in the middle of a write. The archived redo logs allow us to recover from<br />

media failures when, for instance, the hard disk goes bad or human error causes data loss.<br />

Without redo logs, the database would not offer any more protection than a file system.<br />

What Is Undo?<br />

Undo is conceptually the opposite of redo. Undo information is generated by the database as<br />

you make modifications to data to put it back the way it was before the modifications, in the<br />

event the transaction or statement you are executing fails for any reason or if you request it<br />

with a ROLLBACK statement. Whereas redo is used to replay a transaction in the event of failure—<br />

to recover the transaction—undo is used to reverse the effects of a statement or set of<br />

statements. Undo, unlike redo, is stored internally in the database in a special set of segments<br />

known as undo segments.

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

Saved successfully!

Ooh no, something went wrong!