如何在 Postgresql 中使用 tcp_keepalives 设置?
Postgresql 有 3 个 keepalive 设置用于管理断开的连接(在 postgresql.conf 中):
tcp_keepalives_count
tcp_keepalives_idle
tcp_keepalives_interval
默认情况下,这些值为 0。
我想要的行为是,如果客户端失去网络连接或进入睡眠状态,Postgresql 将在一段时间后删除客户端连接。
我当前正在使用这些值:
tcp_keepalives_count = 1
tcp_keepalives_idle = 60
tcp_keepalives_interval = 60
我在 Mac OS X 上运行 PostgreSQL 8.4,但它似乎没有任何效果。我的测试是锁定表中的一行(使用 SELECT FOR UPDATE)并断开工作站与网络的连接。但在 Postgresql 中我仍然看到工作站持有锁。
我希望时间过去后(本例中为 60 秒),连接将终止并且锁将被释放。
要么我做错了什么,要么我完全误解了这是如何工作的。
有什么建议吗?
Postgresql has 3 keepalive settings for managing dropped connections (in postgresql.conf):
tcp_keepalives_count
tcp_keepalives_idle
tcp_keepalives_interval
By default these are 0.
The behavior I would like is for Postgresql to drop client connections after a period of time, should the client lose its network connection or go to sleep.
I am currently using these values:
tcp_keepalives_count = 1
tcp_keepalives_idle = 60
tcp_keepalives_interval = 60
I am running PostgreSQL 8.4 on Mac OS X, but it doesn't seem to have any effect. My test is that I lock a row in a table (using SELECT FOR UPDATE) and disconnect the workstation from the network. But in Postgresql I still see that workstation holding the lock.
I would expect that after the time has passed (60 seconds in this case) the connection would be terminated and the lock would be released.
Either I am doing something wrong or I am completely misunderstanding how this is supposed to work.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你需要配置你的操作系统。通过程序更改保活参数尚未得到广泛支持。这应该对您有帮助:
使用 TCP keepalive 来检测网络错误
另外,您的参数选择也很糟糕。如果
tcp_keepalives_count=1
有效,那么即使丢失一个保活数据包也会断开您的连接。并且单个数据包经常丢失。我将在 MacOSX/FreeBSD 上的/etc/sysctl.conf
中使用以下内容:net.inet.tcp.keepidle = 60000
net.inet.tcp.keepintvl = 10000
失去连接后,操作系统将在最多 140 秒(60 秒空闲 + 10 秒间隔内 8 个保活数据包)内断开连接。
I think you need to configure your operating system instead. Changing keepalive parameters by programs is not widely supported yet. This should help you:
Using TCP keepalive to Detect Network Errors
Also your parameters are chosen badly. If
tcp_keepalives_count=1
worked then even one lost keepalive packet will drop your connection. And single packets get lost often. I'd use the following in/etc/sysctl.conf
on MacOSX/FreeBSD:net.inet.tcp.keepidle = 60000
net.inet.tcp.keepintvl = 10000
OS will then drop connections at most 140 seconds (60 seconds of idle + 8 keepalive packets in 10 seconds intervals) after loosing connectivity.