温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

通过linux命令来将postgresql杀死有什么影响

发布时间:2021-11-26 09:11:46 来源:亿速云 阅读:314 作者:小新 栏目:大数据

这篇文章给大家分享的是有关通过linux命令来将postgresql杀死有什么影响的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

在数据库的服务器上通过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

通过linux命令来将postgresql杀死有什么影响

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

通过linux命令来将postgresql杀死有什么影响

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

1 Abrt  异常终止信号

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

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

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

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

6 用户调用定义

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

通过linux命令来将postgresql杀死有什么影响

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

通过linux命令来将postgresql杀死有什么影响

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

WARNING:  terminating connection because of crash of another server process

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

通过linux命令来将postgresql杀死有什么影响

那马上看到这里有人就会产生一个问题,那我怎么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 掉这个用户的连接。

通过linux命令来将postgresql杀死有什么影响

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

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

感谢各位的阅读!关于“通过linux命令来将postgresql杀死有什么影响”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI