PG数据库管理-表空间管理

1 概述

PostgreSQL是一个功能非常强大的、源代码开放的客户/服务器关系型数据库管理系统(RDBMS)。支持丰富的数据类型(如JSON和JSONB类型,数组类型)和自定义类型。PostgreSQL内存页面的默认大小是8kB。

2 PostgreSQL的层级结构


  1.逻辑层次关系
    Database Cluster(instance)--》Database--》Schema--》Objects(Table)-->Tuples
  2.物理层次关系
    Database Cluster --》Tablespaces --》Files --》 Blocks

3 表空间概念与数据库关系

在 PostgreSQL 中,表空间(tablespace)表示数据文件的存放目录,这些数据文件代表了数据库的对象,例如表或索引。当我们访问表时,系统通过它所在的表空间定位到对应数据文件所在的位置。

在PostgreSQL中表空间实际上就是给表或者索引指定一个存储目录。

在Oracle,db2数据库中;一个表空间只属于一个数据库使用;而一个数据库可以拥有多个表空间。属于"一对多"的关系

在PostgreSQL集群中;一个表空间可以让多个数据库使用;而一个数据库可以使用多个表空间。属于"多对多"的关系。

4 表空间作用

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

1 如果初始化集簇所在的分区或者卷用光了空间,而又不能在逻辑上扩展或者做别的什么操作,那么表空间可以被创建在一个不同的分区上,直到系统可以被重新配置。

2 表空间允许管理员根据数据库对象的使用模式来优化性能。例如,一个很频繁使用的索引可以被放在非常快并且非常可靠的磁盘上,如一种非常贵的固态设备。同时,一个很少使用的或者对性能要求不高的存储归档数据的表可以存储在一个便宜但比较慢的磁盘系统上。

5 系统自带表空间


1表空间pg_default是用来存储系统目录对象、用户表、用户表index、和临时表、临时表index、内部临时表的默认空间。对应存储目录$PADATA/base/

2 表空间pg_global用来存放系统字典表;对应存储目录$PADATA/global/

6 表空间查看


postgres=# \db

postgres=# \db+ 详细信息

postgres=# \db
                 List of tablespaces
    Name    |  Owner   |           Location           
------------+----------+------------------------------
 pg_default | postgres | 
 pg_global  | postgres | 
 tbs1       | postgres | /home/postgres/datafile/tbs1
(3 rows)

postgres=# \db+
                                            List of tablespaces
    Name    |  Owner   |           Location           | Access privileges | Options |  Size  | Description 
------------+----------+------------------------------+-------------------+---------+--------+-------------
 pg_default | postgres |                              |                   |         | 24 MB  | 
 pg_global  | postgres |                              |                   |         | 623 kB | 
 tbs1       | postgres | /home/postgres/datafile/tbs1 |                   |         | 16 kB  | 
(3 rows)

postgres=# 

5.4.1  表空间和数据库关系


select oid, datname, datlastsysoid, dattablespace from pg_catalog.pg_database order by 1,2;

postgres=# select oid,* from pg_catalog.pg_tablespace;

5.4.2 表空间和对象的关系
 

5.4.3 查看表的存储路径与大小


查看单表大小

select pg_size_pretty(pg_relation_size('test01'));

postgres=# select pg_size_pretty(pg_relation_size('test01'));
 pg_size_pretty 
----------------
 8192 bytes
(1 row)

postgres=# 

查看所有表的大小

SELECT

    table_name,

    pg_size_pretty(table_size) AS table_size,

    pg_size_pretty(indexes_size) AS indexes_size,

    pg_size_pretty(total_size) AS total_size

FROM (

    SELECT

        table_name,

        pg_table_size(table_name) AS table_size,

        pg_indexes_size(table_name) AS indexes_size,

        pg_total_relation_size(table_name) AS total_size

    FROM (

        SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name

        FROM information_schema.tables

    ) AS all_tables

    ORDER BY total_size DESC

) AS pretty_sizes;

查看表的物理存储路径

select pg_relation_filepath('test01');

7 创建表空间

目录表空间目录必须是一个已有的空目录,并且属于PostgreSQL操作系统用户

[postgres@Redhat7 ~]$ mkdir /pgdb/jsdb

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

atlasdb=# create tablespace jsdb owner postgres location '/pgdb/jsdb';

atlasdb=# create tablespace jkdb owner postgres location '/pgdb/jkdb';

atlasdb=# CREATE TABLE t2(id int) tablespace jsdb;

