文章目录

  • 前言
  • 集群目录结构
  • 表文件存储形式
  • 结尾

前言

本文是基于postgresql 14的代码进行分析解读,演示是在centos8系统上进行。


数据库中用SQL可以访问的表,在数据库中实际以文件的形式存储,一个或多个文件与之对应。

  • 集群目录结构

postgres数据库,通过 initdb初始化一个数据库集群目录,目录下存放着当前集群的所有数据,在磁盘上以目录和文件的方式来组织。我们下面看一下,集群目录的结构。

./zptest/├── base│   ├── 1│   ├── 4│   └── 5├── global│   ├── 1213│   ├── 1213_fsm│   ├── 1213_vm│   ├── 1214│   ├── 1232│   ├── 1233│   ├── 1260│   ├── 1260_fsm│   ├── 1260_vm│   ├── 1261│   ├── 1261_fsm│   ├── 1261_vm│   ├── 1262│   ├── 1262_fsm│   ├── 1262_vm│   ├── 2396│   ├── 2396_fsm│   ├── 2396_vm│   ├── 2397│   ├── 2671│   ├── 2672│   ├── 2676│   ├── 2677│   ├── 2694│   ├── 2695│   ├── 2697│   ├── 2698│   ├── 2846│   ├── 2847│   ├── 2964│   ├── 2965│   ├── 2966│   ├── 2967│   ├── 3592│   ├── 3593│   ├── 4060│   ├── 4061│   ├── 4175│   ├── 4176│   ├── 4177│   ├── 4178│   ├── 4181│   ├── 4182│   ├── 4183│   ├── 4184│   ├── 4185│   ├── 4186│   ├── 6000│   ├── 6001│   ├── 6002│   ├── 6100│   ├── 6114│   ├── 6115│   ├── pg_control│   └── pg_filenode.map├── 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├── pg_subtrans│   └── 0000├── pg_tblspc├── pg_twophase├── PG_VERSION├── pg_wal│   ├── 000000010000000000000001│   └── archive_status├── pg_xact│   └── 0000├── postgresql.auto.conf└── postgresql.conf

数据库的表文件存储在base目录下。我们有很多database,那么base目录下,那个目录是自己的数据库呢?

每个database都有一个OID,目录以OID来命名。

/** Object ID is a fundamental type in Postgres.*/typedef unsigned int Oid;
postgres=# select oid, datname  from pg_database order by oid;oid |  datname -----+-----------1 | template14 | template05 | postgres(3 rows)

我们当前使用的默认数据库postgres,OID是5,那么路径在base/5/下面,我们来验证一下。

postgres=# select pg_relation_filepath('pg_class');pg_relation_filepath----------------------base/5/1259(1 row)

当前数据库的表pg_class的表文件在base/5/下面。

  • 表文件存储形式

(1) 表文件与表OID的关系:

数据库对象都有一个唯一的OID标识,数据表也不例外,一般情况下,表文件名也和数据库的OID相同,如下:

postgres=# create table test(id integer);CREATE TABLEpostgres=# select oid from pg_class where relname='test';oid -------16384(1 row)postgres=# select pg_relation_filepath('test');pg_relation_filepath----------------------base/5/16384(1 row)

但是这种对应关系也会发生变化,如vaccum full时;所以要找到正确的对应,需要用pg_relation_filepath来查询。

两者如何映射,是由pg_filenode.map文件来维护。

/** The map file is critical data: we have no automatic method for recovering* from loss or corruption of it.  We use a CRC so that we can detect* corruption.  To minimize the risk of failed updates, the map file should* be kept to no more than one standard-size disk sector (ie 512 bytes),* and we use overwrite-in-place rather than playing renaming games.* The struct layout below is designed to occupy exactly 512 bytes, which* might make filesystem updates a bit more efficient.** Entries in the mappings[] array are in no particular order.  We could* speed searching by insisting on OID order, but it really shouldn't be* worth the trouble given the intended size of the mapping sets.*/#define RELMAPPER_FILENAME                "pg_filenode.map"

(2) 表文件大小:

表文件大小,由于各操作系统对于文件大小的限制,postgres将每个表文件限制到了1GB。

当表数据超过1GB时,会创建新的表文件,表文件名由oid.1 oid.2 … 编号,来拆分成多个文件。

/* RELSEG_SIZE is the maximum number of blocks allowed in one disk file. Thus,the maximum size of a single file is RELSEG_SIZE * BLCKSZ; relations biggerthan that are divided into multiple files. RELSEG_SIZE * BLCKSZ must beless than your OS' limit on file size. This is often 2 GB or 4GB in a32-bit operating system, unless you have large file support enabled. Bydefault, we make the limit 1 GB to avoid any possible integer-overflowproblems within the OS. A limit smaller than necessary only means we dividea large relation into more chunks than necessary, so it seems best to errin the direction of a small limit. A power-of-2 value is recommended tosave a few cycles in md.c, but is not absolutely required. ChangingRELSEG_SIZE requires an initdb. */#define RELSEG_SIZE 131072

