top of page

Solving ORA-01555 and ORA-01652: Oracle UNDO and TEMP Management Step by Step

OmerNarman
Sep 7
6 min read

Updated: Oct 24

Two errors every Oracle DBA meets sooner or later are ORA-01555: snapshot too old and ORA-01652: unable to extend temp segment. Both usually appear at night, in a long batch job that has been running for hours. Adding space blindly sometimes helps, but only if you understand which space is missing and why.


This post explains what UNDO and TEMP do, then goes step by step through diagnosing and fixing each error.



UNDO and TEMP in Two Paragraphs

UNDO stores the "before image" of every change. It is used to roll back transactions, and, just as important, to give queries a read-consistent view: a query that started at 10:00 sees the data as it was at 10:00, even if other sessions change it later. Oracle rebuilds the old version of a block from UNDO. If that UNDO has already been overwritten, the query fails with ORA-01555.


TEMP is work space for operations that do not fit in memory (the PGA): large sorts, hash joins, index builds, global temporary tables and so on. If a single operation (or all of them together) needs more TEMP than the temporary tablespace can provide, it fails with ORA-01652.



Part 1: ORA-01555 Snapshot Too Old


Step 1: Read the Error Message Carefully

The full error tells you which undo segment was involved and, often, the SQL and how long it ran. Always find the full message in the alert log first.


Shell

grep -B2 -A3 "ORA-01555" $ORACLE_BASE/diag/rdbms/prod/PROD/trace/alert_PROD.log | tail -20


What to expect: A line such as "ORA-01555 caused by SQL statement below (SQL ID: 7h2k9...., Query Duration=14532 sec, SCN: 0x...)". The query duration is the key number: it tells you how long UNDO needed to be kept for this query to succeed.



Step 2: Check the UNDO Configuration

With automatic undo management, Oracle tries to keep committed undo for at least UNDO_RETENTION seconds. If the undo tablespace can autoextend, Oracle tunes the retention automatically; if it is fixed in size, Oracle keeps as much as the space allows.


SQL

SHOW PARAMETER undo

SELECT tablespace_name, retention, status

FROM dba_tablespaces

WHERE contents = 'UNDO';

SELECT file_name, ROUND(bytes/1024/1024/1024,1) AS gb, autoextensible,

ROUND(maxbytes/1024/1024/1024,1) AS max_gb

FROM dba_data_files

WHERE tablespace_name IN (SELECT tablespace_name FROM dba_tablespaces WHERE contents = 'UNDO');


What to expect: UNDO_MANAGEMENT=AUTO, an UNDO_TABLESPACE (one per instance on RAC), UNDO_RETENTION in seconds (default 900) and RETENTION showing NOGUARANTEE or GUARANTEE.



Step 3: Analyse V$UNDOSTAT

V$UNDOSTAT keeps a row for every 10-minute interval over the last few days. It shows the longest query, the retention Oracle actually achieved, and how many ORA-01555 errors and out-of-space errors happened.


SQL

SELECT TO_CHAR(begin_time,'MM-DD HH24:MI') AS begin_time,

ROUND(undoblks * 8192/1024/1024) AS undo_mb_used,

maxquerylen,

maxqueryid,

tuned_undoretention,

ssolderrcnt AS ora_1555,

nospaceerrcnt AS no_space

FROM v$undostat

WHERE begin_time > SYSDATE - 1

ORDER BY begin_time DESC;


How to read it:


  • MAXQUERYLEN is the longest running query in that interval, in seconds. MAXQUERYID is its SQL_ID.

  • TUNED_UNDORETENTION is how long Oracle managed to keep undo. If it is lower than MAXQUERYLEN when the error occurred, the undo tablespace is too small.

  • SSOLDERRCNT counts ORA-01555 errors. NOSPACEERRCNT counts transactions that failed because undo was full.


(The query assumes an 8 KB block size. Replace 8192 with your DB_BLOCK_SIZE.)



