PostgreSQL Tablespace managment

 

Tablesapce Basics


What is a tablespace?

It defines where the contents of the tables should be saved.

Rules:

  • We can't use a database unless we have tablespace.
  • pg_default is the default tablespace for all the databases.
  • We can define the non-default tablespace while creating a database or can alter the existing database/s by assigning a non-default tablespace.
  • Users can define on which tablespace the objects should be stored when creating them.

What are the different types of tablespaces present in the Postgres database?

There are two different types of tablespace:

Default: by default, two tablespaces will be created at the server level.

pg_default and pg_global

Non-default: User created manually. 

postgres=# select * from pg_tablespace;
  oid  |  spcname   | spcowner | spcacl | spcoptions
-------+------------+----------+--------+------------
  1663 | pg_default |       10 |        |
  1664 | pg_global  |       10 |        |
 16449 | test_tbs   |       10 |        |
(3 rows)

postgres=# \db
                  List of tablespaces
    Name    |  Owner   |            Location
------------+----------+--------------------------------
 pg_default | postgres |
 pg_global  | postgres |
 test_tbs   | postgres | /data/pgsql/databases/test_tbs
(3 rows)

What is the default location of default and non-default tablespaces?

Default tablespace location:  /var/lib/pgsql/15/data/base


[postgres@postgres ~]$ cd $PG_DATA
[postgres@postgres data]$ cd base/
[postgres@postgres base]$ pwd
/var/lib/pgsql/15/data/base


Non-default tablespace location: /var/lib/pgsql/15/data/pg_tblspc

[postgres@postgres ~]$ cd $PG_DATA
[postgres@postgres data]$ cd pg_tblspc/
[postgres@postgres pg_tblspc]$ pwd
/var/lib/pgsql/15/data/pg_tblspc
[postgres@postgres pg_tblspc]$ ls -rlt
total 0
lrwxrwxrwx. 1 postgres postgres 27 Jul 27 14:01 16445 -> /data/pgsql/databases/test_tbs


What is the use of pg_global default tablespace?

pg_default tablespace stores internal objects information like catalog objects.
the location of pg_default tablespace will be same as pg_default tablespace.
 

Tablespace management 

Create non-default tablespace:

postgres=# create tablespace prod_tbs location '/data/pgsql/databases/prod';
CREATE TABLESPACE
postgres=# \db
                  List of tablespaces
    Name    |  Owner   |            Location
------------+----------+--------------------------------
 pg_default | postgres |
 pg_global  | postgres |
 prod_tbs   | postgres | /data/pgsql/databases/prod
 test_tbs   | postgres | /data/pgsql/databases/test_tbs
(4 rows)

postgres=# select * from pg_tablespace;
  oid  |  spcname   | spcowner | spcacl | spcoptions
-------+------------+----------+--------+------------
  1663 | pg_default |       10 |        |
  1664 | pg_global  |       10 |        |
 16448 | prod_tbs   |       10 |        |
 16449 | test_tbs   |       10 |        |
(4 rows)


Drop non-default tablespace:

postgres=# drop tablespace dw;
DROP TABLESPACE

Note: make sure to move or remove all the objects which is part of dropping tablespace.


Creating a database with default and non-default tablespace:

postgres=# create database test_default;   --- no tablespace defined while creating the database
CREATE DATABASE
postgres=# create database test_non_default tablespace test_tbs;   --- defining non-defult tablespace while database creation
CREATE DATABASE
        
postgres=# \l+ test_default
                                                                   List of databases
     Name     |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider | Access privileges |  Size   | Tablespace | Description
--------------+----------+----------+-------------+-------------+------------+-----------------+-------------------+---------+------------+-------------
 test_default | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |                   | 7537 kB | pg_default |
(1 row)

postgres=# \l+ test_non_default
                                                                     List of databases
       Name       |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider | Access privileges |  Size   | Tablespace | Description
------------------+----------+----------+-------------+-------------+------------+-----------------+-------------------+---------+------------+-------------
 test_non_default | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |                   | 7537 kB | test_tbs   |
(1 row)


How to assign the non-default or different tablespce to existing database?

postgres=# alter database test2 set tablespace test_tbs;
ALTER DATABASE


Before:

postgres=# \l+ test2
                                                                List of databases
 Name  |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider | Access privileges |  Size   | Tablespace | Description
-------+----------+----------+-------------+-------------+------------+-----------------+-------------------+---------+------------+-------------
 test2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |                   | 7537 kB | pg_default |
(1 row)

After:

postgres=# alter database test2 set tablespace test_tbs;
ALTER DATABASE
postgres=# \l+ test2
                                                                List of databases
 Name  |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider | Access privileges |  Size   | Tablespace | Description
-------+----------+----------+-------------+-------------+------------+-----------------+-------------------+---------+------------+-------------
 test2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |                   | 7537 kB | test_tbs   |
(1 row)


How to see DATA directory location at database level?

postgres=# show data_directory;
     data_directory
------------------------
 /var/lib/pgsql/15/data
(1 row)


How to see tableapce utilization/size?

postgres=# select pg_size_pretty(pg_tablespace_size('prod_tbs'));
 pg_size_pretty
----------------
 50 MB
(1 row)


How and when to create a temporary tablespace in postgres database?

postgres=# create tablespace temp_tbs location '/data/pgsql/databases/tmp_tbs';
CREATE TABLESPACE
postgres=# show temp_tablespaces;
 temp_tablespaces
------------------

(1 row)

postgres=# set temp_tablespaces='temp_tbs';
SET

postgres=# show temp_tablespaces;
 temp_tablespaces
------------------
 temp_tbs
(1 row)

Note for temporary tablespace: 

We only utilize temporary tablespaces when we create temporary objects in the database. The reason for this is that if we do not designate a temporary tablespace for temp objects, they will be created in the default tablespace and may cause IO challenges with other objects IO, therefore, it is suggested/recommended to have a temporary tablespace assigned for temp tables and also, if the temp tablespace grows due to excessive activity it may cause space concerns.

Comments

Popular posts from this blog

Tuples in Python

Auditing in Oracle database

Boolean in Python