0% found this document useful (0 votes)
18 views6 pages

PostgreSQL Tablespace Management

A PostgreSQL tablespace is a designated disk location for storing database objects like tables and indexes, with two default tablespaces: pg_default for user data and pg_global for global data. Users can create custom tablespaces using the CREATE TABLESPACE command and assign them to databases or tables, while temporary tablespaces can be set for temporary objects. Additionally, users can alter or drop tablespaces, but must ensure they are empty before dropping, and can check objects within a tablespace using specific SQL queries.

Uploaded by

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

PostgreSQL Tablespace Management

A PostgreSQL tablespace is a designated disk location for storing database objects like tables and indexes, with two default tablespaces: pg_default for user data and pg_global for global data. Users can create custom tablespaces using the CREATE TABLESPACE command and assign them to databases or tables, while temporary tablespaces can be set for temporary objects. Additionally, users can alter or drop tablespaces, but must ensure they are empty before dropping, and can check objects within a tablespace using specific SQL queries.

Uploaded by

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

PostgreSQL tablespace:

A tablespace is a disk location where PostgreSQL stores data files containing database
objects such as indexes and tables.

PostgreSQL uses a tablespace to associate a logical name to a physical location on the


disk.
PostgreSQL comes with two default tablespaces:
pg_default tablespace stores user data. (default tablespace)

pg_global tablespace stores global data.


To list all tablespaces in the current PostgreSQL database server, you use the \db
command:

The basic syntax of the CREATE TABLESPACE statement:


CREATE TABLESPACE tablespace_name
[ OWNER { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ]
LOCATION 'directory'

[ WITH ( tablespace_option = value [, ... ] ) ];


To create a tablespace dbspace at file system location /data/dbs, first create the
directory using operating system facilities and set the correct ownership:
mkdir -p /data/dbs
chown -R postgres:postgres /data/dbs

Then issue the tablespace creation command inside PostgreSQL:


CREATE TABLESPACE dbspace LOCATION '/data/dbs';
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the
non-built-in tablespaces defined in the cluster.

A user with appropriate privileges can pass tablespace_name to CREATE DATABASE,


CREATE TABLE, CREATE INDEX to have the data files for these objects stored within the
specified tablespace.
CREATE DATABASE db_name TABLESPACE tablespace_name;
CREATE TABLE table_name(col1 data_type1,col2 data_type 2,…) TABLESPACE
tablespace_name;
CREATE INDEX index_name ON table_name(column_name) TABLESPACE
tablespace_name;

To create a tablespace owned by a different database user:


CREATE TABLESPACE dbs_new_tspace OWNER test_user LOCATION '/data/dbs_new';

To change the default tablespace for the current session:


SET default_tablespace='tablespace_name';

CREATE TABLE foo(i int);


Temporary Tablespace:
The temp_tablespaces parameter in PostgreSQL determines where temporary tables,
indexes, and files (e.g., for sorting large datasets) are stored. It can be a list of
tablespaces, allowing temporary object load to be distributed across multiple
tablespaces. A random tablespace from the list is chosen for each temporary object.
Setup temp_tablespaces:
method 1):
mkdir -p /data/temp_tbls

chown -R postgres:postgres /data/temp_tbls


CREATE TABLESPACE temp_tbls LOCATION '/data/temp_tbls';
ALTER SYSTEM SET temp_tablespaces = 'temp_tbls'; #ALTER SYSTEM — change a server
configuration parameter

SELECT pg_reload_conf(); #To reload the changes

method 2):
Reset the changes:

ALTER SYSTEM RESET temp_tablespaces;


SELECT pg_reload_conf();
Set the temp_tablespaces in a $PGDATA/postgresql.conf file.
SELECT pg_reload_conf();

Create temporary table for testing:


create temporary table test_temp_table(id int);
select pg_relation_filepath(' test_temp_table ');

To moves the data file(s) associated with the database, table, index to the new
tablespace:
CREATE DATABASE db_name;
ALTER DATABASE db_name SET TABLESPACE tablespace_name;
CREATE TABLE table_name(col1 data_type,….);
ALTER TABLE table_name SET TABLESPACE tablespace_name;

CREATE TABLE table_name(col1 data_type,….);


CREATE INDEX index_name ON table_name(col1);
ALTER INDEX index_name SET TABLESPACE tablespace_name;

PostgreSQL does not allow altering the tablespace of a primary key constraint. Instead,
you need to move the index associated with the primary key constraint to the new
tablespace.
CREATE TABLE table_name(column_name data_type PRIMARY KEY);

ALTER INDEX index_name SET TABLESPACE tablespace_name;

ALTER TABLESPACE — change the definition of a tablespace:


ALTER TABLESPACE old_tablespace_name RENAME TO new_tablespace_name;

ALTER TABLESPACE tablespace_name OWNER TO new_user;

DROP TABLESPACE — remove a tablespace:


The error tablespace is not empty in PostgreSQL occurs when you attempt to drop a
tablespace that still contains objects like tables, indexes, or other database objects. To
resolve this issue, you need to ensure that the tablespace is empty before dropping it.

Check Objects in the Tablespace:


SELECT relname AS object_name, relkind AS object_type FROM pg_class c JOIN
pg_tablespace t ON c.reltablespace = t.oid WHERE t.spcname = 'dbs_old_tspace';
Check database in the Tablespace:
SELECT datname AS object_name FROM pg_database c JOIN pg_tablespace t ON
c.dattablespace = t.oid WHERE t.spcname = 'dbs_old_tspace';

Move Objects and databases to Another Tablespace:


ALTER DATABASE db_name SET tablespace_name;
ALTER TABLE table_name SET tablespace_name;
ALTER INDEX index_name SET tablespace_name;
DROP TABLESPACE tablespace_name;

You might also like