Oracle Undo

Undo is the value before a transaction changed it. When Oracle modifies a block, it first records the pre-change value in an undo segment, and uses it in three places: when rolling back a transaction, when another session reads a block that is not yet committed or that changed after its query started (read consistency), and in flashback queries that read data as of a past point in time.

Key specs

ItemValue
Storage locationUndo tablespace (UNDO_TABLESPACE). One per instance in RAC
Management modeAutomatic undo management (UNDO_MANAGEMENT = AUTO, default)
Retention targetUNDO_RETENTION (seconds, default 900). On a fixed-size tablespace, it is tuned automatically to fit the space
Retention guaranteeWith RETENTION GUARANTEE, undo within the retention period is not overwritten even when space runs short (DML can fail instead)
Undo statesACTIVE (in-progress transaction), UNEXPIRED (committed but within retention), EXPIRED (reusable)
Typical errorsORA-01555: snapshot too old, ORA-30036 (out of undo space)
ProtectionChanges to undo blocks are also recorded in redo
Temporary tablesSince 12c, TEMP_UNDO_ENABLED lets you place undo for temporary tables in the temporary tablespace

How it works

Figure. Read consistency: building an old block from undo

flowchart LR
  Q["Query starts<br/>SCN 100"] --> B["Current block<br/>changed at SCN 120"]
  B --> C{"Did the block change after the query started?"}
  C -- "No" --> R["Read as is"]
  C -- "Yes" --> U["Follow the undo address in the ITL<br/>and apply the undo record"]
  U --> CR["CR block<br/>contents as of SCN 100"]
  U -. "Undo was overwritten" .-> E["ORA-01555"]

Diagram explanation: A query reads as of the SCN (system change number) at its start. If the Current block it reads changed after that, Oracle follows the undo address recorded in the block header's ITL, applies the pre-change values backward to build a CR block (consistent read copy) in the buffer cache, and reads that. If the undo needed for this has already been overwritten by other transactions, the old image cannot be built and ORA-01555 occurs.

Readers do not wait. Even if another session has modified a row and not committed, a plain SELECT reads the old version built from undo, so it does not wait for a lock. In exchange, the more blocks have changed, the higher the cost of building CR blocks (undo reads counted in consistent gets), and long queries slow down.

ORA-01555. It occurs when other transactions commit many changes and reuse undo while a long query is running. Set UNDO_RETENTION longer than the longest query and enlarge the undo tablespace, or split the query into shorter ones. Code that commits often inside a loop while reading the same table (fetch across commit) is also a common cause.

Flashback. Queries as of a past time use undo within the retention period. AS OF TIMESTAMP and AS OF SCN queries, FLASHBACK TABLE ... TO TIMESTAMP, and DBMS_FLASHBACK all use undo.

Cleanup. There is no separate cleanup job like PostgreSQL's VACUUM or InnoDB's purge. New transactions simply reuse undo extents whose retention period has passed.

Settings and checks

SELECT name, value FROM v$parameter WHERE name IN ('undo_management', 'undo_tablespace', 'undo_retention');

-- Undo usage per 10 minutes, longest query time, ORA-01555 count, auto-tuned retention time
SELECT begin_time, undoblks, maxquerylen, ssolderrcnt, nospaceerrcnt, tuned_undoretention
FROM v$undostat ORDER BY begin_time DESC FETCH FIRST 12 ROWS ONLY;

-- Undo size by state
SELECT status, SUM(bytes) / 1024 / 1024 AS mb FROM dba_undo_extents GROUP BY status;

-- Undo usage of in-progress transactions
SELECT s.sid, s.username, t.used_ublk, t.used_urec, t.start_time
FROM v$transaction t JOIN v$session s ON s.taddr = t.addr;

-- Query the data as of 10 minutes ago
SELECT * FROM orders AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '10' MINUTE WHERE id = 42;

ALTER SYSTEM SET undo_retention = 3600;
ALTER TABLESPACE undotbs1 RETENTION GUARANTEE;

Comparison with other DBMSs

OracleInnoDBPostgreSQLSQL Server
Where old versions liveUndo tablespaceUndo tablespace (undo log)Old row versions inside the tableVersion store in tempdb (PVS inside the database with ADR)
CleanupReused after the retention periodPurge threadVACUUMVersion cleanup task
When the old version is missingORA-01555A long transaction blocks purge and undo growsA long transaction blocks VACUUM and the table growsThe version store grows

In the articles

NOTE The default retention time and the automatic tuning behavior can vary by version and tablespace settings.

Related pages