pg_cancel_backend()和pg_terminate_backend()

简介:
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
相关文章
|
缓存 关系型数据库
PG的synchronous_commit
PG的synchronous_commit
267 0
PG14中的idle_session_timeout
PG14中的idle_session_timeout
158 0
|
关系型数据库 数据库 PostgreSQL
pg_stat_statements 插件
pg_stat_statements 插件
177 0
|
关系型数据库 MySQL
MySQL里Wating for Slave workers to free pending events到底在等什么
MySQL里Wating for Slave workers to free pending events到底在等什么
211 0
|
SQL 弹性计算 关系型数据库
为什么pg_basebackup或pg_start_backup好像hang住确没有开始拷贝文件 - checkpoint 的几种调度(checkpoint_completion_target)
标签 PostgreSQL , checkpoint , 调度 , lazy , immediate , pg_start_backup , pg_basebackup 背景 PostgreSQL支持在线全量备份与增量归档备份。在线全量备份实际上就是拷贝文件,增量备份则分为两种,一种是基于BLOCK lsn变化的BLOCK即增量备份,另一种是基于WAL的持续归档文件备份。 全量备份通常
1612 0
|
SQL 监控 安全
PostgreSQL 谁堵塞了谁(锁等待检测)- pg_blocking_pids, pg_safe_snapshot_blocking_pids
PostgreSQL 谁堵塞了谁(锁等待检测)- pg_blocking_pids, pg_safe_snapshot_blocking_pids
1980 0
|
关系型数据库
replication crash safe
什么是主从复制的replication crash safe? 参数master_info_repository有两个值: FILE (对应的文件master.info),  or TABLE (对应的表mysql.
763 0
|
SQL 存储
SQL Server等待事件—RESOURCE_SEMAPHORE_QUERY_COMPILE
原文:SQL Server等待事件—RESOURCE_SEMAPHORE_QUERY_COMPILE 等待事件介绍   关于等待事件RESOURCE_SEMAPHORE_QUERY_COMPILE,官方的介绍如下:          Occurs when the number of con...
904 0