pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Stanaway <david@stanaway.net>
To: Aryan Ariel Rodriguez Chalas <wimogan@yahoo.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Making a Persistent Postgres Connection
Date: Wed, 22 Sep 2004 12:17:43 -0500
Message-ID: <1095873463.2101.38.camel@david.dialmex.net> (raw)
In-Reply-To: <20040922135939.86779.qmail@web41015.mail.yahoo.com>
References: <20040922135939.86779.qmail@web41015.mail.yahoo.com>

Check out the 
SO_KEEPALIVE option for the socket.

I am not sure there is a safe way to do this with the PGconn handle, but
looking at the header there is an internal struct 
pg_conn you can look at include/postgresql/internal/libpq-int.h
And the socket file handle is the sock member.

By default, keepalive only kicks in after 2 hours, but you can change it
to some time less than you firewall times out.

Check out the man pages for:
setsockopt(2)
tcp(7)

You might end up with something like this:

PGconn *conn;
int one = 1, idle = 500, intvl = 500, cnt = 3;

conn = PQconnectdb(conninfo);

setsockopt(conn->sock, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
setsockopt(conn->sock, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
setsockopt(conn->sock, SOL_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl));
setsockopt(conn->sock, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));

The code will not be particularly portable. You would be better off
increasing the timeout on the firewall, or ensuring there was some
activity before the timeout.


On Wed, 2004-09-22 at 08:59 -0500, Aryan Ariel Rodriguez Chalas wrote:
> Hi everybody
> 
> I really would apreciate if somebody could help me on
> how to make a persistent connection to a Remote
> Database using "C Language". I work in "Linux"
> Operating System and use the header <pgsql/libpq-fe.h>
> 
> I have any problems when I make a connection to the
> Local Database, but every time I make a postgres
> connection (via Internet) to a Remote Database, I find
> the problem that the connections falls down every 10
> minutes more or less.
> 
> I hope someone to help me to solve this, please.
> 
> My best regards.
> 
> 
> _________________________________________________________
> Do You Yahoo!?
> Información de Estados Unidos y América Latina, en Yahoo! Noticias.
> Visítanos en http://noticias.espanol.yahoo.com
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
> 
-- 
David Stanaway <david@stanaway.net>



view thread (3+ messages)

Message-ID: <1095873463.2101.38.camel@david.dialmex.net>
Permalink:  ../1095873463.2101.38.camel@david.dialmex.net/
Also on:    postgresql.org/message-id/1095873463.2101.38.camel@david.dialmex.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: david@stanaway.net, wimogan@yahoo.com
  Subject: Re: Making a Persistent Postgres Connection
  In-Reply-To: <1095873463.2101.38.camel@david.dialmex.net>
  References: <20040922135939.86779.qmail@web41015.mail.yahoo.com> <1095873463.2101.38.camel@david.dialmex.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox