002postgresinitdb初始化

database

语法说明:

initdb [option...] [ --pgdata | -D ] directory

参数解析:

  • -A authmethod
  • --auth=authmethod

    这个选项为本地用户指定在pg_hba.conf中使用的默认认证方法 (host和local行)。 initdb将使用指定的非复制身份验证方法以及复制连接预填充 pg_hba.conf条目。

    除非你信任你系统上的所有本地用户,不要使用trust。为了安装方便,trust是默认值。

  • --auth-host=authmethod

    这个选项为通过 TCP/IP 连接的本地用户指定在pg_hba.conf中使用的认证方法(host行)。

  • --auth-local=authmethod

    这个选项为通过 Unix 域套接字连接的本地用户指定在pg_hba.conf中使用的认证方法(local行)。

  • -D directory
  • --pgdata=directory

    这个选项指定数据库集簇应该存放的目录。这是initdb要求的唯一信息,但是你可以通过设定PGDATA环境变量来避免书写它,这很方便因为之后数据库服务器(postgres)可以使用同一个变量来找到数据库目录。

  • -E encoding
  • --encoding=encoding

    选择模板数据库的编码。这也将是后来创建的任何数据库的默认编码,除非你覆盖它。默认值来自于区域,或者如果该值不起作用则为SQL_ASCII。PostgreSQL服务器所支持的字符集在第 23.3.1 节中描述。

  • -k
  • --data-checksums

    在数据页面上使用校验码来帮助检测 I/O 系统造成的损坏。启用校验码将会引起显著的性能惩罚。这个选项只能在初始化期间被设置,并且以后不能修改。如果被设置,在所有数据库中会为所有对象计算校验码。

  • --locale=locale

    为数据库集簇设置默认区域。如果这个选项没有被指定,该区域将从initdb所运行的环境中继承。区域支持在第 23.1 节中描述。

  • --lc-collate=locale
  • --lc-ctype=locale
  • --lc-messages=locale
  • --lc-monetary=locale
  • --lc-numeric=locale
  • --lc-time=locale

    和--locale相似,但是只在指定的分类中设置区域。

  • --no-locale

    等效于--locale=C。

  • -N
  • --no-sync

    默认情况下,initdb将等待所有文件被安全地写到磁盘。这个选项会导致initdb不等待就返回,这当然更快,但是也意味着一次后续的操作系统崩溃可能让数据目录损坏。通常,这个选项对测试有用,但是不应该在创建生产安装时使用。

  • --pwfile=filename

    让initdb从一个文件读取数据库超级用户的口令。该文件的第一行被当作口令。

  • -S
  • --sync-only

    安全地把所有数据库文件写入到磁盘并退出。这不会执行任何正常的initdb操作。

  • -T config
  • --text-search-config=config

    设置默认的文本搜索配置。详见default_text_search_config。

  • -U username
  • --username=username

    选择数据库超级用户的用户名。这个的默认值是实际运行initdb的用户的名称。超级用户的名字是什么真的不重要,但是你可以选择保留常用的名字postgres,即使操作系统的用户名不同。

  • -W
  • --pwprompt

    让initdb提示要求为数据库超级用户给予一个口令。如果你没有计划使用口令认证,这就不重要。否则在你设置一个口令之前你就无法使用口令认证。

  • -X directory
  • --waldir=directory

    这个选项指定预写日志会被存储在哪个目录中。

    其他较少使用的选项:

  • -d
  • --debug

    打印来自引导后端的调试输出以及普通大众不那么感兴趣的一些消息。引导后端被程序initdb用来创建目录表。这个选项会生成大量极端无聊的输出。

  • -L directory

    指定initdb应从哪里寻找它的输入文件来初始化数据库集簇。这通常没有必要。如果你需要显式指定它们的位置,你应该被告知。

  • -n
  • --no-clean

    默认情况下,当initdb确定有一个错误阻止它完整地创建数据库集簇,它会移除在它发现无法完成任务之前创建的任何文件。这个选项会抑制这种整理并且对调试有用。

    其他选项:

  • -V
  • --version

    打印initdb版本并退出。

  • -?
  • --help

显示有关initdb命令行参数的帮助并退出。

二、初始化代码解析:

postgresql-12.2srcininitdb文件夹中的initdb.c文件

main函数:

  • help和version直接解析并返回

  • getopt_long:从参数中获取有效值,进行基本信息填充

  • 判断解析到的参数值是否有效

  • 设置wal日志

  • setup_pgdata设置路径

  • setup_bin_path获取其他可执行文件路径

  • 设置相关信息

  • initialize_data_directory初始化

    • setup_signals
    • create_data_directory、create_xlog_or_symlink创建文件夹
    • write_version_file写版本文件PG_VERSION
    • test_config_settings测试基本配置是否成功
    • setup_config相关信息写入配置文件postgresql.conf
    • bootstrap_template1将postgres.bki文件中的信息取出,在bootstrap 模式下运行 BKI script 来创建 template1数据库
    • write_version_file("base/1");在pg_data/base/1/PG_VERSION中写入postgresql的主版本
    • PG_CMD_OPEN;打开 "/home/perrynzhou/Database/pgsql/bin/postgres" --single -F -O -j -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/null 命令,等待执行命令
    • setup_auth(cmdfd);通过执行" REVOKE ALL on pg_authid FROM public"授权
    • setup_depend(cmdfd);往系统表pg_xxx插入默认的数据
    • setup_sysviews(cmdfd);在template1中执行system_views.sql 语句
    • setup_description(cmdfd);初始化系统表xxx_description
    • setup_collation(cmdfd);初始化系统表pg_collation
    • setup_dictionary(cmdfd);执行snowball_create.sql语句
    • setup_privileges(cmdfd);初始化授权相关的表
    • setup_schema(cmdfd);通过执行information_schema.sql初始化information_schema表,
    • load_plpgsql(cmdfd);执行 CREATE EXTENSION plpgsql语句
    • vacuum_db(cmdfd);/执行ANALYZE和VACUUM FREEZE
    • make_template0(cmdfd);通过template1初始化template0
    • make_postgres(cmdfd);创建postgres数据库

  • 显示成功,告诉用户怎么使用

三、bki文件解析

在pg的源码src/backend/catalog/genbki.pl中包含了相关的脚本。在编译pg时,会执行这个脚本、生成一个postgres.bki文件(默认在/usr/local/pgsql/share目录下)

BKI(后端接口)介绍

后端接口(BKI)是用一些特殊语言写的脚本,这些脚本是 PostgreSQL后端可以理解,以特殊的 "bootstrap" 模式执行. 这种模式允许在不存在系统表的零初始条件下执行数据库函数,而普通的SQL命令要求系统表必须存在。 因此BKI文件可以用于在第一时间创建数据库系统,并且除此以外可能也没有其它用处。

在创建一个新的数据库集群时,initdb 使用BKI文件 来完成部分工作。initdb使用的文件是作为编译 PostgreSQL的一部分,由一个叫genbki.pl的程序创建,这个程序读取源代码树木录的src/include/catalog/目录里的几个特殊C开头的文件。生成的BKI文件叫postgres.bki,并且通常安装在安装目录里的share子目录系统初始化的BKI文件的结构.

open命令打开的表需要系统事先存在另外一些基本的表 ,在这些表存在并拥有数据之前,不能使用open命令。 这些最低限度必须存在的表是 pg_class, pg_attribute, pg_proc, and pg_type。 为了允许这些表自己被填充, 带bootstrap 选项的create 隐含打开所创建的表用于插入数据。

同样, declare index和declare toast 命令也不能在它们所需要的系统表创建并填充之前使用。

因此,postgres.bki文件的结构必须是这样的:

  • create bootstrap 其中一个关键表
  • insert 数据,这些数据至少描述这些关键表本身
  • close
  • 重复创建和填充其它关键表
  • create (不带 bootstrap)一个非关键表
  • open
  • insert需要的数据
  • close
  • 重复创建其它非关键表
  • 定义索引和toast表
  • build indices

    当然,肯定还有其它未记录文档的顺序依赖关系。

例子

下面的命令将创建名为 test_table的表,它的OID是420,该表有两个字段cola和colb , 字段类型分别为int4和text, 然后向该表插入两行:

create test_table 420 (cola = int4, colb = text) open test_table insert OID=421 ( 1 "value1" ) insert OID=422 ( 2 null ) close test_table

四、系统表字段增加

为pg_database增加一个字段 datdummy,打开 /src/include/catalog/pg_database.h:

CATALOG(pg_database,1262) BKI_SHARED_RELATION BKI_ROWTYPE_OID(1248) BKI_SCHEMA_MACRO {

NameData datname; /* database name /

Oid datdba; / owner of database /

int32 encoding; / character encoding /

NameData datcollate; / LC_COLLATE setting /

NameData datctype; / LC_CTYPE setting /

bool datistemplate; / allowed as CREATE DATABASE template? /

bool datallowconn; / new connections allowed? /

int32 datconnlimit; / max connections allowed (-1=no limit) /

Oid datlastsysoid; / highest OID to consider a system OID /

TransactionId datfrozenxid; / all Xids < this are frozen in this DB /

TransactionId datminmxid; / all multixacts in the DB are >= this /

Oid dattablespace; / default table space for this DB */

#ifdef CATALOG_VARLEN /* variable-length fields start here / aclitem datacl[1]; /

access permissions */

#endif

} FormData_pg_database;

/* ----------------

* Form_pg_database corresponds to a pointer to a tuple with

* the format of pg_database relation.

* ---------------- */

typedef FormData_pg_database *Form_pg_database;

/* ----------------

* compiler constants for pg_database

* ---------------- */

#define Natts_pg_database 13

#define Anum_pg_database_datname 1

#define Anum_pg_database_datdba 2

#define Anum_pg_database_encoding 3

#define Anum_pg_database_datcollate 4

#define Anum_pg_database_datctype 5

#define Anum_pg_database_datistemplate 6

#define Anum_pg_database_datallowconn 7

#define Anum_pg_database_datconnlimit 8

#define Anum_pg_database_datlastsysoid 9

#define Anum_pg_database_datfrozenxid 10

#define Anum_pg_database_datminmxid 11

#define Anum_pg_database_dattablespace 12

#define Anum_pg_database_datacl 13

1、在 dattablespace 下增加新定义:

int8 datdummy; /* dummy column */

2、/src/include/catalog/pg_database.h中的后边字段序号的定义也是很重要的,必须按顺序修改,也要记得属性数相应修改:

#define Natts_pg_database 14 

#define Anum_pg_database_dattablespace 12

#define Anum_pg_database_datdummy 13

#define Anum_pg_database_datacl 14

3、/src/include/catalog/pg_database.dat预定义的数据库必须也要修改,null 前边增加 100的字段为 datdummy 数据:

[

{ oid => "1", oid_symbol => "TemplateDbOid",

descr => "default template for new databases",

datname => "template1", encoding => "ENCODING", datcollate => "LC_COLLATE",

datctype => "LC_CTYPE", datistemplate => "t", datallowconn => "t",

datconnlimit => "-1", datlastsysoid => "0", datfrozenxid => "0",

datminmxid => "1", dattablespace => "pg_default", datdummy => 111, datacl => "_null_" },

]

4、编译运行

被脚本 genbki.pl 利用生成 postgresql.bki文件

insert OID = 1 ( template1 PGUID ENCODING "LC_COLLATE" "LC_CTYPE" t t -1 0 0 1 1663 111 null));

5、涉及到的相关表操作函数需要将该参数添加进去

例子

修改pg_proc

  • 1、FormData_pg_proc结构体中添加int16 newvalue BKI_DEFAULT(999);
  • 2、pg_proc_d.h中添加#define Anum_pg_proc_newvalue 30
  • 3、pg_proc.dat的每个表插入添加newvalue选项

{ oid => "1242", descr => "I/O",

proname => "boolin", prorettype => "bool", proargtypes => "cstring",

prosrc => "boolin" , newvalue => "999"},

结果显示:

select * from pg_proc;

-[ RECORD 1 ]

-----------------------------------------------

oid | 1242

proname | boolin

pronamespace | 11

proowner | 10

prolang | 12

procost | 1

prorows | 0

provariadic | 0

prosupport | -

prokind | f

prosecdef | f

proleakproof | f

proisstrict | t

proretset | f

provolatile | i

proparallel | s

pronargs | 1

pronargdefaults | 0

prorettype | 16

proargtypes | 2275

proallargtypes |

proargmodes |

proargnames |

proargdefaults |

protrftypes |

prosrc | boolin

probin |

proconfig |

proacl |

newvalue |

注意:此处未增加相关命令的修改,实际修改时需要注意:

  • 1、表的增删改查
  • 2、该列明的具体操作

以上是 002postgresinitdb初始化 的全部内容, 来源链接: utcz.com/z/533710.html

回到顶部