根目录介绍

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名如其意,存放的文件用于存储全局的系统表信息和全局控制信息。

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.

base目录介绍

base目录用于存放数据库的所有实体文件。例如,我们创建的第一个库testdb的OID为16384,那么在data/base下就会产生一个名为16384的目录,用于存储testdb的数据文件。

testdb=# select oid,datname from pg_database;
oid | datname
-------+-----------
12407 | postgres
16384 | testdb
1 | template1
12406 | template0
(4 rows)

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
  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的数据文件
    testdb=# select oid,relfilenode,relname from pg_class where relname='tab1';
oid | relfilenode | relname
-------+-------------+---------
16385 | 16385 | tab1
(1 row)
  1. 空闲空间映射表
    名字以_fsm结尾的文件是数据文件对应的FSM(free space map)文件,用map方式来标识哪些block是空闲的。用一个Byte而不是bit来标识一个block。对于一个有N个字节的block,它在_fsm文件中第blknum个字节中记录的值是(31+N)/32。通过这种方式标识一个block空闲字节数。FSM中不是简单的数组,而是一个三层的树形结构。FSM文件是在需要用到它时才自动产生的。
  2. 可见性映射表文件
    名字以_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位于库级目录的里,但也包含全局系统表信息,因此研发或运维人员在改动全局系统表信息时需要注意。

表空间目录介绍

testdb=# select oid,* from pg_tablespace;
oid | spcname | spcowner | spcacl | spcoptions
-------+------------+----------+--------+------------
1663 | pg_default | 10 | |
1664 | pg_global | 10 | |
49162 | dbspace | 10 | |
(3 rows)

每一个Oid都在data/pg_tblspc下对应一个名为Oid的软链接文件,指向真正的space目录。

 tree ../data/pg_tblspc/
../data/pg_tblspc/
└── 49162 -> /home/postgres/postgresql-9.6.6/postgres/data/dbspace

在space目录是如何组织的呢?

testdb=# create table tab3(a int) tablespace dbspace;
CREATE TABLE testdb=# select oid,relname,relfilenode from pg_class where relname='tab3';
oid | relname | relfilenode
-------+---------+-------------
57351 | tab3 | 57351
(1 row) tree ../data/pg_tblspc/49162
../data/pg_tblspc/49162
└── PG_9.6_201608131
└── 16384
└── 57351

作者:leapking
链接:https://www.jianshu.com/p/cd8c5b988e52
来源:简书

PostgreSQL 数据目录结构的更多相关文章

  1. Postgresql 导出表结构信息

    项目用了Postgresql 数据库,项目组要出表结构的文档,手写太麻烦,想用slq脚本导出一份.查了一番资料,似乎没有多好的方法.dump方式导出的脚本太乱,没法直接写在word文档里.只能自己写s ...

  2. PostgreSQL内存结构图示

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 作者:高健@博客园 luckyjackgao@ ...

  3. PostgreSQL - 查询表结构和索引信息

    前言 PostgreSQL的表一般都是建立在public这个schema下的,假如现在有个数据表t_student,可以用以下几种方式来查询表结构和索引信息. 使用\d元命令查看表字段信息和索引信息 ...

  4. postgresql源代码结构

    转载学习: 德哥培训! 源码下载: https://www.postgresql.org/ftp/source/ 1.postgressql源码目录结构 2.src目录结构

  5. PostgreSQL数据库结构

    PG数据存储结构分为:逻辑结构和物理存储. 一.逻辑存储结构是:内部的组织和管理数据的方式[逻辑存储结构适用于不同的操作系统和硬件平台] 二.物理存储结构是:操作系统中组织和管理数据的方式. 1.逻辑 ...

  6. 1、mysql数据库的数据目录结构

    查看mysql的主要目录结构 通过命名查看mysql的目录结构:find / -name mysql 1.1数据库文件的存放路径 MySQL数据库文件的存放路径:/var/lib/mysql/ 1.2 ...

  7. PostgreSQL 进程结构

    本文主要讲述了PG的几个主要进程,以及PG的核心架构.进程和体系结构详见下图: 从上面的体系结构图可以看出来,PG使用经典的C/S架构,进程架构.在服务器端有主进程.服务进程.子进程.共享内存以及文件 ...

  8. 查询postgresql表结构和索引

    通过系统数据字典查询表结构 selectcol.table_schema,col.table_name,col.ordinal_position,col.column_name,col.data_ty ...

  9. PostgreSql 查询表结构和说明

    select (select relname from pg_class where oid=a.attrelid) relname , () as comment from pg_class whe ...

随机推荐

  1. Python-17-反射

    一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...

  2. svn钩子(hooks)

    目录 钩子脚本的具体写法就是操作系统中shell脚本程序的写法,请根据自己SVN所在的操作系统和shell程序进行相应的写作 所谓钩子就是与一些版本库事件触发的程序,例如新修订版本的创建,或是未版本化 ...

  3. PHP 中 include 和 require 的区别详解

    require() 语句的性能与 include() 相类似,都是包括并运行指定文件.除了处理失败的方式不同之外.require 在出错时产生 E_COMPILE_ERROR 级别的错误,终止脚本运行 ...

  4. SQL高级教程

    一.top子句 top子句用于规定要返回的记录的数目 并非所有数据库系统都支持top子句 # sqlserver SELECT TOP number|percent column_name(s) FR ...

  5. 手动编译ts的经过

    动机 以前写ts或者es6,都是用在脚手架搭建的项目中,比如vue和react,当时当我识图写一个ts的demo的,我还要创建一个完整的vue或者react项目?明显不合适,那就要研究一下如何手动搭建 ...

  6. Codeforces Round #421 (Div. 1) (BC)

    1. 819B Mister B and PR Shifts 大意: 给定排列$p$, 定义排列$p$的特征值为$\sum |p_i-i|$, 可以循环右移任意位, 求最小特征值和对应移动次数. 右移 ...

  7. Luogu5405 CTS2019氪金手游(容斥原理+树形dp)

    考虑外向树怎么做.显然设f[i][j]为i子树中出现权值和为j的合法方案的概率,转移做树形背包即可. 如果树上只有一条反向边,显然可以先不考虑该边计算概率,再减去将整棵树看做外向树的概率.于是考虑容斥 ...

  8. Dockerfile编写,以及设置一个自启动脚本

    FROM:指定基础镜像,必须为第一个命令 MAINTAINER: 维护者信息 RUN:构建镜像时执行的命令 ADD:将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问 ...

  9. SQL Server的非聚集索引中会存储NULL吗?

    原文:SQL Server的非聚集索引中会存储NULL吗? SQL Server的非聚集索引中会存储NULL吗? 这是个很有意思的问题,下面通过如下的代码,来说明,到底会不会存储NULL. --1.建 ...

  10. interface Part4(接口中的多态)

    使用接口实现多态 需要满足以下两个条件. 定义接口并使用类实现了接口中的成员. 创建接口的实例指向不同的实现类对象. 假设接口名称为 ITest,分别定义两个实现类来实现接口的成员,示例代码如下. i ...