Transaction States
A transaction is not simply "running" or "done." It passes through states, and the DBMS makes different guarantees at each one.
In plain English
A transaction is like a parcel: packed, ready, shipped, delivered, or returned.
The one idea
Transaction states make commit and abort explicit: only a committed transaction is durable, and only an active or failed one can be rolled back.
Recovery decisions depend on where a transaction was when the system crashed. The state tells the recovery manager whether to redo or undo.
By the end
- 01Name the five transaction states.
- 02Trace normal and aborted execution paths.
- 03Explain the partially committed state.
- 04Know what happens after a failure.
The state diagram
A transaction starts active. It becomes partially committed after its last operation but before the commit is durable. Once the commit record is safely stored, it is committed. If an error occurs, it enters the failed state and then aborted after rollback. A terminated transaction has finished, whether committed or aborted.
active -> partially committed -> committed
| |
v v
failed ---------> abortedNormal execution
During the active state, the transaction reads and writes data and holds locks. In the partially committed state, all operations are done but the commit is not yet durable. The DBMS writes the commit record and flushes the log; then the transaction is committed and its locks can be released.
Failure and rollback
A failure can come from a constraint violation, a deadlock, a user ROLLBACK, or a system crash. The transaction enters the failed state, the recovery system undoes its changes using the log, and it becomes aborted. The application may retry with a new transaction.
A crash in the partially committed window
T1 has executed its last statement but has not yet written the commit record.
The last write completes; T1 enters partially committed.
The changes may still be in memory or in an uncommitted log record.
The system crashes before the commit record is durable.
Recovery treats T1 as uncommitted and undoes its writes.
The commit record reaches disk before the crash.
Recovery redoes T1 if needed and makes the result visible.
ConclusionCommit is a durable event, not the moment the last SQL statement returns. The log record is the boundary.
Instrument
Watch it happen.
Step through one operation at a time
BEGIN
The transaction starts and begins reading and writing data. Its changes are not visible to other transactions.
The trade-off
No free correctness.
Gain
A precise recovery contract and a clear visibility rule.
Cost
A commit must force a log write, and that fsync is often the latency bottleneck.
Verdict
Batch commits when possible, but never report success before the commit record is durable.
Recap
What to keep.
Takeaways
- +The five states are active, partially committed, committed, failed, and aborted.
- +Partially committed means the work is done but the commit is not durable yet.
- +Committed means the commit record is safely stored.
- +Failed transactions are rolled back and become aborted.
- +A terminated transaction is either committed or aborted.
Traps
- !Thinking partially committed is already durable.
- !Releasing locks before the commit is durable.
- !Retrying a transaction without considering idempotence.
Focus
- →Draw the state diagram.
- →Explain the difference between partially committed and committed.
- →Trace a crash before and after the commit record.
Check yourself
Before you turn the page.
Which state means all operations are done but the commit is not yet durable?
What happens to a failed transaction?