insert into t2 valuse (1);

atlasdb=# CREATE TABLE t3(id int) tablespace jkdb;

atlasdb=# insert into t2 values (1);

INSERT 0 1

Time: 0.670 ms

atlasdb=# insert into t3 values (1);

INSERT 0 1

Time: 0.602 ms

8 为数据库指定默认表空间


查看数据库默认表空间参照5.4.1

  操作失败不能对修改数据库有连接

atlasdb=# alter database atlasdb set tablespace jsdb;

ERROR:  cannot change the tablespace of the currently open database

Time: 0.239 ms

成功

postgres=# alter database atlasdb set tablespace jsdb;

ERROR:  some relations of database "atlasdb" are already in tablespace "jsdb"

HINT:  You must move them back to the database's default tablespace before using this command.

Time: 101.146 ms

错误:数据库“ATLASDB”的一些关系已经在表空间“JSDB”中

提示:在使用此命令之前,必须将它们移回数据库的默认表空间。

atlasdb=# ALTER TABLE t2 SET TABLESPACE pg_default;

ALTER TABLE

postgres=# alter database atlasdb set tablespace jsdb;

ALTER DATABASE

9 移动对象到新表空间


atlasdb=# ALTER TABLE t2 SET TABLESPACE jsdb;

ALTER TABLE

atlasdb=# ALTER TABLE t3 SET TABLESPACE jsdb;

ALTER TABLE

atlasdb=# ALTER TABLE test1 SET TABLESPACE jsdb; 

ALTER TABLE

10 删除表空间
 

atlasdb=# DROP TABLESPACE jkdb;

DROP TABLESPACE

atlasdb=# DROP TABLESPACE jsdb;

ERROR:  tablespace "jsdb" is not empty


11 表空间和对象的关系如何查询

select a.relname,b.spcname from pg_class a,pg_tablespace b where a.reltablespace=b.oid;

postgres=# select a.relname,b.spcname from pg_class a,pg_tablespace b where a.reltablespace=b.oid;
                 relname                 |  spcname  
-----------------------------------------+-----------
 test01                                  | tbs1
 pg_toast_1260                           | pg_global
 pg_toast_1260_index                     | pg_global
 pg_toast_1262                           | pg_global
 pg_toast_1262_index                     | pg_global
 pg_toast_2964                           | pg_global
 pg_toast_2964_index                     | pg_global
 pg_toast_1136                           | pg_global
 pg_toast_1136_index                     | pg_global
 pg_toast_6000                           | pg_global
 pg_toast_6000_index                     | pg_global
 pg_toast_2396                           | pg_global
 pg_toast_2396_index                     | pg_global
 pg_toast_3592                           | pg_global
 pg_toast_3592_index                     | pg_global
 pg_toast_6100                           | pg_global
 pg_toast_6100_index                     | pg_global
 pg_toast_1213                           | pg_global
 pg_toast_1213_index                     | pg_global
 pg_authid_rolname_index                 | pg_global
 pg_authid_oid_index                     | pg_global
 pg_auth_members_role_member_index       | pg_global
 pg_auth_members_member_role_index       | pg_global
 pg_database_datname_index               | pg_global
 pg_database_oid_index                   | pg_global
 pg_shdescription_o_c_index              | pg_global
 pg_pltemplate_name_index                | pg_global
 pg_shdepend_depender_index              | pg_global
 pg_shdepend_reference_index             | pg_global
 pg_tablespace_oid_index                 | pg_global
 pg_tablespace_spcname_index             | pg_global
 pg_db_role_setting_databaseid_rol_index | pg_global
 pg_shseclabel_object_index              | pg_global
 pg_replication_origin_roiident_index    | pg_global
 pg_replication_origin_roname_index      | pg_global
 pg_subscription_oid_index               | pg_global
 pg_subscription_subname_index           | pg_global
 pg_authid                               | pg_global
 test02                                  | tbs1
 pg_subscription                         | pg_global
 pg_database                             | pg_global
 pg_db_role_setting                      | pg_global
 pg_tablespace                           | pg_global
 pg_pltemplate                           | pg_global
 pg_auth_members                         | pg_global
 pg_shdepend                             | pg_global
 pg_shdescription                        | pg_global
 pg_replication_origin                   | pg_global
 pg_shseclabel                           | pg_global
(49 rows)

postgres=# 

                        
原文链接:https://blog.csdn.net/syjhct/article/details/106590293

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值