To calculate the free space in an Oracle 19c database tablespace, taking into account the potential size the tablespace can grow to via autoextend (rather than just the current size), you can use the following SQL script. This script queries the relevant data dictionary views (DBA_DATA_FILES, DBA_FREE_SPACE, etc.) to compute the total potential size, used space, and free space.
SQL Script
SET LINESIZE 200
SET PAGESIZE 100
COLUMN tablespace_name FORMAT A20
COLUMN current_size_mb FORMAT 999,999,999.99
COLUMN max_size_mb FORMAT 999,999,999.99
COLUMN used_space_mb FORMAT 999,999,999.99
COLUMN free_space_mb FORMAT 999,999,999.99
COLUMN free_space_with_autoextend_mb FORMAT 999,999,999.99
WITH
-- Calculate current size of the tablespace (sum of datafile sizes)
current_size AS (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS current_size_mb
FROM dba_data_files
GROUP BY tablespace_name
),
-- Calculate maximum possible size of the tablespace (considering autoextend)
max_size AS (
SELECT
tablespace_name,
SUM(maxbytes) / 1024 / 1024 AS max_size_mb
FROM dba_data_files
GROUP BY tablespace_name
),
-- Calculate used space in the tablespace
used_space AS (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS used_space_mb
FROM dba_segments
GROUP BY tablespace_name
),
-- Calculate free space in the tablespace (current free space)
free_space AS (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS free_space_mb
FROM dba_free_space
GROUP BY tablespace_name
)
-- Combine all metrics and calculate free space considering autoextend
SELECT
c.tablespace_name,
c.current_size_mb,
m.max_size_mb,
NVL(u.used_space_mb, 0) AS used_space_mb,
NVL(f.free_space_mb, 0) AS free_space_mb,
(m.max_size_mb - NVL(u.used_space_mb, 0)) AS free_space_with_autoextend_mb
FROM current_size c
JOIN max_size m ON c.tablespace_name = m.tablespace_name
LEFT JOIN used_space u ON c.tablespace_name = u.tablespace_name
LEFT JOIN free_space f ON c.tablespace_name = f.tablespace_name
ORDER BY c.tablespace_name;
Explanation of the Script
-
Formatting Commands:
SET LINESIZE and SET PAGESIZE are used to format the output for better readability.
COLUMN ... FORMAT is used to format the numeric columns with commas and decimals.
-
Common Table Expressions (CTEs):
current_size: Calculates the current size of the tablespace by summing the sizes of all datafiles (dba_data_files.bytes).
max_size: Calculates the maximum possible size of the tablespace by summing the maxbytes of all datafiles, which accounts for autoextend.
used_space: Calculates the total space used by segments in the tablespace (dba_segments.bytes).
free_space: Calculates the current free space in the tablespace (dba_free_space.bytes).
-
Main Query:
- Combines the results from the CTEs to display:
tablespace_name: Name of the tablespace.
current_size_mb: Current size of the tablespace in MB.
max_size_mb: Maximum possible size of the tablespace in MB (considering autoextend).
used_space_mb: Space currently used in the tablespace in MB.
free_space_mb: Current free space in the tablespace in MB.
free_space_with_autoextend_mb: Free space available if the tablespace grows to its maximum size (i.e., max_size_mb - used_space_mb).
-
Handling NULLs:
NVL is used to handle cases where a tablespace might not have any segments (used_space_mb) or free space (free_space_mb), replacing NULL with 0.
-
Sorting:
- The results are sorted by
tablespace_name for better readability.
Notes
- Permissions: Ensure the user running this script has access to the
DBA_DATA_FILES, DBA_FREE_SPACE, and DBA_SEGMENTS views. Typically, this requires DBA privileges or the SELECT_CATALOG_ROLE role.
- Temporary Tablespaces: This script is designed for permanent tablespaces. For temporary tablespaces, you would need to query
DBA_TEMP_FILES instead of DBA_DATA_FILES.
- Autoextend Limits: The
maxbytes column in DBA_DATA_FILES reflects the maximum size a datafile can grow to. If autoextend is not enabled for a datafile, maxbytes will be equal to bytes.
- Units: The script converts sizes to megabytes (MB) for readability. You can modify the divisor (
/ 1024 / 1024) to use GB (/ 1024 / 1024 / 1024) or another unit if desired.
Sample Output
TABLESPACE_NAME CURRENT_SIZE_MB MAX_SIZE_MB USED_SPACE_MB FREE_SPACE_MB FREE_SPACE_WITH_AUTOEXTEND_MB
-------------------- --------------- --------------- --------------- --------------- -----------------------------
EXAMPLE 1,000.00 5,000.00 800.00 200.00 4,200.00
SYSAUX 2,000.00 8,000.00 1,500.00 500.00 6,500.00
SYSTEM 1,500.00 3,000.00 1,200.00 300.00 1,800.00
USERS 500.00 2,000.00 400.00 100.00 1,600.00
Customization
- If you want to filter for a specific tablespace, add a
WHERE clause to the main query, e.g., WHERE c.tablespace_name = 'USERS'.
- To include temporary tablespaces, you can create a similar script using
DBA_TEMP_FILES and related views.
This script provides a comprehensive view of tablespace usage, taking into account the potential growth via autoextend, which is particularly useful for capacity planning.