- In Oracle, tablespaces are logical storage units containing various types of database objects like tables, indexes, and others. To check the details of tablespaces in an Oracle database, you can use SQL queries against the data dictionary views
- Here in this article, we will check how to check tablespace in Oracle database, tablespace free space, tablespace growth information, tablespace size, associated datafiles with tablespace, checking the highest allocated extent in the tablespace
How to check tablespace name in Oracle
To list the names and various other parameters of all tablespace in a database, use the following query on the DBA_TABLESPACES view:
SELECT TABLESPACE_NAME "TABLESPACE", EXTENT_MANAGEMENT,FORCE_LOGGING,
BLOCK_SIZE,
SEGMENT_SPACE_MANAGEMENT
FROM DBA_TABLESPACES;
With CDB Databases, you must connect to individual PDB and execute the query to check the tablespace. If you want to gather all the tablespace across all the PDBs and Root containers, then you need to fire the below query connected to the root container
select tablespace_name, con_id from cdb_tablespaces;
How to List the Datafiles and Associated Tablespace of a Database
To list the datafiles, sizes, and associated tablespace of a database, enter the following query on the DBA_DATA_FILES view
SELECT
FILE_NAME,
BLOCKS,
TABLESPACE_NAME
FROM DBA_DATA_FILES;
How to check Oracle tablespace Utilization
To produce statistics about total space, used space, free space and % used for each tablespace in the database, enter the following query:
set echo off feedback off verify off pages 75 col tablespace_name format a20 head 'Tablespace Name' col total format 999,999,999,999 head 'Total(KB)' col used format 999,999,999,999 head 'Used(KB)' col free format 999,999,999,999 head 'Free(KB)' col pct format 999 head 'Percent|Used' break on report compute sum of total on report compute sum of used on report compute sum of free on report select tbs.tablespace_name, tot.bytes/1024 total, tot.bytes/1024-sum(nvl(fre.bytes,0))/1024 used, sum(nvl(fre.bytes,0))/1024 free, (1-sum(nvl(fre.bytes,0))/tot.bytes)100 pct from dba_free_space fre, (select tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) tot, dba_tablespaces tbs where tot.tablespace_name = tbs.tablespace_name and fre.tablespace_name(+) = tbs.tablespace_name group by tbs.tablespace_name, tot.bytes/1024, tot.bytes union select tsh.tablespace_name, dtf.bytes/1024 total, sum(nvl(tsh.bytes_used,0))/1024 used, sum(nvl(tsh.bytes_free,0))/1024 free, (1-sum(nvl(tsh.bytes_free,0))/dtf.bytes)100 pct from v$temp_space_header tsh, (select tablespace_name, sum(bytes) bytes from dba_temp_files group by tablespace_name) dtf where dtf.tablespace_name = tsh.tablespace_name(+) group by tsh.tablespace_name, dtf.bytes/1024, dtf.bytes order by 1 /
With CDB Databases, you need to connect to individual PDB and execute the query.
We have another view dba_tablespace_usage_metrics introduced from 11.2 which can be used to find Oracle tablespace utilization. The values reported are in blocks, not bytes, which can be easily computed using the database block size
column value new_value dbblock noprint
select value from v$parameter where name = 'db_block_size';
select tablespace_name,
used_space used_blocks,
(used_space&dbblocks)/(10241024) used_mb,
tablespace_size tablespace_blocks,
(tablespace_size&dbblocks)/(10241024) tablespace_mb,
used_percent
from dba_tablespace_usage_metrics;
How to check the highest allocated extent
The highest allocated extent is the high watermark of the data file. This information is helpful when reducing the size of the data file
column file_name format a50; column tablespace_name format a15; column highwater format 9999999999; set pagesize 9999 select a.tablespace_name ,a.file_name ,(b.maximum+c.blocks-1)*d.db_block_size highwater from dba_data_files a ,(select file_id,max(block_id) maximum from dba_extents group by file_id) b ,dba_extents c ,(select value db_block_size from v$parameter where name='db_block_size') d where a.file_id = b.file_id and c.file_id = b.file_id and c.block_id = b.maximum order by a.tablespace_name,a.file_name;
How to check tablespace free space in oracle
we can use below query to find allocated space and free space for all the tablespace in Oracle Database
SELECT /* + RULE */ df.tablespace_name "Tablespace",
df.bytes / (1024 * 1024) "Size (MB)",
SUM(fs.bytes) / (1024 * 1024) "Free (MB)",
Nvl(Round(SUM(fs.bytes) * 100 / df.bytes),1) "% Free",
Round((df.bytes - SUM(fs.bytes)) * 100 / df.bytes) "% Used"
FROM dba_free_space fs,
(SELECT tablespace_name,SUM(bytes) bytes
FROM dba_data_files
GROUP BY tablespace_name) df
WHERE fs.tablespace_name (+) = df.tablespace_name
GROUP BY df.tablespace_name,df.bytes
UNION ALL
SELECT /* + RULE */ df.tablespace_name tspace,
fs.bytes / (1024 * 1024),
SUM(df.bytes_free) / (1024 * 1024),
Nvl(Round((SUM(fs.bytes) - df.bytes_used) * 100 / fs.bytes), 1),
Round((SUM(fs.bytes) - df.bytes_free) * 100 / fs.bytes)
FROM dba_temp_files fs,
(SELECT tablespace_name,bytes_free,bytes_used
FROM v$temp_space_header
GROUP BY tablespace_name,bytes_free,bytes_used) df
WHERE fs.tablespace_name (+) = df.tablespace_name
GROUP BY df.tablespace_name,fs.bytes,df.bytes_free,df.bytes_used
ORDER BY 4 DESC;
We can use the below query to determine the total free space, largest free check size and number of free chunks for the particular tablespace. This queries the dictionary view DBA_FREES_SPACE to get these details
set feedback off set echo off set numwidth 15 set linesize 150 set pages 1000 Accept tname Prompt "Enter Tablespace Name : " Select (Sum(bytes)/1024/1024) Free_space_MB,(max(bytes)/1024/1024) Largest_Free_chunck_MB, count(*) No_of_free_chunk from dba_free_space where tablespace_name=upper('&tname');
How to check tablespace size in Oracle
The below query will provide the tablespace size in MB
Select (sum(bytes)/1024/1024) Space_allocated
from dba_data_files
where tablespace_name=upper('&tname');
The below query will provide the tablespace size in GB
Select (sum(bytes)/1024/1024/1024) Space_allocated
from dba_data_files
where tablespace_name=upper('&tname');
If Autoextensible is “On” on the tablespace, Tablespace can grow without explicitly adding the space. we can use the below query to find the size of the tablespace to which, it can be extended automatically if required
For MB Select (sum(maxbytes)/1024/1024) Space_allocated from dba_data_files where tablespace_name=upper('&tname'); For GB Select (sum(maxbytes)/1024/1024/1024) Space_allocated from dba_data_files where tablespace_name=upper('&tname');
To check all tablespace information in the database
The below query can be used to get detailed information about all the tablespace in the database
set echo off feedback off verify off pages 60
col tablespace_name format a16 head 'Tablespace Name'
col initial_extent format 99,999,999 head 'Initial|Extent(K)'
col next_extent format 99,999,999 head 'Next|Extent(K)'
col max_extents format a4 head 'Max|Ext'
col pct_increase format 999 head 'Pct|Inc'
col extent_management format a10 head 'Extent|Management'
col allocation_type format a10 head 'Allocation|Type'
col status format a7
select tbs.tablespace_name
, tbs.initial_extent
, tbs.next_extent
, decode(tbs.max_extents,2147483645,'UL',tbs.max_extents) max_extents
, tbs.pct_increase
, tbs.extent_management
, tbs.allocation_type
, tbs.status
from dba_tablespaces tbs
order by 1
/
How to check Oracle tablespace growth history
Starting Oracle 10G, Oracle records tablespaces usage (allocated, used, etc.) in AWR which can be retrieved by querying the data dictionary view dba_hist_tbspc_space_usage. We can use the below script to view the history of tablespace(s) usage and predict the expected growth for the future.
This script is based on AWR. If your AWR retention period is 7 days, this script can only tell the growth history of the last 7 days and predict based on the last 7 day’s growth. I would recommend changing AWR retention to at least 35 days – this will also be more helpful in case of a performance tuning situation as you will have a longer window from the past to look into for performance comparisons.
SELECT TO_CHAR (sp.begin_interval_time,'DD-MM-YYYY') days
, ts.tsname
, max(round((tsu.tablespace_size* dt.block_size )/(10241024),2) ) cur_size_MB , max(round((tsu.tablespace_usedsize dt.block_size )/(1024*1024),2)) usedsize_MB
FROM DBA_HIST_TBSPC_SPACE_USAGE tsu
, DBA_HIST_TABLESPACE_STAT ts
, DBA_HIST_SNAPSHOT sp
, DBA_TABLESPACES dt
WHERE tsu.tablespace_id= ts.ts#
AND tsu.snap_id = sp.snap_id
AND ts.tsname = dt.tablespace_name
AND ts.tsname NOT IN ('SYSAUX','SYSTEM')
GROUP BY TO_CHAR (sp.begin_interval_time,'DD-MM-YYYY'), ts.tsname
ORDER BY ts.tsname, days;
How to check tablespace used by schema in Oracle
select distinct tablespace_name from dba_segments where owner='&schema';
How to list datafiles in tablespace in Oracle
select file_name from dba_data_files where tablespace_name like '%&tablespace%';
How to list datafiles with size in tablespace in Oracle
In MB
select file_name,bytes/1024/1024 from dba_data_files where tablespace_name like '%&tablespace%';
In GB
select file_name,bytes/1024/1024/1024 from dba_data_files where tablespace_name like '%&tablespace%';
I hope you like the queries given in the post for Oracle tablespace. Please do let me know your feedback
Related Articles
Oracle Create Tablespace: This article on how to create tablespace in Oracle, the various characteristics associated with it and different tablespace statements
ORA-01652:ORA-01652 error usually occurs when the tablespace does not have free space in Permanent and Temporary tablespace in the Oracle database. Check out how to resolve it
Shrink datafile in Oracle: Check out how to shrink the datafile and reclaim space on the filesystem. How to resolve ORA-03297
Oracle database administration tutorial: This lists all the Oracle DBA-related stuff. Very helpful for the administration
How to change default tablespace in Oracle: Default tablespace is the tablespace where the objects are created when no tablespace name is specified by users. Find out how to check the default tablespace
How to check temp tablespace in Oracle: This article is about temp tablespace in Oracle, resize the tempfile, drop the temp file, find the temp usage by Session
alter tablespace add datafile: Check out this post on How to add a datafile to tablespace in Oracle, add tempfile to the temporary tablespace, how to add datafile in ASM
https://docs.oracle.com/cd/B28359_01/server.111/b28310/tspaces002.htm
tq
Thanks Babu
Hi, I would like to know how to reduce de size of sysaux tbs (pdb), right now the %used is 3%, i need to resize the tablespace. Thxs. I will appreciate any help that you can give me.
(I’m new working with pluggable db, and english isn’t my first language, excuse me).
SYSAUX tablespace contains objects on AWH,ASH, AUD$ ,Advisor Optimizer statistics.
You need to evaluate size of each of these and then purge them
Below queries will help determining it
SQL> SET LINES 120
SQL> COL OCCUPANT_NAME FORMAT A30
SQL> SELECT OCCUPANT_NAME,SPACE_USAGE_KBYTES FROM V$SYSAUX_OCCUPANTS ORDER BY SPACE_USAGE_KBYTES DESC;
SQL> COL SEGMENT_NAME FORMAT A30
SQL> COL OWNER FORMAT A10
SQL> COL TABLESPACE_NAME FORMAT A10
SQL> COL SEGMENT_TYPE FORMAT A15
SQL> SELECT * FROM (SELECT SEGMENT_NAME,OWNER,TABLESPACE_NAME,BYTES/1024/1024 “SIZE(MB)”,SEGMENT_TYPE FROM DBA_SEGMENTS WHERE TABLESPACE_NAME=’SYSAUX’ ORDER BY BYTES DESC) WHERE ROWNUM<=10;