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:

  • ConnectionPool is a synchronous connection pool yielding Connection objects and can be used by multithread applications.

  • AsyncConnectionPool has an interface similar to ConnectionPool , but with asyncio functions replacing blocking functions, and yields AsyncConnection instances.

  • NullConnectionPool is a ConnectionPool subclass 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.

  • AsyncNullConnectionPool has the same behaviour of the NullConnectionPool , but with the same async interface of the AsyncConnectionPool .

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 with different signature from ConnectionPool are listed here.

Null connection pools #

New 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 .