PostgreSQL数据目录结构详解:值得收藏
发表时间: 2019-08-09 00:02
我们一般解压PG后可以看到有很多个目录,那么这些目录都代表什么意思呢?
data├── base # use to store database file(SELECT oid, datname FROM pg_database;)├── global # under global, all the filenode is hard-code(select oid,relname,relfilenode from pg_class where relfilenode=0 order by oid;)├── pg_clog # dir of transaction commit log│ └── 0000├── pg_commit_ts├── pg_dynshmem├── pg_hba.conf # client authentication config file├── pg_ident.conf # user ident map file├── pg_logical│ ├── mappings│ └── snapshots├── pg_multixact│ ├── members│ │ └── 0000│ └── offsets│ └── 0000├── pg_notify│ └── 0000├── pg_replslot├── pg_serial├── pg_snapshots # dir of snapshot file├── pg_stat├── pg_stat_tmp # dir of tmp stat file│ ├── db_0.stat│ ├── db_12407.stat│ ├── db_16384.stat│ └── global.stat├── pg_subtrans│ └── 0000├── pg_tblspc├── pg_twophase├── PG_VERSION # version file├── pg_xlog # dir of xlog file│ ├── 000000010000000000000001│ └── archive_status # status info of xlog archive├── postgresql.auto.conf├── postgresql.conf # config file of postmaster progress├── postmaster.opts└── postmaster.pid # pid file of postmaster progress
可以看到有这么多的根目录,下面对每个目录做一下简单介绍。
global名如其意,存放的文件用于存储全局的系统表信息和全局控制信息。
global下有四种文件:
结构如下:
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.
base目录用于存放数据库的所有实体文件。例如,我们创建的第一个库testdb的OID为16384,那么在data/base下就会产生一个名为16384的目录,用于存储testdb的数据文件。
postgres=# select oid,datname from pg_database;--base目录结构data├── base # use to store database file(SELECT oid, datname FROM pg_database;)│ ├── 1 # template1 database│ ├── 13857 # template0 database│ ├── 13858 # postgres database│ └── 16384 # hwbdb, first user database│ │ ├── 3600│ │ ├── 3600_fsm│ │ ├── 3600_vm│ │ ├── 16385│ │ ├── pg_filenode.map│ │ ├── pg_internal.init│ │ └── PG_VERSION
testdb=# select oid,relfilenode,relname from pg_class where relname='tab1'; oid | relfilenode | relname -------+-------------+--------- 16385 | 16385 | tab1 (1 row)
注:系统表分为全局系统表和库级系统表。
postgres=# select oid,* from pg_tablespace;
每一个Oid都在data/pg_tblspc下对应一个名为Oid的软链接文件,指向真正的space目录。
$ tree ../data/pg_tblspc/
在space目录是如何组织的呢?
postgres=# create table t2(a int) tablespace tbs_hwb;CREATE TABLEpostgres=# select oid,relname,relfilenode from pg_class where relname='t2'; oid | relname | relfilenode -------+---------+------------- 16444 | t2 | 16444(1 row)postgres=# \q[pg@PG bin]$ tree ../../data/pg_tblspc/16437/../../data/pg_tblspc/16437/└── PG_10_201707211 └── 13858 ├── 16439 └── 164442 directories, 2 files
后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注一下~
如果你觉得这篇文章对你有帮助,请小小打赏一下~