Bootstrap

2024-12-13 AnolisOS 8.8 PostgreSQL 内存架构图

在安装完 PostgreSQL 之后,我们需要一些基础的理论来加深我们对该数据库的理解。下图解释了 PostgreSQL 的内存架构和一些关键的进程。

[postgres@localhost data]$ ps -ef | grep postgres
postgres    5895       1  0 18:17 ?        00:00:00 /usr/local/pgsql-14/bin/postgres -D /usr/local/pgsql-14/data
postgres    5896    5895  0 18:17 ?        00:00:00 postgres: logger
postgres    5898    5895  0 18:17 ?        00:00:00 postgres: checkpointer
postgres    5899    5895  0 18:17 ?        00:00:00 postgres: background writer
postgres    5900    5895  0 18:17 ?        00:00:00 postgres: walwriter
postgres    5901    5895  0 18:17 ?        00:00:00 postgres: autovacuum launcher
postgres    5902    5895  0 18:17 ?        00:00:00 postgres: stats collector
postgres    5903    5895  0 18:17 ?        00:00:00 postgres: logical replication launcher
root        6160    4677  0 18:30 pts/1    00:00:00 su - postgres
postgres    6161    6160  0 18:30 pts/1    00:00:00 -bash
postgres    7779    6161  3 19:57 pts/1    00:00:00 ps -ef
postgres    7780    6161  0 19:57 pts/1    00:00:00 grep --color=auto postgres
[postgres@localhost data]$

下面关于这些进程做一个简单的介绍。

Postmaster 是 PostgreSQL 的主进程,主要负责启动和管理所有其他进程。它监听客户端连接请求,并为每个连接创建新的后端进程。后端进程处理客户端请求、执行 SQL 查询并返回处理的结果。PostgreSQL 的每个连接都是一个独立的后端进程,这样可以有效地隔离不同的连接之间的工作,避免互相干扰。

Checkpointer 检查点进程,定期将内存中的数据写入磁盘,以保持数据的一致性并减少异常恢复时间。

Archiver 归档进程,负责将 WAL Write-Ahead Logging 日志文件存档,以便在需要时进行恢复。

Autovacuum 进程,自动清理死元组 deleted tuples 并回收空间,优化数据库性能。

WAL 进程,WAL Write-Ahead Logging 用于确保数据的持久性和一致性。在进行任何数据更改之前,WAL 记录会被写入日志,确保即使发生系统故障也能恢复数据。

Background Writer 进程的主要功能是将共享内存中的“脏页”写入磁盘‌。在数据库操作中,数据通常从磁盘读取到内存中进行处理,处理完毕后需要将这些修改过的数据写回磁盘。

Stats Collector 统计信息进程,收集关于查询执行时间、锁等待时间等的统计信息,监控和性能调优提供数据支持。

Logical Replication Launcher 逻辑复制启动器进程,逻辑复制工作者进程,管理逻辑复制过程中的各种任务。

Logger 日志进程,负责将数据库的日志信息记录到日志文件中,便于后续的故障排查和监控。

说明:PostgreSQL 内存架构通常也是面试的一个考点。