]> granicus.if.org Git - postgresql/commitdiff
Support setting the keepalive idle time on MacOS X.
authorRobert Haas <rhaas@postgresql.org>
Tue, 6 Jul 2010 21:14:25 +0000 (21:14 +0000)
committerRobert Haas <rhaas@postgresql.org>
Tue, 6 Jul 2010 21:14:25 +0000 (21:14 +0000)
MacOS X uses TCP_KEEPALIVE rather than TCP_KEEPIDLE for this purpose.

Thanks to Fujii Masao for the review.

doc/src/sgml/config.sgml
doc/src/sgml/libpq.sgml
src/backend/libpq/pqcomm.c
src/interfaces/libpq/fe-connect.c

index 9631492ea5a0333c2c12657beb09fff9a6b22be2..7e0c7647a4a5a88b8518216dcc044c977cea029b 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.291 2010/07/03 22:52:25 momjian Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.292 2010/07/06 21:14:25 rhaas Exp $ -->
 
 <chapter Id="runtime-config">
   <title>Server Configuration</title>
@@ -523,11 +523,12 @@ SET ENABLE_SEQSCAN TO OFF;
       </indexterm>
       <listitem>
        <para>
-        On systems that support the <symbol>TCP_KEEPIDLE</symbol> socket option, specifies the
+        On systems that support the <symbol>TCP_KEEPIDLE</symbol> or
+        <symbol>TCP_KEEPALIVE</> socket option, specifies the
         number of seconds between sending keepalives on an otherwise idle
-        connection. A value of zero uses the system default. If <symbol>TCP_KEEPIDLE</symbol> is
-        not supported, this parameter must be zero. This parameter is ignored for
-        connections made via a Unix-domain socket.
+        connection. A value of zero uses the system default. If neither of
+        these socket options is supported, this parameter must be zero. This
+        parameter is ignored for connections made via a Unix-domain socket.
        </para>
       </listitem>
      </varlistentry>
index 9f74024d45724723b76fa90a33cea1c3aefb2550..f70541bb7f1584c5e27d424f260d39414586095f 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.311 2010/06/29 22:29:14 momjian Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.312 2010/07/06 21:14:25 rhaas Exp $ -->
 
 <chapter id="libpq">
  <title><application>libpq</application> - C Library</title>
           <para>
            Controls the number of seconds of inactivity after which TCP should
            send a keepalive message to the server.  A value of zero uses the
-           system default.  This parameter is ignored if the
-           <symbol>TCP_KEEPIDLE</> socket option is not supported, for
-           connections made via a Unix-domain socket, or if keepalives are
-           disabled.
+           system default.  This parameter is ignored if the neither the
+           <symbol>TCP_KEEPIDLE</> nor the <symbol>TCP_KEEPALIVE</> socket
+           options are supported, for connections made via a Unix-domain
+           socket, or if keepalives are disabled.
           </para>
          </listitem>
         </varlistentry>
index 7bf82933d5c606e0b1a07bd6e44257b2d48f8c2a..46577f379ac969bd2e32df85dd1ba254c1fae577 100644 (file)
@@ -30,7 +30,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *     $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.209 2010/03/21 00:17:58 petere Exp $
+ *     $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.210 2010/07/06 21:14:25 rhaas Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1317,7 +1317,7 @@ pq_endcopyout(bool errorAbort)
 int
 pq_getkeepalivesidle(Port *port)
 {
-#ifdef TCP_KEEPIDLE
+#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
        if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
                return 0;
 
@@ -1328,6 +1328,7 @@ pq_getkeepalivesidle(Port *port)
        {
                ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
 
+#ifdef TCP_KEEPIDLE
                if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
                                           (char *) &port->default_keepalives_idle,
                                           &size) < 0)
@@ -1335,6 +1336,15 @@ pq_getkeepalivesidle(Port *port)
                        elog(LOG, "getsockopt(TCP_KEEPIDLE) failed: %m");
                        port->default_keepalives_idle = -1; /* don't know */
                }
+#else
+               if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
+                                          (char *) &port->default_keepalives_idle,
+                                          &size) < 0)
+               {
+                       elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
+                       port->default_keepalives_idle = -1; /* don't know */
+               }
+#endif
        }
 
        return port->default_keepalives_idle;
@@ -1349,7 +1359,7 @@ pq_setkeepalivesidle(int idle, Port *port)
        if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
                return STATUS_OK;
 
-#ifdef TCP_KEEPIDLE
+#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
        if (idle == port->keepalives_idle)
                return STATUS_OK;
 
@@ -1367,18 +1377,27 @@ pq_setkeepalivesidle(int idle, Port *port)
        if (idle == 0)
                idle = port->default_keepalives_idle;
 
+#ifdef TCP_KEEPIDLE
        if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
                                   (char *) &idle, sizeof(idle)) < 0)
        {
                elog(LOG, "setsockopt(TCP_KEEPIDLE) failed: %m");
                return STATUS_ERROR;
        }
+#else
+       if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
+                                  (char *) &idle, sizeof(idle)) < 0)
+       {
+               elog(LOG, "setsockopt(TCP_KEEPALIVE) failed: %m");
+               return STATUS_ERROR;
+       }
+#endif
 
        port->keepalives_idle = idle;
 #else
        if (idle != 0)
        {
-               elog(LOG, "setsockopt(TCP_KEEPIDLE) not supported");
+               elog(LOG, "setting the keepalive idle time is not supported");
                return STATUS_ERROR;
        }
 #endif
index 2c8a059347104ec9baeffb8614f6665254e38387..5bb404fc27ebd1350675048b787a1f4f3ca26138 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.395 2010/07/06 19:19:00 momjian Exp $
+ *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.396 2010/07/06 21:14:25 rhaas Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1008,6 +1008,20 @@ setKeepalivesIdle(PGconn *conn)
                                                  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
                return 0;
        }
+#else
+#ifdef TCP_KEEPALIVE
+       /* Darwin uses TCP_KEEPALIVE rather than TCP_KEEPIDLE */
+       if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPALIVE,
+                                  (char *) &idle, sizeof(idle)) < 0)
+       {
+               char    sebuf[256];
+
+               appendPQExpBuffer(&conn->errorMessage,
+                                                 libpq_gettext("setsockopt(TCP_KEEPALIVE) failed: %s\n"),
+                                                 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
+               return 0;
+       }
+#endif
 #endif
 
        return 1;