如何通过 keepalive postgreSQL 断开 TCP/IP 连接而不更改寄存器中的任何内容?

发布于 2024-09-11 21:01:45 字数 467 浏览 4 评论 0原文

我有一个使用客户端-服务器结构和 PostgreSQL 8.4 的系统。 我的问题是,如果客户端正在编辑记录并失去与服务器的连接,则仍会考虑 TCPIP 连接!因此,记录在我的数据库中保留分配给客户端。 我需要在几分钟内免费编辑这些记录。因此,我在“postgresql.conf”中设置了 KEEPALIVE 配置:

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 明白它必须按照配置断开这些连接!

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

After making these settings and restart the server the system continues to function the same way: Just breaks the connection TCPIP after two hours and then deallocates the records in PostgreSQL. I need an efficient and safe way for the PostgreSQL to understand that it has to break these connections as configured!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2024-09-18 21:01:45

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

您可以为 Postgres 实施补丁,以便它使用 SIO_KEEPALIVE_VALS。在 StreamConnection 函数的 src/backend/libpq/pqcomm.c 中,有类似的内容。

{
    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");
    }
}

或者,您可以使用 KeepAliveIntervalKeepAliveTime 设置(在 Windows Vista 及更高版本中计数始终为 10)。

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.

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");
    }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文