Why does updating to psycopg 3.x cause database timeouts?
I have a Django 5.x app. I've been using psycopg2, and I'm now trying to update to psycopg 3. This is pretty simple: I uninstalled everything to do with psycopg2 and installed psycopg[binary]
.
However, when I run my test suite now (in parallel), I am suddenly getting connection timeout expired
and canceling statement due to statement timeout
errors that I weren't getting before!
My database configuration is pretty straightforward:
STATEMENT_TIMEOUT = get_from_env(
"POSTGRES_STATEMENT_TIMEOUT", default=30000, type_cast=int
)
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": get_from_env("DB_NAME", "my-db"),
"USER": get_from_env("DB_USERNAME", "my-db-user"),
"PASSWORD": get_from_env("DB_PASSWORD", DEFAULT_DB_PASSWORD),
"HOST": get_from_env("DB_HOSTNAME", "localhost"),
"PORT": get_from_env("DB_PORT", "5432"),
"OPTIONS": {
"connect_timeout": 3,
"options": f"-c statement_timeout={STATEMENT_TIMEOUT}ms",
},
# Keep database connections open for 1 minute
"CONN_MAX_AGE": 60,
# But ensure that persistent connections are healthy before using them
"CONN_HEALTH_CHECKS": True,
},
}
Is this configuration no longer valid with psycopg 3? Or are there some other changes in 3.x that could mean more timeouts?
This is all using synchronous Python, no async or anything.
This appears to be due to a bug in psycopg 3.2. By using psycopg 3.1, things work as expected. There is a GitHub issue for the bug here: https://github.com/psycopg/psycopg/issues/888