博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pg_cancel_backend()和pg_terminate_backend()
阅读量:5960 次
发布时间:2019-06-19

本文共 2518 字,大约阅读时间需要 8 分钟。

pg_cancel_backend()和pg_terminate_backend() 


两个函数的官方解释: 

pg_cancel_backend() 取消后台操作,回滚未提交事物 

pg_terminate_backend() 中断session,回滚未提交事物 


pg_cancel_backend()举例: 

session A: 

postgres=# create table t1 (a int); 

CREATE TABLE 

postgres=# begin; 

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


session B: 

tina=# select datname,pid, query from pg_stat_activity; 

datname  | pid  |                        query                        

----------+------+----------------------------------------------------- 

postgres | 1923 | insert into t2 select generate_series(1,100000000); 

tina     | 1922 | select datname,pid, query from pg_stat_activity; 

(2 rows) 


tina=# select pg_cancel_backend(1923); 

pg_cancel_backend 

------------------- 


(1 row) 


session A: 

STATEMENT:  insert into t2 select generate_series(1,100000000); 

ERROR:  canceling statement due to user request 

postgres=# commit; 

ROLLBACK 

postgres=# select * from t2; 


--- 

(0 rows) 


session B: 

tina=#  select datname,pid, query from pg_stat_activity; 

datname  | pid  |                      query                       

----------+------+-------------------------------------------------- 

postgres | 1923 | commit; 

tina     | 1922 | select datname,pid, query from pg_stat_activity; 



pg_terminate_backend() 举例: 

session A: 

postgres=# create table t2 (a int); 

CREATE TABLE 

postgres=# begin; 

BEGIN 

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


session B: 

tina=# select datname,pid, query from pg_stat_activity; 

datname  | pid  |  query                        

---------+------+------------- 

postgres | 1874 | insert into t2 select generate_series(1,100000000); 

tina     | 1914 | select * from pg_stat_activity; 

(2 rows) 


postgres=# select pg_terminate_backend(1874); 

pg_terminate_backend 

---------------------- 

t (1 row) 



session A: 

FATAL:  terminating connection due to administrator command 

STATEMENT:  insert into t2 select generate_series(1,100000000); 

FATAL:  terminating connection due to administrator 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. 

postgres=# commit; 

WARNING:  there is no transaction in progress 

WARNING:  there is no transaction in progress 

COMMIT 

postgres=# select * from t2; 


--- 

(0 rows) 


session B: 

tina=# select datname,pid, query from pg_stat_activity; 

datname  | pid  |  query                        

---------+------+------------- 

tina     | 1914 | select * from pg_stat_activity; 

(1 rows) 



在pg_cancel_backend()下,session还在,事物回退; 

在pg_terminate_backend()操作后,session消失,事物回退。 


如果在某些时候pg_terminate_backend()不能杀死session,那么可以在os层面,直接kill -9 pid

转载地址:http://wgyax.baihongyu.com/

你可能感兴趣的文章
3D地图的定时高亮和点击事件(基于echarts)
查看>>
mysql开启binlog
查看>>
设置Eclipse编码方式
查看>>
分布式系统唯一ID生成方案汇总【转】
查看>>
并查集hdu1232
查看>>
Mysql 监视工具
查看>>
从前后端分离到GraphQL,携程如何用Node实现?\n
查看>>
Linux Namespace系列(09):利用Namespace创建一个简单可用的容器
查看>>
博客搬家了
查看>>
Python中使用ElementTree解析xml
查看>>
jquery 操作iframe、frameset
查看>>
解决vim中不能使用小键盘
查看>>
jenkins权限管理,实现不同用户组显示对应视图views中不同的jobs
查看>>
我的友情链接
查看>>
CentOS定时同步系统时间
查看>>
批量删除用户--Shell脚本
查看>>
Eclipse Java @Override 报错
查看>>
知道双字节码, 如何获取汉字 - 回复 "pinezhou" 的问题
查看>>
linux中cacti和nagios整合
查看>>
Python高效编程技巧
查看>>