• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

pg_cancel_backend() 与pg_terminate_backend()

mysql 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

先看下两个函数的官方解释: pg_cancel_backend() 取消后台操作,回滚未提交事物 pg_terminate_backend() 中断session,回滚未提交事物 这里和oracle类似kill session的操作是pg_terminate_backend() pg_cancel_backend() 举例: session A: postgres=# creat

先看下两个函数的官方解释:

pg_cancel_backend() 取消后台操作,回滚未提交事物 pg_terminate_backend() 中断session,回滚未提交事物 这里和oracle类似kill session的操作是pg_terminate_backend() pg_cancel_backend() 举例:


session A:

postgres=# create table tb1 (a int); CREATE TABLE postgres=# begin;

postgres=# insert into tb1 select generate_series(1,100000000);

session B:

postgres=# select * from pg_stat_activity; datid | datname | procpid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | waiting | current_query ——-+———-+———+———-+———-+——————+————-+—————–+————-+——————————-+——————– ———–+——————————-+———+—————————————————— 12699 | postgres | 10305 | 10 | postgres | psql | | | -1 | 2013-12-03 14:38:05.887116+08 | 2013-12-03 15:12:17 .773935+08 | 2013-12-03 15:14:31.454816+08 | f | insert into tb1 select generate_series(1,100000000);

postgres=# select pg_cancel_backend(10305); pg_cancel_backend ——————- t

session A: ERROR: canceling statement due to user request postgres=# commit; ROLLBACK postgres=# select * from tb1; a — (0 rows)

session B:

postgres=# select * from pg_stat_activity; datid | datname | procpid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | waiting | current_query ——-+———-+———+———-+———-+——————+————-+—————–+————-+——————————-+——————– ———–+——————————-+———+——————————— 12699本文来源[email protected]搞@^&代*@码)网9 | postgres | 10305 | 10 | postgres | psql | | | -1 | 2013-12-03 14:38:05.887116+08 |

pg_terminate_backend() 举例:


session A:

postgres=# create table tb2 (a int); CREATE TABLE postgres=# begin; BEGIN postgres=# insert into tb2 select generate_series(1,100000000);

session B:

postgres=# select * from pg_stat_activity; datid | datname | procpid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | waiting | current_query ——-+———-+———+———-+———-+——————+————-+—————–+————-+——————————-+——————– ———–+——————————-+———+—————————————————— 12699 | postgres | 10305 | 10 | postgres | psql | | | -1 | 2013-12-03 14:38:05.887116+08 | 2013-12-03 15:24:22 .45073+08 | 2013-12-03 15:24:33.362185+08 | f | insert into tb2 select generate_series(1,100000000);

postgres=# select pg_terminate_backend(10305); pg_terminate_backend ———————- t (1 row)

session A:

postgres=# insert into tb2 select generate_series(1,100000000); FATAL: terminating connection due to administrator command FATAL: terminating connection due to administrator command The connection to the server was lost. Attempting reset: Succeeded. postgres=# commit; WARNING: there is no transaction in progress COMMIT postgres=# select * from tb2; a — (0 rows)

postgres=# select * from pg_stat_activity;

datid | datname | procpid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | waiting | current_query ——-+———-+———+———-+———-+——————+————-+—————–+————-+——————————-+——————– ———–+——————————-+———+——————————— 12699 | postgres | 10918 | 10 | postgres | psql | | | -1 | 2013-12-03 15:25:12.613672+08 | 2013-12-03 15:30:29 .544088+08 | 2013-12-03 15:30:29.544088+08 | f | select * from pg_stat_activity;

通过以上实验理解起来应该 很简单了,procpid=10305在pg_cancel_backend()下,session还在,事物回退,在pg_terminate_backend()操作后,session消失,事物回退。如果在某些时候pg_terminate_backend()不能杀死session,那么可以在os层面,直接kill -9 pid


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:pg_cancel_backend() 与pg_terminate_backend()

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址