Reputation: 19918
If there is WAL (redo) log available, then it would allow us to persist to disk the records and commit the transaction without needing to update the data structures first.
Since you can always re-apply from the redo log and update the data structures on crashes or failures, why do we still have a need to undo the log.
My understanding for this question is that we still need undo log because:
Is my understanding correct about why we need undo logging even if redo log exists?
Upvotes: 4
Views: 2070
Reputation: 562270
The assumption is that commits happen more often than rollbacks. I.e. a client would not have executed an update if they didn't intend that it should be committed. Rollbacks are the exception.
When you execute a change, the change is written to the data pages, and the old version of the page is written to the undo log. The change is also written to the redo log for crash recovery. On commit, then the only thing that needs to happen is that the new version is now considered committed. The copy in the undo log may be scheduled to be discarded as garbage, at least once other concurrent transactions no longer need it for their MVCC snapshot. In fact, there may be multiple versions of a given row in the undo log, to satisfy different snapshots.
If there were no undo log, and everything relied on the redo log, then the consequences would be:
Given all this, it seems like using an undo log helps optimize for the most common case: new transactions reading fresh snapshots of the most recently-committed data, as pages directly from the buffer pool.
Upvotes: 2