Tech Interview RoomStudy guides

Oracle

How does Oracle provide read consistency? Explain undo, SCN and ORA-01555.

Intermediate · 2 min read · Lesson 2 of 5 in Oracle

Oracle uses multi-version read consistency based on undo:

  • Every change records the old values in undo segments (in the UNDO tablespace).
  • Every committed change has an SCN (System Change Number), a logical clock.
  • A query sees data as of the SCN at which it started (statement-level consistency by default; the whole transaction for SERIALIZABLE or read-only transactions). When it reads a block changed after its SCN, Oracle reconstructs the older version from undo.

Result: readers never block writers and writers never block readers, and there are no dirty reads. Oracle's default isolation is Read Committed.

ORA-01555 "snapshot too old"

A long query needs undo to rebuild an old version of a block, but that undo was already overwritten because the undo space was reused. Causes: long queries combined with heavy updates, a small UNDO tablespace or low UNDO_RETENTION, and fetching across commits (committing inside a loop over an open cursor).

Fixes: size the undo tablespace and set UNDO_RETENTION (with autoextend or RETENTION GUARANTEE), shorten long queries, avoid fetch-across-commit, run reports on a standby (Active Data Guard).

Bonus features from undo

Flashback Query (AS OF TIMESTAMP) reads past data, Flashback Table restores it, and Flashback Database rewinds the whole database (with flashback logs).

Example

sql
-- See a row as it was 15 minutes ago (undo still available)
SELECT * FROM orders AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '15' MINUTE)
WHERE order_id = 1001;

-- Undo an accidental delete
INSERT INTO orders
SELECT * FROM orders AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '15' MINUTE)
WHERE order_id = 1001;

-- Current SCN and undo settings
SELECT current_scn FROM v$database;
SHOW PARAMETER undo_retention;

Follow-up questions interviewers ask

Try answering each one before you open it.

How is this different from PostgreSQL's MVCC?

Both give consistent snapshots without read locks. PostgreSQL keeps old row versions in the table itself and needs VACUUM to remove them; Oracle updates rows in place and keeps old versions in separate undo segments, so there is no table bloat from updates, but long queries can hit ORA-01555 when undo is recycled.