Postgresql 中文操作指南

Chapter 75. System Catalog Declarations and Initial Contents

Table of Contents

PostgreSQL 使用许多不同的系统目录来跟踪数据库对象(如表和函数)的存在和属性。实际上,系统目录与普通用户表没有区别,但后端 C 代码知道每个目录的结构和属性,并且可以在较低级别直接操作它。因此,例如,我们不建议在实际操作中尝试更改目录的结构;这会打破 C 代码中关于目录行布局方式的内置假设。但是,不同版本之间的目录结构可能会发生变化。

PostgreSQL uses many different system catalogs to keep track of the existence and properties of database objects, such as tables and functions. Physically there is no difference between a system catalog and a plain user table, but the backend C code knows the structure and properties of each catalog, and can manipulate it directly at a low level. Thus, for example, it is inadvisable to attempt to alter the structure of a catalog on-the-fly; that would break assumptions built into the C code about how rows of the catalog are laid out. But the structure of the catalogs can change between major versions.

目录的结构在源树的 src/include/catalog/ 目录中用特定格式的 C 标头文件声明。对于每个目录,有一个以目录命名的标头文件(例如, pg_classpg_class.h ),该文件定义了目录具有的列集,以及一些其他基本属性,如其 OID。

The structures of the catalogs are declared in specially formatted C header files in the src/include/catalog/ directory of the source tree. For each catalog there is a header file named after the catalog (e.g., pg_class.h for pg_class), which defines the set of columns the catalog has, as well as some other basic properties such as its OID.

许多目录在 initdb 的“引导”阶段必须加载初始数据,才能使系统达到能够执行 SQL 命令的程度。(例如, pg_class.h 必须包含一个对自身和每个其他系统目录和索引的条目。)该初始数据以可编辑形式保存 src/include/catalog/ 目录中存储的数据文件中。例如, pg_proc.dat 描述了必须插入到 pg_proc 目录中的所有初始行。

Many of the catalogs have initial data that must be loaded into them during the “bootstrap” phase of initdb, to bring the system up to a point where it is capable of executing SQL commands. (For example, pg_class.h must contain an entry for itself, as well as one for each other system catalog and index.) This initial data is kept in editable form in data files that are also stored in the src/include/catalog/ directory. For example, pg_proc.dat describes all the initial rows that must be inserted into the pg_proc catalog.

要创建目录文件并将初始数据加载到其中,一个在引导模式下运行的后端将读取一个包含命令和初始数据的 BKI(后端接口)文件。在此模式中使用的 postgres.bki 文件是通过一个名为 genbki.pl 的 Perl 脚本从上述标头和数据文件中准备的,同时构建 PostgreSQL 发行版。即使 postgres.bki 特定于某个 PostgreSQL 版本,也是与平台无关的,并且安装在安装树的 share 子目录中。

To create the catalog files and load this initial data into them, a backend running in bootstrap mode reads a BKI (Backend Interface) file containing commands and initial data. The postgres.bki file used in this mode is prepared from the aforementioned header and data files, while building a PostgreSQL distribution, by a Perl script named genbki.pl. Although it’s specific to a particular PostgreSQL release, postgres.bki is platform-independent and is installed in the share subdirectory of the installation tree.

genbki.pl 还为每个目录生成一个派生标头文件,例如 pg_class 目录的 pg_class_d.h 。此文件包含自动生成的宏定义,并且可能包含其他可用于读取特定目录的客户端 C 代码的宏、枚举声明等。

genbki.pl also produces a derived header file for each catalog, for example pg_class_d.h for the pg_class catalog. This file contains automatically-generated macro definitions, and may contain other macros, enum declarations, and so on that can be useful for client C code that reads a particular catalog.

大多数 PostgreSQL 开发人员无需直接关注 BKI 文件,但是后端的几乎所有非琐碎功能添加都会要求修改目录头文件和/或初始数据文件。本章的其余部分将提供一些相关信息,并为了完整性而描述 BKI 文件格式。

Most PostgreSQL developers don’t need to be directly concerned with the BKI file, but almost any nontrivial feature addition in the backend will require modifying the catalog header files and/or initial data files. The rest of this chapter gives some information about that, and for completeness describes the BKI file format.