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

浏览: 65 发布日期: 2016-11-06 分类: postgresql

熟悉 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;

francs=> select 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;
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 进程的连接由于空闲事务超时被断开连接。

idle_in_transaction_session_timeout (integer)
Preventing Transaction ID Wraparound Failures