Skip site navigation (1) Skip section navigation (2)

Re: Making a Persistent Postgres Connection

From: David Stanaway <david(at)stanaway(dot)net>
To: Aryan Ariel Rodriguez Chalas <wimogan(at)yahoo(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: Making a Persistent Postgres Connection
Date: 2004-09-22 17:17:43
Message-ID: 1095873463.2101.38.camel@david.dialmex.net (view raw or flat)
Thread:
Lists: pgsql-admin
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(at)stanaway(dot)net>

In response to

pgsql-admin by date

Next:From: Michael AdlerDate: 2004-09-22 18:19:05
Subject: Re: analytics
Previous:From: G u i d o B a r o s i oDate: 2004-09-22 17:12:18
Subject: Re: Important Question.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group