Even if you are a developer, or Linux sysadmin, sometimes you might still end-up dealing with Oracle database in your organization.
One of the essential Oracle DBA task is to manage the tablespace.
This tutorial covers everything that you need to know to effectively manage both your tablespaces and datafiles in an Oracle database.
The following are covered in this tutorial:
- Create Tablespace with an Example
- Create Tablespace with Additional Storage Parameters
- Add New Datafile to Increase the Size of a Tablespace
- Add New Datafile with Storage Parameters
- How to Increase Size of an Existing Datafile
- View Tablespace and datafile Information
- Tablespace Extent Management
- Calculate the Size of your Tablespace (Both Total Space and Free Space Available)
- Bigfile Tablespace Management
- Rename Tablespace
- Drop Tablespace
- Drop a Specific datafaile from a Tablespace
- Bring Tablespace Online or Offline
- Set a Tablespace as Read-Only Temporarily
- Rename or Move Datafile to a Different Folder
1. Create Tablespace Basic Example
The following command will create a new tablespace called “thegeekstuff” with /u02/oradata/tgs/thegeekstuff01.dbf as the datafile. The initial size of the datafile will be 100M.
CREATE TABLESPACE thegeekstuff DATAFILE '/u02/oradata/tgs/thegeekstuff01.dbf' SIZE 100M;
Note: It is recommended that you keep the name of your datafile the same as the tablespace name. Sine this is the first datafile in this table, it is called as thegeekstuff01.dbf. When you add a 2nd file to this tablespace, call that as thegeekstuff02.dbf, etc.
2. Create Tablespace with Extra Parameters
The following command will create a new tablespace but this specifies some additional storage related parameters.
CREATE TABLESPACE thegeekstuff DATAFILE '/u02/oradata/tgs/thegeekstuff01.dbf' SIZE 100M REUSE AUTOEXTEND ON NEXT 1M MAXSIZE 2G;
In the above command:
- Datafile – The above creates thegeekstuff tablespace with thegeekstuff01.dbf as the datafile.
- Size 100M – The initial size of this dbf file will be 100M.
- AUTOEXTEND ON NEXT 1M – When it runs out of space, it will automatically extend this dbf file by 1M whenever required.
- MAXSIZE 2G – This will keep extending the size of this particular dbf file until it reaches 2GB.
3. Add New Datafile to Increase the Size of a Tablespace
After a tablespace is created, you can also add more datafile using ALTER TABLESPACE command as shown below.
ALTER TABLESPACE thegeekstuff ADD DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf' SIZE 100M;
The above command will add a 2nd datafile called thegeekstuff02.dbf to the existing thegeekstuff tablespace. The initial size of this tablespace will be 100M.
Some of the commands explained here can be modified slightly modified to work with other types oracle tablespace like undo tablespace, temp tablespace, system tablespace, etc.
4. Add New Datafile with Extra Parameters
You can enable or disable automatic file extension for existing datafiles, or manually resize a datafile, using the ALTER DATABASE statement. For a bigfile tablespace, you are able to perform these operations using the ALTER TABLESPACE statement.
The following example enables automatic extension for a datafile added to the users tablespace. The value of NEXT is the minimum size of the increments added to the file when it extends. The value of MAXSIZE is the maximum size to which the file can automatically extend.
The following command will add a new datafile to an existing tablespace with some additional storage related parameters.
ALTER TABLESPACE thegeekstuff ADD DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf' SIZE 100M AUTOEXTEND ON NEXT 512K MAXSIZE 2G;
In the above:
- ADD DATAFILE – thegeekstuff02.dbf file will be added to the existing tablespace
- SIZE 100M – Initial size of this datafile will be 100M
- AUTOEXTEND ON – The automatic extension for this datafile is enabled. This will keep extending this datafile whenever space is required.
- NEXT 512K – When it runs out of space, this will extend the size by 512K
- MAXSIZE 2G – thegeekstuff02.dbf file will keep growing upto maximum of 2GB in size.
5. Increase Size of an Existing Datafile
When you don’t have the autoextend on, you can also increase the size of a particular datafile as shown below.
ALTER DATABASE DATAFILE '/u02/oradata/tgs/thegeekstuff01.dbf' RESIZE 200M;
The above command will resize thegeekstuff01.dbf file to 200MB.
If the dbf file is currently only 100MB in size, then the above command will increase the size.
If the dbf file is currently 500MB (or anything more than 200MB), then the above command will decrease the size if possible. It will decrease the size of the dbf file only if the existing content is less than 200MB.
6. View Tablespace and datafile Information
You can view all the tablespace in your system from the dba_tablespace as shown below.
SELECT * from DBA_TABLESPACES;
To view all the datafiles of a particular tablespace, execute the following command.
This command will display all the datafiles that as currently associated with thegeekstuff tablespace. This will also display the size of the datafiles in MB.
column file_name format A50;
SELECT file_id, file_name, bytes/1024/1024 size_mb FROM dba_data_files WHERE tablespace_name = 'THEGEEKSTUFF';
The following is the output:
FILE_ID FILE_NAME SIZE_MB ------- ------------------------------------- ------- 4 /u02/oradata/tgs/thegeekstuff01.dbf 200 5 /u02/oradata/tgs/thegeekstuff02.dbf 100
Also, you’ll see these two dbf files physically on your filesystem as shown below:
$ ls -l /u02/oradata/tgs/* -rw-r----- 1 oracle dba 209719296 Oct 15 11:43 /u02/oradata/tgs/thegeekstuff01.dbf -rw-r----- 1 oracle dba 104861696 Oct 15 11:39 /u02/oradata/tgs/thegeekstuff02.dbf
Both the dba_tablespaces and dba_data_files have lot of additional columns that can give you more useful information about your tablespace and datafiles
DESC dba_tablespaces; DESC dba_data_files;
7. Tablespace Extent Management
The following are two methods that tablespace uses to manage their extents. Extents are nothing but the unit in which a tablespace allocates space.
Locally managed Tablespaces – In this method, extents are automatically managed by the tablespace itself.
Dictionary managed tablespace – In this method, extents are managed by the data dictionary.
In the latest version of oracle, when you create a tablespace, by default, it will use locally managed tablespace, which is highly recommended. Don’t use dictionary managed, unless you know what you are doing.
Use the following query to identify whether your tablespace is using dictionary or locally managed.
SELECT extent_management, segment_space_management FROM dba_tablespaces WHERE tablespace_name = 'THEGEEKSTUFF'
The following is the output:
EXTENT_MAN SEGMEN ---------- ------ LOCAL AUTO
As you see above, in this example, thegeekstuff tablespace is using locally managed tablespace for extent management.
In the older version of Oracle, by default it might create a tablespace in dictionary managed. In that case, if you want to specify the extent management during the tablespace create command, you can use “EXTENT MANAGEMENT LOCAL” in the create tablespace command as shown below.
CREATE TABLESPACE thegeekstuff DATAFILE '/u02/oradata/tgs/thegeekstuff01.dbf' SIZE 100M EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
Note: The Segment space management can either be AUTO or MANUAL. AUTO is the recommended method.
You can also specify uniform size for locally managed tablespace as shown below. The following will create thegeekstuff tablespace with locally managed tablespace with a uniform extent size of 128k.
CREATE TABLESPACE thegeekstuff DATAFILE '/u02/oradata/tgs/thegeekstuff01.dbf' SIZE 100M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K;
Note: You can see this in the ALLOCATION_TYPE column on dba_tablespaces. The value can be either UNIFORM or SYSTEM (which is default)
Note: Typically you don’t have to worry about specifying any of these extra options. Just create the tablespace with default option, and let the database do the locally managed tablespace with all default values.
8. Calculate the Size of Tablespace (Both Total Space and Free Space Available)
The following command will display the total space used, and the free space available in your tablespace.
select a.tablespace_name, sum(a.bytes)/(1024*1024) total_space_MB, round(b.free,2) Free_space_MB, round(b.free/(sum(a.bytes)/(1024*1024))* 100,2) percent_free from dba_data_files a, (select tablespace_name,sum(bytes)/(1024*1024) free from dba_free_space group by tablespace_name) b where a.tablespace_name = b.tablespace_name(+) group by a.tablespace_name,b.free
The following output indicates that this particular tablespace still have 34% of free space (i.e around 867MB free space is available for new data)
TABLESPACE_NAME TOTAL_SPACE_MB FREE_SPACE_MB PERCENT_FREE --------------- -------------- ------------- ------------ THEGEEKSTUFF 2547 867.69 34.07
9. Bigfile Tablespace Management
Typically you can add as many datafiles you want to a specific tablespace.
But, in a bigfile tablespace will have only one datafile.
An oracle bigfile tablespace will allow you to create a tablespace with a single datafile that can grow really large.
The advantage is that you don’t have to worry about managing datafiles, you’ll simply manage the bigfile tablespace.
Use create bigfile tablespace command as shown below to create a bigfile tablespace.
CREATE BIGFILE TABLESPACE thegeekstuffbig DATAFILE '/u02/oradata/tgs/thegeekstuffbig.dbf' SIZE 50G;
In the above example, there will be only one datafile called thegeekstuffbig.dbf, with initial size of 50GB.
For size, you can also specify K (for kilobytes), or M (for megabytes), or G (for gigabytes), or T (for terabytes).
The following query will display whether a particular tablespace is a bigfile tablespace or not.
SELECT tablespace_name, bigfile FROM dba_tablespaces;
The following is the output:
TABLESPACE_NAME BIG ---------------- --- THEGEEKSTUFF NO THEGEEKSTUFFBIG YES
Later if you want to extent the size of the bigfile tablespace even further, all you have to do is, use alter tablespace command as shown below. This will resize the single datafile thegeekstuffbig.dbf from 50G to 100G
ALTER TABLESPACE thegeekstuffbig RESIZE 100G;
You can also use the autoextend on and specify the next next as shown below. This will automatically extent that single datafile by 10G whenever more space is required.
ALTER TABLESPACE thegeekstuff AUTOEXTEND ON NEXT 10G;
While this is obvious, it is worth mentioning, you cannot add more datafile to the bigfile tablespace. It will display the following error message.
ALTER TABLESPACE thegeekstuffbig ADD DATAFILE '/u02/oradata/tgs/thegeekstuffbig02.dbf' SIZE 50G;
The following is the output:
ERROR at line 1: ORA-32771: cannot add file to bigfile tablespace
If the default tablespace type was set to BIGFILE at database creation, but you want to create a traditional (smallfile) tablespace, then specify a CREATE SMALLFILE TABLESPACE statement to override the default tablespace type for the tablespace that you are creating.
You can also set the default tablespace type in your database to BIGFILE, in which case, you can simply use “CREATE TABLESPACE” command instead of “CREATE BIGFILE TABLESPACE”.
In that case (when your default tablespace type is BIGFILE), and if you want to create the traditional tablespace, when you have to use “CREATE SMALLFILE TABLESPACE” as shown below.
CREATE SMALLFILE TABLESPACE thegeekstuff DATAFILE '/u02/oradata/tgs/thegeekstuff01.dbf' SIZE 100M;
10. Rename Tablespace
Using the RENAME TO clause of the ALTER TABLESPACE, you can rename a permanent or temporary tablespace. For example, the following statement renames the users tablespace:
You can change the name of an existing tablespace using alter tablespace command as shown below.
This will change the tablespace name from thegeekstuff to tgs.
ALTER TABLESPACE thegeekstuff RENAME TO TGS;
Now, the tablespce is renamed:
TABLESPACE_NAME STATUS --------------- ------ TGS ONLINE
Just like renaming a regular tablespace, you an also rename SYSTEM tablespace, or SYSAUX tablespace, or temporary tablespace, or an undo tablespace.
11. Drop Tablespace
To drop a tablespace, you should have DROP TABLESPACE privilege.
The following command will drop (delete) thegeekstuff tablespace.
DROP TABLESPACE thegeekstuff;
Keep in mind that the above command will work only if the tablespace that is getting deleted is empty. If the tablespace has something, it will thrown the following error message.
SQL> DROP TABLESPACE thegeekstuff;
The following is the error message:
ERROR at line 1: ORA-01549: tablespace not empty, use INCLUDING CONTENTS option
So, if you want to drop the tablespace along with all the objects (tables, indexes, etc) in it, then use the following DROP command with “INCLUDING CONTENTS”.
SQL> DROP TABLESPACE thegeekstuff INCLUDING CONTENTS; Tablespace dropped.
When you drop a tablespace, the datafiles will still be there. You have to remove them manually using rm command from the command line.
$ ls -l /u02/oradata/tgs/ -rw-r----- 1 oracle dba 209719296 Oct 12 13:02 thegeekstuff01.dbf -rw-r----- 1 oracle dba 104861696 Oct 12 13:02 thegeekstuff02.dbf
Or, the following command will drop the tablespace along with all the objects AND will automatically remove the associated datafiles form the operating system.
DROP TABLESPACE thegeekstuff INCLUDING CONTENTS AND DATAFILES;
Warning: Needless to say, be extra careful with drop tablespace command. You don’t want to use the “INCLUDING” clause unless you are absolutely sure that you don’t want anything from that particular tablespace.
12. Drop a Specific datafaile from a Tablespace
Instead of deleting the whole tablespace, you also have the option of deleting a specific datafile using the drop datafile as shown below.
This command will drop thegeekstuff02.dbf file (both from the tablespace and from the operating system level).
ALTER TABLESPACE thegeekstuff DROP DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf';
The above command will thrown an error message, if that particular datafile has any data in it.
13. Bring Tablespace Online or Offline
The following will make thegeekstuff tablespace unavailable for all the users. Every other tablespace in the database will be operational except this one. When successful, you’ll get “Tablespace altered.” message.
ALTER TABLESPACE thegeekstuff OFFLINE;
The status for this tablespace will now be OFFLINE as shown below.
TABLESPACE_NAME STATUS --------------- --------- THEGEEKSTUFF OFFLINE
Sometimes this is helpful when you want to just backup a tablespace using rman command.
Note: when you don’t specify a clause, it will use the normal offline mode. The following command is same as the above command.
ALTER TABLESPACE users OFFLINE NORMAL;
The following are three offline modes for tablespace:
- NORMAL – This is the default. This will take the tablespace offline only when none of the datafile has any errors associated with it.
- TEMPORARY – This will take the tablespace offline even if there are any errors associated with any of the datafiles for this tablespace.
- IMMEDIATE – This will take the tablespace offline immediately even when there are errors, and even without anything any checkpoint of the datafiles. Don’t use this unless you know exactly what you are doing.
To bring a tablespace back online, execute the following command.
ALTER TABLESPACE thegeekstuff ONLINE;
Similar to taking the whole database offline, you can also take a specific datafile offline and later bring it back online as shown below.
ALTER DATABASE DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf' offline; ALTER DATABASE DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf' online;
14. Set a Tablespace as Read-Only Temporarily
All tablespaces are initially created as read/write. Use the READ ONLY clause in the ALTER TABLESPACE statement to change a tablespace to read-only. You must have the ALTER TABLESPACE or MANAGE TABLESPACE system privilege.
By default, you can both read and write to a tablespace. This means that you can create any new objects (tables, etc) on the tablespace, insert data to it, and also select from the table, etc.
But, you can also make an existing tablespace read only. This means that you cannot create new object, or insert/delete/update/etc on the existing object. The only thing you can do is read the data.
The following will put thegeekstuff tablespace in read only mode.
ALTER TABLESPACE THEGEEKSTUFF READ ONLY;
Now, the status of this tablespace will be read only as shown below.
TABLESPACE_NAME STATUS --------------- --------- THEGEEKSTUFF READ ONLY
The following command will put the database back to the regular read write mode.
ALTER TABLESPACE THEGEEKSTUFF READ WRITE;
Note: When it is in read/write mode, the status will simply say “ONLINE”.
TABLESPACE_NAME STATUS --------------- --------- THEGEEKSTUFF ONLINE
15. Rename or Move Datafile to a Different Folder
Use the rename datafile as shown below, to simply rename a particular data file.
In this example, we are simply rename the file from thegeekstuff02.dbf to tgs02.dbf
ALTER TABLESPACE thegeekstuff RENAME DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf' TO '/u02/oradata/tgs/tgs02.dbf';
Note: If the file is currently in use, you’ll get the following error message:
ORA-01525: error in renaming data files ORA-01121: cannot rename database file 35 - file is in use or recovery ORA-01110: data file 35: '/u02/oradata/tgs/thegeekstuff02.dbf'
If you want to physically move the location of the datafile from one folder to another folder, do the following:
First, take the tablespace offline as shown below:
ALTER TABLESPACE thegeekstuff OFFLINE NORMAL;
Next, copy the files to the new location manually at the operating system level:
cp /u02/oradata/tgs/thegeekstuff02.dbf /u03/oradata/tgs/thegeekstuff02.dbf
Finally, execute the rename datafile command with the new location in the “TO” as shown below.
In this example, the file is getting moved from u02 to u03 mount point.
ALTER TABLESPACE thegeekstuff RENAME DATAFILE '/u02/oradata/tgs/thegeekstuff02.dbf' TO '/u03/oradata/tgs/thegeekstuff02.dbf';
Comments on this entry are closed.
Very Nice!!!
Hmm,
On a recent oracle system, built using best practices and oracle managed files, creating a new tablespace is as simple as ‘CREATE TABLESPACE new_ts_name;’ the db_create_file_dest parameter will make sure it gets created in the right place.
The tablespace can be altered to have an additional datafile with a given starting size that autoextends as necessary as follows:
ALTER TABLESPACE “small_ts” ADD DATAFILE ‘+ASM_DISKGROUP’ SIZE 8192M AUTOEXTEND ON NEXT 4096M MAXSIZE UNLIMITED;
Adds an extra 8G that will grow in 4G chunks until it gets to 32G, usually enough time to have a chat with a manager about data retention policies or disk purchases 🙂
cheers,