kill -9 a postgres process

  在postgresql中,不推荐使用Kill -9直接杀掉异常连接,因为直接使用kill -9会引起整个数据库核心进程的重启,同时其他正常程序连接也会被杀掉。

  现开启两个psql连接,然后使用Kill -9杀掉其中一个:

session1: 

swrd=# select pg_backend_pid();
 pg_backend_pid 
----------------
          26965
(1 row)

swrd=# select pg_backend_pid();
WARNING:  terminating connection because of crash of another server process
DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, 
because another server process exited abnormally and possibly corrupted shared memory. HINT: In a moment you should be able to reconnect to the database and repeat your command. server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Succeeded. swrd=# select pg_backend_pid(); pg_backend_pid ---------------- 833 (1 row)
session2:

postgres=# select pg_backend_pid();
 pg_backend_pid 
----------------
          25975
(1 row)

postgres=# select pg_backend_pid();
server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.
postgres=# select pg_backend_pid();
 pg_backend_pid 
----------------
            835
(1 row)

观察上面的日志信息,可以看到session2是被杀掉的会话,但同时session1也被杀掉了。

下面是有关postgre的进程信息,发现除了守护进程和logger进程外,其他进程都重启了。

[root@db~]# ps auxwwf |grep postgres
root     25657  0.0  0.0 163400  1976 pts/0    S    15:51   0:00  |       \_ su - postgres
postgres 25658  0.0  0.0 108484  1900 pts/0    S    15:51   0:00  |           \_ -bash
postgres 25704  0.0  0.0 173472  3164 pts/0    S+   15:51   0:00  |               \_ psql
root     14932  0.0  0.0 163400  1976 pts/2    S    11:29   0:00  |       \_ su - postgres
postgres 14933  0.0  0.0 108484  1992 pts/2    S    11:29   0:00  |           \_ -bash
postgres 25721  0.0  0.0 173472  3060 pts/2    S+   15:52   0:00  |               \_ psql
root       752  0.0  0.0 103316   824 pts/4    S+   18:47   0:00          \_ grep postgres
postgres  4966  0.0  0.0 184692  4060 ?        S     2017   0:19 /opt/pg96/bin/postgres
postgres  4978  0.0  0.0 182588  1412 ?        Ss    2017   0:00  \_ postgres: logger process   
postgres 25914  0.0  0.0 184812  1940 ?        Ss   15:52   0:00  \_ postgres: checkpointer process   
postgres 25915  0.0  0.0 184692  1644 ?        Ss   15:52   0:00  \_ postgres: writer process   
postgres 25916  0.0  0.0 184692  1624 ?        Ss   15:52   0:00  \_ postgres: wal writer process   
postgres 25917  0.0  0.0 185128  2348 ?        Ss   15:52   0:00  \_ postgres: autovacuum launcher process   
postgres 25918  0.0  0.0 184684  1340 ?        Ss   15:52   0:00  \_ postgres: archiver process   
postgres 25919  0.0  0.0 184828  1580 ?        Ss   15:52   0:00  \_ postgres: stats collector process   
postgres 25975  0.0  0.0 185620  3684 ?        Ss   15:52   0:00  \_ postgres: postgres postgres [local] idle
postgres 26965  0.0  0.0 188044  7224 ?        Ss   16:15   0:00  \_ postgres: swrd swrd [local] idle
[root@db~]# kill -9 25975
[root@db~]# ps auxwwf |grep postgres
root     25657  0.0  0.0 163400  1976 pts/0    S    15:51   0:00  |       \_ su - postgres
postgres 25658  0.0  0.0 108484  1900 pts/0    S    15:51   0:00  |           \_ -bash
postgres 25704  0.0  0.0 173472  3164 pts/0    S+   15:51   0:00  |               \_ psql
root     14932  0.0  0.0 163400  1976 pts/2    S    11:29   0:00  |       \_ su - postgres
postgres 14933  0.0  0.0 108484  1992 pts/2    S    11:29   0:00  |           \_ -bash
postgres 25721  0.0  0.0 173472  3060 pts/2    S+   15:52   0:00  |               \_ psql
root       820  0.0  0.0 103316   824 pts/4    S+   18:48   0:00          \_ grep postgres
postgres  4966  0.0  0.0 184692  4060 ?        S     2017   0:19 /opt/pg96/bin/postgres
postgres  4978  0.0  0.0 182588  1412 ?        Ss    2017   0:00  \_ postgres: logger process   
postgres   813  0.0  0.0 184692  1536 ?        Ss   18:48   0:00  \_ postgres: checkpointer process   
postgres   814  0.0  0.0 184692  1544 ?        Ss   18:48   0:00  \_ postgres: writer process   
postgres   815  0.0  0.0 184692  1528 ?        Ss   18:48   0:00  \_ postgres: wal writer process   
postgres   816  0.0  0.0 185128  2264 ?        Ss   18:48   0:00  \_ postgres: autovacuum launcher process   
postgres   817  0.0  0.0 184684  1328 ?        Ss   18:48   0:00  \_ postgres: archiver process   
postgres   818  0.0  0.0 184684  1440 ?        Ss   18:48   0:00  \_ postgres: stats collector process 

 

  postgres之所以这么做是因为使用kill -9杀掉的进程,是没有机会做好清理工作的,因为kill -9是SIGKILL信号,程序不属于正常退出,杀掉的进程可能由于正在进行写入或修改的操作,从而造成内存页的损坏,为了保证数据库的正常故会重启内部进程。

  如果不使用kill -9,而使用普通的kill操作是不会引起postgres内部进程的重启的。对于中断操作或杀掉会话,建议使用pg自带的pg_cancel_backend()和pg_terminate_backend()。

 

参考:

https://serverfault.com/questions/415188/kill-9-a-postgres-process

posted on 2018-01-08 19:34  Still water run deep  阅读(758)  评论(0编辑  收藏  举报

导航