psycopg_pool – Connection pool implementations¶
A connection pool is an object used to create and maintain a limited amount of PostgreSQL connections, reducing the time requested by the program to obtain a working connection and allowing an arbitrary large number of concurrent threads or tasks to use a controlled amount of resources on the server. See Connection pools for more details and usage pattern.
This package exposes a few connection pool classes:
ConnectionPoolis a synchronous connection pool yieldingConnectionobjects and can be used by multithread applications.AsyncConnectionPoolhas an interface similar toConnectionPool, but withasynciofunctions replacing blocking functions, and yieldsAsyncConnectioninstances.NullConnectionPoolis aConnectionPoolsubclass exposing the same interface of its parent, but not keeping any unused connection in its state. See Null connection pools for details about related use cases.AsyncNullConnectionPoolhas the same behaviour of theNullConnectionPool, but with the same async interface of theAsyncConnectionPool.
Note
The psycopg_pool package is distributed separately from the main
psycopg package: use pip install "psycopg[pool]", or pip install
psycopg_pool, to make it available. See Installing the connection pool.
The version numbers indicated in this page refer to the psycopg_pool
package, not to psycopg.
The ConnectionPool class¶
Pool exceptions¶
The AsyncConnectionPool class¶
AsyncConnectionPool has a very similar interface to the ConnectionPool
class but its blocking methods are implemented as async coroutines. It
returns instances of AsyncConnection, or of its subclass if
specified so in the connection_class parameter.
Only the functions and parameters with different signature from
ConnectionPool are listed here.
Null connection pools¶
Added in version 3.1.
The NullConnectionPool is a ConnectionPool subclass which doesn’t create
connections preemptively and doesn’t keep unused connections in its state. See
Null connection pools for further details.
The interface of the object is entirely compatible with its parent class. Its behaviour is similar, with the following differences:
The AsyncNullConnectionPool is, similarly, an AsyncConnectionPool subclass
with the same behaviour of the NullConnectionPool.