KB · Oracle Database · Fix

"ORA-01652: unable to extend temp segment": who is using TEMP right now

Oracle Database · Fix · Verified on Oracle 23ai · Updated 2026-12-03

Error this answers

  • ORA-01652: unable to extend temp segment

Symptom

A query, index build or batch job fails with:

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

Why it happens

Sorts, hash joins, index builds and global temporary tables spill to the TEMP tablespace when they don't fit in memory. One statement with a bad plan (a hash join on two big tables, a sort of millions of rows) can use all of it, and then every other session that needs TEMP fails too. So the session that gets the error is often not the one using the space.

Check

SQL> @tmp.sql
SQL> @sort.sql

tmp.sql shows how big TEMP is and how much is free. sort.sql shows who is using it, ending with every session, its SQL_ID, segment type and extents on every instance.

tmp.sqlMIT · Download ·
-- tmp.sql: TEMP tablespace size, allocation and free space
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/temp-tablespace-usage/
--
-- Usage: @tmp.sql

clear col
ttit off
btit off
set lines 132
set head on
col file_name format a45
col tablespace_name format a10
col mbytes format 999,999,999

select tablespace_name,file_id,file_name,bytes/1024/1024 mbytes from dba_temp_files ;
select * from v$temp_extent_pool;
select * from v$temp_space_header;

col tot_mb format 999,999,999
col alloc_mb format 999,999,999
col free_mb format 999,999,999

SELECT TABLESPACE_NAME
,      TABLESPACE_SIZE/1024/1024 tot_mb
,      ALLOCATED_SPACE/1024/1024 alloc_mb
,      FREE_SPACE/1024/1024      free_mb
FROM dba_temp_free_space;

set echo on
/*
	To get some space back:
	ALTER TABLESPACE temp SHRINK SPACE;
	ALTER TABLESPACE temp SHRINK SPACE KEEP 40M;

*/

curl -O https://hashjoin.com/kb/oracle/temp-tablespace-usage/tmp.sql

sort.sqlMIT · Download ·
-- sort.sql: who is using TEMP right now: sort segments and per-session usage
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/temp-tablespace-usage/
--
-- Usage: @sort.sql

set echo off

prompt CURRENT ACTIVE SORTS
prompt =====================
prompt from (v$sort_segment)
prompt      (v$session)
prompt =====================

col tablespace_name format a10
set lines 132
set trims on

select vsg.tablespace_name
,      vsg.current_users
,      vsg.total_extents
,      vsg.used_extents
,      vsg.extent_hits
,      vsg.max_used_blocks
,      vsg.max_sort_blocks
from   v$sort_segment   vsg;


prompt CURRENT SORT SEGMENTS
prompt =====================
prompt from (v$sort_segment)
prompt =====================

set pages 0
set feedback off
set verify off
col block_size noprint new_value x

select value/1024 block_size, 'Getting DB_BLOCK_SIZE ...'
from v$parameter
where name='db_block_size';


set lines 155
set feedback on
set pages 60
set trims on

SELECT tablespace_name
,      extent_size*&&x                ext_size_KB
,      total_extents*extent_size*&&x  tot_tmp_KB
,      used_extents*extent_size*&&x   used_KB
,      free_extents*extent_size*&&x   free_KB
,      max_used_size*extent_size*&&x  max_used_KB
FROM v$sort_segment;

prompt SORT usage
prompt ===================
prompt from (v$sort_usage)
prompt ===================

col sid_serial format a10 Heading "Sid,Serial"
col machine format a20 trunc
col program format a15 trunc
col kb format 99999999999 heading "KB"
col tablespace format a7 trunc heading "TS name"
col extents format 99999 Heading "Ext"
col idle format a8 heading "Idle"

break on status skip 2

compute sum of kb on status

SELECT s.username, s.sid||','||s.serial# sid_serial, TO_CHAR(s.logon_time,'mon-dd hh24:mi') logon_time,
       floor(last_call_et/3600)||':'||
          floor(mod(last_call_et,3600)/60)||':'||
          mod(mod(last_call_et,3600),60) IDLE,
       s.machine, s.program,
       s.status, u.tablespace, u.contents, u.extents, u.blocks*&&x KB
   FROM v$session s, v$sort_usage u
   WHERE s.saddr=u.session_addr;



set lines 132
set pages 1000
col osuser format a10
col sid format 99999
col machine format a30 trunc
col module format a20 trunc
col username format a10
col serial# format 9999999
col tablespace format a4 heading "TBSP"
col EXTENTS format 999999999

break on tablespace skip 2
compute sum of EXTENTS on tablespace

select a.sid,a.serial#,a.osuser,a.username,a.machine,a.module,c.sql_id,c.SEGTYPE,c.EXTENTS, tablespace
from gv$session a,
gv$tempseg_usage c
where a.SADDR=c.session_addr
and a.inst_id=c.inst_id
order by 1;


set echo on

curl -O https://hashjoin.com/kb/oracle/temp-tablespace-usage/sort.sql

Fix

Verify

Rerun the failing job, and watch sort.sql while it runs.

Gotchas

What it reads

gv$tempseg_usage v$sort_usage v$sort_segment dba_temp_files dba_temp_free_space v$temp_space_header

Tired of fixing the same thing twice?

HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.