How to Shrink the Datafile of Temporary Tablespace?

Hello,
The database has a program which performs a huge sort operation. This may cause the TEMP tablespace to grow and occupy most of the space on the file system. In this example, the report process may run once or twice a year and there is no need to maintain/keep a huge tempfile. The TEMP tablespace was created with datafiles (dictionary managed tablespace temporary) as AUTOEXTEND ON MAXSIZE UNLIMITED to avoid the Error:

ORA-1652 : unable to extend temp segment by %s in tablespace %s.

Attempts have been made to “alter database datafile .. resize” which fail with:

Error: ORA 3297 : file contains blocks of data beyond requested RESIZE value

You want to shrink the datafile to utilize the disk space for other tablespaces or other purposes.

To solve this issue;

1- Create a new temporary tablespace with desired smaller size:

create temporary tablespace TEMP1 tempfile ‘/oradata/DBM/temp01.dbf’ size 100M extent management
local uniform size 128K;

2- If the original tablespace is a default temporary tablespace, set the new tablespace as default temporary tablespace for all users in the database:

alter database default temporary tablespace TEMP1;

3- If necessary, explicitly re-assign specific users to the new tablespace:

alter user temporary tablespace TEMP1;

4- Drop the old tablespace:

drop tablespace temp including contents;

Or the drop command can also drop datafiles at OS level:

drop tablespace temp INCLUDING CONTENTS AND DATAFILES;

Leave a comment