在之前的博客,我们了解的PG的相关信息,如下:
PostgreSQL 学习笔记(1) — PG 概述
https://www.cndba.cn/dave/article/116370
PostgreSQL 学习笔记(2) — PG 版本发布策略 和 生命周期说明
https://www.cndba.cn/dave/article/116372
PostgreSQL 学习笔记(3) — PG 单实例安装手册
https://www.cndba.cn/dave/article/116374
PostgreSQL 学习笔记(4) — PG 启动 与 关闭
https://www.cndba.cn/dave/article/116375
PostgreSQL 学习笔记(5) — psql 工具使用说明
https://www.cndba.cn/dave/article/116376
PostgreSQL 学习笔记(6) — PG 参数管理
https://www.cndba.cn/dave/article/116377
PostgreSQL 学习笔记(7) — PG 数据库 基本操作
https://www.cndba.cn/dave/article/116379
PostgreSQL 学习笔记(8) — PG 模式 与 用户 管理
https://www.cndba.cn/dave/article/116380
PostgreSQL 学习笔记(9) — PG 进程和内存结构说明
https://www.cndba.cn/dave/article/116381
本篇我们看下PG的目录结构。
1 目录结构位置
在初始化数据库时我们可以指定实例的目录,如果不指定,默认会在/var/lib/pgsql/14/data 目录下:
/usr/pgsql-14/bin/postgresql-14-setup initdb
initdb -D /pgdata/14/data -W
pg_ctl init -D /pgdata/14/data -o
[dave@www.cndba.cn data]# ps -ef|grep pg
postgres 3559 1 0 Aug09 ? 00:00:04 /usr/pgsql-14/bin/postgres -D /var/lib/pgsql/14/data
root 25634 3323 0 03:24 pts/2 00:00:00 grep --color=auto pg
[dave@www.cndba.cn data]#
我们这里讲的目录就够就是这个数据库的目录:/var/lib/pgsql/14/data。
[dave@www.cndba.cn 14]# pwd
/var/lib/pgsql/14
[dave@www.cndba.cn 14]# tree -L 3
.
├── backups
├── data
│ ├── base
│ │ ├── 1
│ │ ├── 14485
│ │ ├── 14486
│ │ ├── 16384
│ │ └── 16708
│ ├── current_logfiles
│ ├── global
│ │ ├── 1213
│ │ ├── 1213_fsm
│ │ ├── 1213_vm
│ │ ├── 1214
│ │ ├── 2396_fsm
│ │ ├── 2396_vm
│ │ ├── 2397
│ │ ├── 6115
│ │ ├── pg_control
│ │ ├── pg_filenode.map
│ │ └── pg_internal.init
│ ├── log
│ │ ├── postgresql-Mon.log
│ │ ├── postgresql-Tue.log
│ │ └── postgresql-Wed.log
│ ├── pg_commit_ts
│ ├── pg_dynshmem
│ ├── pg_hba.conf
│ ├── pg_ident.conf
│ ├── pg_logical
│ │ ├── mappings
│ │ ├── replorigin_checkpoint
│ │ └── snapshots
│ ├── pg_multixact
│ │ ├── members
│ │ └── offsets
│ ├── pg_notify
│ ├── pg_replslot
│ ├── pg_serial
│ ├── pg_snapshots
│ ├── pg_stat
│ ├── pg_stat_tmp
│ │ ├── db_0.stat
│ │ ├── db_14485.stat
│ │ ├── db_14486.stat
│ │ ├── db_16384.stat
│ │ ├── db_16708.stat
│ │ └── global.stat
│ ├── pg_subtrans
│ │ └── 0000
│ ├── pg_tblspc
│ ├── pg_twophase
│ ├── PG_VERSION
│ ├── pg_wal
│ │ ├── 000000010000000000000001
│ │ ├── 000000010000000000000002
│ │ └── archive_status
│ ├── pg_xact
│ │ └── 0000
│ ├── postgresql.auto.conf
│ ├── postgresql.conf
│ ├── postmaster.opts
│ └── postmaster.pid
└── initdb.log
30 directories, 81 files
[dave@www.cndba.cn 14]#
[dave@www.cndba.cn data]# ll
total 68
drwx------. 7 postgres postgres 67 Aug 9 20:47 base
-rw-------. 1 postgres postgres 30 Aug 10 00:00 current_logfiles
drwx------. 2 postgres postgres 4096 Aug 9 20:54 global
drwx------. 2 postgres postgres 84 Aug 10 00:00 log
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_commit_ts
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_dynshmem
-rw-------. 1 postgres postgres 4577 Aug 8 09:47 pg_hba.conf
-rw-------. 1 postgres postgres 1636 Aug 8 09:47 pg_ident.conf
drwx------. 4 postgres postgres 68 Aug 9 21:37 pg_logical
drwx------. 4 postgres postgres 36 Aug 8 09:47 pg_multixact
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_notify
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_replslot
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_serial
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_snapshots
drwx------. 2 postgres postgres 6 Aug 9 05:02 pg_stat
drwx------. 2 postgres postgres 126 Aug 10 03:47 pg_stat_tmp
drwx------. 2 postgres postgres 18 Aug 8 09:47 pg_subtrans
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_tblspc
drwx------. 2 postgres postgres 6 Aug 8 09:47 pg_twophase
-rw-------. 1 postgres postgres 3 Aug 8 09:47 PG_VERSION
drwx------. 3 postgres postgres 92 Aug 8 23:25 pg_wal
drwx------. 2 postgres postgres 18 Aug 8 09:47 pg_xact
-rw-------. 1 postgres postgres 113 Aug 9 04:40 postgresql.auto.conf
-rw-------. 1 postgres postgres 28770 Aug 8 09:47 postgresql.conf
-rw-------. 1 postgres postgres 57 Aug 9 05:02 postmaster.opts
-rw-------. 1 postgres postgres 103 Aug 9 05:02 postmaster.pid
[dave@www.cndba.cn data]# pwd
/var/lib/pgsql/14/data
[dave@www.cndba.cn data]#
相关目录和文件说明如下:
- postgresql.conf∶数据库实例的主配置文件,基本上所有的配置参数都在此文件中。
- pg_hba.conf∶认证配置文件,配置了允许哪些IP的主机访问数据库,认证的方法是什么等信息。
- pg_ident.conf∶”ident”认证方式的用户映射文件。
- base∶默认表空间的目录。
- global∶一些共享系统表的目录。
- pg_clog∶commit log的目录。
- pe_log∶系统日志目录,在查询一些系统错误时就可查看此目录下的日志文件。
- pg_stat_tmp∶统计信息的存储目录。
- pg_tblsp∶存储了指向各个用户自建表空间实际目录的链接文件。
- pg_twophase∶使用两阶段提交功能时分布式事务的存储目录。
- pg_xlog∶WAL日志的目录。
2 global目录说明
global名如其意,存放的文件用于存储全局的系统表信息和全局控制信息。
global下有四种文件:
1.pg_control:用于存储全局控制信息
2.pg_filenode.map: 用于将当前目录下系统表的OID与具体文件名进行 硬编码 映射(每个用户创建的数据库目录下也有同名文件)。
3.pg_internal.init:用于缓存系统表,加快系统表读取速度(每个用户创建的数据库目录下也有同名文件)。
4.全局系统表文件:数字命名的文件,用于存储系统表的内容。它们在pg_class里的relfilenode都为0,是靠pg_filenode.map将OID与文件硬编码映射。但并不是所有的系统表的relfilenode都为0。
[dave@www.cndba.cn global]# pwd
/var/lib/pgsql/14/data/global
[dave@www.cndba.cn global]# ll -lr
total 580
-rw-------. 1 postgres postgres 23840 Aug 9 20:54 pg_internal.init
-rw-------. 1 postgres postgres 512 Aug 8 09:47 pg_filenode.map
-rw-------. 1 postgres postgres 8192 Aug 9 21:37 pg_control
-rw-------. 1 postgres postgres 8192 Aug 8 09:47 6115
-rw-------. 1 postgres postgres 8192 Aug 8 09:47 6114
-rw-------. 1 postgres postgres 0 Aug 8 09:47 6100
-rw-------. 1 postgres postgres 8192 Aug 8 09:47 6002
-rw-------. 1 postgres postgres 8192 Aug 8 09:47 6001
-rw-------. 1 postgres postgres 0 Aug 8 09:47 6000
-rw-------. 1 postgres postgres 8192 Aug 8 09:47 4186
-rw-------. 1 postgres postgres 0 Aug 8 09:47 4185
-rw-------. 1 postgres postgres 8192 Aug 8 09:47 4184
-rw-------. 1 postgres postgres 0 Aug 8 09:47 4183
……
3 base目录说明
base目录用于存放数据库的所有实体文件。例如,我们创建的库cndba的OID为16384,那么在data/base下就会产生一个名为16384的目录,用于存储cndba的数据文件。
postgres=# select oid,datname from pg_database;
oid | datname
-------+-----------
14486 | postgres
16384 | cndba
1 | template1
14485 | template0
16708 | ustc
(5 rows)
[dave@www.cndba.cn 16384]# pwd
/var/lib/pgsql/14/data/base/16384
[dave@www.cndba.cn 16384]# ll -r
total 16172
-rw-------. 1 postgres postgres 3 Aug 8 14:41 PG_VERSION
-rw-------. 1 postgres postgres 155936 Aug 9 05:03 pg_internal.init
-rw-------. 1 postgres postgres 512 Aug 8 14:41 pg_filenode.map
-rw-------. 1 postgres postgres 8192 Aug 8 14:41 828
-rw-------. 1 postgres postgres 8192 Aug 8 14:41 827
……
-rw-------. 1 postgres postgres 16384 Aug 8 14:41 3766
-rw-------. 1 postgres postgres 8192 Aug 8 14:41 3764_vm
-rw-------. 1 postgres postgres 24576 Aug 8 14:41 3764_fsm
-rw-------. 1 postgres postgres 8192 Aug 8 14:41 3764
……
[dave@www.cndba.cn 16384]# cat PG_VERSION
14
[dave@www.cndba.cn 16384]#
参数说明:
- pg_filenode.map :pg_class里relfilenode为0的系统表,OID与文件的硬编码映射。
- pg_internal.init :系统表的cache文件,用于加快读取。默认不存在,查询系统表后自动产生。
- PG_VERSION :当前数据库数据格式对应的版本号
- 其它文件是需要到pg_class里根据OID查到对应的relfilenode来与文件名匹配的。例如:dave表的relfilenode是16385,那么16385这个文件就是dave表的数据文件:
postgres=# /c cndba
You are now connected to database "cndba" as user "postgres".
cndba=# select oid,relfilenode,relname from pg_class where relname='dave';
oid | relfilenode | relname
-------+-------------+---------
16385 | 16385 | dave
(1 row)
cndba=# select * from dave;
id | url
----+----------------------
1 | https://www.cndba.cn
2 | https://www.cndba.cn
3 | https://www.cndba.cn
(3 rows)
[dave@www.cndba.cn 16384]# cat 16385
HX▒,▒ ȟb▒▒bX▒b ▒b▒b▒ +https://www.cndba.cn▒
+https://www.cndba.cn▒
+https://www.cndba.cn▒ +https://www.cndba.cn▒ +https://www.cndba.cn[dave@www.cndba.cn 16384]# xterm-256color
bash: xterm-256color: command not found...
[dave@www.cndba.cn 16384]#
[dave@www.cndba.cn 16384]#
对于系统表分为:全局系统表和库级系统表。
- 全局系统表:位于global下,例如:pg_database, pg_tablespace,pg_auth_members这种存储系统级对象的表。
- 库级系统表:位于数据库目录下,例如:pg_type,pg_proc,pg_attribute这种存储库级对象的表。
4 表空间目录说明
cndba=# select oid,* from pg_tablespace;
oid | oid | spcname | spcowner | spcacl | spcoptions
------+------+------------+----------+--------+------------
1663 | 1663 | pg_default | 10 | |
1664 | 1664 | pg_global | 10 | |
(2 rows)
对于每个非默认的表空间,oid都在data/pg_tblspc下对应一个名为oid的软链接文件,指向真正的space目录。
cndba=# create tablespace cndba location '/data/postgres/data';
CREATE TABLESPACE
cndba=# select oid,* from pg_tablespace;
oid | oid | spcname | spcowner | spcacl | spcoptions
-------+-------+------------+----------+--------+------------
1663 | 1663 | pg_default | 10 | |
1664 | 1664 | pg_global | 10 | |
16719 | 16719 | cndba | 10 | |
(3 rows)
[dave@www.cndba.cn pg_tblspc]# pwd
/var/lib/pgsql/14/data/pg_tblspc
[dave@www.cndba.cn pg_tblspc]# ll
total 0
lrwxrwxrwx. 1 postgres postgres 19 Aug 10 05:55 16719 -> /data/postgres/data
[dave@www.cndba.cn pg_tblspc]#
cndba=# create table cndba(id int) tablespace cndba;
CREATE TABLE
cndba=# /dt cndba
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | cndba | table | postgres
(1 row)
cndba=#
cndba=# select oid,relfilenode,relname from pg_class where relname='cndba';
oid | relfilenode | relname
-------+-------------+---------
16720 | 16720 | cndba
(1 row)
[dave@www.cndba.cn 16719]# pwd
/var/lib/pgsql/14/data/pg_tblspc/16719
[dave@www.cndba.cn 16719]# tree
.
└── PG_14_202107181
└── 16384
└── 16720
2 directories, 1 file
[dave@www.cndba.cn 16719]#
[dave@www.cndba.cn data]# pwd
/data/postgres/data
[dave@www.cndba.cn data]# tree
.
└── PG_14_202107181
└── 16384
└── 16720
2 directories, 1 file
[dave@www.cndba.cn data]#
版权声明:本文为博主原创文章,未经博主允许不得转载。