Skip to content

pg数据库data目录

pg数据库和mysql数据库的安装目录结构很类似,程序和数据是分开的,在启动时候可以自行指定不同的数据目录来做到启动不同的数据库

释pg数据目录下,每个文件夹以及文件名的作用

目录名作用
base该目录包含了数据库中各个数据库,每个数据库都是由一个文件夹组成,文件名是该数据库的oid,这个可以通过数据字典pg_database来查看对应的数据库名(select oid,datname from pg_database)。
pg_xlog/pg_wal该目录包含wal(预写)日志。注意在10版本后,该目录重命名为"pg_wal"。
global该目录包含集群范围的各个表和相关视图。 ( pg_database、 pg_tablespace )
pg_clog该目录包含事务提交状态数据。
pg_multixact该目录包含多事务状态数据(等待锁定的并发事务)
pg_notify该目录包含LISTEN/NOTIFY状态数据。存储通知信息,用于在事务提交时发送通知
pg_serial该目录包含了已经提交的序列化事务的有关信息。
pg_snapshots该目录包含导出的快照,存储快照文件,用于逻辑复制的初始同步
pg_stat_tmp该目录包含统计子系统的临时文件。
pg_subtrans该目录包含子事务状态数据。
pg_tblspc该目录包含表空间的符号链接。
pg_twophase该目录包含预备事务的状态文件。
pg_commit_ts该目录包含已提交事务的时间。
pg_dynshmem该目录动态共享内存目录,用于存储动态分配的共享内存块
pg_logical该目录包含逻辑解码的状态数据。
pg_replslot该目录包含复制槽数据,用于物理复制和逻辑复制
pg_stat该目录包含统计子系统的永久文件,存储统计信息,用于数据库的性能监控

文件名作用
pg_hba.conf数据库访问控制文件,一般新安装的数据库都需要进行配置,在流复制中也需要配置。
pg_ident.conf将数据库用户映射到本地用户的一种认证方式,使用比较少,在特殊场景下需要配置(控制哪一个本地用户可以连接到哪一个数据库)。
postgresql.conf/postgresql.auto.conf主要配置文件,在数据库安装完后,需要手动更改里面的监听地址,否则默认只能本地连接。
PG_VERSION包含版本信息。
postmaster.pid启动后pg主进程ID

1.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)
data
├── global                # under global, all the filenode is hard-code(select oid,relname,relfilenode from pg_class where relfilenode=0 order by oid;)
│   ├── 1136              # pg_pltemplate
│   ├── 1137              # pg_pltemplate_name_index
│   ├── 1213              # pg_tablespace
│   ├── 1214              # pg_shdepend
│   ├── 1232              # pg_shdepend_depender_index
│   ├── 1233              # pg_shdepend_reference_index
│   ├── 1260              # pg_authid
│   ├── 1261              # pg_auth_members
│   ├── 1262              # pg_database
│   ├── 2396              # pg_shdescription
│   ├── 2397              # pg_shdescription_o_c_index
│   ├── 2671              # pg_database_datname_index
│   ├── 2672              # pg_database_oid_index
│   ├── 2676              # pg_authid_rolname_index
│   ├── 2677              # pg_authid_oid_index
│   ├── 2694              # pg_auth_members_role_member_index
│   ├── 2695              # pg_auth_members_member_role_index
│   ├── 2697              # pg_tablespace_oid_index
│   ├── 2698              # pg_tablespace_spcname_index
│   ├── 2846              # pg_toast_2396
│   ├── 2847              # pg_toast_2396_index
│   ├── 2964              # pg_db_role_setting
│   ├── 2965              # pg_db_role_setting_databaseid_rol_index
│   ├── 2966              # pg_toast_2964
│   ├── 2967              # pg_toast_2964_index
│   ├── 3592              # pg_shseclabel
│   ├── 3593              # pg_shseclabel_object_index
│   ├── 4060              # pg_toast_3592x
│   ├── 4061              # pg_toast_3592_index
│   ├── 6000              # pg_replication_origin
│   ├── 6001              # pg_replication_origin_roiident_index
│   ├── 6002              # pg_replication_origin_roname_index
│   ├── pg_control        # global control file, use pgcheck -pc to see it.
│   ├── pg_filenode.map   # system table (oid -> filenode) mapping file, use pgcheck -pm to see it.
│   └── pg_internal.init  # system table cache file, use pgcheck -pr to see it.
data
├── global                # under global, all the filenode is hard-code(select oid,relname,relfilenode from pg_class where relfilenode=0 order by oid;)
│   ├── 1136              # pg_pltemplate
│   ├── 1137              # pg_pltemplate_name_index
│   ├── 1213              # pg_tablespace
│   ├── 1214              # pg_shdepend
│   ├── 1232              # pg_shdepend_depender_index
│   ├── 1233              # pg_shdepend_reference_index
│   ├── 1260              # pg_authid
│   ├── 1261              # pg_auth_members
│   ├── 1262              # pg_database
│   ├── 2396              # pg_shdescription
│   ├── 2397              # pg_shdescription_o_c_index
│   ├── 2671              # pg_database_datname_index
│   ├── 2672              # pg_database_oid_index
│   ├── 2676              # pg_authid_rolname_index
│   ├── 2677              # pg_authid_oid_index
│   ├── 2694              # pg_auth_members_role_member_index
│   ├── 2695              # pg_auth_members_member_role_index
│   ├── 2697              # pg_tablespace_oid_index
│   ├── 2698              # pg_tablespace_spcname_index
│   ├── 2846              # pg_toast_2396
│   ├── 2847              # pg_toast_2396_index
│   ├── 2964              # pg_db_role_setting
│   ├── 2965              # pg_db_role_setting_databaseid_rol_index
│   ├── 2966              # pg_toast_2964
│   ├── 2967              # pg_toast_2964_index
│   ├── 3592              # pg_shseclabel
│   ├── 3593              # pg_shseclabel_object_index
│   ├── 4060              # pg_toast_3592x
│   ├── 4061              # pg_toast_3592_index
│   ├── 6000              # pg_replication_origin
│   ├── 6001              # pg_replication_origin_roiident_index
│   ├── 6002              # pg_replication_origin_roname_index
│   ├── pg_control        # global control file, use pgcheck -pc to see it.
│   ├── pg_filenode.map   # system table (oid -> filenode) mapping file, use pgcheck -pm to see it.
│   └── pg_internal.init  # system table cache file, use pgcheck -pr to see it.

