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
相关内容
-
华硕电脑系统win10|华硕电脑系统win10系统
华硕电脑系统win10|华硕电脑系统win10系统,,华硕电脑系统win10...
-
金蝶担保机构业务管理系统
金蝶担保机构业务管理系统,,1.金蝶财务软件怎么使用?软件使用...
-
联想台式机怎么恢复出厂系统
联想台式机怎么恢复出厂系统,所示,如下图,如所示:首先关闭计算...
-
电脑系统城|电脑系统下载 win7
电脑系统城|电脑系统下载 win7,电脑系统城,不少朋友在使用电脑...
-
主角电脑系统的小说|主角大脑里面电脑系统的小
主角电脑系统的小说|主角大脑里面电脑系统的小说,,1. 主角大脑...
-
电脑系统文字怎么修改|电脑系统文字怎么修改颜
电脑系统文字怎么修改|电脑系统文字怎么修改颜色,,1. 电脑系统...
-
金蝶系统的其他出入库核算|金蝶精斗云的云进销
金蝶系统的其他出入库核算|金蝶精斗云的云进销存软件有其他...
-
苹果系统的电脑好用吗|苹果电脑系统好用不
苹果系统的电脑好用吗|苹果电脑系统好用不,,苹果电脑系统好用...
-
怎样升级电脑cpu|怎样升级电脑系统版本
怎样升级电脑cpu|怎样升级电脑系统版本,,怎样升级电脑系统版本...
-
装电脑系统要多少钱|装个电脑系统要多少钱
装电脑系统要多少钱|装个电脑系统要多少钱,,装个电脑系统要多...
-
金蝶报表系统怎么取消隐藏|表格怎么取消隐藏
金蝶报表系统怎么取消隐藏|表格怎么取消隐藏,,1.表格怎么取消...
-
电脑系统升级到win10|电脑系统升级到win10之后
电脑系统升级到win10|电脑系统升级到win10之后无线网卡无法...
-
2021电脑系统在哪下载|2021年电脑最新系统
2021电脑系统在哪下载|2021年电脑最新系统,,1. 2021年电脑最新...
-
影子系统是什么影子系统安装卸载win10影子系统
影子系统是什么影子系统安装卸载win10影子系统崩溃修复教程,...
-
装一个电脑系统多少钱|重装电脑系统一般多少钱
装一个电脑系统多少钱|重装电脑系统一般多少钱,,重装电脑系统...