PostgreSQL tablespace

I. Overview

   Tablespaces in PostgreSQL allow database administrators to define the location in the file system where files representing database objects are stored. Once created, tablespaces can be referenced when creating database objects, such as specifying tablespaces when creating databases, tables, indexes, and adding indexes. Administrators can use tablespaces to control the disk layout of PostgreSQL, which has at least two uses.

  • When the partition or volume on which the cluster was initialized runs out of space and cannot be logically extended, the tablespace can be created on a different partition until the system can be reconfigured.
  • Tablespaces allow administrators to optimize performance based on usage patterns of database objects. For example, a very heavily used index could be placed on a very fast and very reliable disk, such as a very expensive solid-state device. At the same time, a table storing archived data that is rarely used or performance-critical can be stored on an inexpensive but slow disk system.

2. Create and use tablespace

2.1 Grammar

CREATE TABLESPACE tablespace_name
    [ OWNER { new_owner | CURRENT_USER | SESSION_USER } ]
    LOCATION 'directory'
    [ WITH ( tablespace_option = value [, ... ] ) ]

Parameter description :

tablespace_name : The name of the tablespace to create. The name cannot begin with pg_, as such names are reserved for system tablespaces.
new_owner : The username that will own the tablespace. If omitted, defaults to the user executing the command. Only superusers can create tablespaces, but they can assign tablespace ownership to non-superusers.
directory : The directory to be used for the tablespace. This directory must exist (CREATE TABLESPACE will not create it), should be empty, and must be owned by the PostgreSQL system user. The directory must be specified with an absolute path.
tablespace_option : The tablespace parameter to be set or reset. Currently, the only parameters available are seq_page_cost, random_page_cost, effective_io_concurrency, and maintenance_io_concurrency. Setting these values ​​for a particular tablespace overrides the planner's normal cost estimate for table page reads in that tablespace, and the executor's prefetching behavior, as determined by the configuration parameter of the same name. These parameters come into play if a tablespace is on a slower or faster disk than other I/O subsystems.

2.2 Examples

To create the tablespace dbspace at the filesystem location /data/dbs, first create the directory and set the correct ownership using operating system tools

mkdir /data/dbs
chown postgres:postgres /data/dbs

Issue the tablespace create command in the database

--创建表空间 dbspace
CREATE TABLESPACE dbspace LOCATION '/data/dbs';
--创建数据库时使用指定表空间 dbspace
CREATE DATABASE db01 TABLESPACE dbspace;
--创建表时使用指定表空间 dbspace
CREATE TABLE test01(id int, note text) TABLESPACE dbspace;
--创建索引时使用指定表空间 dbspace
CREATE INDEX idx_test01_id on test01(id) TABLESPACE dbspace;
--添加约束时使用指定表空间 dbspace
ALTER TABLE test01 ADD CONSTRAINT unique_test01_id unique(id) USING INDEX TABLESPACE dbspace;

Third, modify the table space

3.1 Grammar

ALTER TABLESPACE name RENAME TO new_name
ALTER TABLESPACE name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
ALTER TABLESPACE name SET ( tablespace_option = value [, ... ] )
ALTER TABLESPACE name RESET ( tablespace_option [, ... ] )

Parameter description :

  • name : The name of an existing tablespace.
  • new_name : The new name of the tablespace. New names cannot start with pg_, since such names are reserved for system tablespaces.
  • new_owner : The new owner of the tablespace.
  • tablespace_option : A tablespace parameter to set or reset. Currently, the only parameters available are seq_page_cost, random_page_cost, effective_io_concurrency, and maintenance_io_concurrency. Setting these two parameter values ​​for a particular tablespace will override the planner's estimate of the cost of reading pages from tables in that tablespace, as well as the executor's prefetching behavior, established by the configuration parameter with the same name . These parameters can come in handy if a table space is located on a disk that is faster or slower than the rest of the I/O subsystem.

3.2 Examples

--将表空间 index_space 重命名为 fast_raid
ALTER TABLESPACE index_space RENAME TO fast_raid;
--更改表空间 index_space 的拥有者
ALTER TABLESPACE index_space OWNER TO mary;

Fourth, move the table space

The files in the original disk space directory can be moved to the new directory, but the entire moving object will be locked during the moving process, and all operations cannot be performed, including select.

4.1 Grammar

--移动数据库表空间
ALTER DATABASE name SET TABLESPACE tablespace_name;
--移动表表空间
ALTER TABLE name SET TABLESPACE tablespace_name;
--移动索引表空间
ALTER INDEX name SET TABLESPACE tablespace_name;

4.2 Examples

--查询表 user_info 所在表空间
SELECT A.RELNAME,B.SPCNAME FROM PG_CLASS A, PG_TABLESPACE B 
WHERE (CASE A.RELTABLESPACE WHEN 0 THEN 
(SELECT OID FROM PG_TABLESPACE WHERE SPCNAME = 'pg_default') 
ELSE A.RELTABLESPACE END ) = B.OID 
AND A.RELNAME = 'user_info' ; 

--移动表 user_info 至新表空间 exam
ALTER TABLE user_info SET TABLESPACE exam;

Five, delete the table space

5.1 Grammar

DROP TABLESPACE [ IF EXISTS ] name

Parameter description :

IF EXISTS : Do not throw an error if the tablespace does not exist, but issue a notice.
name : The name of a tablespace.

5.2 Examples

--将表空间 fast_raid
DROP TABLESPACE fast_raid;

Guess you like

Origin blog.csdn.net/songyundong1993/article/details/131687527