如何通过存活PostgreSQL的突破连接的TCP / IP,而不在寄存器中改变什么吗?器中、PostgreSQL、TCP、IP

2023-09-03 15:35:37 作者:牛轧奶芙糖

海兰,

我有一个与客户端 - 服务器结构和PostgreSQL 8.4工作制度。 我的问题是,如果一个客户端编辑记录而失去了连接到服务器,TCPIP的连接仍然被认为是!因此,记录保持分配给我的数据库客户端。 我需要的记录,以免费为编辑在几分钟内。因此,我在我的postgresql.conf中设置KEEPALIVE配置:

I have a system working with the client-server structure and PostgreSQL 8.4. My problem is that if a client is editing a record and lose his connection to the server,the TCPIP connection is still considered! So, the record stay allocated for the client in my database. I need the records to be free for edit in a few minutes. Therefore I set the KEEPALIVE configuration in my "postgresql.conf":

tcp_keepalives_idle = 60 # TCP_KEEPIDLE, in seconds; 
tcp_keepalives_interval = 60 # TCP_KEEPINTVL, in seconds; 
tcp_keepalives_count  = 5 # TCP_KEEPCNT

使得这些设置并重新启动服务器后,系统可以继续运行方式相同:只需休息两小时后,连接TCPIP,然后将释放的PostgreSQL的记录。我需要一个高效,安全的方式为PostgreSQL的理解,它的配置,打破这些连接!我需要帮助,急! 请!

谢谢

推荐答案

PostgreSQL的Windows上不支持保活设置一个连接。它使用的setsockopt(S,IPPROTO_TCP,TCP_KEEPIDLE,...)这是恕我直言Linux特有的。

PostgreSQL on Windows does not support keepalive settings for one connection. It is using setsockopt(s, IPPROTO_TCP, TCP_KEEPIDLE, ...) which is IMHO Linux-specific.

所以它使用的 SIO_KEEPALIVE_VALS 。这样的事情在的src /后端/ libpq的/ pqcomm.c 的StreamConnection 的功能。

You can implement a patch for Postgres so it uses SIO_KEEPALIVE_VALS. Something like this in src/backend/libpq/pqcomm.c in StreamConnection function.

{
    DWORD bytesReturned = 0;
    tcp_keepalive vals;
    vals.keepalivetime = 60*1000; /* milliseconds */
    vals.keepaliveinterval = 60*1000; /* milliseconds */
    vals.onoff = 1;
    err = WSAIoctl(
        port->sock,
        SIO_KEEPALIVE_VALS,
        (char *)&vals, sizeof(vals), NULL, 0,
        &bytesReturned, NULL, NULL);
    if (err == -1) {
        elog(LOG, "WSAIoctl(SIO_KEEPALIVE_VALS) failed: %m");
    }
}

或者你也可以设置使用的的KeepAliveInterval 和的 KeepAliveTime 设置(计数总是在10 Windows Vista和更高版本)。

Or you can set system-wide settings in Windows registry HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters using KeepAliveInterval and KeepAliveTime settings (count is always 10 on Windows Vista and later).