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

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 reloadserver signaled

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

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

[pg96@db1 ~]$ psql francs francspsql (9.6beta1)Type "help" for help.francs=> begin;BEGINfrancs=> select 1; ?column? ----------        1(1 row)

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

--开启会话二:监控

postgres=# select * from pg_stat_activity where pid<>pg_backend_pid();-[ RECORD 1 ]----+------------------------------datid            | 16386datname          | francspid              | 7776usesysid         | 16384usename          | francsapplication_name | psqlclient_addr      | client_hostname  | client_port      | -1backend_start    | 2016-06-01 16:03:12.557328+08xact_start       | 2016-06-01 16:03:16.921353+08query_start      | 2016-06-01 16:03:18.754706+08state_change     | 2016-06-01 16:03:18.755422+08wait_event_type  | wait_event       | state            | idle in transactionbackend_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 timeoutserver 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

推荐阅读