Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 9 ■ REDO AND UNDO 287 ops$tkyte@ORA10G> select * from t; no rows selected Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 689 consistent gets ... ops$tkyte@ORA10G> set autotrace off The blocks that our INSERT caused to be added under the table’s high-water mark (HWM) are still there—formatted, but empty. Our full scan had to read them to see if they contained any rows. That shows that a rollback is a logical “put the database back the way it was” operation. The database will not be exactly the way it was, just logically the same. How Redo and Undo Work Together In this section, we’ll take a look at how redo and undo work together in various scenarios. We will discuss, for example, what happens during the processing of an INSERT with regard to redo and undo generation and how Oracle uses this information in the event of failures at various points in time. An interesting point to note is that undo information, stored in undo tablespaces or undo segments, is protected by redo as well. In other words, undo data is treated just like table data or index data—changes to undo generate some redo, which is logged. Why this is so will become clear in a moment when we discuss what happens when a system crashes. Undo data is added to the undo segment and is cached in the buffer cache just like any other piece of data would be. Example INSERT-UPDATE-DELETE Scenario As an example, we will investigate what might happen with a set of statements like this: insert into t (x,y) values (1,1); update t set x = x+1 where x = 1; delete from t where x = 2; We will follow this transaction down different paths and discover the answers to the following questions: • What happens if the system fails at various points in the processing of these statements? • What happens if we ROLLBACK at any point? • What happens if we succeed and COMMIT?
288 CHAPTER 9 ■ REDO AND UNDO The INSERT The initial INSERT INTO T statement will generate both redo and undo. The undo generated will be enough information to make the INSERT “go away.” The redo generated by the INSERT INTO T will be enough information to make the insert “happen again.” After the insert has occurred, we have the scenario illustrated in Figure 9-1. Figure 9-1. State of the system after an INSERT There are some cached, modified undo blocks, index blocks, and table data blocks. Each of these blocks is protected by entries in the redo log buffer. Hypothetical Scenario: The System Crashes Right Now Everything is OK. The SGA is wiped out, but we don’t need anything that was in the SGA. It will be as if this transaction never happened when we restart. None of the blocks with changes got flushed to disk, and none of the redo got flushed to disk. We have no need of any of this undo or redo to recover from an instance failure. Hypothetical Scenario: The Buffer Cache Fills Up Right Now The situation is such that DBWR must make room and our modified blocks are to be flushed from the cache. In this case, DBWR will start by asking LGWR to flush the redo entries that protect these database blocks. Before DBWR can write any of the blocks that are changed to disk, LGWR must flush the redo information related to these blocks. This makes sense: if we were to flush the modified blocks for table T without flushing the redo entries associated with the undo blocks, and the system failed, we would have a modified table T block with no undo information associated with it. We need to flush the redo log buffers before writing these blocks out so that we can redo all of the changes necessary to get the SGA back into the state it is in right now, so that a rollback can take place. This second scenario shows some of the foresight that has gone into all of this. The set of conditions described by “If we flushed table T blocks and did not flush the redo for the undo blocks and the system failed” is starting to get complex. It only gets more complex as we add users, and more objects, and concurrent processing, and so on.
- Page 282 and 283: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 284 and 285: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 286 and 287: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 288 and 289: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 290 and 291: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 292 and 293: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 294 and 295: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 296 and 297: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 298 and 299: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 300 and 301: CHAPTER 8 ■ ■ ■ Transactions
- Page 302 and 303: CHAPTER 8 ■ TRANSACTIONS 257 •
- Page 304 and 305: CHAPTER 8 ■ TRANSACTIONS 259 So,
- Page 306 and 307: CHAPTER 8 ■ TRANSACTIONS 261 X --
- Page 308 and 309: CHAPTER 8 ■ TRANSACTIONS 263 “s
- Page 310 and 311: CHAPTER 8 ■ TRANSACTIONS 265 busi
- Page 312 and 313: CHAPTER 8 ■ TRANSACTIONS 267 Many
- Page 314 and 315: CHAPTER 8 ■ TRANSACTIONS 269 ops$
- Page 316 and 317: CHAPTER 8 ■ TRANSACTIONS 271 last
- Page 318 and 319: CHAPTER 8 ■ TRANSACTIONS 273 Dist
- Page 320 and 321: CHAPTER 8 ■ TRANSACTIONS 275 Auto
- Page 322 and 323: CHAPTER 8 ■ TRANSACTIONS 277 3 Au
- Page 324 and 325: CHAPTER 8 ■ TRANSACTIONS 279 5 pr
- Page 326: CHAPTER 8 ■ TRANSACTIONS 281 scot
- Page 329 and 330: 284 CHAPTER 9 ■ REDO AND UNDO cri
- Page 331: 286 CHAPTER 9 ■ REDO AND UNDO Fir
- Page 335 and 336: 290 CHAPTER 9 ■ REDO AND UNDO We
- Page 337 and 338: 292 CHAPTER 9 ■ REDO AND UNDO Wha
- Page 339 and 340: 294 CHAPTER 9 ■ REDO AND UNDO row
- Page 341 and 342: 296 CHAPTER 9 ■ REDO AND UNDO If
- Page 343 and 344: 298 CHAPTER 9 ■ REDO AND UNDO ops
- Page 345 and 346: 300 CHAPTER 9 ■ REDO AND UNDO Inv
- Page 347 and 348: 302 CHAPTER 9 ■ REDO AND UNDO The
- Page 349 and 350: 304 CHAPTER 9 ■ REDO AND UNDO 41
- Page 351 and 352: 306 CHAPTER 9 ■ REDO AND UNDO ins
- Page 353 and 354: 308 CHAPTER 9 ■ REDO AND UNDO So,
- Page 355 and 356: 310 CHAPTER 9 ■ REDO AND UNDO ops
- Page 357 and 358: 312 CHAPTER 9 ■ REDO AND UNDO ops
- Page 359 and 360: 314 CHAPTER 9 ■ REDO AND UNDO •
- Page 361 and 362: 316 CHAPTER 9 ■ REDO AND UNDO ...
- Page 363 and 364: 318 CHAPTER 9 ■ REDO AND UNDO •
- Page 365 and 366: 320 CHAPTER 9 ■ REDO AND UNDO bac
- Page 367 and 368: 322 CHAPTER 9 ■ REDO AND UNDO As
- Page 369 and 370: 324 CHAPTER 9 ■ REDO AND UNDO ops
- Page 371 and 372: 326 CHAPTER 9 ■ REDO AND UNDO wil
- Page 373 and 374: 328 CHAPTER 9 ■ REDO AND UNDO Thi
- Page 375 and 376: 330 CHAPTER 9 ■ REDO AND UNDO ops
- Page 377 and 378: 332 CHAPTER 9 ■ REDO AND UNDO Whe
- Page 379 and 380: 334 CHAPTER 9 ■ REDO AND UNDO Tha
- Page 381 and 382: 336 CHAPTER 9 ■ REDO AND UNDO tou
CHAPTER 9 ■ REDO AND UNDO 287<br />
ops$tkyte@ORA10G> select * from t;<br />
no rows selected<br />
Statistics<br />
----------------------------------------------------------<br />
0 recursive calls<br />
0 db block gets<br />
689 consistent gets<br />
...<br />
ops$tkyte@ORA10G> set autotrace off<br />
The blocks that our INSERT caused to be added under the table’s high-water mark (HWM)<br />
are still there—formatted, but empty. Our full scan had to read them to see if they contained<br />
any rows. That shows that a rollback is a logical “put the database back the way it was” operation.<br />
The database will not be exactly the way it was, just logically the same.<br />
How Redo <strong>and</strong> Undo Work Together<br />
In this section, we’ll take a look at how redo <strong>and</strong> undo work together in various scenarios. We<br />
will discuss, for example, what happens during the processing of an INSERT with regard to redo<br />
<strong>and</strong> undo generation <strong>and</strong> how <strong>Oracle</strong> uses this information in the event of failures at various<br />
points in time.<br />
An interesting point to note is that undo information, stored in undo tablespaces or<br />
undo segments, is protected by redo as well. In other words, undo data is treated just like table<br />
data or index data—changes to undo generate some redo, which is logged. Why this is so will<br />
become clear in a moment when we discuss what happens when a system crashes. Undo data<br />
is added to the undo segment <strong>and</strong> is cached in the buffer cache just like any other piece of<br />
data would be.<br />
Example INSERT-UPDATE-DELETE Scenario<br />
As an example, we will investigate what might happen with a set of statements like this:<br />
insert into t (x,y) values (1,1);<br />
update t set x = x+1 where x = 1;<br />
delete from t where x = 2;<br />
We will follow this transaction down different paths <strong>and</strong> discover the answers to the<br />
following questions:<br />
• What happens if the system fails at various points in the processing of these statements?<br />
• What happens if we ROLLBACK at any point?<br />
• What happens if we succeed <strong>and</strong> COMMIT?