Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views9 pages

12 Tablespace

This document provides an overview of PostgreSQL tablespaces, including their advantages, default tablespaces, and how to create, move, and drop tablespaces. It explains the purpose of tablespaces in managing data storage and performance optimization, as well as the process for handling temporary tablespaces. Key syntax for creating and managing tablespaces is also included.

Uploaded by

rizqi ardiansyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

12 Tablespace

This document provides an overview of PostgreSQL tablespaces, including their advantages, default tablespaces, and how to create, move, and drop tablespaces. It explains the purpose of tablespaces in managing data storage and performance optimization, as well as the process for handling temporary tablespaces. Key syntax for creating and managing tablespaces is also included.

Uploaded by

rizqi ardiansyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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. 

You might also like