PostgreSql9.6 新增加“idle in transaction”超时空闲事务自动查杀功能

熟悉 PostgreSQL 的朋友应该知道 “idle in transaction” 进程,引发 idle in transaction 的原因很多,例如应用代码中忘记关闭已开启的事务,或者系统中存在僵死进程等,曾经看到过某个库中的 idle in transaction 进程存在一年有余,这类进程严重危害了数据库的安全,例如它会阻止 VACUUM 进程回收记录,造成表数据膨胀,同时它有可能引起整个 PostgreSQL 数据库 Transaction ID Wraparound 的风险。

Allow sessions to be terminated automatically if they sit too long in
an idle-in-transaction state (Vik Fearing) This behavior is enabled
and controlled by the new configuration parameter
idle_in_transaction_session_timeout. It can be useful to prevent
forgotten transactions from holding onto locks or preventing vacuum
cleanup for very long periods.

以上出自 PostgreSQL9.6 Beta1 发行说明,这段指出9.6版本 PostgreSQL 支持自动查杀超过指定时间的 idle in transaction 空闲事务连接,下面演示下。

--修改 postgresql.conf 以下参数

idle_in_transaction_session_timeout = 20000

备注:参数单位为毫秒,这里设置 idle in transaction 超时空闲事务时间为 20 秒。

--重载配置文件

[pg96@db1 pg_root]$ pg_ctl reload
server signaled

备注:此参数修改后对当前连接依然生效,应用不需要重连即能生效。

--开启会话一:模拟一个事务

[pg96@db1 ~]$ psql francs francs
psql (9.6beta1)
Type "help" for help.

francs=> begin;
BEGIN

francs=> select 1;
 ?column? 
----------
        1
(1 row)

事务中,不提交也不回滚。

--开启会话二:监控

postgres=# select * from pg_stat_activity where pid<>pg_backend_pid();
-[ RECORD 1 ]----+------------------------------
datid            | 16386
datname          | francs
pid              | 7776
usesysid         | 16384
usename          | francs
application_name | psql
client_addr      | 
client_hostname  | 
client_port      | -1
backend_start    | 2016-06-01 16:03:12.557328+08
xact_start       | 2016-06-01 16:03:16.921353+08
query_start      | 2016-06-01 16:03:18.754706+08
state_change     | 2016-06-01 16:03:18.755422+08
wait_event_type  | 
wait_event       | 
state            | idle in transaction
backend_xid      | 
backend_xmin     | 
query            | select 1;

postgres=# select * from pg_stat_activity where pid<>pg_backend_pid();
(0 rows)

备注:开始还能监控到这个 "idle in transaction" 的事务,大概过了 20秒后,这个事务查询不到了。

--再回到会话一

francs=> select 1;
 ?column? 
----------
        1
        
FATAL:  terminating connection due to idle-in-transaction timeout
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.

备注:回到会话一执行 select 1 测试命令,发现连接被断开了,报错代码很明显,idle-in-transaction 超时了。

--数据库日志

2016-06-01 16:03:38.756
CST,"francs","francs",7776,"[local]",574e96c0.1e60,1,"idle in
transaction",2016-06-01 16:03:12 CST,2/5887,0,FATAL,25P03,"terminating
connection due to idle-in-transaction timeout",,,,,,,,,"psql"

备注:数据库日志里清晰地记录了 7796 进程的连接由于空闲事务超时被断开连接。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值