Saturday, March 6, 2010

Undo and Redo concept ===>Aman's at its Best

Why not use Redo log for consistent read  by Aman:

Its not a stupid question as anything related to the functioning of Oralce database is not stupid. But the understanding of yours about redo is wrong. I do know that its termed that redo contains both the images of old and new but its actually not the correct thing. What redo contains is only change vectors which simply put, are just the changed entries of those places in the block which have got modified. This is true for both Data blocks which contain your data and Undo blocks as well which contain your old image. Think about it, what is an Undo Segment, its just like any other segment with the only difference that its maintained and modified by Oracle itself. When you modify a value, for example 100 to 200, the redo entery or the changed entry 200 is maintained in the Redo Buffer and from there, to the redo log file. The old entry must also modify the Undo Segment's some block and has to write itself there so there is a modification involved for that also which is again nothing but an update issued to the Undo segment. So the image changed for the Undo is also maintained in the Redo. Now, if let's suppose you lose both of your data block and the undo block, which means you lose the files of these two, using the redo you can recover both! Taht's what actually means by when it is said that redo contains both old and new image.

Now, if you have understood the above said, you must know that actual old/consistent image is maintained in the Undo segment only using which the consistent copy is created. Redo is used for the recovery whereas, Undo is used for the Read Consistacy.



Well, you haven't understood still I guess. Okay, let's take a little deep dive.

When you do an update to an Oracle data block, whch means you change a salary value from 100 to 200, a change is done over the actual value(here 100). For this change, a Change Vector is generated which is actually an information that tells to oracle that this thing has happened and a code representation of the Update command for this value is generated. This Change Vector gets logged in the redo log buffer. For a transaction, multiple changes are logged into a redo stream which isnothing but the collection of the change vectors of that transaction. So the first thing you need to remember from now is that with the DML, the entire block doesn't get logged into the log buffer. Think about it, how it is possible in anyway since the size of the log buffer, in general is much smaller compared to other memory areas. If we would log a full block inthe log buffer, with few blocks only, it would touch its maximum size. So this is something you need to understand and remember.

Now if you have understood what I just said, you should understand this as well that the Undo blocks are no different from the data blocks. Both contain segments only but for the Undo blocks, the control is not with you. Only Oracle knows how to go and make changes to them! That's all what is different for them other than data blocks. So if there would be an update happening to the Undo segment to maintain the old image, from the above example 100, it would also be doing the same behavior that the normal table Update statment would do to the EMP table. So again, the same thing would be going to the redo stream. That's what I explained that in the redo, both the entries for the change of your data and the change for the Undo image are maintained and that's what the meaning of both Old and New values in the redo since using it , both can be recovered.

If all what above said is clear to you, you should have got an answer for your question that the recovery is done using the Redo's change vectors which are applied to the data files( Undo is also stored in a datafile which is a part of a tablespace) . If the Undo required is not available for the recovery of the uncommitted data, that would also be recreated using the Redo only which would recover the Undo as well , regenerating all of its contents! So you do need to care about the Redo/Archive only as the rest is going to be taken care by the Change Vectors stored in them!

I hope this all makes some sense for you and would help.



  Thanks Aman  for the excellent explanation.
Best regards,

No comments:

Post a Comment