PHP: pg_pconnect - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pg_ping pg_parameter_status

Last updated: Sat, 27 Jan 2007

view this page in

# pg_pconnect

(PHP 3, PHP 4, PHP 5)

pg_pconnect -- Open a persistent PostgreSQL connection

## Описание

resource pg_pconnect ( string connection_string [, int connect_type] )

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_persistent `php.ini` directive must be set to "On" (which is the default). The maximum number of persistent connection can be defined with the pgsql.max_persistent `php.ini` directive (defaults to -1 for no limit). The total number of connections can be set with the pgsql.max_links `php.ini` directive.

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

## Список параметров

`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 \\.

  The currently recognized parameter keywords are: `host`, `hostaddr`, `port`, `dbname`, `user`, `password`, `connect_timeout`, `options`, `tty` (ignored), `sslmode`, `requiressl` (deprecated in favor of `sslmode`), and `service`. Which of these arguments exist depends on your PostgreSQL version.

`connect_type`

  If `PGSQL_CONNECT_FORCE_NEW` is passed, then a new connection is created, even if the `connection_string` is identical to an existing connection.

## Возвращаемые значения

PostgreSQL connection resource on success, `FALSE` on failure.

## Примеры

Пример 1. Using pg_pconnect()

`<?php
$dbconn = pg_pconnect("dbname=mary");
//connect to a database named "mary"
$dbconn2 = pg_pconnect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"
$dbconn3 = pg_pconnect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password
$conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar";
$dbconn4 = pg_pconnect($conn_string);
//connect to a database named "test" on the host "sheep" with a username and password
?>`

## Смотрите также

pg_connect()
Persistent Database Connections

add a note User Contributed Notes
pg_pconnect
info at karl-philipp dot de
06-Oct-2003 04: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 05: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
29-Mar-2002 08: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 01: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
08-Feb-2002 04: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 06: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 06:14
`For resetting an apache web server and in same time the persistent connections you may use :
./apachectl graceful`
dmitri at graphictype dot com
10-May-2000 04:12
`How to reset persistent connections:
killall -USR1 httpd`
tumor at iris dot kkt dot bme dot hu
30-Mar-2000 05: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
23-Mar-2000 10: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.`
add a note
pg_ping pg_parameter_status

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST