典籍篇
聚社区之智,传技术之火
postgresql 的组成结构
内存
Local memory area:由每个后端进程分配给自己使用。
Share memory area: 由 postgresql 服务器的所有进程使用。

work_mem: 执行器执行 order by、distinct、join tables、hash-join操作时使用。
maintenance_work_mem: vacuum 归整、reindex重建索引,时使用。
temp_buffers: 临时表使用。
shared_buffer_pool: 从持久化的存储加载表页和索引页到此。
wal_buffer: 防止服务异常停止导致数据没有落盘,创建了 wal 缓冲区,相当于 redo log。
commit log: 记录所有的事务状态如:再处理,已提交、回退,保持事务的一致性。相当于 undo log。
进程
PostgreSQL 采用 C/S 模式,系统为每个连接的客户端分配一个服务进程 Postgres

当运行 pg_ctl 命令进入 Postgres 程序时,其进程创建流程如下:
PostMaster

- PostMaster:进程是整个数据库实例的总控进程,负责启动关闭该数据实例。并且在服务进程出现错误时完成系统的恢复,还要在系统奔溃的时候重启系统。它是运行在服务器上的总控进程,同时也负责整个系统范围内的操作,例如中断操作与信号处理。但是 Postmaster 本身并不执行这些操作,而是指派一个子进程在适当的时间处理它们。Postmaster 进程在起始时会创建共享内存与信号库,用于与子进程的通信,同时也能在某个子进程奔溃的时候重置共享内存即可恢复。
Postmaster 守护进程的执行流程如下

SysLogger
- SysLogger:(系统日志)进程,日志信息是数据库管理员获取数据库系统运行状态的有效手段。在数据库出现故障时,日志信息是非常有用的。把数据库日志信息集中输出到一个位置将极大方便管理员维护数据库系统。然而,日志输出将产生大量数据(特别是在比较高的调试级别上),单文件保存时不利于日志文件的操作。因此,在SysLogger的配置选项中可以设置日志文件的大小,SysLogger会在日志文件达到指定的大小时关闭当前日志文件,产生新的日志文件。
# - Where to Log -
log_destination = 'stderr' # Valid values are combinations of
# stderr, csvlog, jsonlog, syslog, and
# eventlog, depending on platform.
# csvlog and jsonlog require
# logging_collector to be on.
# This is used when logging to stderr:
logging_collector = on # Enable capturing of stderr, jsonlog,
# and csvlog into log files. Required
# to be on for csvlogs and jsonlogs.
# (change requires restart)
# These are only used if logging_collector is on:
#log_directory = 'log' # directory where log files are written,
# can be absolute or relative to PGDATA
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
# can include strftime() escapes
#log_file_mode = 0600 # creation mode for log files,
# begin with 0 to use octal notation
#log_rotation_age = 1d # Automatic rotation of logfiles will
# happen after that time. 0 disables.
#log_rotation_size = 10MB # Automatic rotation of logfiles will
# happen after that much log output.
# 0 disables.
#log_truncate_on_rotation = off # If on, an existing log file with the
# same name as the new log file will be
# truncated rather than appended to.
# But such truncation only occurs on
# time-driven rotation, not on restarts
# or size-driven rotation. Default is
# off, meaning append to existing files
# in all cases.
log_destination:配置日志输出目标,根据不同的运行平台会设置不同的值,Linux下默认为stderr。