Module-12
PostgreSQL Tablespace
Module Objective:
• Tablespace & its advantages
• PostgreSQL default tablespaces
• Create tablespaces
• Move table from one tablespace to another
• Drop tablespaces
• Temporary tablespaces
Tablespace & its advantages
• PostgreSQL stores data logically in tablespaces and physically in datafiles.
• PostgreSQL uses a tablespace to map a logical name to a physical location
on disk.
• Tablespace allows the user to control the disk layout of PostgreSQL.
• Statistics of database objects usage to optimize the performance of
databases.
• Allocate data storage across devices to improve performance .
• WAL files object on fast media and archive data on slow media.
Default Tablespaces
• Default comes with two out of the box tablespaces namely pg_default and
pg_global
• pg_default tablespace stores all user data.
• pg_global tablespace stores all global data.
• pg_default tablespace is the default tablespace of the template1 and
template0 databases.
• All newly created database uses pg_default tablespace, unless overridden by a
TABLESPACE clause while CREATING DATABASE.
• Location of Default Tablespaces is data directory.
Create tablespace
• Syntax for creating tablespace: (ensure the location exist)
create tablespace hrd location '/opt/app/hrd/';
• Syntax for creating a table on a newly created tablespace
create table test1(studid int,stuname varchar(50)) tablespace hrd;
• Query to find which tablespace the table belong to
Syntax : select * from pg_tables where tablespace='hrd';
or
select * from pg_tables where tablename=‘test1';
Move Table between tablespaces
Move tables from one tablespace to another
Syntax :
alter table test1 set tablespace pg_default
Check whether the table is moved successfully to another tablespae
Syntax : select * from pg_tables where tablename=‘test1’
Find physical location of the table
Syntax : select pg_relation_filepath(‘test1');
Find physical location of the tablespace
Syntax : postgres#/dt
Drop Tablespace
• Dropping a tablespace all the reference from the system automatically.
• We cannot drop a tablespace which is not empty.
• Find objects associate with the tablespace
Syntax : select * from pg_tables where tablespace = 'hrd';
• Drop tablespace
Syntax : drop tablespace hrd;
• Query pg system catalog view to check the tablespace is dropped.
Syntax : select * from pg_tablespace;
Temporary tablespace
• Temporary tables and indexes are created by PostgreSQL when it needs to hold large
datasets temporarily for completing a query. EX: Sorting
• Temporary tablespace does not store any data and their no persistent file left when we
shutdown database.
• How to create temporary tablespace
Syntax : CREATE TABLESPACE temp01 OWNER ownername LOCATION '\opt\app\hrd\'
• Set temp_tablespaces=temp01 in postgresql.conf and reloaded configuration.
• PG will automatically create a subfolder in the above location when a temp table is
created.
• When we shutdown the database the temp files will be delete automatically.
Thank you.