PostgreSql 进程及内存结构

一、进程及内存架构

在这里插入图片描述

PostgreSQL 数据库运行时,使用如下命令可查询数据库进程,正对应上述结构图。

[postgres@localhost ~]$ ps -ef|grep post
postgres   8649      1  0 15:05 ?        00:00:00 /app/pg13/bin/postgres -D /data/pg13/data
postgres   8657   8649  0 15:05 ?        00:00:00 postgres: logger
postgres   8659   8649  0 15:05 ?        00:00:00 postgres: checkpointer
postgres   8660   8649  0 15:05 ?        00:00:00 postgres: background writer
postgres   8661   8649  0 15:05 ?        00:00:00 postgres: walwriter
postgres   8662   8649  0 15:05 ?        00:00:00 postgres: autovacuum launcher
postgres   8663   8649  0 15:05 ?        00:00:00 postgres: archiver last was 0000000100000003000000CD
postgres   8664   8649  0 15:05 ?        00:00:00 postgres: stats collector
postgres   8666   8649  0 15:05 ?        00:00:00 postgres: logical replication launcher
postgres  10188   8649  0 16:19 ?        00:00:00 postgres: postgres postgres 192.168.100.1(53966) idle
postgres  10234   8649  0 16:20 ?        00:00:00 postgres: syd postgres [local] idle

[postgres@localhost ~]$ ll /app/pg13/bin/postgres
-rwxr-xr-x 1 postgres postgres 8389568 Jun 30  2022 /app/pg13/bin/postgres
[postgres@localhost ~]$
[postgres@localhost ~]$ ll /app/pg13/bin/postmaster
lrwxrwxrwx 1 postgres postgres 8 Jun 30  2022 /app/pg13/bin/postmaster -> postgres

二、进程说明

  • postmaster 进程:数据库主进程,如上所示,postmaster 是个软连接,对应 postgres 进程,为历史版本兼容,仍保留 postmaster 写法,该进程可用于启停数据库(pg_ctl 封装的 postgres 命令),创建(fork)客户端连接用的子进程。
  • logger 进程:日志进程,只有在参数 logging_collect 设置为 on 时,才会启动 logger 辅助进程。
  • checkpointer 进程:检查点进程,辅助 background write 进行内存中脏数据块的写入,并且记录已经写入的脏块,从而保证下次只会写入新的脏块。
  • background writer 进程:数据写入进程,将共享内存中的脏数据块写入到磁盘上。
  • walwriter 进程:wal 日志写进程,修改的数据记录到 wal 日志中。
  • autovacuum launcher 进程:自动清理进程,自动清理被标记为删除状态的数据(自动清理 MVCC 多版本产生的死元组)。
  • archiver 进程:归档进程,将 wal 日志在覆盖使用前备份出来。
  • stats collector 进程:统计数据收集进程,主要作用就是数据的统计收集,比如在一个表上进行了多少次的插入、更新和删除操作,磁盘块的读写次数、行的读写次数等等。
  • logical replication launcher 进程:逻辑复制进程,订阅者通过 logical replication launcher 进程向发布者订阅表的更新并获取更新。
  • postgres 进程:postmaster 创建出的客户端连接子进程。

三、内存说明

3.1 共享内存

  PostgreSQL 启动后会生成一块共享内存,共享内存主要用作数据块的缓冲区,以便提高读写性能。WAL 日志缓冲区 和 CLOG(CommitLog)缓冲区也存在于共享内存中。除此以外,一些全局信息也保存在共享内存中,如进程信息、锁的信息、全局统计信息等,相关数据库参数如下:

  • shared_buffers:一个合理开始值是系统内存的 25%,设置的太大效果可能并不总是理想的,因为 PostgreSQL 同样依赖操作系统的高速缓冲区,默认值为 128MB。

3.2 本地内存

  后台服务进程除访问共享内存以外,还会申请分配一些本地内存,以便暂存一些不需要全局存储的数据,相关数据库参数如下:

  • temp_buffers:为每个数据库会话设置用于临时缓冲区的最大内存,用于访问临时表的本地缓冲区,默认值为 8MB。
  • work_mem:设置在写入临时磁盘文件之前每个查询操作(排序或哈希表)可使用的基础最大内存容量,默认值是 4MB。
  • maintenance_work_mem:指定在维护性操作(例如VACUUM、CREATE INDEX和ALTER TABLE ADD FOREIGN KEY)中使用的最大的内存量,默认值是 64MB。

PostgreSql 逻辑结构:https://xiaosonggong.blog.csdn.net/article/details/131452022
PostgreSql 文件目录结构:https://xiaosonggong.blog.csdn.net/article/details/120303380

猜你喜欢

转载自blog.csdn.net/songyundong1993/article/details/132026800