Immediate database modification is an approach in which a DBMS applies a transaction’s changes to the database while the transaction is still running, before it commits. Because those changes can reach disk before the transaction finishes, the log has to record enough to undo them after a rollback or crash, and to redo committed work that had not yet been written to disk.
How it works step by step
- The transaction reads a value, such as an account balance.
- It asks to write a new value. The DBMS logs the change, including the old value and the new value.
- The DBMS updates the data. The log entry must reach stable storage before the changed data can be written to disk.
- Further changes follow, and the database holds them while the transaction stays open.
- On COMMIT, the DBMS records a commit entry in the log. On ROLLBACK, it uses the logged old values to restore the data.
A simple example
Suppose account A has a balance of 500. Transaction T1 subtracts 50, so the balance becomes 450.
Under immediate modification, the database shows 450 while T1 is still open, even though T1 has not committed. The log holds both values for that change: the old value, 500, and the new value, 450.
- If T1 rolls back, the DBMS uses the old value to restore 500.
- If T1 commits, the balance stays at 450.
Why recovery needs both undo and redo
The DBMS may write changed data to disk before a transaction commits. A crash at that moment can leave uncommitted changes on disk. Recovery must undo them, so that the database reflects only committed work.
A crash can also happen after a transaction commits but before its changes reached disk. In that case, recovery must redo the committed changes from the log.
Textbook recovery usually runs a redo pass that repeats logged changes, followed by an undo pass that rolls back unfinished transactions. The details differ from one system to another.
Advantages and costs
Immediate modification has real benefits. A transaction reads its own changes directly from the database, without a separate workspace copy. Changes do not have to wait until commit before they are applied.
It also has costs:
- The log keeps old values as well as new ones, so it takes more space.
- Recovery is more involved, because both undo and redo may be needed.
- Other transactions can see uncommitted changes unless isolation rules block them. That risk is described in What is dirty read in DBMS?
Where it fits
Immediate modification is one of two approaches covered in deferred and immediate database modification. The other approach, deferred modification, holds a transaction’s changes until it commits, so the database is not touched until then.
How checkpoints fit in
Recovery has to know where to start reading the log. Checkpoints provide that starting point, and they are covered in What are checkpoints in a database management system (DBMS)? Checkpoints can write changes from transactions that later fail, which is why the undo step described above matters.
Summary
- Immediate modification writes changes to the database before the transaction commits.
- The log stores old and new values, so uncommitted work can be undone and committed work can be redone.
- The approach avoids a separate workspace for pending changes, but it needs more logging and more involved recovery.