PostgreSQL: What’s the location of pg_default tablespace ?

pg_default 为 PostgreSQL 的默认表空间,也可以理解成系统表空间,也许有人疑问这个表空间物理的位置在哪里?在不同操作系统上 PostgreSQL 的安装路径可能不同,但在 Linux 环境下,pg_default 表空间对应的物理位置为 $PGDATA/base 目录下。

查看 PostgreSQL 的表空间

1
2
3
4
5
6
7
8
9
10
11
[postgres@redhat6 ~]$ psql  
psql (9.2beta1)
Type "help" for help.

postgres=# /db
List of tablespaces
Name | Owner | Location
------------+----------+------------------------------------------
pg_default | postgres |
pg_global | postgres |
tbs_francs | postgres | /database/1922/pgdata1/pg_tbs/tbs_francs

备注:上面显示了三个表空间,其中 tbs_francs 为后来创建的表空间,另外两个为系统表空间。

查看表空间的 oid

1
2
3
4
5
6
7
postgres=# select oid,spcname from pg_tablespace;  
oid | spcname
-------+------------
1663 | pg_default
1664 | pg_global
16385 | tbs_francs
(3 rows)

查看表空间目录

查看 tbs_francs 表空间目录,如下:

1
2
3
4
5
postgres=# select pg_tablespace_location(16385) ;  
pg_tablespace_location
------------------------------------------
/database/1922/pgdata1/pg_tbs/tbs_francs
(1 row)

查看表空间 pg__default 的目录

1
2
3
4
5
postgres=# select pg_tablespace_location(1663) ;  
pg_tablespace_location
------------------------

(1 row)

备注:系统默认表空间 pg_default 查出来的物理位置为空。

查看数据库的 oid

1
2
3
4
5
6
7
8
postgres=# select oid,datname from pg_database;  
oid | datname
-------+-----------
1 | template1
12865 | template0
12870 | postgres
16386 | francs
(4 rows)

操作系统对应的目录

1
2
3
4
5
6
7
8
9
10
[postgres@redhat6 base]$ cd $PGDATA/base  

[postgres@redhat6 base]$ pwd
/opt/pgdata9.2/pg_root/base

[postgres@redhat6 base]$ ll
total 20K
drwx------. 2 postgres postgres 12K May 31 03:07 1
drwx------. 2 postgres postgres 4.0K May 17 14:37 12865
drwx------. 2 postgres postgres 4.0K Jul 7 04:08 12870

备注:在 $PGDATA/base 目录下有三个子目录,分别为数据库 template1,template0,postgres 的数据库对像存储目录。

创建测试表

在 francs 库中创建测试表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[postgres@redhat6 ~]$ psql  
psql (9.2beta1)
Type "help" for help.

postgres=# grant all on tablespace pg_default to francs;
GRANT
postgres=# /c francs francs
You are now connected to database "francs" as user "francs".

francs=> show default_tablespace;
default_tablespace
--------------------

(1 row)
francs=> set default_tablespace='pg_default';
SET

francs=> show default_tablespace;
default_tablespace
--------------------
pg_default
(1 row)

francs=> create table test_default (id integer);
CREATE TABLE

francs=> insert into test_default values (1),(2),(3);
INSERT 0 3

再次查看系统目录

1
2
3
4
5
6
7
8
9
10
[postgres@redhat6 base]$ cd $PGDATA/base  
[postgres@redhat6 base]$ pwd
/opt/pgdata9.2/pg_root/base

[postgres@redhat6 base]$ ll
total 24K
drwx------. 2 postgres postgres 12K May 31 03:07 1
drwx------. 2 postgres postgres 4.0K May 17 14:37 12865
drwx------. 2 postgres postgres 4.0K Jul 7 04:08 12870
drwx------. 2 postgres postgres 4.0K Jul 7 05:03 16386

备注: $PGDATA/base 下正好多了目录 16386。

接着往下看

1
2
3
4
5
6
7
8
francs=> select oid,relname from pg_class where relname='test_default';  
oid | relname
-------+--------------
24706 | test_default
(1 row)
[postgres@redhat6 base]$ ll 16386
total 8.0K
-rw-------. 1 postgres postgres 8.0K Jul 7 05:03 24706

备注:已经很明白了,不多解释。

原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/database/237882.html

(0)
上一篇 2022年1月29日 22:30
下一篇 2022年1月29日 22:30

相关推荐

发表回复

登录后才能评论