pg_cancel_backend() 与pg_terminate_backend()

原创 2013年12月05日 15:02:04
先看下两个函数的官方解释:
pg_cancel_backend() 与pg_terminate_backend() - dazuiba_008 - 魂醉的一亩二分地
 
pg_cancel_backend() 与pg_terminate_backend() - dazuiba_008 - 魂醉的一亩二分地
 
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 | 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

相关文章推荐

解决在安装openstack swift过程中遇到的"liberasurecode[XX]: liberasurecode_backend_open: dynamic linking error"

解决在安装openstack swift过程中遇到的"liberasurecode[XX]: liberasurecode_backend_open: dynamic linking error"...

postgresql src/backend/storage/file/fd.c 源代码解读 (jung)

/*------------------------------------------------------------------------- * * fd.c * Virtual ...

【SystemTap】c独立安装elfutils导致错误 cannot load ld backend library 'libld_elf_x86_64.so' 和 error: C compiler cannot create executables

场景:        运行程序时出现了一系列的错误,例如:(1)编译内核执行命令 make menuconfig 命令时出现如下错误:/usr/local/bin/ld: cannot load ld...
  • zklth
  • zklth
  • 2011年03月14日 15:35
  • 2403

keras的backend 设置 tensorflow,theano

win7 系统环境安装步骤: 1.首先是安装Python,建议安装anaconda 2.安装完anaconda后打开anaconda promp命令行promp,输入conda list. 可以看到...

Post Json Data by Ajax, and Parse the Json Data in backend

前几天,项目需要在后台接受Posted json data。于是我做了一个简单的json发送页面如下:页面上: JS处理:$.ajaxSetup({ cache: f...

libevent源码分析:backend的选择

libevent提供了7种io后端(eventop)/*io后端*/ struct eventop { /*名字*/ const char *name; /* 使用前通...
  • xtchina
  • xtchina
  • 2016年07月10日 17:52
  • 757

backend design flow

  • 2011年09月23日 10:46
  • 2.27MB
  • 下载

[OpenStack Cinder] 配置multi-backend 存储卷及其Tempest测试

Multi-backend这个功能是从grizzly 开始引入过来的, 它能使得Cinder同时能够配置多个后端,让cinder-scheduler根据用户需求灵活选择相应的存储. 当配置三个b...
  • juvxiao
  • juvxiao
  • 2014年03月26日 09:33
  • 4520

Backend Attribute Exchange

  • 2010年09月08日 10:08
  • 883KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:pg_cancel_backend() 与pg_terminate_backend()
举报原因:
原因补充:

(最多只允许输入30个字)