Postgres进程之间如何协作的?

来源:互联网 发布:json lib apache 编辑:程序博客网 时间:2024/06/14 08:19

昨天阅读Postgres9.5.3官方文档时,有一句话让我不是很理解:

Smaller values of bgwriter_lru_maxpages and bgwriter_lru_multiplier reduce the extra I/O load caused by the background writer, but make it more likely that server processes will have to issue writes for themselves, delaying interactive queries.

让我产生疑惑的主要是后半句:
that server processes will have to issue writes for themselves, delaying interactive queries.
其大意为,当shared buffer 满的时候,server process 会自己处理 dirty buffer的问题。

同时文档对参数bgwriter_lru_multiplier (floating point),也有如下描述:
Larger values provide some cushion against spikes in demand, while smaller values intentionally leave writes to be done by server processes. 
大意为如果该参数值设置的较小,则会导致server process 自己完成写 dirty buffer 的过程。

关于server process 写 dirty buffer,我突然回想到:除了bgwriter 、checkpoint进程会写 dirty buffer外,backend process (也就是这里的 server process)也会写 dirty buffer,只不过仅仅当buffer不够使用的时候,backend 进程才会写 dirty buffer。


在这里,发下自己对进程结构的理论知识掌握不够到位,于是乎,今天从头到位,简述下PG9.5.3的进程体系吧。

Postgres有哪些后台进程?

postgres@pgdb-> ps -ef|grep postgrespostgres   1874      1  0 03:04 ?        00:00:00 /home/postgres/pgsql9.5.3/bin/postmaster -D /home/postgres/pgdata第一个为主进程,也就是PG的监听进程,同时也是所有进程的父进程。也就说pg启动的时候,先启动父进程,然后在fork出下面这些子进程:可以看出其父进程都是 1874进程号代表的postmasterpostgres   1928   1874  0 03:04 ?        00:00:00 postgres: logger process                                         postgres   1930   1874  0 03:04 ?        00:00:00 postgres: checkpointer process                                   postgres   1931   1874  0 03:04 ?        00:00:01 postgres: writer process                                         postgres   1932   1874  0 03:04 ?        00:00:00 postgres: wal writer process                                     postgres   1933   1874  0 03:04 ?        00:00:00 postgres: autovacuum launcher process                            postgres   1934   1874  0 03:04 ?        00:00:00 postgres: stats collector process                                root       2109   2089  0 03:06 pts/0    00:00:00 su - postgrespostgres   2110   2109  0 03:06 pts/0    00:00:00 -bashpostgres   2186   2110  0 03:28 pts/0    00:00:00 ps -efpostgres   2187   2110  0 03:28 pts/0    00:00:00 grep postgrespostgres@pgdb-> 我这里的pg端口为1921,netstat 1921,可以发现,postmaster 进程正在占用1921端口,也就是说该进程就是PG的监听进程。postgres@pgdb-> netstat -anp|grep 1921(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)tcp        0      0 0.0.0.0:1921                0.0.0.0:*                   LISTEN      1874/postmaster     unix  2      [ ACC ]     STREAM     LISTENING     12887  1874/postmaster     /tmp/.s.PGSQL.1921

Postgres进程结构图解


Paste_Image.png

大概说说这些进程是如何协作的:

  • 首先,postmaster进程监听来自客户端的请求,也就是1
  • 成功处理请求后,会fork出相应的进程来与该客户端建立连接,也就是 backend process。这时来自客户端的各种sql,各种请求都由这个 backend process 来完成。如下所示(用**号标识):

    postgres   1934   1874  0 03:04 ?        00:00:00 postgres: stats collector process                                root       2109   2089  0 03:06 pts/0    00:00:00 su - postgrespostgres   2110   2109  0 03:06 pts/0    00:00:00 -bash**postgres   2298   1874  0 04:02 ?        00:00:00 postgres: postgres postgres 192.168.100.1(11696) idle **postgres   2300   2110  0 04:02 pts/0    00:00:00 ps -efpostgres   2301   2110  0 04:02 pts/0    00:00:00 grep postgres
  • 在处理请求期间,如果 shard buffer里的buffer不够用,那么backend process 就会 flush 一部分 dirty buffer 也就是3 。

  • 同理,wal buffer 满了的时候,backend process 也会将一部分buffer flush 到 xlog里。

  • 其余进程就很简单了, bgwriter 就是将dirty buffer 写入 datafile,
    archiver就是将xlog做归档。

    防盗链接:http://www.jianshu.com/p/41d8b57a271a

  • autocacuum相关进程就是清理作用,这里不做详细概述,以后会对着这些进程具体是如何工作的,进行深入摸索。


                            Francis He                             2017.6.29                               济南
原创粉丝点击