陈老师:1415968548 郑老师:2735197625 乐老师:354331153
客服热线:
19941464235/19906632509(微信同号)

客服微信

【PostgreSQL PGCA题目解析40】在PostgreSQL的数据目录结构中,默认表空间的目录是哪个?

作者:炎燚小寶
原创
发布时间:2024-01-11 09:26
浏览量:235

考试科目PGCA-E-090

考试题量:40 道单项选择题、10 道多项选择题(每题 2 分)

通过分数:60%

考试时间:60min

本文为云贝教育刘峰(微信:yunbee_DBA)原创,请尊重知识产权,转发请注明出处,不接受任何抄袭、演绎和未经注明出处的转载。


在PostgresSQL的数据目录结构中,默认表空间的目录是哪个?

A.base

B.global

C.pg_tblsp

D.pg_log


参考答案:A


解析这个题目前,我们先了解下pg中的表空间


一、super用户创建表空间

testdb=# select user;
   user   
----------
 postgres
(1 row)
testdb=# CREATE TABLESPACE fastspace LOCATION '/home/postgres/fastspace';
2023-10-24 10:03:59.323 CST [114668] LOG:  statement: CREATE TABLESPACE fastspace LOCATION '/home/postgres/fastspace';
CREATE TABLESPACE


如果用普通用户创建,则提示:ERROR:  permission denied to create tablespace "fastspace"

testdb=> CREATE TABLESPACE fastspace LOCATION '/home/postgres/fastspace';
ERROR:  permission denied to create tablespace "fastspace"
HINT:  Must be superuser to create a tablespace.



二、在表空间上创建表


1)给用户授权在表空间中创建对象

grant create on tablespace fastspace to test;


查看新建表空间的OID

testdb=# select * from pg_tablespace where spcname='fastspace';
  oid  |  spcname  | spcowner |                spcacl                 | spcoptions 
-------+-----------+----------+---------------------------------------+------------
 24599 | fastspace |       10 | {postgres=C/postgres,test=C/postgres} | 
(1 row)


2)不授权无法创建表

testdb=> create table s1.t2(id int) tablespace fastspace;
ERROR:  permission denied for tablespace fastspace


3)普通用户创建表

testdb=> create table s1.t2(id int) tablespace fastspace;
CREATE TABLE


三、查看对象路径

testdb=> select pg_relation_filepath('s1.t2');
            pg_relation_filepath             
---------------------------------------------
 pg_tblspc/24599/PG_15_202209061/16391/24600


这个路径省略了默认的$PGDATA目录

[postgres@ora19c02 pg_tblspc]$ pwd
/data/pgdata/data/pg_tblspc
[postgres@ora19c02 pg_tblspc]$ ll
total 0
lrwxrwxrwx 1 postgres postgres 21 Oct 18 15:05 16395 -> /home/postgres/tbs_t1
lrwxrwxrwx 1 postgres postgres 24 Oct 24 10:03 24599 -> /home/postgres/fastspace



四、设置默认表空间

SET default_tablespace = fastspace;


创建表

CREATE TABLE s1.foo(i int);


查看对象路径

testdb=> select pg_relation_filepath('s1.foo');


解析

可以通过以下视图查看默认表空间

testdb=# select * from pg_tablespace;
  oid  |  spcname   | spcowner |                spcacl                 | spcoptions 
-------+------------+----------+---------------------------------------+------------
  1663 | pg_default |       10 |                                       | 
  1664 | pg_global  |       10 |                                       | 
 16395 | tbs_t1     |       10 |                                       | 
 24599 | fastspace  |       10 | {postgres=C/postgres,test=C/postgres} | 
(4 rows)


验证默认表空间的情况下,表的路径

testdb=# create table t3(id int);
CREATE TABLE
select pg_relation_filepath('t3');
 pg_relation_filepath 
----------------------
 base/16391/24603
(1 row)

可以看到,默认表空间下的表的存储路径是在/base下


关于另外一个表空间pg_global


pg_global 表空间用于共享系统目录。 pg_default 表空间是 template1 和 template0 数据库的默认表空间(因此,也将是其他数据库的默认表空间,除非被 CREATE DATABASE 中的 TABLESPACE 子句覆盖)。同时它不也允许被用来创建模式对象。

testdb=# SET default_tablespace = pg_global;
SET
testdb=# create table t4(id int);
2023-10-24 10:56:19.765 CST [115229] ERROR:  only shared relations can be placed in pg_global tablespace
2023-10-24 10:56:19.765 CST [115229] STATEMENT:  create table t4(id int);
ERROR:  only shared relations can be placed in pg_global tablespace





PostgreSQL PGCA考试为理论考试,需通过PostgreSQL PGCA-E-090 考试才能拿到“PostgreSQL PGCA证书”。



以下是PostgreSQL官网的考试费,可以添加云贝教育的课程顾问老师微信19941464235或者19906632509,咨询有优惠


PostgreSQL证书如下 合格考生公布:考后不超过 5 个工作日  “开源软件联盟 PostgreSQL 分会”公众号发布  证书寄送:考后统一寄送至培训机构)



云贝教育有专业的PostgreSQL PGCA认证培训课程,想快速通过考试的同学可以扫码联系老师咨询(点击文字转跳至PostgreSQL PGCA纲)