学习目标
学习表空间与数据库对象的关系。
在musicdb数据库中创建的所有的表,没有指定表空间的名字,因此都创建在数据库默认的表空间music_tbs中,当我们在musicdb数据库中创建表warehouse_t1的时候,明确指定在表空间ds_location1中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.
课后作业
1.创建表空间newtbs1、 ds_location1,查看表空间
---连接数据库,准备测试环境
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;
CREATE DATABASE
omm=# CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user1 SYSADMIN;
ALTER ROLE
--执行下面的命令,查看当前表空间:
omm=# \db
List of tablespaces
Name | Owner | Location
------------+-------+---------------------
music_tbs | omm | tablespace/test_ts1
pg_default | omm |
pg_global | omm |
(3 rows)
--创建一个新的名为ds_location1的表空间:
omm=# CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/tablespace_1';
omm=# CREATE TABLESPACE
--创建一个新的名为newtbs1的表空间:
omm=# CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/testspace_1';
omm=# CREATE TABLESPACE
2.创建一个数据库newdb1,默认表空间为newtbs1
omm=# CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1;
CREATE DATABASE
3.创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
omm=# CREATE USER user5 IDENTIFIED BY 'kunpeng@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user5 SYSADMIN;
ALTER ROLE
---切换用户user5访问新建数据库newdb1
omm=# \c newdb1 user5
Password for user user5:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "user5".
newdb1=>
newdb1=> create table new1 (col1 char(10)) tablespace ds_location1;
CREATE TABLE
newdb1=> insert into new1 values('Hello new1');
INSERT 0 1
---查看newdb1数据库目前有哪些表:
newdb1=> select table_catalog, table_schema, table_name, table_type
newdb1-> from information_schema.tables
newdb1-> where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
table_catalog | table_schema | table_name | table_type
---------------+-----------------+------------+------------
newdb1 | db4ai | snapshot | BASE TABLE
newdb1 | dbe_pldeveloper | gs_errors | BASE TABLE
newdb1 | dbe_pldeveloper | gs_source | BASE TABLE
newdb1 | public | new1 | BASE TABLE
(4 rows)
newdb1=>
4.查看表所在的表空间
---系统表在默认表空间,非系统表在指定的表空间中(否则在默认表空间)
--建表new1指定表空间ds_location1,查看表new1所在的表空间:
newdb1=> select * from pg_tables where tablename = 'new1';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator |
created | last_ddl_time
------------+-----------+------------+--------------+------------+----------+-------------+--------------+----------
---------------------+-------------------------------
public | new1 | user5 | ds_location1 | f | f | f | user5 | 2022-12-0
1 12:27:07.990365+08 | 2022-12-01 12:27:07.990365+08
(1 row)
--创建表new2未指定表空间,则在默认表空间(不显示默认表空间名),查看表new2所在的表空间:
newdb1=> create table new2 (col1 char(10));
CREATE TABLE
newdb1=> select * from pg_tables where tablename = 'new2';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator |
created | last_ddl_time
------------+-----------+------------+------------+------------+----------+-------------+--------------+------------
-------------------+-------------------------------
public | new2 | user5 | | f | f | f | user5 | 2022-12-01
12:35:47.270803+08 | 2022-12-01 12:35:47.270803+08
---查看opengauss默认表空间
newdb1=> select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid;
datname | dattablespace | spcname
-----------+---------------+------------
template1 | 1663 | pg_default
omm | 1663 | pg_default
musicdb | 16389 | music_tbs
template0 | 1663 | pg_default
newdb1 | 16402 | newtbs1
postgres | 1663 | pg_default
(6 rows)
(1 row)
5.查看表空间newtbs1、 ds_location1上的对象
---查询数据库的默认表空间上的对象
newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner
newdb1-> from pg_class a
newdb1-> where a.relkind in ('r', 'i')
newdb1-> and reltablespace='0'
newdb1-> order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
------------------------------------------------+---------+----------+----------------+---------------+----------
pg_attribute | r | 184 | 1472 kB | 0 | 10
pg_proc | r | 140 | 1120 kB | 0 | 10
pg_depend | r | 59 | 472 kB | 0 | 10
pg_class | r | 52 | 416 kB | 0 | 10
pg_attribute_relid_attnam_index | i | 44 | 352 kB | 0 | 10
pg_proc_proname_all_args_nsp_index | i | 41 | 328 kB | 0 | 10
pg_proc_proname_args_nsp_new_index | i | 39 | 312 kB | 0 | 10
pg_proc_proname_args_nsp_index | i | 39 | 312 kB | 0 | 10
pg_rewrite | r | 37 | 296 kB | 0 | 10
pg_depend_depender_index | i | 34 | 272 kB | 0 | 10
pg_depend_reference_index | i | 34 | 272 kB | 0 | 10
pg_description | r | 31 | 248 kB | 0 | 10
pg_attribute_relid_attnum_index | i | 30 | 240 kB | 0 | 10 pg_amop | r | 9 | 72 kB | 0 | 10
--More--
pg_type | r | 20 | 160 kB | 0 | 10
pg_statistic | r | 19 | 152 kB | 0 | 10
pg_description_o_c_o_index | i | 16 | 128 kB | 0 | 10
pg_operator | r | 15 | 120 kB | 0 | 10
pg_class_relname_nsp_index | i | 14 | 112 kB | 0 | 10
pg_proc_oid_index | i | 13 | 104 kB | 0 | 10
pg_class_tblspc_relfilenode_index | i | 10 | 80 kB | 0 | 10
pg_class_oid_index | i | 7 | 56 kB | 0 | 10
sql_features | r | 7 | 56 kB | 0 | 10
pg_type_typname_nsp_index | i | 7 | 56 kB | 0 | 10
pg_index | r | 7 | 56 kB | 0 | 10
pg_amop_fam_strat_index | i | 6 | 48 kB | 0 | 10
pg_operator_oprname_l_r_n_index | i | 6 | 48 kB | 0 | 10
pg_amop_opr_fam_index | i | 6 | 48 kB | 0 | 10
pg_amop_oid_index | i | 5 | 40 kB | 0 | 10
pg_operator_oid_index | i | 5 | 40 kB | 0 | 10
pg_type_oid_index | i | 5 | 40 kB | 0 | 10
pg_opclass_am_name_nsp_index | i | 4 | 32 kB | 0 | 10
pg_statistic_relid_kind_att_inh_index | i | 4 | 32 kB | 0 | 10
pg_ts_config_map_index | i | 4 | 32 kB | 0 | 10 pg_cast_oid_index | i | 4 | 32 kB | 0 | 10
pg_amproc_oid_index | i | 4 | 32 kB | 0 | 10
pg_toast_2618_index | i | 4 | 32 kB | 0 | 10
pg_rewrite_rel_rulename_index | i | 4 | 32 kB | 0 | 10
pg_amproc_fam_proc_index | i | 4 | 32 kB | 0 | 10
pg_amproc | r | 4 | 32 kB | 0 | 10
pg_opclass | r | 4 | 32 kB | 0 | 10
pg_cast | r | 3 | 24 kB | 0 | 10
--More-- pg_cast_source_target_index | i | 4 | 32 kB | 0 | 10
pg_conversion | r | 3 | 24 kB | 0 | 10
---查询表空间ds_location1上的对像
newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner
newdb1-> newdb1-> from pg_class a, pg_tablespace tb
where a.relkind in ('r', 'i')
newdb1-> and a.reltablespace=tb.oid
newdb1-> and tb.spcname='ds_location1'
newdb1-> order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
new1 | r | 0 | 8192 bytes | 16395 | 16404
(1 row)
newdb1=>
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




