Skip to content

Commit 5acd417

Browse files
committed
Support setting the keepalive idle time on MacOS X.
MacOS X uses TCP_KEEPALIVE rather than TCP_KEEPIDLE for this purpose. Thanks to Fujii Masao for the review.
1 parent 3f12653 commit 5acd417

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

doc/src/sgml/config.sgml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.291 2010/07/03 22:52:25 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.292 2010/07/06 21:14:25 rhaas Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -523,11 +523,12 @@ SET ENABLE_SEQSCAN TO OFF;
523523
</indexterm>
524524
<listitem>
525525
<para>
526-
On systems that support the <symbol>TCP_KEEPIDLE</symbol> socket option, specifies the
526+
On systems that support the <symbol>TCP_KEEPIDLE</symbol> or
527+
<symbol>TCP_KEEPALIVE</> socket option, specifies the
527528
number of seconds between sending keepalives on an otherwise idle
528-
connection. A value of zero uses the system default. If <symbol>TCP_KEEPIDLE</symbol> is
529-
not supported, this parameter must be zero. This parameter is ignored for
530-
connections made via a Unix-domain socket.
529+
connection. A value of zero uses the system default. If neither of
530+
these socket options is supported, this parameter must be zero. This
531+
parameter is ignored for connections made via a Unix-domain socket.
531532
</para>
532533
</listitem>
533534
</varlistentry>

doc/src/sgml/libpq.sgml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.311 2010/06/29 22:29:14 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.312 2010/07/06 21:14:25 rhaas Exp $ -->
22

33
<chapter id="libpq">
44
<title><application>libpq</application> - C Library</title>
@@ -298,10 +298,10 @@
298298
<para>
299299
Controls the number of seconds of inactivity after which TCP should
300300
send a keepalive message to the server. A value of zero uses the
301-
system default. This parameter is ignored if the
302-
<symbol>TCP_KEEPIDLE</> socket option is not supported, for
303-
connections made via a Unix-domain socket, or if keepalives are
304-
disabled.
301+
system default. This parameter is ignored if the neither the
302+
<symbol>TCP_KEEPIDLE</> nor the <symbol>TCP_KEEPALIVE</> socket
303+
options are supported, for connections made via a Unix-domain
304+
socket, or if keepalives are disabled.
305305
</para>
306306
</listitem>
307307
</varlistentry>

src/backend/libpq/pqcomm.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.209 2010/03/21 00:17:58 petere Exp $
33+
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.210 2010/07/06 21:14:25 rhaas Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -1317,7 +1317,7 @@ pq_endcopyout(bool errorAbort)
13171317
int
13181318
pq_getkeepalivesidle(Port *port)
13191319
{
1320-
#ifdef TCP_KEEPIDLE
1320+
#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
13211321
if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
13221322
return 0;
13231323

@@ -1328,13 +1328,23 @@ pq_getkeepalivesidle(Port *port)
13281328
{
13291329
ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
13301330

1331+
#ifdef TCP_KEEPIDLE
13311332
if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
13321333
(char *) &port->default_keepalives_idle,
13331334
&size) < 0)
13341335
{
13351336
elog(LOG, "getsockopt(TCP_KEEPIDLE) failed: %m");
13361337
port->default_keepalives_idle = -1; /* don't know */
13371338
}
1339+
#else
1340+
if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1341+
(char *) &port->default_keepalives_idle,
1342+
&size) < 0)
1343+
{
1344+
elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
1345+
port->default_keepalives_idle = -1; /* don't know */
1346+
}
1347+
#endif
13381348
}
13391349

13401350
return port->default_keepalives_idle;
@@ -1349,7 +1359,7 @@ pq_setkeepalivesidle(int idle, Port *port)
13491359
if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
13501360
return STATUS_OK;
13511361

1352-
#ifdef TCP_KEEPIDLE
1362+
#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
13531363
if (idle == port->keepalives_idle)
13541364
return STATUS_OK;
13551365

@@ -1367,18 +1377,27 @@ pq_setkeepalivesidle(int idle, Port *port)
13671377
if (idle == 0)
13681378
idle = port->default_keepalives_idle;
13691379

1380+
#ifdef TCP_KEEPIDLE
13701381
if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
13711382
(char *) &idle, sizeof(idle)) < 0)
13721383
{
13731384
elog(LOG, "setsockopt(TCP_KEEPIDLE) failed: %m");
13741385
return STATUS_ERROR;
13751386
}
1387+
#else
1388+
if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1389+
(char *) &idle, sizeof(idle)) < 0)
1390+
{
1391+
elog(LOG, "setsockopt(TCP_KEEPALIVE) failed: %m");
1392+
return STATUS_ERROR;
1393+
}
1394+
#endif
13761395

13771396
port->keepalives_idle = idle;
13781397
#else
13791398
if (idle != 0)
13801399
{
1381-
elog(LOG, "setsockopt(TCP_KEEPIDLE) not supported");
1400+
elog(LOG, "setting the keepalive idle time is not supported");
13821401
return STATUS_ERROR;
13831402
}
13841403
#endif

src/interfaces/libpq/fe-connect.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.395 2010/07/06 19:19:00 momjian Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.396 2010/07/06 21:14:25 rhaas Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1008,6 +1008,20 @@ setKeepalivesIdle(PGconn *conn)
10081008
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
10091009
return 0;
10101010
}
1011+
#else
1012+
#ifdef TCP_KEEPALIVE
1013+
/* Darwin uses TCP_KEEPALIVE rather than TCP_KEEPIDLE */
1014+
if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1015+
(char *) &idle, sizeof(idle)) < 0)
1016+
{
1017+
char sebuf[256];
1018+
1019+
appendPQExpBuffer(&conn->errorMessage,
1020+
libpq_gettext("setsockopt(TCP_KEEPALIVE) failed: %s\n"),
1021+
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1022+
return 0;
1023+
}
1024+
#endif
10111025
#endif
10121026

10131027
return 1;

0 commit comments

Comments
 (0)