Step 4: Size the UNDO Tablespace

The undo you need is roughly: undo generation rate multiplied by the retention you need. Oracle can calculate this for you from V$UNDOSTAT history.


SQL

-- Undo needed for a 4-hour (14400 second) retention, based on recent history

SELECT ROUND(DBMS_UNDO_ADV.REQUIRED_UNDO_SIZE(14400)/1024) AS required_gb

FROM dual;

-- Manual calculation: peak undo blocks per second times retention

SELECT ROUND(MAX(undoblks/((end_time-begin_time)*86400)) * 14400 * 8192

/1024/1024/1024, 1) AS required_gb

FROM v$undostat;


What to expect: A size in GB. Add 20% headroom. If the current undo tablespace is smaller, grow it.



Step 5: Grow UNDO and Set the Retention

Give the undo tablespace the space it needs, and set UNDO_RETENTION to cover your longest important query. Use a fixed size (or a sensible MAXSIZE) so a runaway transaction cannot fill the disk.


SQL

ALTER TABLESPACE undotbs1 ADD DATAFILE '+DATA' SIZE 30G AUTOEXTEND ON MAXSIZE 32767M;

ALTER SYSTEM SET undo_retention = 14400 SCOPE=BOTH SID='*';


What this does: The extra datafile gives Oracle room to keep old undo longer. UNDO_RETENTION=14400 tells Oracle to aim for four hours.


Tip: RETENTION GUARANTEE (ALTER TABLESPACE undotbs1 RETENTION GUARANTEE) makes Oracle keep undo for the full retention even if that means new transactions fail for lack of space. Use it only when long queries are more important than DML, for example during a consistent export.


Step 6: Fix the Query Itself

Often the real fix is in the application, not in UNDO. Two patterns cause most ORA-01555 errors:


  • Fetch across commit: a PL/SQL loop opens a cursor on a table, then updates and commits the same table inside the loop. The cursor needs old versions of blocks the loop keeps changing. Commit less often, or process in independent batches.

  • A very slow query: if MAXQUERYLEN is many hours, tune the SQL (see the SQL tuning and AWR posts on this blog). A query that runs 10 minutes instead of 4 hours will not hit ORA-01555.


SQL

-- Find the statement from V$UNDOSTAT.MAXQUERYID

SELECT sql_id, executions, ROUND(elapsed_time/1e6/NULLIF(executions,0)) AS sec_per_exec,

SUBSTR(sql_text,1,100) AS sql_text

FROM v$sql

WHERE sql_id = '7h2k9abcd1234';



Part 2: ORA-01652 Unable to Extend Temp Segment


Step 7: Identify Which Tablespace Ran Out

ORA-01652 names the tablespace. If it is a temporary tablespace, the problem is sort and hash space. If it names a permanent tablespace (for example during CREATE INDEX or CREATE TABLE AS SELECT), the temporary segment for the new object is being built in that permanent tablespace, and you need to add space there instead.


Shell

grep "ORA-01652" $ORACLE_BASE/diag/rdbms/prod/PROD/trace/alert_PROD.log | tail -5

# ORA-01652: unable to extend temp segment by 128 in tablespace TEMP



Step 8: See Who Is Using TEMP Right Now

V$TEMPSEG_USAGE shows every session currently holding temporary space, with the SQL that is using it. This is what you check while the problem is happening.


SQL

SELECT s.sid, s.serial#, s.username, s.program, u.sql_id,

u.segtype, u.tablespace,

ROUND(u.blocks * t.block_size/1024/1024) AS temp_mb

FROM v$tempseg_usage u

JOIN v$session s ON s.saddr = u.session_addr

JOIN dba_tablespaces t ON t.tablespace_name = u.tablespace

ORDER BY u.blocks DESC

FETCH FIRST 20 ROWS ONLY;

-- Overall TEMP usage

SELECT tablespace_name,

