当前位置: 首页 > news >正文

pg表空间管理

https://www.cnblogs.com/lottu/category/535826.html

PostgreSQL中的表空间允许在文件系统中定义用来存放表示数据库对象的文件的位置。在PostgreSQL中表空间实际上就是给表指定一个存储目录。

官方解释

通过使用表空间,管理员可以控制一个PostgreSQL安装的磁盘布局。这么做至少有两个用处。

如果初始化集簇所在的分区或者卷用光了空间,而又不能在逻辑上扩展或者做别的什么操作,那么表空间可以被创建在一个不同的分区上,直到系统可以被重新配置。
表空间允许管理员根据数据库对象的使用模式来优化性能。例如,一个很频繁使用的索引可以被放在非常快并且非常可靠的磁盘上,如一种非常贵的固态设备。同时,一个很少使用的或者对性能要求不高的存储归档数据的表可以存储在一个便宜但比较慢的磁盘系统上。
用一句话来讲:能合理利用磁盘性能和空间,制定最优的物理存储方式来管理数据库表和索引。

在Oracle数据库中;一个表空间只属于一个数据库使用;而一个数据库可以拥有多个表空间。属于"一对多"的关系
在PostgreSQL集群中;一个表空间可以让多个数据库使用;而一个数据库可以使用多个表空间。属于"多对多"的关系。
表空间pg_default是用来存储系统目录对象、用户表、用户表index、和临时表、临时表index、内部临时表的默认空间。对应存储目录$PADATA/base/
表空间pg_global用来存放系统字典表;对应存储目录$PADATA/global/
列出现有的表空间

postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+---------------------
pg_default | postgres |
pg_global | postgres |
tp_lottu | lottu | /data/pg_data/lottu
(3 rows)

postgres=# select oid,* from pg_tablespace;
oid | spcname | spcowner | spcacl | spcoptions
-------+------------+----------+--------+------------
1663 | pg_default | 10 | |
1664 | pg_global | 10 | |
16385 | tp_lottu | 16384 | |
(3 rows)
Syntax:

CREATE TABLESPACE tablespace_name [ OWNER { new_owner | CURRENT_USER | SESSION_USER } ] LOCATION 'directory'
示例如下:

postgres=# \c lottu postgres
You are now connected to database "lottu" as user "postgres".
lottu=# CREATE TABLESPACE tsp01 OWNER lottu LOCATION '/data/pg_data/tsp';
CREATE TABLESPACE
目录"/data/pg_data/tsp"必须是一个已有的空目录,并且属于PostgreSQL操作系统用户

$ mkdir -p /data/pg_data/tsp
$ chown -R postgres:postgres /data/pg_data/tsp
表空间的创建本身必须作为一个数据库超级用户完成,但在创建完之后之后你可以允许普通数据库用户来使用它.要这样做,给数据库普通用户授予表空间上的CREATE权限。表、索引和整个数据库都可以被分配到特定的表空间.
示例用户"rax":为普通用户。

lottu=# \c lottu01 rax
You are now connected to database "lottu01" as user "rax".
lottu01=> create table test_tsp(id int) tablespace tsp01;
ERROR: permission denied for tablespace tsp01
lottu01=> \c lottu01 postgres
You are now connected to database "lottu01" as user "postgres".
lottu01=# GRANT CREATE ON TABLESPACE tsp01 TO rax;
GRANT
lottu01=# \c lottu01 rax
You are now connected to database "lottu01" as user "rax".
lottu01=> create table test_tsp(id int) tablespace tsp01;
CREATE TABLE
Syntax:

ALTER DATABASE name SET TABLESPACE new_tablespace
以数据库lottu01为例:

ALTER DATABASE lottu01 SET TABLESPACE tsp01;
lottu01=> \c lottu01 lottu
You are now connected to database "lottu01" as user "lottu".
注意1:执行该操作;不能连着对应数据库操作

lottu01=# ALTER DATABASE lottu01 SET TABLESPACE tsp01;
ERROR: cannot change the tablespace of the currently open database
lottu01=# \c postgres postgres
You are now connected to database "postgres" as user "postgres".
注意2:执行该操作;对应的数据库不能存在表或者索引已经指定默认的表空间

postgres=# ALTER DATABASE lottu01 SET TABLESPACE tsp01;
ERROR: some relations of database "lottu01" are already in tablespace "tsp01"
HINT: You must move them back to the database's default tablespace before using this command.
postgres=# \c lottu01
You are now connected to database "lottu01" as user "postgres".
lottu01=# drop table test_tsp ;
DROP TABLE
lottu01=# create table test_tsp(id int);
CREATE TABLE
lottu01=# \c postgres postgres
You are now connected to database "postgres" as user "postgres".
注意3:执行该操作;必须是没有人连着对应的数据库

postgres=# ALTER DATABASE lottu01 SET TABLESPACE tsp01;
ERROR: database "lottu01" is being accessed by other users
DETAIL: There is 1 other session using the database.
postgres=# ALTER DATABASE lottu01 SET TABLESPACE tsp01;
ALTER DATABASE
查看数据库默认表空间