2.base

data
├── base                  # use to store database file(SELECT oid, datname FROM pg_database;)
│   ├── 1                 # template database
│   ├── 12406             # template0 database
│   ├── 12407             # postgres database
│   └── 16384             # testdb, first user database
│   │   ├── 3600
│   │   ├── 3600_fsm
│   │   ├── 3600_vm
│   │   ├── 16385
│   │   ├── pg_filenode.map
│   │   ├── pg_internal.init
│   │   └── PG_VERSION
data
├── base                  # use to store database file(SELECT oid, datname FROM pg_database;)
│   ├── 1                 # template database
│   ├── 12406             # template0 database
│   ├── 12407             # postgres database
│   └── 16384             # testdb, first user database
│   │   ├── 3600
│   │   ├── 3600_fsm
│   │   ├── 3600_vm
│   │   ├── 16385
│   │   ├── pg_filenode.map
│   │   ├── pg_internal.init
│   │   └── PG_VERSION
  1. pg_filenode.map 是pg_class里relfilenode为0的系统表,OID与文件的硬编码映射。
  2. pg_internal.init 是系统表的cache文件,用于加快读取。默认不存在,查询系统表后自动产生。
  3. PG_VERSION 是当前数据库数据格式对应的版本号
  4. 其它文件是需要到pg_class里根据OID查到对应的relfilenode来与文件名匹配的。 例如:tab1的relfilenode是16385,那么16385这个文件就是tab1的数据文件
sql
han=# select oid,relfilenode,relname from pg_class where relname='test';
  oid  | relfilenode | relname 
-------+-------------+---------
 16393 |       16393 | test
(1 row)
han=# select oid,relfilenode,relname from pg_class where relname='test';
  oid  | relfilenode | relname 
-------+-------------+---------
 16393 |       16393 | test
(1 row)

5.空闲空间映射表 名字以_fsm结尾的文件是数据文件对应的FSM(free space map)文件,用map方式来标识哪些block是空闲的。用一个Byte而不是bit来标识一个block。对于一个有N个字节的block,它在_fsm文件中第blknum个字节中记录的值是(31+N)/32。通过这种方式标识一个block空闲字节数。FSM中不是简单的数组,而是一个三层的树形结构。FSM文件是在需要用到它时才自动产生的。

6.可见性映射表文件 名字以_vm结尾的文件是数据文件对应的VM(visibility map)。PostgreSQL中在做多版本并发控制时是通过在元组头上标识“已无效”来实现删除或更新的,最后通过VACUUM功能来清理无效数据回收空闲空间。在做VACUUM时就使用VM开快速查找包含无效元组的block。VM仅是个简单的bitmap,一个bit对应一个block。

💡 说明

系统表分为全局系统表和库级系统表。

全局系统表位于global下,例如:pg_database,pg_tablespace,pg_auth_members这种存储系统级对象的表。

库级系统表位于数据库目录下,例如:pg_type,pg_proc,pg_attribute这种存储库级对象的表。

值得注意的是pg_class位于库级目录的里,但也包含全局系统表信息,因此研发或运维人员在改动全局系统表信息时需要注意

3.表空间

sql
han=# select oid,* from pg_tablespace;
 oid  | oid  |  spcname   | spcowner | spcacl | spcoptions 
------+------+------------+----------+--------+------------
 1663 | 1663 | pg_default |       10 |        | 
 1664 | 1664 | pg_global  |       10 |        | 
(2 rows)
han=# 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目录

sql
han=# create table test1(a int) tablespace dbspace;
CREATE TABLE

han=# select oid,relname,relfilenode from pg_class where relname='test1';
  oid  | relname | relfilenode
-------+---------+-------------
 57351 | tab3    |       57351
(1 row)

#查看
 tree ../data/pg_tblspc/49162
../data/pg_tblspc/49162
└── PG_9.6_201608131
    └── 16384
        └── 57351
han=# create table test1(a int) tablespace dbspace;
CREATE TABLE

han=# select oid,relname,relfilenode from pg_class where relname='test1';
  oid  | relname | relfilenode
-------+---------+-------------
 57351 | tab3    |       57351
(1 row)

#查看
 tree ../data/pg_tblspc/49162
../data/pg_tblspc/49162
└── PG_9.6_201608131
    └── 16384
        └── 57351