PostgreSQL9.6:新增加“idle in transaction”超时空闲事务自动查杀功能
2017-05-22 16:44
549 查看
熟悉 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 超时空闲事务时间为 20 秒。
--重载配置文件
备注:此参数修改后对当前连接依然生效,应用不需要重连即能生效。
--开启会话一:模拟一个事务
事务中,不提交也不回滚。
--开启会话二:监控
备注:开始还能监控到这个 "idle in transaction" 的事务,大概过了 20秒后,这个事务查询不到了。
--再回到会话一
备注:回到会话一执行 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 进程的连接由于空闲事务超时被断开连接。
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 进程的连接由于空闲事务超时被断开连接。
相关文章推荐
- ORM中的对象间关联及其默认事务需要 - The Object Associations And The Default Transaction Requirements In ORM
- Executing a batch in a transaction--PHP DATA OBJECT(PDO)事务处理中的批量操作
- 让PPC手机增加自动对时功能
- 为jquery.ui.dialog 增加“自动记住关闭时的位置”的功能。
- 通过xajax给fckeditor增加自动保存草稿功能
- 给表格的单元格增加编辑功能(In place edit)
- 用代码来实现oracle自动增加1功能
- FlyIe1.63增加了左侧收藏夹和简易收集版的自动保存功能
- 强烈建议:撰写博客增加自动保存功能
- 操作XML添加获取功能(自动增加XML节点)
- Spring远程服务增加自动发现和群集功能
- 如何在Oracle 中实现类似自动增加 ID 的功能?
- 折磨我的VS IDE文件自动定位功能(Track Active Item in Solution Explorer)
- 自动查杀超时或僵死的进程的脚本(zt)
- 给MS SQL增加自动编号功能
- 露雨资源库三(第一个.net2.0软件)-为控件增加自动完成功能
- 通过xajax给fckeditor增加自动保存草稿功能
- 如何在Oracle 中实现类似自动增加 ID 的功能?
- 通过xajax给fckeditor增加自动保存草稿功能
- 在smarty中增加类似foreach的功能自动加载数据