patroni.postgresql.connection module

class patroni.postgresql.connection.ConnectionPool

Bases: object

Helper class to manage named connections from Patroni to PostgreSQL.

The instance keeps named NamedConnection objects and parameters that must be used for new connections.

__init__() → None

Create an instance of ConnectionPool class.

close() → None

Close all named connections from Patroni to PostgreSQL registered in the pool.

conn_kwargs

Connection parameters that must be used for new psycopg connections.

get(name: str, kwargs_override: Optional[Dict[str, Any]] = None) → patroni.postgresql.connection.NamedConnection

Get a new named NamedConnection object from the pool.

Note

Creates a new NamedConnection object if it doesn’t yet exist in the pool.

Parameters:
  • name – name of the connection.
  • kwargs_overridedict object with connection parameters that should be different from default values provided by conn_kwargs.
Returns:

NamedConnection object.

class patroni.postgresql.connection.NamedConnection(pool: patroni.postgresql.connection.ConnectionPool, name: str, kwargs_override: Optional[Dict[str, Any]])

Bases: object

Helper class to manage psycopg connections from Patroni to PostgreSQL.

Variables:server_version – PostgreSQL version in integer format where we are connected to.
__init__(pool: patroni.postgresql.connection.ConnectionPool, name: str, kwargs_override: Optional[Dict[str, Any]]) → None

Create an instance of NamedConnection class.

Parameters:
  • pool – reference to a ConnectionPool object.
  • name – name of the connection.
  • kwargs_overridedict object with connection parameters that should be different from default values provided by connection pool.
_conn_kwargs

Connection parameters for this NamedConnection.

close(silent: bool = False) → bool

Close the psycopg connection to postgres.

Parameters:silent – whether the method should not write logs.
Returns:True if psycopg connection was closed, False otherwise.``
get() → Union[connection, Connection[Any]]

Get psycopg/psycopg2 connection object.

Note

Opens a new connection if necessary.

Returns:psycopg or psycopg2 connection object.
query(sql: str, *params) → List[Tuple[Any, ...]]

Execute a query with parameters and optionally returns a response.

Parameters:
  • sql – SQL statement to execute.
  • params – parameters to pass.
Returns:

a query response as a list of tuples if there is any.

Raises:

Error if had issues while executing sql.

PostgresConnectionException: if had issues while connecting to the database.

patroni.postgresql.connection.get_connection_cursor(**kwargs) → Iterator[Union[cursor, Cursor[Any]]]