问题描述
如何查看 PostgreSQL 库表对应的物理数据文件?
问题分析
在默认表空间的 base 目录下有很多子目录,这些子目录的名称与相应数据库的 oid 相同。通过 SQL 查询 系统表 pg_class 中的 oid 字段 和 系统表 pg_class 的 relfilenode 字段可以分别获取对应的数据物文件目录。
解决方案
- 查看数据库的文件路径
登陆数据库
$ psql
psql (12.11)
Type "help" for help.
# select oid, datname from pg_database;
oid | datname
-------+---------------
14187 | postgres
16384 | tmp_database1
1 | template1
14186 | template0
16651 | mytestdb01
16817 | charp6
16650 | testdb01
17086 | db01
(8 rows)
base 目录下查看文件
$ ll
total 88
drwx------ 2 postgres postgres 12288 May 18 00:37 1
drwx------ 2 postgres postgres 12288 Jun 8 19:38 14186
drwx------ 2 postgres postgres 12288 Jun 13 14:46 14187
drwx------ 2 postgres postgres 12288 May 24 20:55 16384
drwx------ 2 postgres postgres 12288 May 27 16:45 16650
drwx------ 2 postgres postgres 12288 May 24 20:56 16651
drwx------ 2 postgres postgres 12288 May 24 20:57 16817
drwx------ 2 postgres postgres 4096 Jun 8 11:35 pgsql_tmp
通过上面的查询结果可以看出,数据库 oid 与 base 目录下的文件结构是对应的,对应的子目录下放着对应的数据库的表、索引等数据文件。
- 查看表的对应数据文件路径,一张表的数据文件的路径为
<默认表空间的目录>/<oid>/<relfilenode>[.顺序号]
。查询语句如下:
mytestdb01=# select relnamespace, relname, relfilenode from pg_class where relname='test01';
relnamespace | relname | relfilenode
--------------+---------+-------------
2200 | test01 | 16726
(1 row)
# 确认数据目录的文件存在
ls /data/pgdata/base/16651/16726
可以看出这个表的 relfilenode 为 "16726", 对应的这张表的数据文件为 $PGDATA/base/16651/16726
(16651 是数据库 mytestdb01 的 oid)
参考文档
[1] https://www.postgresql.org/docs/12/catalog-pg-class.html [2] https://www.postgresql.org/docs/12/catalog-pg-database.html 如果您有其他问题,欢迎您联系火山引擎技术支持服务