0
点赞
收藏
分享

微信扫一扫

PostgreSQL KILL -9 用户连接进程 , OMG 胆真肥



PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_数据库

在数据库的服务器上通过linux 命令来将postgresql 杀死 例如 kill -9  xxxx,这样做到底会对postgresql 产生什么影响,不少人都曾经这样干过,为什么说这样做,糟糕透了。


首先我们需要理解一下postgresql 在 linux中的运行机制,system call and fork() system call ,其中

是system call


postgres 102414      1  0 08:34 ?        00:00:00 /usr/local/postgres/bin/postgres -D /pgdata/data


在下边的这些就为 fork() sytem call

postgres 102415 102414  0 08:34 ?        00:00:00 postgres: logger   

postgres 102417 102414  0 08:34 ?        00:00:00 postgres: checkpointer   

postgres 102418 102414  0 08:34 ?        00:00:00 postgres: background writer   

postgres 102419 102414  0 08:34 ?        00:00:00 postgres: walwriter   

postgres 102420 102414  0 08:34 ?        00:00:00 postgres: autovacuum launcher   

postgres 102421 102414  0 08:34 ?        00:00:00 postgres: archiver   

postgres 102422 102414  0 08:34 ?        00:00:01 postgres: stats collector   

postgres 102423 102414  0 08:34 ?        00:00:00 postgres: logical replication launcher 



postgres 102415 102414  0 08:34 ?        00:00:00 postgres: logger   

postgres 102417 102414  0 08:34 ?        00:00:00 postgres: checkpointer   

postgres 102418 102414  0 08:34 ?        00:00:00 postgres: background writer   

postgres 102419 102414  0 08:34 ?        00:00:00 postgres: walwriter   

postgres 102420 102414  0 08:34 ?        00:00:00 postgres: autovacuum launcher   

postgres 102421 102414  0 08:34 ?        00:00:00 postgres: archiver   

postgres 102422 102414  0 08:34 ?        00:00:01 postgres: stats collector   

postgres 102423 102414  0 08:34 ?        00:00:00 postgres: logical replication launcher 


上面的通过调用fork()来创建一个新进程。此系统调用复制当前进程,在进程表中创建一个具有许多与当前进程相同属性的新条目。新创建的进程将是调用进程的子进程


所以父进程为pid 字进程为ppid

PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_数据库_02


当然也可以通过 pstree 来查看当前主线程之间的从属关系

PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_postgresql_03



PostgreSQL 调用LINUX 操作系统的信号种类有以下几种


1 Abrt  异常终止信号

 2 int  外部中断,类似于Ctrl +C 的结果

3 quit  直接清理正在运行的线程,并且对临时产生的文件不清理

4  term  这个命令就是就是我们熟悉的 kill  命令

5  hup   重新初始化线程的命令

6 用户调用定义


下面我们做一个test , 看看我们要是kill -9 一个用户连接会有什么问题


PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_postgresql_04

下面是在kill 当前连接到数据库的用户process 后的日志

PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_数据库_05


如果图看不清我把关键的一句粘一下

WARNING:  terminating connection because of crash of another server process


我们在对比一下kill 用户连接的进程后的,database 的那些子进程的数字,倒吸一口凉气了吧。


PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_postgresql_06


那马上看到这里有人就会产生一个问题,那我怎么kill掉 那个 白占着资源,不干活的用户。

(问题答案将在文末给出)


那么我们翻过来的看一下,在kill -9 一个用户连接的process后会发生什么


1 用户的连接process 被kill -9 干掉2

2 终止任何其他活跃的服务器进程

3 警告因为你的这项操作会导致服务器postgresql其他的进程crash

4 所有的服务器的postgresql processes 重新初始化

也就是日志的这几句话

database system was not properly shut down; automatic recovery in progress

redo starts at 5/1C000098

invalid record length at 5/1C000108: wanted 24, got 0

redo done at 5/1C0000D0

database system is ready to accept connections


OK 到这里你还敢随便 kill -9 人家用户的连接,尤其是几百G 或上T 的大库,Are you crazy ?


那到底正确的做法是什么


pg_terminate_backend 命令在数据库里面去KILL 掉这个用户的连接。

PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_数据库_07

而且不会对数据库产生任何的危害。(见上图)


所以你还在天天的kill -9  用户的process ,建议你住手。

PostgreSQL  KILL -9  用户连接进程 ,  OMG 胆真肥_服务器_08


举报

相关推荐

0 条评论