Oracle Database - data file / space handling

Search for a command to run...

That was a nice read! Liked, bookmarked, and followed, keep up the good work!
Just a very short blog post... for everyone who is not yet known to it. Since .Net Framework 4.6 for two immutable types we got new static fields in the framework: Array.Empty<T> string.Empty // instead of string foo = ""; byte[] bar = new byte[0...

Introduction In one of my older projects, I often had to synchronize and work with different parallel threads. Using async, parallel or 'real' threads does not make a real difference when it is coming to thread safety. As a very short short instructi...

In part III we take a closer look at where to upgrade our UserInterface from WindowsForms. As NyxSql was created as a learning project more than a decade ago, there is no MVVM or anything else implemented, which will make the upgrade more hard as the...

When using package manager with sources which require to authenticate yourself (e. g. internal sources or paid subscriptions), it may happen that you need to change your password or just entered the wrong credentials. Now, it is kinda like 'evil', be...

When working with multiple workstations (workstation / laptop) due to pandemic, we suddenly experienced the following error when opening solutions: Current solution contains incorrect configurations mappings. It may cause projects to not work correct...

As I am already working a very long time (around 22 years) with oracle databases using file storage (no ASM, datafiles), a repetitive task always has been managing and extending the datafiles. This post is more a scratchpad of common queries I used to handle the file storage. Please refer to the official oracle documentation if you are not yet used to file handling as this blog post is no replacement for the oracle documentation nor make me responsible if you made something stupid : )
The post will be extended step by step...
Before extending oracle files, always make sure to check the:
Imho, best practice for file names is the following schematics: __.dbf Naturally, the extension is free of choice, dbf for DataBaseFile
A tablespace is a logical organization unit for one or more datafiles. The datafiles itself are holding the data, not the tablespace.
SELECT df.tablespace_name "TS",
df.total_gb "GB total",
used_gb "GB used",
(df.total_gb - ds.used_gb) "GB free",
ROUND(100 * ( (df.total_gb - ds.used_gb)/ df.total_gb),2) "% free"
FROM (SELECT tablespace_name,
ROUND(SUM(bytes) / 1024 / 1024 / 1024,2) total_gb
FROM dba_data_files
GROUP BY tablespace_name) df,
(SELECT ROUND(SUM(bytes)/(1024*1024*1024),2) used_gb,
tablespace_name
FROM dba_segments
GROUP BY tablespace_name) ds
WHERE df.tablespace_name = ds.tablespace_name;
Output is in GB - if you wish to use MB, just remove one 1024 in line 7 and 10.
Here we add a new tablespace 'NEWTS' with one datafile:
create tablespace NEWTS
DATAFILE '/opt/ora/data/EXA/NEWTS/EXA_NEWTS_01.dbf' SIZE 2M;
Now with two:
create tablespace NEWTS
DATAFILE '/opt/ora/data/EXA/NEWTS/EXA_NEWTS_01.dbf' SIZE 2M,
'/opt/ora/data/EXA/NEWTS/EXA_NEWTS_02.dbf' SIZE 2M;
Really? : )
drop tablespace NEWTS;
If you want to drop the tablespace with ALL content:
drop tablespace NEWTS including contents;
SELECT df.NAME as file_name,
round(df.bytes / 1024 / 1024 / 1024,2) "GB total",
round(((df.bytes / 1024 / 1024 / 1024) - NVL (SUM (dfs.bytes) / 1024 / 1024 / 1024, 0)),2) "GB used",
round(NVL (SUM (dfs.bytes) / 1024 / 1024 / 1024, 0),2) "GB free"
FROM v$datafile df, dba_free_space dfs
WHERE df.file# = dfs.file_id(+)
GROUP BY dfs.file_id, df.NAME, df.file#, df.bytes
ORDER BY file_name;
Tablespace name in this example is "NEWTS"
alter tablespace NEWTS add datafile '/opt/ora/data/EXA/NEWTS/EXA_NEWTS_03.dbf' size 32M;
alter database datafile '/opt/ora/data/EXA/NEWTS/EXA_NEWTS_03.dbf' resize 64M;
SELECT tf.NAME file_name,
round(tf.bytes / 1024 / 1024 / 1024,2) "GB total",
round(((tf.bytes / 1024 / 1024 / 1024) - NVL (SUM (dfs.bytes) / 1024 / 1024 / 1024, 0)),2) "GB used",
round(NVL (SUM (dfs.bytes) / 1024 / 1024 / 1024, 0),2) "GB free"
FROM v$tempfile tf, dba_free_space dfs
WHERE tf.file# = dfs.file_id(+)
GROUP BY dfs.file_id, tf.NAME, tf.file#, tf.bytes
ORDER BY file_name;
Temporary tablespace name in this example is "temp_data"
alter tablespace temp_data add tempfile '/opt/ora/temp/EXA/EXA_temp_03.dbf' size 32M;
alter database tempfile '/opt/ora/temp/EXA/EXA_temp_03.dbf' resize 64M;