Database Space with Autoextend (ddbspacea.sql)
What This Script Does
Section titled “What This Script Does”This script provides an enhanced database space analysis that accounts for autoextend settings, showing the true available capacity rather than just current allocated space. It calculates free space based on maximum file sizes, giving DBAs accurate capacity planning information for tablespaces with autoextensible datafiles.
Script
Section titled “Script”rem ddbspacea.sqlremset linesize 80remttitle 'Database Space Summary by Datafile|using Autoextend Formulas'remcol tablespace_name format a15 heading 'TABLESPACE'col file_id format 999 heading 'ID'col file_bytes format 999,999,999 heading 'CURRENT|FILE SIZE|(K)'col max_bytes format 999,999,999 heading 'MAXIMUM|FILE SIZE|(K)'col free_bytes format 999,999,999 heading 'FREE SIZE|(K)'col used_bytes format 999,999,999 heading 'USED SIZE|(K)'col free_pct format 999.99 heading 'FREE|PCT'rembreak on report on tablespace_name skip 1compute sum of file_bytes max_bytes free_bytes used_bytes file_size - on report tablespace_namerem
select tablespace_name, file_id, file_bytes/1024 file_bytes, max_bytes/1024 max_bytes, (file_bytes - nvl(free_bytes,0))/1024 used_bytes, (max_bytes - (file_bytes - nvl(free_bytes,0)))/1024 free_bytes, ((max_bytes - (file_bytes - nvl(free_bytes,0)))/max_bytes) * 100 free_pct from ( select df.tablespace_name, df.file_id, df.bytes file_bytes, greatest(df.maxbytes,df.bytes) max_bytes, sum(fs.bytes) free_bytes from sys.dba_free_space fs, (select * from sys.dba_data_files union all select * from sys.dba_temp_files) df where df.file_id = fs.file_id(+) and df.tablespace_name = fs.tablespace_name(+) and df.tablespace_name like nvl(upper('&tablespace'),'%') group by df.tablespace_name, df.file_id, df.bytes,df.maxbytes )
/
-- Run the script in SQL*Plus or SQLcl@ddbspacea.sql
-- When prompted, enter:-- tablespace: Tablespace name pattern (% for all)
-- ExamplesEnter value for tablespace: %-- Or filter specific tablespaceEnter value for tablespace: USERS
Parameters
Section titled “Parameters”- &tablespace: Tablespace name pattern (use % for all tablespaces)
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_FREE_SPACE
- SELECT on SYS.DBA_DATA_FILES
- SELECT on SYS.DBA_TEMP_FILES
- Typically requires DBA role
Sample Output
Section titled “Sample Output”Database Space Summary by Datafileusing Autoextend Formulas
TABLESPACE ID CURRENT MAXIMUM USED SIZE FREE SIZE FREE FILE SIZE FILE SIZE (K) (K) PCT (K) (K)--------------- --- ----------- ----------- ----------- ----------- ------SYSTEM 1 1,048,576 32,767,968 987,654 31,780,314 97.02 ----------- ----------- -----------sum 1 1,048,576 32,767,968 987,654 31,780,314
SYSAUX 2 524,288 32,767,968 456,789 32,311,179 98.60 ----------- ----------- -----------sum 2 524,288 32,767,968 456,789 32,311,179
USERS 3 5,242,880 32,767,968 3,456,789 29,311,179 89.45 4 2,097,152 32,767,968 1,234,567 31,533,401 96.23 ----------- ----------- -----------sum 7 7,340,032 65,535,936 4,691,356 60,844,580
TEMP 1 1,048,576 10,485,760 12,345 10,473,415 99.88 ----------- ----------- -----------sum 1 1,048,576 10,485,760 12,345 10,473,415
Key Output Columns
Section titled “Key Output Columns”- TABLESPACE: Tablespace name
- ID: Datafile ID number
- CURRENT FILE SIZE (K): Current allocated size in KB
- MAXIMUM FILE SIZE (K): Maximum size with autoextend in KB
- USED SIZE (K): Space currently used in KB
- FREE SIZE (K): Available space based on max size in KB
- FREE PCT: Percentage of maximum size that is available
Understanding Autoextend Space
Section titled “Understanding Autoextend Space”Space Calculation Method
Section titled “Space Calculation Method”- Used Space: Current file size minus free extents
- Free Space: Maximum file size minus used space
- Free Percentage: (Free Space / Maximum Size) × 100
Autoextend Considerations
Section titled “Autoextend Considerations”- MAXSIZE set: Shows actual growth potential
- MAXSIZE unlimited: Uses system limits (32GB for smallfile)
- No autoextend: Max size equals current size
Capacity Planning Benefits
Section titled “Capacity Planning Benefits”- True capacity: Shows actual available space including growth
- Proactive management: Identify tablespaces approaching limits
- Accurate forecasting: Plan based on maximum potential
Performance Analysis
Section titled “Performance Analysis”Space Utilization Patterns
Section titled “Space Utilization Patterns”- High FREE PCT (>90%): Plenty of growth capacity
- Medium FREE PCT (50-90%): Normal utilization
- Low FREE PCT (<50%): Monitor growth closely
- Very low FREE PCT (<10%): Immediate attention needed
Critical Thresholds
Section titled “Critical Thresholds”- FREE PCT < 20%: Consider adding datafiles
- FREE PCT < 10%: Risk of space exhaustion
- FREE PCT < 5%: Critical - immediate action required
Common Use Cases
Section titled “Common Use Cases”Daily Space Monitoring
Section titled “Daily Space Monitoring”-- Check all tablespace capacity@ddbspacea.sql-- Enter % for all tablespaces-- Focus on low FREE PCT values
Capacity Planning
Section titled “Capacity Planning”-- Analyze specific tablespace growth potential@ddbspacea.sql-- Enter specific tablespace name-- Calculate time to exhaustion based on growth rate
Autoextend Validation
Section titled “Autoextend Validation”-- Verify autoextend settings are appropriate@ddbspacea.sql-- Compare current vs maximum sizes-- Identify files without autoextend
Troubleshooting Space Issues
Section titled “Troubleshooting Space Issues”Low Free Space Actions
Section titled “Low Free Space Actions”-
Increase MAXSIZE:
ALTER DATABASE DATAFILE '/path/to/file.dbf'AUTOEXTEND ON MAXSIZE 50G; -
Add new datafile:
ALTER TABLESPACE tablespace_nameADD DATAFILE '/path/to/new_file.dbf'SIZE 1G AUTOEXTEND ON MAXSIZE 32G; -
Enable autoextend:
ALTER DATABASE DATAFILE '/path/to/file.dbf'AUTOEXTEND ON NEXT 100M MAXSIZE 32G;
Space Monitoring Best Practices
Section titled “Space Monitoring Best Practices”- Regular monitoring: Run daily during peak growth periods
- Threshold alerts: Set up monitoring for FREE PCT < 20%
- Growth trending: Track usage patterns over time
- Proactive management: Add space before critical thresholds
Advanced Analysis
Section titled “Advanced Analysis”Growth Rate Calculation
Section titled “Growth Rate Calculation”-- Calculate daily growth rate-- Run this script daily and compare used_bytes-- Growth Rate = (Today's Used - Yesterday's Used) / Days
Tablespace-Level Summary
Section titled “Tablespace-Level Summary”-- Aggregate all datafiles by tablespaceSELECT tablespace_name, COUNT(*) datafiles, SUM(file_bytes)/1024/1024 current_size_mb, SUM(max_bytes)/1024/1024 max_size_mb, SUM(used_bytes)/1024/1024 used_mb, SUM(free_bytes)/1024/1024 free_mb, ROUND(SUM(free_bytes)/SUM(max_bytes)*100, 2) free_pctFROM (/* query from script */)GROUP BY tablespace_nameORDER BY free_pct;
Identify Non-Autoextend Files
Section titled “Identify Non-Autoextend Files”-- Files where current size equals max sizeSELECT tablespace_name, file_idFROM dba_data_filesWHERE bytes = maxbytesOR maxbytes = 0;
Related Scripts
Section titled “Related Scripts”- Database Space Usage (ddbspace.sql) - Basic space analysis without autoextend
- Tablespace Definitions (../database-info/dtspace.md) - Tablespace configuration
- Database Files (dfile.sql) - Detailed file information
- Maximum Shrink Analysis (maxshrink.sql) - Space reclamation analysis