Oracle Tablespace and Datafile Space Management: Monitor, Grow and Reclaim Step by Step
Updated: Oct 24
Running out of space in a tablespace is one of the most common causes of application errors in an Oracle database. Errors like ORA-01653 (unable to extend table) or ORA-01688 (unable to extend table partition) stop inserts immediately. The good news is that space problems are almost always predictable.
In this post we build a complete space management routine: how to measure usage correctly, how to add space, how to get alerted early, and how to reclaim space when it is no longer needed.
Key Concepts in One Minute
A tablespace is a logical storage container. It is made of one or more datafiles (or tempfiles for temporary tablespaces).
A segment is the storage of one object, such as a table, index or partition. Segments grow in extents.
A datafile with AUTOEXTEND ON grows automatically up to its MAXSIZE. A smallfile datafile with an 8 KB block size can be at most about 32 GB.
A bigfile tablespace has exactly one datafile, which can be up to 32 TB with 8 KB blocks.
Step 1: Measure Real Tablespace Usage
A common mistake is to compare used space with the current datafile size. If the files can still autoextend, a tablespace that looks 98% full may actually have plenty of room. DBA_TABLESPACE_USAGE_METRICS does the right calculation: it measures used space against the maximum size the files can grow to.
SQL |
SELECT m.tablespace_name, ROUND(m.used_space * t.block_size / 1024/1024/1024, 1) AS used_gb, ROUND(m.tablespace_size * t.block_size / 1024/1024/1024, 1) AS max_gb, ROUND(m.used_percent, 1) AS used_pct FROM dba_tablespace_usage_metrics m JOIN dba_tablespaces t ON t.tablespace_name = m.tablespace_name ORDER BY m.used_percent DESC; |
What to expect: One row per tablespace, sorted by how full it is. USED_PCT is the number to alert on, because it already includes autoextend headroom. In a multitenant database, run it inside each PDB, or use CDB_TABLESPACE_USAGE_METRICS from the root.
Step 2: See Each Datafile and Its Autoextend Settings
Before you add space, look at the files that already exist. You may be able to simply raise a MAXSIZE instead of adding a file.
SQL |
SELECT file_id, file_name, ROUND(bytes/1024/1024/1024,1) AS size_gb, autoextensible, ROUND(maxbytes/1024/1024/1024,1) AS max_gb, increment_by FROM dba_data_files WHERE tablespace_name = 'SALES_DATA' ORDER BY file_id; |
What to expect: For each file you see its current size, whether it can autoextend and to what maximum. INCREMENT_BY is in database blocks, not bytes. A file already at 32 GB (with 8 KB blocks) cannot grow further, so you need another file.
Step 3: Check That the Storage Underneath Has Room
Autoextend headroom means nothing if the disk group or file system is full. Always check the physical storage at the same time.
SQL |
-- ASM SELECT name, type, ROUND(total_mb/1024) AS total_gb, ROUND(free_mb/1024) AS free_gb, ROUND(usable_file_mb/1024) AS usable_gb FROM v$asm_diskgroup; |
Shell |
# File system df -h /u02/oradata |
What this does: In ASM, USABLE_FILE_MB is the space you can really use after mirroring. For NORMAL redundancy disk groups it is roughly half of FREE_MB, minus space reserved to survive a disk failure. If it is negative, the disk group cannot fully re-mirror after a failure.
Step 4: Add Space to a Tablespace
There are three ways to add space. Pick the one that fits your file layout.
SQL |
-- Option A: let an existing file grow further ALTER DATABASE DATAFILE '+DATA/PROD/DATAFILE/sales_data.270.1153' AUTOEXTEND ON NEXT 1G MAXSIZE 32767M;
-- Option B: add a new datafile (ASM / Oracle Managed Files) ALTER TABLESPACE sales_data ADD DATAFILE '+DATA' SIZE 10G AUTOEXTEND ON NEXT 1G MAXSIZE 32767M;
-- Option B on a file system ALTER TABLESPACE sales_data ADD DATAFILE '/u02/oradata/PROD/sales_data05.dbf' SIZE 10G AUTOEXTEND ON NEXT 1G MAXSIZE 32767M;
-- Option C: resize a file to a fixed larger size ALTER DATABASE DATAFILE 12 RESIZE 20G; |
What this does: Option A raises the growth limit of an existing file. Option B adds a new file; with ASM and DB_CREATE_FILE_DEST you only name the disk group and Oracle creates the file name. Option C grows a file immediately, which is useful before a large data load so the load does not wait on many small autoextend operations.
Tip: On a Data Guard primary, set STANDBY_FILE_MANAGEMENT=AUTO so new datafiles are created on the standby automatically.
Step 5: Consider a Bigfile Tablespace for Large Data
If a tablespace needs dozens of 32 GB files, a bigfile tablespace is easier to manage: one file, resized with one command at the tablespace level. Oracle Database 23ai creates bigfile tablespaces by default.
SQL |
CREATE BIGFILE TABLESPACE sales_hist DATAFILE '+DATA' SIZE 100G AUTOEXTEND ON NEXT 10G MAXSIZE 4T;
-- Resize at the tablespace level ALTER TABLESPACE sales_hist RESIZE 200G; |
What to expect: One large datafile. Backups of one very large file can be split across channels with RMAN SECTION SIZE, so a bigfile tablespace does not slow your backups down.
Step 6: Set Space Alert Thresholds
Oracle can raise alerts on its own when a tablespace crosses a usage threshold. The default thresholds are 85% (warning) and 97% (critical). You can change them globally or per tablespace, and the alerts appear in DBA_OUTSTANDING_ALERTS and in Enterprise Manager.
SQL |
BEGIN DBMS_SERVER_ALERT.SET_THRESHOLD( metrics_id => DBMS_SERVER_ALERT.TABLESPACE_PCT_FULL, warning_operator => DBMS_SERVER_ALERT.OPERATOR_GE, warning_value => '80', critical_operator => DBMS_SERVER_ALERT.OPERATOR_GE, critical_value => '90', observation_period => 1, consecutive_occurrences => 1, instance_name => NULL, object_type => DBMS_SERVER_ALERT.OBJECT_TYPE_TABLESPACE, object_name => 'SALES_DATA'); END; /
SELECT object_name, reason, suggested_action FROM dba_outstanding_alerts WHERE object_type = 'TABLESPACE'; |
What this does: SALES_DATA now raises a warning at 80% and a critical alert at 90%. The percentage is measured the same way as in Step 1, so autoextend headroom is respected.
Step 7: Find Out What Is Using the Space
When a tablespace grows faster than expected, find the largest segments and the ones that grew the most. That tells you whether the growth is normal business data, a forgotten log table, or a runaway batch job.
SQL |
-- Largest segments in the tablespace SELECT owner, segment_name, partition_name, segment_type, ROUND(bytes/1024/1024/1024,2) AS gb FROM dba_segments WHERE tablespace_name = 'SALES_DATA' ORDER BY bytes DESC FETCH FIRST 20 ROWS ONLY;
-- Growth per day of the tablespace over the last 30 days (needs AWR) SELECT TRUNC(TO_DATE(h.rtime,'MM/DD/YYYY HH24:MI:SS')) AS day, ROUND(MAX(h.tablespace_usedsize) * t.block_size / 1024/1024/1024, 1) AS used_gb FROM dba_hist_tbspc_space_usage h JOIN v$tablespace v ON v.ts# = h.tablespace_id JOIN dba_tablespaces t ON t.tablespace_name = v.name WHERE v.name = 'SALES_DATA' AND TO_DATE(h.rtime,'MM/DD/YYYY HH24:MI:SS') > SYSDATE - 30 GROUP BY TRUNC(TO_DATE(h.rtime,'MM/DD/YYYY HH24:MI:SS')), t.block_size ORDER BY 1; |
What to expect: The first query shows the top 20 consumers. The second shows the daily trend, which you can use to forecast when the tablespace will be full. DBA_HIST views require the Diagnostics Pack licence.
Step 8: Reclaim Space Inside a Table
When many rows are deleted, the table keeps its extents and its high water mark. Full scans still read the empty blocks, and the space is not returned to the tablespace. There are two online ways to compact a table.
SQL |
-- Option A: SHRINK SPACE (ASSM tablespaces, needs row movement) ALTER TABLE sales.order_audit ENABLE ROW MOVEMENT; ALTER TABLE sales.order_audit SHRINK SPACE CASCADE;
-- Option B: MOVE ONLINE (12.2 and later), indexes stay valid ALTER TABLE sales.order_audit MOVE ONLINE TABLESPACE sales_data;
SELECT segment_name, ROUND(bytes/1024/1024) AS mb FROM dba_segments WHERE segment_name = 'ORDER_AUDIT'; |
What this does: SHRINK SPACE moves rows towards the start of the segment and then lowers the high water mark. CASCADE also shrinks dependent indexes. MOVE ONLINE rebuilds the table into new extents while DML continues, and maintains the indexes. Both release free space back to the tablespace. Run them during a quiet period, because they generate redo and undo.
Step 9: Shrink a Datafile
Space freed inside a tablespace is not automatically given back to the disk. To make a datafile smaller, you can only cut space that lies above the last used extent in that file. This query shows how far each file can be shrunk.
SQL |
SELECT f.file_id, f.file_name, CEIL(f.bytes/1024/1024) AS current_mb, CEIL(NVL(e.hwm,1) * t.block_size / 1024/1024) AS smallest_mb, CEIL(f.bytes/1024/1024) - CEIL(NVL(e.hwm,1) * t.block_size / 1024/1024) AS savings_mb FROM dba_data_files f JOIN dba_tablespaces t ON t.tablespace_name = f.tablespace_name LEFT JOIN (SELECT file_id, MAX(block_id + blocks - 1) AS hwm FROM dba_extents GROUP BY file_id) e ON e.file_id = f.file_id WHERE f.tablespace_name = 'SALES_DATA' ORDER BY savings_mb DESC;
-- Resize to a value at or above SMALLEST_MB ALTER DATABASE DATAFILE 12 RESIZE 8G; |
What to expect: If SAVINGS_MB is large, the resize succeeds immediately. If you try to go below the last used extent, you get ORA-03297 (file contains used data beyond requested RESIZE value). In that case, move the segments at the end of the file first (Step 8), then retry. On 23ai, DBMS_SPACE.SHRINK_TABLESPACE can move the segments and shrink a bigfile tablespace for you.
Step 10: Remove a Tablespace Safely
When a tablespace is no longer needed, check that it really is empty and not referenced by any user as a default tablespace before dropping it.
SQL |
SELECT COUNT(*) FROM dba_segments WHERE tablespace_name = 'OLD_DATA';
SELECT username FROM dba_users WHERE default_tablespace = 'OLD_DATA';
ALTER TABLESPACE old_data OFFLINE; -- a short safety period first -- after confirmation: DROP TABLESPACE old_data INCLUDING CONTENTS AND DATAFILES; |
What this does: Taking the tablespace offline for a few days is a cheap safety net: if any application still uses it, you will hear about it and can simply bring it back online. DROP ... INCLUDING CONTENTS AND DATAFILES removes the tablespace and its files from disk.
Summary
Good space management is a habit, not an emergency. Measure usage against MAXSIZE, check the storage underneath, add space with autoextend limits, set alert thresholds so you hear about problems early, track growth with AWR, and reclaim space with SHRINK or MOVE ONLINE when large deletes leave tables mostly empty. Put the queries from Steps 1 and 7 into a daily report and space errors will become very rare.
Comments