ROUND(tablespace_size/1024/1024/1024,1) AS size_gb,

ROUND(allocated_space/1024/1024/1024,1) AS allocated_gb,

ROUND(free_space/1024/1024/1024,1) AS free_gb

FROM dba_temp_free_space;


What to expect: One or two sessions often use most of TEMP. SEGTYPE shows SORT, HASH, DATA (global temporary tables), INDEX or LOB_DATA. The SQL_ID tells you which statement to look at.



Step 9: Find Who Used TEMP in the Past

If the error happened last night, the sessions are gone. ASH keeps the TEMP usage of each sampled session, so you can find the statement afterwards (Diagnostics Pack licence required).


SQL

SELECT sql_id, MAX(temp_space_allocated)/1024/1024/1024 AS max_temp_gb,

MIN(sample_time) AS first_seen, MAX(sample_time) AS last_seen

FROM dba_hist_active_sess_history

WHERE sample_time BETWEEN TIMESTAMP '2026-09-06 01:00:00' AND TIMESTAMP '2026-09-06 04:00:00'

AND temp_space_allocated > 0

GROUP BY sql_id

ORDER BY max_temp_gb DESC

FETCH FIRST 10 ROWS ONLY;


What to expect: The statements that used the most TEMP during the failure window. If one SQL needed 200 GB of TEMP, adding 10 GB will not help: the plan needs to change (for example a missing join condition, or a hash join on the wrong row source).



Step 10: Add TEMP Space

If the usage is legitimate, add space. Temporary tablespaces use tempfiles, which are created instantly and are not backed up by RMAN.


SQL

ALTER TABLESPACE temp ADD TEMPFILE '+DATA' SIZE 20G AUTOEXTEND ON NEXT 1G MAXSIZE 32767M;

SELECT file_name, ROUND(bytes/1024/1024/1024,1) AS gb, autoextensible

FROM dba_temp_files;


Tip: Give heavy batch users their own temporary tablespace (ALTER USER batch_user TEMPORARY TABLESPACE temp_batch). Then one large report cannot take all the TEMP space from the online users.


Step 11: Shrink TEMP After an Exceptional Load

After a one-time job made TEMP grow very large, you can give the space back. Shrinking is online and only releases space that is not currently in use.


SQL

ALTER TABLESPACE temp SHRINK SPACE KEEP 20G;

-- Or shrink a single tempfile

ALTER TABLESPACE temp SHRINK TEMPFILE '+DATA/PROD/TEMPFILE/temp.264.1153' KEEP 10G;



Step 12: Use Resumable Space Allocation for Batch Jobs

Instead of failing immediately when space runs out, a session can be suspended and wait for you to add space. When you add it, the statement simply continues. This can save hours of rework on long batch jobs.


SQL

-- Enable for the whole instance: wait up to 2 hours (7200 s)

ALTER SYSTEM SET resumable_timeout = 7200 SCOPE=BOTH SID='*';

-- Or only for one session

ALTER SESSION ENABLE RESUMABLE TIMEOUT 7200 NAME 'nightly_load';

-- See suspended statements

SELECT session_id, name, status, error_msg, suspend_time

FROM dba_resumable;


What to expect: When space runs out, the alert log reports "statement suspended, wait error to be cleared". DBA_RESUMABLE shows the error. Add space (Step 5 or Step 10), and the session resumes without failing.



Summary

ORA-01555 means the undo a query needed was overwritten: compare MAXQUERYLEN with TUNED_UNDORETENTION in V$UNDOSTAT, size UNDO with DBMS_UNDO_ADV, set UNDO_RETENTION, and fix fetch-across-commit code or slow SQL. ORA-01652 means an operation needed more temporary space: find the SQL with V$TEMPSEG_USAGE or ASH, decide whether the usage is legitimate, then add TEMP or tune the statement. For batch work, resumable space allocation turns a failure into a pause.

Recent Posts

See All

Comments


bottom of page