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
相关文章
|
7月前
|
SQL 关系型数据库 MySQL
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
178 0
|
7月前
|
数据库
Greenplum【部署 09】has an active database process on port = 6000 和 [Errno 2] No such file or directory
Greenplum【部署 09】has an active database process on port = 6000 和 [Errno 2] No such file or directory
117 0
|
关系型数据库 数据库 PostgreSQL
pg_ctl: server did not start in time
postgresql 启动超时异常,可怎么解决
pg_ctl: server did not start in time
|
关系型数据库 MySQL
MySQL里Wating for Slave workers to free pending events到底在等什么
MySQL里Wating for Slave workers to free pending events到底在等什么
202 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的持续归档文件备份。 全量备份通常
1595 0
|
弹性计算 关系型数据库 数据库连接
PostgreSQL 12 preview - Move max_wal_senders out of max_connections for connection slot handling
标签 PostgreSQL , max_wal_senders , max_connections , sorry, too many clients already 背景 如果你需要使用PG的流复制,上游节点的max_wal_senders参数,用来限制这个节点同时最多可以有多少个wal sender进程。 包括逻辑复制、物理复制、pg_basebackup备份等,只要是使用stre
376 0
|
Windows 关系型数据库 PostgreSQL