Working with Databases-Creating and Managing Tablespaces
Tags: opengauss
Published: 2023-03-13 09:35:00

**Background**

The administrator can use tablespaces to control the layout of disks where a database is installed. This has the following advantages:

**·** If the initial disk partition or volume allocated to the database is full and the space cannot be logically increased, you can create and use tablespaces in other partitions until the space is reconfigured.

**·** Tablespaces allow the administrator to distribute data based on the schema of database objects, improving system performance.

**-** A frequently used index can be placed in a disk having stable performance and high computing speed, such as a solid device.

**-** A table that stores archived data and is rarely used or has low performance requirements can be placed in a disk with a slow computing speed.

**·** The administrator can use tablespaces to set the maximum available disk space. In this way, when a partition is shared with other data, tablespaces will not occupy excessive space in the partition.

**·** Each tablespace corresponds to a file system directory. Assume that Database node data directory**/pg_location/mount1/path1** is an empty directory for which users have read and write permissions.

If the tablespace quota management is used, the performance may deteriorate by about 30%. **MAXSIZE** specifies the maximum quota for each database node. The deviation must be within 500 MB. Determine whether to set a tablespace to its maximum size as required.

openGauss provides two tablespaces: **pg_default** and **pg_global**.

**·** Default tablespace **pg_default**: stores non-shared system tables, user tables, user table indexes, temporary tables, temporary table indexes, and internal temporary tables. The corresponding storage directory is the base directory in the instance data directory.

**·** Shared tablespace **pg_global**: stores shared system tables. The corresponding storage directory is the base directory in the global data directory.

**Precautions**
In scenarios such as HCS, you are not advised to use user-defined tablespaces. This is because user-defined tablespaces are usually used with storage media other than the main storage (storage device where the default tablespace is located, such as a disk) to isolate I/O resources that can be used by different services. In HCS scenarios, storage devices use standard configurations and do not have other available storage media. If the customized tablespace is not properly used, the system cannot run stably for a long time and the overall performance is affected. Therefore, you are advised to use the default tablespace.

**Procedure**
**·** Create a tablespace.

1. Run the following command to create user jack:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3utxtgfy6ufdj0swnd6j.png)

If the following information is displayed, the user has been created:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjg0xeburwgtwta8rcrp.png)

2. Run the following command to create a tablespace:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x9nu8lapq0mbjiuzz25.png)

If the following information is displayed, the tablespace has been created:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avezv60zmlmwo6s6awpe.png)

**fastspace** is the new tablespace, and Database node data directory**/pg_location/tablespace/tablespace_1** is an empty directory for which users have read and write permissions.

3. A database system administrator can run the following command to grant the permission of accessing the **fastspace** tablespace to user jack:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1vfhku3tholebkm1b4x.png)

If the following information is displayed, the permission has been assigned:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ok2k9benvnqlfkyz026.png)

**·** Create an object in a tablespace.

If you have the CREATE permission on the tablespace, you can create database objects in the tablespace, such as tables and indexes.

Take creating a table as an example:

**-** Method 1: Run the following command to create a table in a specified tablespace:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po7qg3g0pahwm4esgiow.png)

If the following information is displayed, the table has been created:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymxn3o211hqcm1qw7rcc.png)

**-** Method 2: Use set **default_tablespace** to set the default tablespace and then create a table:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uasr7bxdm10lsgbz7pml.png)

In this example, **fastspace** is the default tablespace, and **foo2** is the created table.

**·** Use one of the following methods to query a tablespace:

**-** Method 1: Check the pg_tablespace system catalog. Run the following command to view all the tablespaces defined by the system and users:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkbtx14x8ma8r5hmkzds.png)

**-** Method 2: Run the following meta-command of the gsql program to query the tablespaces:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4kgcbk33hsglulsgnn3.png)

**·** Query the tablespace usage.

1. Query the current usage of the tablespace.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ekq8qjml34ova5x5fq9.png)

The following information is displayed:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2ni7uhuhmggyg430xq7.png)

2146304 is the size of the tablespace, and its unit is byte.

2. Calculate the tablespace usage.

Tablespace usage rate = **PG_TABLESPACE_SIZE/Size** of the disk where the tablespace resides

· Modify a tablespace.

Run the following command to rename tablespace fastspace to fspace:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nd2tu20c5gqu14zjvect.png)

**·** Delete a tablespace and related data.

**·** Run the following command to delete user **jack**:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/au1mid7vseuxnt5z8sws.png)

**·** Run the following commands to delete tables foo and foo2:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idvoku90z2y89jkrp6cr.png)

If the following information is displayed, the tables have been deleted:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64va46cnovk62n7swrow.png)

**·** Run the following command to delete tablespace **fspace**:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhag77y63fk3p3n2zx1c.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbvsczx25ibm9hcpn010.png)