lottu01=# select d.datname,p.spcname from pg_database d, pg_tablespace p where d.datname='lottu01' and p.oid = d.dattablespace;
datname | spcname
---------+---------
lottu01 | tsp01
(1 row)
我们知道表空间pg_default是用来存储系统目录对象、用户表、用户表index、和临时表、临时表index、内部临时表的默认空间。若没指定默认表空间;表就所属的表空间就是pg_default。"当然也可以通过参数设置"。而不是数据库默认的表空间。这个时候我们可以将表移到默认的表空间
Syntax:

ALTER TABLE name SET TABLESPACE new_tablespace
将表从一个表空间移到另一个表空间

lottu01=# create table test_tsp03(id int) tablespace tp_lottu;
CREATE TABLE
lottu01=# alter table test_tsp03 set tablespace tsp01;
ALTER TABLE
注意:该操作时会锁表。

PostgreSQL的临时表空间,通过参数temp_tablespaces进行配置,PostgreSQL允许用户配置多个临时表空间。配置多个临时表空间时,使用逗号隔开。如果没有配置temp_tablespaces 参数,临时表空间对应的是默认的表空间pg_default。PostgreSQL的临时表空间用来存储临时表或临时表的索引,以及执行SQL时可能产生的临时文件例如排序,聚合,哈希等。为了提高性能,一般建议将临时表空间放在SSD或者IOPS,以及吞吐量较高的分区中。

$ mkdir -p /data/pg_data/temp_tsp
$ chown -R postgres:postgres /data/pg_data/temp_tsp
postgres=# CREATE TABLESPACE temp01 LOCATION '/data/pg_data/temp_tsp';
CREATE TABLESPACE
postgres=# show temp_tablespaces ;
temp_tablespaces

(1 row)
设置临时表空间

会话级生效
postgres=# set temp_tablespaces = 'temp01';
SET
永久生效
修改参数文件postgresql.conf
执行pg_ctl reload
[postgres@Postgres201 data]$ grep "temp_tablespace" postgresql.conf
temp_tablespaces = 'temp01' # a list of tablespace names, '' uses
查看临时表空间

postgres=# show temp_tablespaces ;
temp_tablespaces

temp01
(1 row)

http://www.jsqmd.com/news/346364/

相关文章:

  • 2026澳州10日精华行程:城市+自然风光探索与机票套票预订攻略 - 资讯焦点
  • 制造业智能化转型:AI原生企业为何比传统数字化更有效?
  • 2026年精密钢管厂家权威推荐:珩磨管/冷拔精密管/厚壁无缝管/碳钢无缝管源头厂家精选 - 品牌推荐官
  • 引领产业标杆的氯化胆碱生产商:解码济南亚西亚药业有限公司的卓越之路 - 资讯焦点
  • 中国商业卫星产业分析:产业链协同与数字化转型
  • 专注匠心,引领全球:济南亚西亚药业有限公司的氯化胆碱制造实力解析 - 资讯焦点
  • 2026行星式重力搅拌机优质厂家推荐榜 - 资讯焦点
  • 【毕业设计】基于python+CS架构的医院财务管理系统(源码+文档+远程调试,全bao定制等)
  • 五家股票配资公司排行榜:正规安全平台真实对比 - 资讯焦点
  • Hospital
  • LangChain 1.0 Agent开发:从创建到部署的完整指南
  • 结论
  • LangChain 1.0 工具系统:从内置工具到自定义工具开发
  • 2026美国旅游全流程攻略:行程案例与机票预订指南 - 资讯焦点
  • 2026年早强剂外加剂供应商推荐,帮助您快速找到合适的批发商 - 睿易优选
  • 2026匠心制造:探秘高品质酒石酸氢胆碱生产商济南亚西亚药业有限公司 - 资讯焦点
  • LangChain 1.0 记忆系统:会话管理与长期记忆存储
  • 基于Java和Html的在线考试管理系统开题报告
  • Antigravity 使用 LLDB 调试 Qt6 MinGW
  • Java做人工智能:JBoltAI框架多模态与OCR技术解度
  • 在线教育互动课堂开发实战|从技术选型到高互动体验打造
  • 广州雅思封闭营哪家环境好、管理严?2026寒假机构避坑指南 - 讯息观点
  • 十大正规股票配资平台靠谱吗:从实盘交易与口碑看 - 资讯焦点
  • 静态住宅ISP代理:企业如何选择住宅代理IP?2026深度解析指南
  • 18-iptables防火墙
  • 狂飙的 165k Star:OpenClaw 开启个人 AGI 时代,让 AI 真正“接管”你的电脑
  • 长沙本地生活代运营榜:三十六行长沙分公司 9.99 分领跑 - 野榜数据排行
  • 豆瓣电影数据分析可视化系统 基于Python Flask+MySQL实现多维度电影数据洞察
  • 同程国际航班变更通知体系深度解析:技术、渠道与服务的全流程保障 - 资讯焦点
  • 用Python来学微积分23-微分中值定理 - 详解