I initialized a SQLAlchemy/psycopg(3) connection/engine like:
engine = create_engine(
PG_URI,
echo=True,
echo_pool=True,
pool_size=3,
pool_timeout=5,
pool_pre_ping=True,
connect_args={
"connect_timeout": 5,
"options": f"-c statement-timeout={5 * 1000}",
},
)
I realise when I use it later, it seems to hang at engine.begin()
even tho I have configured connect_timeout
(and hopefully set statement_timeout
correctly, for example:
logger.info("Pinging Postgres (sync) ...")
with engine.begin() as conn: # Seems to hang here
conn.execute(text("SELECT 1"))
logger.info("Ping PG successful")
return True
I noticed if I use NullPool its ok, so it seems like its failing to connect to the pool. Or perhaps reconnect, no logs related to pool tho, it seems to only print
Pinging Postgres (sync) ...
I think I need to pass reconnect_timeout
for pool, but how?
reconnect_timeout (float, default: 5 minutes) – Maximum time, in seconds, the pool will try to create a connection. If a connection attempt fails, the pool will try to reconnect a few times, using an exponential backoff and some random factor to avoid mass attempts. If repeated attempts fail, after reconnect_timeout second the connection attempt is aborted and the reconnect_failed() callback invoked.