Error this answers
ORA-03297: file contains used data beyond requested RESIZE value
After a big purge, a tablespace can be mostly free while its datafiles stay large. You can only shrink a datafile down to its last used extent (its high-water mark); ask for less and Oracle refuses with:
ORA-03297: file contains used data beyond requested RESIZE value
This script works out, for every file in a tablespace, the size that ends exactly at its last used extent, and writes the RESIZE statements to a file for you to review and run.
Usage
SQL> @dbf_trim.sql USERS
SQL> host cat dbf_trim.run
SQL> @dbf_trim.run
Nothing is resized until you run dbf_trim.run.
-- dbf_trim.sql: generate RESIZE statements that shrink each datafile to its last used extent
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/shrink-datafiles-to-high-water-mark/
--
-- Usage: @dbf_trim.sql <tablespace>
-- review dbf_trim.run, then @dbf_trim.run
--
-- Writes the statements to dbf_trim.run and stops: nothing is resized until you run that file.
set verify off
set trims on
set pages 0
set feedback on
set lines 300
set feedback off
set echo off
set timing off
SELECT /*+ ORDERED */
'ALTER DATABASE DATAFILE '||chr(39)||dbf.file_name||chr(39)||
' RESIZE '||LTRIM(TO_CHAR((dbf.bytes-free.bytes)/1024,'999999999999'))||'K ;'
FROM (SELECT fr.file_id
, fr.bytes
FROM dba_free_space fr
WHERE (file_id, block_id) IN
(SELECT file_id, MAX(BLOCK_ID)
FROM dba_free_space
WHERE tablespace_name = UPPER('&&1')
GROUP BY file_id)
AND TABLESPACE_NAME = UPPER('&&1')
AND block_id > (SELECT MAX(block_id)
FROM dba_extents
WHERE file_id = fr.file_id
and TABLESPACE_NAME = UPPER('&&1'))) free
, dba_data_files dbf
WHERE free.file_id = dbf.file_id
spool dbf_trim.run
/
spool off
set feedback on
set timing on
prompt
prompt Wrote dbf_trim.run. Review it, then run it: @dbf_trim.run
undefine 1curl -O https://hashjoin.com/kb/oracle/shrink-datafiles-to-high-water-mark/dbf_trim.sql
Reading the output
Each line of dbf_trim.run is one ALTER DATABASE DATAFILE '…' RESIZE nK;. In a lab run on a 32 MB file whose tail had been freed by dropping a table, it wrote:
ALTER DATABASE DATAFILE '/opt/oracle/oradata/FREE/FREEPDB1/kb_trim01.dbf' RESIZE 6400K ;
and after running it the file was 6.25 MB. Files whose free space isn't at the end (a used extent sits at the end of the file) get no statement, because they can't shrink at all.
Gotchas
- One extent at the end pins the whole file. If a file won't shrink, find the segment owning its last extent in
dba_extentsand move or rebuild it (alter table … move,alter index … rebuild), then run the script again. - Scanning
dba_extentsis slow on tablespaces with millions of extents. Run it outside busy hours. - Shrunk files grow back if autoextend is on and the space is needed again; shrinking is for space you're sure is gone for good.
What it reads
dba_free_space dba_extents dba_data_files
Related
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.
New fixes and field notes by email, about once a month.