Postgres進程之間如何協(xié)作的?

昨天閱讀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.

讓我產(chǎn)生疑惑的主要是后半句:
that server processes will have to issue writes for themselves, delaying interactive queries.
其大意為,當shared buffer 滿的時候,server process 會自己處理 dirty buffer的問題。

同時文檔對參數(shù)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. *
大意為如果該參數(shù)值設置的較小,則會導致server process 自己完成寫 dirty buffer 的過程。

關于server process 寫 dirty buffer,我突然回想到:除了bgwriter 、checkpoint進程會寫 dirty buffer外,backend process (也就是這里的 server process)也會寫 dirty buffer,只不過僅僅當buffer不夠使用的時候,backend 進程才會寫 dirty buffer。


在這里,發(fā)下自己對進程結(jié)構的理論知識掌握不夠到位,于是乎,今天從頭到位,簡述下PG9.5.3的進程體系吧。

Postgres有哪些后臺進程?

postgres@pgdb-> ps -ef|grep postgres
postgres   1874      1  0 03:04 ?        00:00:00 /home/postgres/pgsql9.5.3/bin/postmaster -D /home/postgres/pgdata

第一個為主進程,也就是PG的監(jiān)聽進程,同時也是所有進程的父進程。
也就說pg啟動的時候,先啟動父進程,然后在fork出下面這些子進程:

可以看出其父進程都是 1874進程號代表的postmaster
postgres   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 - postgres
postgres   2110   2109  0 03:06 pts/0    00:00:00 -bash
postgres   2186   2110  0 03:28 pts/0    00:00:00 ps -ef
postgres   2187   2110  0 03:28 pts/0    00:00:00 grep postgres
postgres@pgdb-> 

我這里的pg端口為1921,netstat 1921,可以發(fā)現(xiàn),
postmaster 進程正在占用1921端口,也就是說該進程就是PG的監(jiān)聽進程。
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進程結(jié)構圖解

Paste_Image.png

大概說說這些進程是如何協(xié)作的:

  • 首先,postmaster進程監(jiān)聽來自客戶端的請求,也就是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 - postgres
postgres   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 -ef
postgres   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.itdecent.cn/p/41d8b57a271a

  • autocacuum相關進程就是清理作用,這里不做詳細概述,以后會對著這些進程具體是如何工作的,進行深入摸索。


                            Francis He
                             2017.6.29
                               濟南
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容