PG体系结构之进程结构(Process Architecture)
进程结构(Process Architecture)
PostgreSQL是一个客户端/服务器类型的关系数据库管理系统,具有多进程架构,运行在单个主机上。
1 2 3 4 5 6 7 8 9 10 11 12 | [root@lhrpg /]# ps -ef|grep postgres postgres 14092 1 0 09:46 ? 00:00:00 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/ postgres 14093 14092 0 09:46 ? 00:00:00 postgres: logger postgres 14095 14092 0 09:46 ? 00:00:00 postgres: checkpointer postgres 14096 14092 0 09:46 ? 00:00:00 postgres: background writer postgres 14097 14092 0 09:46 ? 00:00:00 postgres: walwriter postgres 14098 14092 0 09:46 ? 00:00:00 postgres: autovacuum launcher postgres 14099 14092 0 09:46 ? 00:00:00 postgres: archiver postgres 14100 14092 0 09:46 ? 00:00:00 postgres: stats collector postgres 14101 14092 0 09:46 ? 00:00:00 postgres: logical replication launcher postgres 14121 14092 0 09:47 ? 00:00:00 postgres: postgres postgres 172.17.0.1(14877) idle postgres 14139 14092 0 09:47 ? 00:00:00 postgres: postgres postgres 172.17.0.1(14911) idle |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [pg13@lhrrepmgr64361 ~]$ ps -ef|grep postgres pg13 6806 11347 0 14:32 ? 00:00:00 postgres: pgpool postgres 172.72.6.66(38816) idle pg13 11347 0 0 Apr29 ? 00:00:19 /pg13/pg13/bin/postgres -D /pg13/pgdata pg13 11348 11347 0 Apr29 ? 00:00:00 postgres: logger pg13 11354 11347 0 Apr29 ? 00:00:07 postgres: checkpointer pg13 11355 11347 0 Apr29 ? 00:00:01 postgres: background writer pg13 11357 11347 0 Apr29 ? 00:00:07 postgres: stats collector pg13 18895 11347 0 11:17 ? 00:00:03 postgres: walwriter pg13 18896 11347 0 11:17 ? 00:00:00 postgres: autovacuum launcher pg13 18897 11347 0 11:17 ? 00:00:00 postgres: archiver last was 0000000C0000000100000022.partial pg13 18898 11347 0 11:17 ? 00:00:00 postgres: logical replication launcher pg13 18909 11347 0 11:17 ? 00:00:03 postgres: walsender repmgr 172.72.6.62(51722) streaming 1/22349A48 pg13 18920 11347 0 11:17 ? 00:00:03 postgres: walsender repmgr 172.72.6.63(51920) streaming 1/22349A48 pg13 18916 11347 0 11:17 ? 00:00:04 postgres: repmgr repmgr 172.72.6.62(51742) idle pg13 18935 11347 0 11:17 ? 00:00:05 postgres: repmgr repmgr 172.72.6.63(51970) idle pg13 18936 11347 0 11:17 ? 00:00:00 postgres: repmgr repmgr 172.72.6.64(59288) idle pg13 11362 11347 0 Apr29 ? 00:00:35 postgres: repmgr repmgr 172.72.6.61(59970) idle [pg13@lhrrepmgr64361 ~]$ pstree -p 11347 postgres(11347)─┬─postgres(6806) ├─postgres(11348) ├─postgres(11354) ├─postgres(11355) ├─postgres(11357) ├─postgres(11362) ├─postgres(18895) ├─postgres(18896) ├─postgres(18897) ├─postgres(18898) ├─postgres(18909) ├─postgres(18916) ├─postgres(18920) ├─postgres(18935) └─postgres(18936) |
https://www.interdb.jp/pg/pgsql02.html
多个进程协同管理一个数据库集群通常被称为“PostgreSQL server”,它包含以下类型的进程:
- postgres 服务器进程:postgres服务器进程是所有与数据库集群管理相关进程的父进程。
- backend 进程: 每个backend进程处理由连接的客户机发出的所有查询和语句。
- background 进程: 不同的background 进程执行不同特性的进程(例如,VACUUM进程和CHECKPOINT进程),用于数据库管理。
- replication associated 进程: 与复制相关的进程负责执行流复制。
- background worker 进程:background worker process进程可以执行用户实现的任何处理。
PostgreSQL进程架构示例
这张图显示了一个PostgreSQL服务器的进程:一个postgres服务器进程,两个backend进程,七个background进程和两个客户端进程。还演示了数据库集群、共享内存和两个客户机进程。
Postgres服务器进程
如上所述,postgres服务器进程是PostgreSQL服务器中所有进程的父进程。在早期的版本中,它被称为postmaster(postmaster is a deprecated alias of postgres.)。postgres服务器进程是整个数据库实例的总控进程,负责启动关闭该数据库实例。
当某个服务出现错误的时候,Postmaster主进程会自动完成系统的修复,修复过程中会停掉所有的服务进程,然后进行数据库数据的一致性恢复,等待恢复完成后,数据库又可以接受新的连接了.
PG9.5之前的postmaster进程实际上就是PG9.5及其以后版本中第一个启动的postgres进程,该进程的ospid(操作系统进程的进程号)在$PGDATA/下的postmaster.pid中有记录.
通过执行带有start选项的pg_ctl程序,postgres服务器进程将启动。然后,它在内存中分配一个共享内存区域,启动各种background进程,必要时启动与复制相关的进程和background workers进程,并等待客户机的连接请求。每当收到来自客户机的连接请求时,它就启动backend进程。(然后,启动的backend进程处理连接的客户端发出的所有查询。)
1 2 3 4 5 | [root@lhrpg /]# ps -ef|grep postgres postgres 14092 1 0 09:46 ? 00:00:00 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/ [pg13@lhrrepmgr64361 ~]$ ps -ef|grep postgres pg13 11347 0 0 Apr29 ? 00:00:19 /pg13/pg13/bin/postgres -D /pg13/pgdata |
Backend 进程
backend进程也称为postgres,由postgres服务器进程启动,并处理由一个连接的客户机发出的所有查询。它通过一个TCP连接与客户机通信,并在客户机断开连接时终止。
因为它只允许操作一个数据库,所以在连接到PostgreSQL服务器时,必须明确指定想要使用的数据库。
允许多个客户端同时连接;配置参数max_connections控制客户机的最大数量(默认为100)。
如果许多客户端(如WEB应用程序)频繁地重复连接和断开与PostgreSQL服务器的连接,这会增加建立连接和创建后端进程的成本,因为PostgreSQL没有实现本机连接池特性。这种情况会对数据库服务器的性能产生负面影响。为了处理这种情况,通常使用池中间件(pgbouncer或pgpool-II)。
1 2 3 | [root@lhrpg /]# ps -ef|grep postgres postgres 14121 14092 0 09:47 ? 00:00:00 postgres: postgres postgres 172.17.0.1(14877) idle postgres 14139 14092 0 09:47 ? 00:00:00 postgres: postgres postgres 172.17.0.1(14911) idle |
Background 进程
1 2 3 4 5 6 7 8 9 | [root@lhrpg /]# ps -ef|grep postgres postgres 14093 14092 0 09:46 ? 00:00:00 postgres: logger postgres 14095 14092 0 09:46 ? 00:00:00 postgres: checkpointer postgres 14096 14092 0 09:46 ? 00:00:00 postgres: background writer postgres 14097 14092 0 09:46 ? 00:00:00 postgres: walwriter postgres 14098 14092 0 09:46 ? 00:00:00 postgres: autovacuum launcher postgres 14099 14092 0 09:46 ? 00:00:00 postgres: archiver postgres 14100 14092 0 09:46 ? 00:00:00 postgres: stats collector postgres 14101 14092 0 09:46 ? 00:00:00 postgres: logical replication launcher |
1、background writer:后台写进程
在这个过程中,共享缓冲池中的脏页会被逐步地定期写入持久存储(例如,HDD、SSD)。把共享内存中的脏页写到磁盘上的进程,主要是为了提高插入、更新和删除数据的性能。Bgwriter是一个Background Writer进程的简称。Bgwriter会将缓存中一些已经更改的数据(脏缓冲区)写入到磁盘。checkpoint也是会将缓存中的脏数据写入到磁盘。
background writer的作用是什么?
提高了缓存的替换速度,提高了数据查询性能。因为数据库在进行查询处理时,若发现要读取的数据不在缓冲区要先从磁盘中读入该页,这时如果缓冲区已满,就需要先选择一些缓冲区中的页面替换出去。如果要被替换的页被修改了,则必须先将这个页面读出到磁盘才能替换,这样数据库的查询就会被阻塞在这里。通过bgwriter定期的写出缓冲区的部分页面,就可以为缓冲区腾出空间。就可以防止这一情况。
同时,因为bgwriter预先写出了一些脏页面,可以减少checkpoint时要进行的io操作,使系统的IO负载趋于平稳。
那么checkpoint与Bgwriter的区别是什么?
checkpoint是以特定的时间间隔刷新所有脏页,并创建一个用于数据库恢复用的检查点。
而Bgwriter是在检查点之间刷新一些脏页面,以便始终有足够多的干净页面可以使用。
两者的目的和执行频率都有不同。
2、walwriter:预写式日志进程
Write Ahead Log (预写式日志),在修改数据之前把修改操作记录到磁盘中,以便后面更新实时数据时就不需要数据持久化到文件中。该进程定期将WAL缓冲区中的WAL数据写入并刷新到持久存储中。
3、archiver:归档进程
WAL日志会被循环使用,archiver在归档前会把WAL日志备份出来。通过PITY ( Pointin Time Recovery )技术,可以对数据库进行一次全量备份后,该技术将备份时间点之后的WAL日志通过归档进行备份,使用数据库的全量备份再加上后面产生的WAL日志,即可把数据库向前推到全量备份后的任意一个时间点。
只有开启归档模式后,才会有该进程,开启方式如下:
1 2 3 4 | alter system set archive_mode='on'; alter system set archive_command='cp %p /opt/pgsql/archive_wals/%f'; systemctl restart postgresql-12.service |
4、autovacuum launcher:系统自动清理进程
在PostgreSQL数据库中,对表进行DELETE操作后,旧的数据并不会立即被删除,并且,在更新数据时,也并不会在旧的数据上做更新,而是新生成一行数据。旧的数据只是被标识为删除状态,只有在没有并发的其他事务读到这些就数据时,它们才会被清除。这个清除工作就由AutoVacuum进程完成。
5、stats collector:统计收集进程
做数据的统计收集工作。主要用于查询优化时的代价估算,包括一个表和索引进行了多少次的插入、更新、删除操作,磁盘块读写的次数、行的读次数。pg_statistic中存储了PgStat收集的各类信息。在这个过程中,会收集诸如pg_stat_activity和pg_stat_database等统计信息。
6、checkpointer:检查点进程
checkpoint又名检查点,一般checkpoint会将某个时间点之前的脏数据全部刷新到磁盘,以实现数据的一致性与完整性。目前各个流行的关系型数据库都具备checkpoint功能,其主要目的是为了缩短崩溃恢复时间,以Oracle为例,在进行数据恢复时,会以最近的checkpoint为参考点执行事务前滚。而在WAL机制的浅析中,也提过PostgreSQL在崩溃恢复时会以最近的checkpoint为基础,不断应用这之后的WAL日志。
7、logger :系统日志进程
需要将postgresql.conf中的logging_collector设置为on ,此时主进程才会启动Syslogger辅助进程。默认为off。
1 2 3 4 | [root@lhrpg /]# more /var/lib/pgsql/12/data/postgresql.conf | grep logging_coll # requires logging_collector to be on. logging_collector = on # Enable capturing of stderr and csvlog # These are only used if logging_collector is on: |