BLCKSZ * RELSEG_SIZE 来限制每个表文件里的block数量,BLCKSZ 默认为8KB;


结尾

作者邮箱:study@senllang.onaliyun.com
如有错误或者疏漏欢迎指出,互相学习。

注:未经同意,不得转载!

postgresql 表文件介绍相关推荐

  1. mysql数据库熟悉表空间数据文件_MySQL数据文件介绍及存放位置

    MySQL的每个数据库都对应存放在一个与数据库同名的文件夹中,MySQL数据库文件包括MySQL所建数据库文件和MySQL所用存储引擎创建的数据库文件. 1.MySQL创建并管理的数据库文件: .fr ...

  2. PostgreSQL:psql 介绍

    本文分享自天翼云开发者社区<PostgreSQL:psql 介绍>,作者: 周****平 psql 命令是与 PostgreSQL 服务器交互的客户端程序,要登录到数据库服务器,需要使用p ...

  3. PostgreSQL 表空间(TABLESPACE)

    1. 表空间介绍 表空间即PostgreSQL存储数据文件的位置,其中包括数据库对象.如,索引.表等. PostgreSQL使用表空间映射逻辑名称和磁盘物理位置.默认提供了两个表空间: pg_defa ...

  4. PostgreSQL 数据库备份与恢复介绍

    防止数据库数据丢失的最重要的方法就是备份.造成数据丢失可能的原因有很多种,例如服务器的硬件损坏,而有的是人为的原因导致的(例如误删数据),还有的就是应用程序的bug导致数据误删.因此关于数据库的备份与 ...

  5. mysql表文件被删_mysql表物理文件被误删的解决方法

    前言 1.该方法只介绍了如何救回这个表名(数据不恢复) 如果想要恢复原来数据 直接用extundelete把文件恢复后放回去即可 2.并且是适用于平时没有全备的情况下  如果有全备 直接那全备的frm ...

  6. linux服务器都有注册表吗,NT服务器的注册表结构介绍

    NT服务器的注册表结构介绍 HKEY_CLASSES_ROOT:它包括与OLE和文件关联有关的信息.设置这一项的目的是提供和目前的Windows 3.x相兼容. HKEY_CURRENT_USER:它 ...

  7. cab文件介绍及制作方法

    转自:http://bbs.pcpop.com/091030/5945399.html 1. 什么是cab文件 CAB在电脑上是一种压缩文件,微软出品的东西,用WinRAR可以解压缩,在DOS启动盘里 ...

  8. 命令编写注册表文件修改注册表项

    命令编写注册表文件修改注册表项 1.何谓REG文件 REG文件实际上是一种注册表脚本文件,双击REG文件即可将其中的数据导入到注册表中.利用REG文件我们可以直接对注册表进行任何修改操作,它对注册表的 ...

  9. wince中的BSP工程的相关文件介绍

    一.pbcxml分析         每一个BSP都有一个工程文件,比如MyBSP.pbcxml,里面描述了BSP的信息.下面就来介绍一下BSP的pbcxml文件. 文件的大致格式应该是这样的: &l ...

最新文章

  1. 用java编写汉诺塔问题_数据结构与算法之汉诺塔问题(Java递归)
  2. Oracle的FIXED_DATE参数
  3. 入门Python,限时1元!
  4. centos7安装samba服务器
  5. 论文浅尝 | 具有图卷积网络和顺序注意力机制的应用于目标导向的对话系统
  6. 解决火狐https问题 安全连接问题
  7. SAMSUNG的CMOS 图像传感器技术发展路线
  8. 07@Pattern_Note_命令模式
  9. Ubuntu20.04之安装VirtualBox虚拟机
  10. CFS调度主要代码分析一
  11. 【缺陷检测】基于matlab形态学液晶显示器表面缺陷检测【含Matlab源码 1304期】
  12. 【图像配准】基于matlab SIFT图像配准【含Matlab源码 463期】
  13. ios 渐变透明背景_PPT背景常见的6种设计方法
  14. Steam帐号被盗怎么办
  15. 基于Arch的live系统
  16. springAop原理之(三)Advised接口族
  17. android 区分平板,加量不加价!台电首款基于Android 11的平板终上市
  18. 三春过后诸芳尽. 荼蘼
  19. Oracle分区表操作
  20. 产品经理必读的十本好书

热门文章

  1. doraemon的python 网络基础、进程和操作系统发展史
  2. logback的xml文件,b站黑马程序员
  3. 宾馆客户管理系统数据库java_JAVA连接数据库酒店管理系统.docx
  4. 这些年,为保住阿里饭碗学习的并发编程
  5. Co. - Microsoft - Windows - 快捷键
  6. 10年大数据平台经验,总结出这份数据建设干货(内含多张架构图)
  7. Top 150 Questions - 1.4
  8. 华为OD机试真题 C++ 实现【Linux发行版的数量】【2022.11 Q4 新题】
  9. iPhone 6 图像渲染揭秘
  10. 苹果新品发布会?看美维公司的小伙伴如何评论!