Both PostgreSQL and Oracle provide read consistency (consistent reads), but they implement it very differently.
| Aspect | PostgreSQL (MVCC) | Oracle (Undo-based Consistency) |
|---|---|---|
| Old row versions | Stored directly in the table (tuple versions) | Stored in Undo/Rollback segments |
| UPDATE | Creates a new row version | Updates row in place and saves old image in Undo |
| Read consistency | Reader chooses visible tuple version based on transaction snapshot | Reader reconstructs old version from Undo if needed |
| VACUUM/cleanup | VACUUM removes dead tuples | Undo retention manages old versions |
| Disk usage | Table can grow due to multiple versions | Undo tablespace grows |
| Snapshot storage | Snapshot contains Transaction IDs (XIDs) | SCN (System Change Number) based |
PostgreSQL MVCC
Suppose table contains:
Transaction T1:
What happens internally:
PostgreSQL does not overwrite the row.
Instead:
A transaction started before T1 sees:
A transaction started after commit sees:
No undo lookup is required because both versions exist in the table.
Advantage
Reads are very fast:
No rollback segment access.
Disadvantage
Dead tuples accumulate:
Requires:
to reclaim space.
Oracle Undo-Based Consistency
Same update:
Oracle performs:
- Update row in table block.
- Store previous image (
John) in Undo segment.
If another transaction started earlier wants the row:
Oracle detects:
Then it uses Undo:
Advantage
Table does not contain multiple row versions.
Disadvantage
Consistent reads may require:
extra I/O.
Example Timeline
PostgreSQL
Reader R1 started before update:
Reader R2 started after commit:
Both versions coexist.
Oracle
Table:
Undo:
Reader before update:
Reader after update:
Why Oracle Can Throw "Snapshot Too Old"
Oracle relies on Undo.
If long query needs an old version:
and Undo has already been overwritten:
This occurs because Oracle lost the history required to reconstruct the row.
Why PostgreSQL Usually Doesn't Have This Problem
PostgreSQL keeps old row versions in the table itself.
Long-running transaction:
Old tuples cannot be vacuumed until that transaction ends.
Result:
but:
Trade-off:
Architecture View
PostgreSQL
Oracle
Interview Answer (Short)
PostgreSQL MVCC stores multiple row versions directly in the table and uses transaction snapshots (XIDs) to determine visibility. Updates create new tuples, and old tuples are later removed by VACUUM.
Oracle stores only the latest row version in the table and keeps old versions in Undo segments. Consistent reads are generated by reconstructing older versions using Undo records and SCNs.
In one line:
PostgreSQL achieves consistency by storing multiple row versions in the data table, while Oracle achieves consistency by reconstructing older versions from Undo segments when required.
No comments:
Post a Comment