pg_pconnect

Description

pg_pconnect() opens a connection to a
PostgreSQL database. It returns a connection resource that is
needed by other PostgreSQL functions.

If a second call is made to pg_pconnect() with
the same connection_string as an existing connection, the
existing connection will be returned unless you pass
PGSQL_CONNECT_FORCE_NEW as
connect_type.

To enable persistent connection, the pgsql.allow_persistentphp.ini directive must be set to "On" (which is the default).
The maximum number of persistent connection can be defined with the pgsql.max_persistentphp.ini directive (defaults to -1 for no limit). The total number
of connections can be set with the pgsql.max_linksphp.ini directive.

pg_close() will not close persistent links
generated by pg_pconnect().

Parameters

connection_string

The connection_string can be empty to use all default parameters, or it
can contain one or more parameter settings separated by whitespace.
Each parameter setting is in the form keyword = value. Spaces around
the equal sign are optional. To write an empty value or a value
containing spaces, surround it with single quotes, e.g., keyword =
'a value'. Single quotes and backslashes within the value must be
escaped with a backslash, i.e., \' and \\.

User Contributed Notes 10 notes

You should not use pg_pconnect - it's broken. It will work but it doesn't really pool, and it's behaviour is unpredictable. It will only make you rise the max_connections parameter in postgresql.conf file until you run out of resources (which will slow your database down).

If you have many concurrent connections to your database, you should use the PostgreSQL connection pooler PgBouncer (developed by the Skype-team). When using pgbouncer, make sure you use pg_connect and NOT pg_pconnect. Also, make sure you close your connections with pg_close.

A contribution to the transaction issue raised by "garrett at bgb dot cc".

In a German book about PostgreSQL in connection with PHP (Cornelia Boenigk, PostgreSQL - Grundlagen, Praxis, Anwendungsentwicklung mit PHP) one can read in chapter 19.3 about persistent connections:If the page processing aborts and the transaction is not finished yet, the next script using the same persistent connection will be considered as the continuation of the transaction. In particular a lock of a table will persist. The explanation is as follows: After the abort of the script no COMMIT or ROLLBACK was sent to the db server.

The author describes a hint to avoid the scenario above:You can create a function for resolving transactions and locks erroneously not closed. For invoking the function after execution of a script it has to be registered with the function register_shutdown_function():

Be careful when using Apache/PHP dynamic module/PostgreSQL :in httpd.conf (Apache conf) default MaxClients is 150, whereas default PG's max_connections is 32 which is much fewer than 150. You have to set max_connections to at least MaxClients (and pg's shared_buffers to 2*max_connections at least) to avoid PG's errors with pg_pconnect like : "Sorry, too many clients already connected"

To setup a high availability server with apache as a static module and postgreSQL, change httpd.conf and set MaxClients to less than max postgreSQL simultaneous connections (like 32 or 64).This way pg_pconnect will allways return a valid handle under heavy traffic or under a request flow attack without wasting resources and without connection problems.

If a transaction is in progress when page processing ends, is it aborted before the connection placed bak in the pool? Or is the connection added "as is"?

It would seem that the correct thing to do is to always 'ABORT' before adding to the pool.

As a note, this would be a good time to check and see if the connection is still open before readding it. Thus allowing closed connections to be cleaned up over time, instead of hanging around for ever as they do now.

<?php//// Using pg_pconnect in a class.//// Why this? Because the manual says://// If a second call is made to pg_pconnect() with the same// connection_string as an existing connection, the existing // connection will be returned unless you pass // PGSQL_CONNECT_FORCE_NEW as connect_type.//// This is not always true.///** * MyClassA creates a postgresql connection using pg_pconnect * and stores the resulting resource id to $this->conn */class MyClassA{ function __construct($connection_string) {$this->conn = pg_pconnect($connection_string) or die('Wrong CONN_STRING'); }}