前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PostgreSQL KILL -9 用户连接进程 , OMG 胆真肥

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

作者头像
AustinDatabases
发布2019-12-17 15:21:51
1.1K0
发布2019-12-17 15:21:51
举报
文章被收录于专栏:AustinDatabasesAustinDatabases
在数据库的服务器上通过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

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

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

1 Abrt 异常终止信号

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

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

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

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

6 用户调用定义

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

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

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

WARNING: terminating connection because of crash of another server process

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

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

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

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AustinDatabases 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档