A report that ran fine last month now fails at 90% complete:
ORA-01652: unable to extend temp segment by 128 in tablespace TEMP
Or, during an index build:
ORA-01652: unable to extend temp segment by 8192 in tablespace USERS
The same error means two different things depending on which tablespace it names. Read that name first.
Case 1: TEMP (or another temporary tablespace)
Oracle uses the temporary tablespace for work that doesn't fit in memory (the PGA): large sorts (ORDER BY, GROUP BY, DISTINCT, UNION), hash joins, index builds' sort phase, and global temporary tables. ORA-01652 on TEMP means the sessions together needed more temp space than the tablespace could provide.
See who's using temp space right now:
SELECT s.sid, s.serial#, s.username, s.sql_id, u.tablespace,
ROUND(u.blocks * t.block_size / 1024 / 1024) AS mb, u.segtype
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;
And the history, for a query that already failed:
SELECT sql_id, MAX(temp_space_allocated) / 1024 / 1024 AS max_temp_mb
FROM dba_hist_active_sess_history -- requires the Diagnostics Pack license
WHERE sample_time > SYSDATE - 1
GROUP BY sql_id ORDER BY 2 DESC NULLS LAST FETCH FIRST 10 ROWS ONLY;
Fixes, in order of preference:
-
Fix the query that needs gigabytes of temp. Look at its plan for a huge hash join or sort caused by a missing join condition (a cartesian product), bad cardinality estimates (stale statistics), or sorting far more rows than needed. A query that needs 200 GB of temp space is usually a bug, not a sizing problem.
-
Give the PGA more memory if many legitimate operations spill:
PGA_AGGREGATE_TARGET(and a sensiblePGA_AGGREGATE_LIMIT). -
Add temp space when the workload really needs it:
ALTER TABLESPACE temp ADD TEMPFILE '/u02/oradata/PROD/temp02.dbf' SIZE 16G AUTOEXTEND ON NEXT 1G MAXSIZE 64G;Check the existing files in
dba_temp_files, and remember thatAUTOEXTENDcan't exceed free disk space. ASM and OMF setups can omit the file name. -
Separate heavy workloads: give ETL or reporting users their own temporary tablespace (
ALTER USER etl TEMPORARY TABLESPACE temp_etl) so one batch job can't starve the OLTP application.
Case 2: a permanent tablespace (USERS, DATA…)
When the error names a regular tablespace, the "temp segment" is the new object being built: during CREATE INDEX, CREATE TABLE … AS SELECT, ALTER TABLE … MOVE or ALTER INDEX … REBUILD, Oracle builds the new segment as a temporary segment in the target tablespace, and turns it into the permanent object at the end. The target tablespace ran out of room.
SELECT df.tablespace_name,
ROUND(SUM(df.bytes) / 1024 / 1024 / 1024, 1) AS size_gb,
ROUND(SUM(df.maxbytes) / 1024 / 1024 / 1024, 1) AS max_gb
FROM dba_data_files df
GROUP BY df.tablespace_name;
Fixes: add a datafile or enable autoextend on the target tablespace, build the object in a tablespace with more room, or free space first. Rebuilding an index or moving a table needs space for both the old and the new copy until the operation finishes.
Don't forget
- A failed statement releases its temp space, but a stuck session can keep holding it. Check
v$tempseg_usagebefore adding more. - On RAC, temp space is allocated per instance, and one instance can hold extents another needs. That usually balances itself out, but keep it in mind when one node reports shortages.
- Temp tablespaces are often shared by all users. Set up alerts on temp usage (for example over 80%) instead of finding out from users.
Checklist
- Check which tablespace the error names:
TEMPmeans sorts and hashes; a permanent tablespace means the object being built. - For
TEMP: find the heavy sessions or SQL, fix bad plans first, then tune PGA or add tempfiles. - For permanent tablespaces: add space for the new object (old and new copies coexist during rebuilds).
- Isolate heavy workloads in their own temp tablespace, and alert on usage.
Get the weekly commit
New database deep dives every week.
