Oracle Database - TableSpace

1 - About

A tablespace is a logical data structure which is physically represented by one or more datafiles physically.

From a analysis point of view, a tablespace is a group of one or more:

3 - Type

SELECT DISTINCT contents FROM dba_tablespaces;
CONTENTS
---------
PERMANENT
TEMPORARY

3.1 - Permanent

A permanent tablespace contains persistent schema objects. Objects in permanent tablespaces are stored in datafiles.

3.2 - Undo

An undo tablespace is a type of permanent tablespace used by Oracle Database to manage undo data if you are running your database in automatic undo management mode. Oracle strongly recommends that you use automatic undo management mode rather than using rollback segments for undo.

3.3 - Temporary

A temporary tablespace contains schema objects only for the duration of a session. Objects in temporary tablespaces are stored in tempfiles. files that are stored in temporary tablespaces are called tempfiles and are subject to some restrictions.

4 - System Tablespace

Created when you create a database.

Tablespace Type Description
SYSTEM Primary contains information basic to the functioning of the database server, such as the data dictionary and the system rollback segment
SYSAUX Auxiliary contains information about the schemas used by various Oracle products and features, so that those products do not require their own tablespaces.

5 - Tablespace FILE type

Oracle Database uses the current default tablespace type of permanent or temporary tablespace set for the database if the type of file is not set during the creation.

Oracle database - File (Common Specification for data file, control file,...)

5.1 - BIGFILE

A bigfile tablespace contains only one datafile or tempfile, which can contain up to approximately 4 billion (<math>2^{32}</math>) blocks. The maximum size of the single datafile or tempfile is:

  • 128 terabytes (TB) for a tablespace with 32K blocks
  • and 32TB for a tablespace with 8K blocks.

The database by default creates a locally managed tablespace with automatic segment-space management.

Restrictions on Bigfile Tablespaces:

  • You can specify only one datafile in the DATAFILE clause or one tempfile in the TEMPFILE clause.
  • You cannot specify EXTENT MANAGEMENT DICTIONARY.

5.2 - SMALLFILE

A smallfile tablespace is a traditional Oracle tablespace, which can contain 1022 datafiles or tempfiles, each of which can contain up to approximately 4 million (<math>2^{22}</math>) blocks.

6 - How to

6.1 - Creation

You must have the CREATE TABLESPACE system privilege. To create the SYSAUX tablespace, you must have the SYSDBA system privilege.

Before you can create a tablespace, you must create a database to contain it, and the database must be open.

When you create a tablespace, it is initially a read/write tablespace.

6.1.1 - DDL

Example: temp tablespace:

SELECT DBMS_METADATA.get_ddl('TABLESPACE','TEMP') FROM dual;
CREATE TEMPORARY TABLESPACE "TEMP" TEMPFILE 
  '/home/oracle/app/oracle/oradata/cdb1/orcl/orcl_temp012014-07-30_04-39-23-PM.dbf' SIZE 206569472
  AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M
  EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1048576"
  • Users tablespace
SELECT DBMS_METADATA.get_ddl('TABLESPACE','USERS') FROM dual;
CREATE BIGFILE TABLESPACE "USERS" 
  DATAFILE '/home/oracle/app/oracle/oradata/cdb1/orcl/SAMPLE_SCHEMA_users01.dbf' SIZE 5242880 AUTOEXTEND ON NEXT 1310720 MAXSIZE 32767M
  LOGGING 
  ONLINE 
  PERMANENT 
  BLOCKSIZE 8192
  EXTENT MANAGEMENT LOCAL AUTOALLOCATE DEFAULT 
  NOCOMPRESS  
  SEGMENT SPACE MANAGEMENT AUTO

where:

  • the BIGFILE clause specifies that it's a bigfile
  • the DATAFILE clause specifies a data file of size 5242880 bytes (ie 5242880 = 1024*1024*5= 5MB). Default size: 100M file. See Oracle database - File (Common Specification for data file, control file,...). The AUTOEXTEND clause said that when more space is required, 1310720 byte (1,25MB) extents will be added up to a maximum size of 32767 megabytes (size of a segment)
  • ONLINE specifies the status of the tablespace
  • PERMANENT: the type of tablespace
  • the SEGMENT clause specifies an automatic segment-space management

