Site icon IT Tutorial

How to Drop and Recreate Temp Tablespace in Oracle Database

Hi,

I will explain How to Drop and Recreate Temp Tablespace in Oracle Database in this article.

 

I have seen ” enq: SS – contention Wait Event ” wait event of running session.

This wait event means, there are some problems in temp tablespace.

you can query the enq: SS – contention wait event and object, user details.

SELECT DISTINCT u.username,
u.osuser,
w.event,
w.p2text AS reason,
ts.name AS tablespace,
NVL (ddf.file_name, dtf.file_name)
FROM v$session_wait w,
v$session u,
v$tablespace ts
LEFT OUTER JOIN dba_data_files ddf ON ddf.tablespace_name = ts.name
LEFT OUTER JOIN DBA_TEMP_FILES dtf ON dtf.tablespace_name = ts.name
WHERE u.sid = w.sid AND w.p2 = ts.TS# AND w.event = 'enq: SS - contention';

 

 

Temp tablespace has been 20TB, but most of this size is related with fragmentation.

To solve this problem, you need to create a new temporary tablespace and drop existing temp tablespace.

 

Firstly, you need to create the new temporary tablespace, and alter users’ default temporary tablespaces, then wait for temporary objects completion or you may kill related sessions.

If the existing tablespace has been empty, then you can drop the old tablespace.

 

Step-1: Create the new temporary tablespace.

CREATE TEMPORARY TABLESPACE TEMP_NEW TEMPFILE
'+DATA' SIZE 100M AUTOEXTEND ON NEXT 1G MAXSIZE UNLIMITED
TABLESPACE GROUP ''
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;

 

Step-2: Alter default temporary tablespace of database.

ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEMP_NEW;

 

Step-3: alter application user’s temporary tablespace as follows.

alter user MSD temporary tablespace TEMP_NEW;

 

Step-4: Find the related objects that are using the old temporary tablespace.

select tu.tablespace,tu.username,s.sid,s.serial#,s.inst_id from gv$tempseg_usage tu, gv$session s
where tu.session_addr=s.saddr;


Generate kill session script as follows.

select 'ALTER SYSTEM KILL SESSION '''||s.sid||','||s.serial#||',@'||s.inst_id||''';' from gv$tempseg_usage tu, gv$session s
where tu.session_addr=s.saddr and tu.tablespace='TEMP';

 

Step-5: Wait for completion of the Step-4, or kill the related sessions as follows.

ALTER SYSTEM KILL SESSION ‘sid,serial#,@inst_id’ IMMEDIATE;

ALTER SYSTEM KILL SESSION '682,5094,@6' IMMEDIATE;
ALTER SYSTEM KILL SESSION '1031,18702,@6' IMMEDIATE;
ALTER SYSTEM KILL SESSION '208,24717,@8' IMMEDIATE;
ALTER SYSTEM KILL SESSION '682,4169,@8' IMMEDIATE;
ALTER SYSTEM KILL SESSION '1031,27591,@8' IMMEDIATE;
ALTER SYSTEM KILL SESSION '208,17983,@1' IMMEDIATE;
ALTER SYSTEM KILL SESSION '682,32246,@1' IMMEDIATE;

 

 

Step-6: Drop the old temp tablespace as follows.

Make sure that any session and objects don’t use this tablespace.

DROP TABLESPACE TEMP INCLUDING CONTENTS AND DATAFILES;



 

Do you want to learn Oracle Database for Beginners, then read the following articles.

https://ittutorial.org/oracle-database-19c-tutorials-for-beginners/

Exit mobile version