Discussion:
Making a Persistent Postgres Connection
(too old to reply)
Aryan Ariel Rodriguez Chalas
2004-09-22 13:59:39 UTC
Permalink
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
Tom Lane
2004-09-22 15:08:22 UTC
Permalink
Post by Aryan Ariel Rodriguez Chalas
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'm betting there is a firewall with a short idle-connection timeout
between you and the database.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
David Stanaway
2004-09-22 17:17:43 UTC
Permalink
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.
Post by Aryan Ariel Rodriguez Chalas
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 <***@stanaway.net>

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html
Loading...