PostgreSQL 表空间

PostgreSQL 表空间

一 介绍
使用表空间可以将不同的表放到不同的存储介质或不同的文件系统下,实际上是为表指定一个存储的目录。
创建数据库,表,索引时可以指定表空间,将数据库,表,索引放到指定的目录下。

二 使用示例

创建表空间
create tablespace tbs_data location '/home/postgres/tbs_data';

查看
db
List of tablespaces
Name | Owner | Location
------------+----------+-------------------------
pg_default | postgres |
pg_global | postgres |
tbs_data | postgres | /home/postgres/tbs_data
(3 rows)

创建数据库
create database db_tbs tablespace tbs_data;
更改数据库
alter database db_tbs set tablespace tbs_data;

查看
l+ db_tbs
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
--------+----------+----------+-------------+-------------+-------------------+---------+------------+-------------
db_tbs | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6984 kB | tbs_data |
(1 row)


创建表
create table t_tbs1(id int ,name varchar,t text) tablespace tbs_data;
alter table t_tbs1 set tablespace tbs_data;

d+ t_tbs1
Table "public.t_tbs1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-------------------+-----------+----------+--------------+-------------
id | integer | | plain | |
name | character varying | | extended | |
t | text | | extended | |
Tablespace: "tbs_data"

三 表空间目录说明

默认表空间pg_deflaut 的目录位置为base文件夹


ls /var/lib/postgresql/9.5/main/base/
1 12410 12415 126072 16384 23868 27602 pgsql_tmp

ls /home/postgres/tbs_data/PG_9.5_201510051/
128848 130024 27602

ls -al /var/lib/postgresql/9.5/main/pg_tblspc/
total 8
drwx------ 2 postgres postgres 4096 9月 19 14:45 .
drwx------ 19 postgres postgres 4096 9月 19 15:02 ..
lrwxrwxrwx 1 postgres postgres 23 9月 19 14:45 129008 -> /home/postgres/tbs_data

select oid,datname from pg_database ;
oid | datname
--------+------------------
1 | template1
12410 | template0
12415 | postgres
16384 | index_t
128848 | pgbench
130024 | db_tbs
23868 | sentry
27602 | new_test

进一步查看表存储位置

tree 27602/
27602/
├── 130015
├── 130015_fsm
├── 130015_vm
├── 130016
├── 130016_fsm
├── 130016_vm
├── 130017
├── 130018
├── 130021
├── 130023
├── 130025
├── 130028
└── 130030

new_test=# d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------------+-------+----------+------------+-------------
public | score | table | postgres | 0 bytes |
public | student | table | postgres | 0 bytes |
public | t_fillfactor | table | postgres | 8192 bytes |
public | t_fillfactor01 | table | postgres | 8192 bytes |
public | t_tbs | table | postgres | 6240 kB |


select oid,relname , reltablespace ,relfilenode from pg_class where relname = 't_tbs';
oid | relname | reltablespace | relfilenode
--------+---------+---------------+-------------
129009 | t_tbs | 129008 | 130015

原文地址:https://www.cnblogs.com/zhangeamon/p/7552584.html