6.2 - blocksize

The existing tablespace's blocksize can't be changed without recreating it. See :

In order to specify a nonstandard block size for the tablespace.:

  • the following parameters must be set:
  • the integer you specify in this clause must correspond with the setting of one DB_n K_CACHE_SIZE parameter setting.

Restriction: You cannot specify nonstandard block sizes for a temporary tablespace (or if you intend to assign this tablespace as the temporary tablespace for any users).

6.3 - Offline/Online

use the ALTER TABLESPACE statement.

Online is the default value.

ALTER TABLESPACE tablespaceName ONLINE;
ALTER TABLESPACE tablespaceName OFFLINE;
 
select status from dba_tablespaces;

6.4 - Maxsize

The maxsize of a tablespace is determined by:

  • the type of tabelspace
  • the file autoextend property

6.4.1 - Type of tabelspace

Type File Max Blocks With a
Block Size
of
Allowed Max Size
smallfile 4 million (<math>2^{22}</math>) blocks 32K 128 Gb
smallfile 4 million (<math>2^{22}</math>) blocks 8K 32 Gb
bigfile 4 billion (<math>2^{32}</math>) blocks 32K 128 terabytes (TB)
bigfile 4 billion (<math>2^{32}</math>) blocks 8K 32 terabytes (TB)

6.4.2 - File autoextend property

Maxsize Limitation for that (data|temp) file is determined by the file autoextend property in the create statement.

If the (data|temp) file is:

  • auto extensible: the total sum of MAXBYTES of DBA_…_FILES
  • not auto extensible: the total sum of BYTES of DBA_…_FILES
SELECT   TABLESPACE_NAME
  , AUTOEXTENSIBLE
  , COUNT ( 1 )                                                                    AS Number_Of_Files
  , SUM ( DECODE ( AUTOEXTENSIBLE, 'YES', MAXBYTES, BYTES ) ) / 1024 / 1024 / 1024 AS MAX_SIZE_GB
  , SUM ( MAXBYTES )                                          / 1024 / 1024 / 1024 AS MAXBYTES_GB
  , SUM ( BYTES )                                             / 1024 / 1024 / 1024 AS BYTES_GB
  FROM DBA_DATA_FILES
  GROUP BY TABLESPACE_NAME
  , AUTOEXTENSIBLE;
  • Temp:
SELECT TABLESPACE_NAME, SUM(DECODE(AUTOEXTENSIBLE, 'YES', MAXBYTES, BYTES)) MAX_SIZE
FROM   DBA_TEMP_FILES
GROUP  BY TABLESPACE_NAME;

6.5 - Read-Only

When you create a tablespace, it is initially a read/write tablespace. use the ALTER TABLESPACE statement to make it a read-only tablespace.

6.6 - Default

ALTER DATABASE DEFAULT TABLESPACE "USERS";

6.7 - Rename

ALTER tablespace temp3 rename TO temp2; 

The following errors can occur when renaming a tablespace to a name that existed for version previous to 11.2: ORA-959 Tablespace '_$deleted$0$0' Does Not Exist Error Executing Some Code

6.8 - Drop

Take the tablespace offline before dropping it to ensure that no SQL statements in currently running transactions access any of the objects in the tablespace.

DROP TABLESPACE statement.

  • Dropping a Tablespace and all referential integrity constraints that refer to primary and unique keys inside tbs_01:
DROP TABLESPACE tbs_01 
    INCLUDING CONTENTS 
        CASCADE CONSTRAINTS; 
  • Deleting Operating System Files: Example The following example drops the tbs_02 tablespace and deletes all associated operating system datafiles:
DROP TABLESPACE tbs_02
   INCLUDING CONTENTS AND DATAFILES;

6.9 - Segment space management

6.10 - Data Dictionary

See:

in the oracle database reference manual

7 - Documentation / Reference

db/oracle/tablespace.txt · Last modified: 2017/09/20 15:25 by gerardnico