# 70.1. Database File Layout

This section describes the storage format at the level of files and directories.

Traditionally, the configuration and data files used by a database cluster are stored together within the cluster's data directory, commonly referred to asPGDATA(after the name of the environment variable that can be used to define it). A common location forPGDATAis/var/lib/pgsql/data. Multiple clusters, managed by different server instances, can exist on the same machine.

ThePGDATAdirectory contains several subdirectories and control files, as shown inTable 70.1. In addition to these required items, the cluster configuration filespostgresql.conf,pg_hba.conf, andpg_ident.confare traditionally stored inPGDATA, although it is possible to place them elsewhere.

Table 70.1. Contents ofPGDATA

Item Description
PG_VERSION A file containing the major version number of PostgreSQL
base Subdirectory containing per-database subdirectories
current_logfiles 记录日志收集器当前写入的日志文件的文件
全球的 包含集群范围表的子目录,例如pg_database
pg_commit_ts 包含事务提交时间戳数据的子目录
pg_dynshmem 包含动态共享内存子系统使用的文件的子目录
pg_logical 包含用于逻辑解码的状态数据的子目录
pg_multixact 包含多事务状态数据的子目录(用于共享行锁)
pg_notify 包含 LISTEN/NOTIFY 状态数据的子目录
pg_replslot 包含复制槽数据的子目录
pg_serial 包含有关已提交可序列化事务信息的子目录
pg_snapshots 包含导出快照的子目录
pg_stat 包含统计子系统的永久文件的子目录
pg_stat_tmp 包含统计子系统临时文件的子目录
pg_subtrans 包含子事务状态数据的子目录
pg_tblspc 包含指向表空间的符号链接的子目录
pg_twophase 包含准备交易的状态文件的子目录
pg_wal 包含 WAL(预写日志)文件的子目录
pg_xact 包含事务提交状态数据的子目录
postgresql.auto.conf 用于存储由设置的配置参数的文件改变系统
postmaster.opts 记录服务器命令行选项的文件
最后开始于
邮递员.pid 记录当前 postmaster 进程 ID (PID)、集群数据目录路径、postmaster 启动时间戳、端口号、Unix 域套接字目录路径(可以为空)、第一次有效侦听的锁定文件_地址(IP 地址或*, 如果不监听 TCP,则为空)和共享内存段 ID(服务器关闭后此文件不存在)

集群中的每个数据库都有一个子目录PGD​​ATA``/base, 以数据库的 OID 命名pg_database.该子目录是数据库文件的默认位置;特别是,它的系统目录存储在那里。

请注意,以下部分描述了内置的行为 表访问方法, 和内置索引访问方法.由于 PostgreSQL 的可扩展性,其他访问方法可能会有所不同。

每个表和索引都存储在一个单独的文件中。对于普通关系,这些文件以表或索引的名称命名文件节点编号,可以在pg_class.relfilenode.但对于临时关系,文件名的形式为时间*BBB*_*FFF*, 在哪里*BBB是创建文件的后端的后端 ID,并且FFF是文件节点号。无论哪种情况,除了主文件(a/k/a main fork)之外,每个表和索引都有一个自由空间地图*(看第 70.3 节),它存储有关关系中可用空间的信息。可用空间映射存储在以文件节点号加后缀命名的文件中_fsm.表也​​有能见度图, 存储在带有后缀的 fork 中_vm,以跟踪哪些页面已知没有死元组。可见性地图在中进一步描述第 70.4 节.未记录的表和索引有第三个叉子,称为初始化叉子,它存储在带有后缀的叉子中_在里面(看第 70.5 节).

# Caution

Note that while a table's filenode often matches its OID, this isnotnecessarily the case; some operations, likeTRUNCATE,REINDEX,CLUSTERand some forms ofALTER TABLE, can change the filenode while preserving the OID. Avoid assuming that filenode and table OID are the same. Also, for certain system catalogs includingpg_classitself,pg_class.relfilenodecontains zero. The actual filenode number of these catalogs is stored in a lower-level data structure, and can be obtained using thepg_relation_filenode()function.

When a table or index exceeds 1 GB, it is divided into gigabyte-sizedsegments. The first segment's file name is the same as the filenode; subsequent segments are named filenode.1, filenode.2, etc. This arrangement avoids problems on platforms that have file size limitations. (Actually, 1 GB is just the default segment size. The segment size can be adjusted using the configuration option--with-segsizewhen building PostgreSQL.) In principle, free space map and visibility map forks could require multiple segments as well, though this is unlikely to happen in practice.

A table that has columns with potentially large entries will have an associatedTOASTtable, which is used for out-of-line storage of field values that are too large to keep in the table rows proper.pg_class.瑞曲林从表链接到其 TOAST 表(如果有)。看第 70.2 节了解更多信息。

表和索引的内容将在第 70.6 节.

表空间使场景更加复杂。每个用户定义的表空间在PGD​​ATA``/pg_tblspc目录,它指向物理表空间目录(即,在表空间的创建表空间命令)。此符号链接以表空间的 OID 命名。在物理表空间目录中有一个子目录,其名称取决于 PostgreSQLserver 版本,例如PG_9.0_201008051.(使用这个子目录的原因是为了使数据库的后续版本可以使用相同的创建表空间位置值没有冲突。)在特定于版本的子目录中,每个数据库都有一个子目录,该子目录在表空间中具有元素,以数据库的 OID 命名。表和索引存储在该目录中,使用文件节点命名方案。这pg_default表空间不通过访问pg_tblspc, 但对应于PGD​​ATA``/base.同样,pg_global表空间不通过访问pg_tblspc, 但对应于PGD​​ATA``/全局.

pg_relation_filepath()函数显示整个路径(相对于PGD​​ATA) 的任何关系。它通常可以用来替代记住上述许多规则。但请记住,此函数仅给出关系主分支的第一段的名称——您可能需要附加段号和/或_fsm,_vm, 要么_在里面查找与该关系关联的所有文件。

临时文件(用于对超出内存容量的数据进行排序等操作)在内部创建PGD​​ATA``/base/pgsql_tmp, 或在一个pgsql_tmp表空间目录的子目录,如果表空间不是pg_default是为他们指定的。临时文件的名称具有以下形式pgsql_tmp*购买力平价*.*神经网络*, 在哪里*购买力平价是拥有后端的 PID,并且神经网络*区分该后端的不同临时文件。