How do you deal with pooled connection errors when using the Python library psycopg = {extras = ["binary", "pool"], version = "^3.1.16"}?
For context: I have implemented a Python web app which runs on aiohttp and uses a connection pool to access Postgres.
The pool is instantiated at startup, like so:
import asyncio
from psycopg_pool import AsyncConnectionPool
def create_pool():
async_pool = AsyncConnectionPool(conninfo=db_cfg.db_conn_str, open=False)
logger.info("Using", db_cfg.db_conn_str)
return async_pool
asynch_pool = create_pool()
async def open_pool():
try:
await asynch_pool.open()
await asynch_pool.wait()
logger.info("Asynch connection pool Opened")
except:
logger.error("Could not open pool")
asyncio.run(open_pool())
Then we open the connections and open cursors like this:
async def create_cursor(func: Callable) -> Any:
await asynch_pool.check()
async with asynch_pool.connection() as conn:
async with conn.cursor() as cur:
return await func(cur)
After starting up the application all works fine (1 hour or so). However after some time, all connections in the pool are unusable and timeout all the time.
Does anyone know what is wrong here?
Just some extra information about my environment:
- Python 3.12
- Conda 22.9.0
- aiohttp 3.9.1