Identifying the exact SQL statement causing ORA-1652 can often be challenging and time-consuming. Understand that Oracle uses temporary tablespaces as work areas for tasks such as sorting operations for users and sorting during index creation. Increasing the temporary tablespace size can be the quickest way to resolve the issue, but it is only sometimes the long-term solution. However, understanding the root cause of ORA-1652 is essential for preventing future occurrences.

With the help of the document that Oracle created (Doc ID 364417.1 – How Can Temporary Segment Usage Be Monitored Over Time? ), we could identify the top consumers of the temporary segments. With it, queries can be investigated and tuned. We need to create a table, procedure, and job.

– TABLE
CREATE TABLE XX_TEMP_SEG_USAGE(
DATE_TIME DATE,
USERNAME VARCHAR2(30),
SID VARCHAR2(6),
SERIAL# VARCHAR2(6),
OS_USER VARCHAR2(30),
SPACE_USED NUMBER,
SQL_TEXT VARCHAR2(1000));

– PROCEDURE
CREATE OR REPLACE PROCEDURE XX_TEMP_SEG_USAGE_INSERT IS
BEGIN
insert into XX_TEMP_SEG_USAGE
SELECT sysdate,a.username, a.sid, a.serial#, a.osuser, b.blocks, c.sql_text
FROM v$session a, v$sort_usage b, v$sqlarea c
WHERE b.tablespace = 'TEMP'
and a.saddr = b.session_addr
AND c.address= a.sql_address
AND c.hash_value = a.sql_hash_value
AND b.blocks*(select block_size from dba_tablespaces where tablespace_name = b.tablespace) > 1024*1024;
COMMIT;
END;
/

– JOB
BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name        => 'XX_MONITOR_TEMP_USAGE_JOB',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN XX_TEMP_SEG_USAGE_INSERT; END;',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'FREQ=MINUTELY;INTERVAL=5',
    end_date        => NULL,
    enabled         => TRUE,
    comments        => 'Job created to monitor temp usage.');
END;
/

Preventing Future ORA-1652 Errors with Proactive Monitoring

Once everything is in place, you can periodically monitor the table XX_TEMP_SEG_USAGE; additionally, monitor the space usage of the table as it can grow depending on the data it is capturing, delete rows, or truncate the table as needed. I would only enable the job if troubleshooting is required, but if you need it long-term, you can create another job to handle the purging.

For any questions, please contact us!

Share This