PostgreSQL DBA(187) - TCP keepalive

TCP keepalive可用于:
1、使网络连接处于活跃(相对于Idle而言)状态
2、检测在没有关闭网络连接情况下的通信中断

本节介绍OS和PG的相关参数说明和配置。

OS配置

keepalive idle time: tpc连接处于空闲状态下,超过该时间则发送keepalive packet;2 hours on both Linux and Windows
keepalive interval: 如keepalive packet没有应答,则在间隔后再次发送packet;75 seconds on Linux, 1 second on Windows
keepalive count: 发送packet的次数;9 on Linux, 10 on Windows (this value cannot be changed on Windows)

在Linux中,可通过如下配置

# On Linux, this is done by editing the /etc/sysctl.conf file:
# detect dead connections after 70 seconds
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 5
net.ipv4.tcp_keepalive_probes = 3

使用命令生效

sysctl -p

PG参数配置

keepalives_idle:为0则使用OS配置
keepalives_interval:为0则使用OS配置
keepalives_count:为0则使用OS配置

参考资料
TCP keepalive for a better PostgreSQL experience
LIBPQ-KEEPALIVES-IDLE

请使用浏览器的分享功能分享到微信等