Re: persistent db connections in PHP

From: PFC <lists(at)peufeu(dot)com>
To: lawpoop(at)gmail(dot)com, pgsql-general(at)postgresql(dot)org
Subject: Re: persistent db connections in PHP
Date: 2007-06-16 15:46:25
Message-ID: op.tt0t3ng0cigqcu@apollo13
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Hello all!
>
> I'm working on a PHP site using Postgres as a back-end. I have an
> include at the top of each page that runs the pg_connect function.
>
> I'm implementing some temporary tables, which I understand are
> destroyed automatically at the end of the session. It seems to me that

- Temp tables are local to a session (ie. a connection)
- Persistent connections in PHP creates a pool of connections. When you
pg_pconnect() you get a connection from the pool. You have no control over
which connection you get, so you don't really know if the temp tables have
been created by a previous connection or not.

So, the best solution is to create the temp tables inside a transaction,
with ON COMMIT DROP, do your work, and let them disappear. This will save
you from getting temp tables already filled by stuff from the previous
webpage which got this connection. For the same reason, pg_pconnect()
should ALWAYS be followed by pg_query( "ROLLBACK" ). If you put rollback
in register_shutdown_function(), be aware that it will NOT be called if
your script is nuked for trying to malloc more than the allowed memory
limit, or for other brutal reasons...

Also note that PHP, being PHP, sucks, and thusly, will not reconnect
persistent connections when they fail. You have to kick it a bit.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lincoln Yeoh 2007-06-16 15:47:02 What O/S or hardware feature would be useful for databases?
Previous Message Martin Gainty 2007-06-16 15:23:05 Re: persistent db connections in PHP