pg_pconnect

(PHP 3, PHP 4, PHP 5)

pg_pconnect -- 打开一个持久的 PostgreSQL 连接

说明

resource pg_pconnect ( string connection_string )

pg_pconnect() 打开一个到 PostgreSQL 数据库的持久连接。返回其它 PostgreSQL 函数所需要的连接资源号。

connection_string 参数的定义见 pg_connect()

要打开持久连接功能,php.ini 中的 pgsql.allow_persistent 参数必须为 "On"(也是默认值)。 最大持久连接数目由 php.ini 中的 pgsql.max_persistent 参数定义(默认为 -1 表示没有限制)。所有连接的数目可由 php.ini 中的 pgsql.max_links 参数设置。

pg_close() 不能关闭由 pg_pconnect() 打开的持久连接。

参见 pg_connect() 以及持久数据库连接


add a note add a note User Contributed Notes
info at karl-philipp dot de
06-Oct-2003 07:05
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():

 http://de2.php.net/manual/en/function.register-shutdown-function.php
Spiros Ioannou
30-Jan-2003 09:40
Instead of reducing MaxClients in apache you may try to
reduce pgsql.max_links in php to at least the number of
postmasters. It should work and leave
you with more available httpds for static html pages.
raggaflo at libertysurf dot fr
30-Mar-2002 12:03
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"
etsysx dot i dot hate dot spam at teleline dot es
10-Mar-2002 05:56
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.
garrett at bgb dot cc
09-Feb-2002 08:39
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.
zarathustra dot heuser at sita dot int
23-Nov-2001 10:15
I was having the same problem with persistent connections to an Oracle database. Compiling PHP with the option --enable-sigchild solved the problem.
herve at elma dot fr
04-Dec-2000 10:14
For resetting an apache web server and in same time the persistent connections you may use :
./apachectl graceful
dmitri at graphictype dot com
11-May-2000 07:12
How to reset persistent connections:

killall -USR1 httpd
tumor at iris dot kkt dot bme dot hu
30-Mar-2000 09:39
Well, pg_pconnect is ALMOST identical to pg_Connect. The difference is that when you make something nasty with the PSQL server (like reseting it) then pg_pConnect still tryes to use the previous connection. This creates some nice broken_pipe errors (and maybe some more). I do not know that resetting the http server would help it, I think it would. As pg_connect creates a new connection every time (that is why it is \"so slow\") this error does not occurs with it. Really this is not a bug, but perphaps you could do something with it, and it may help some people working with recently changing pg security settings... Good bug hunting...
clecerf at univ-paris8 dot fr
24-Mar-2000 02:00
The "Options" parameter. It seems to worry some users...
Refering to the libpq doc of Postgres, it is used to set trace/debug options to be sent to backend.
Probably no use for a PHP3 programmer.