A FastAPI service works locally, then fails in production with asyncpg.exceptions.DuplicatePreparedStatementError: prepared statement "__asyncpg_stmt_1__" already exists , or with ConnectionFailureError: connection is closed mid-request. The database is Supabase or PgBouncer, the connection string came from the dashboard, and no application code changed.
Neither error is about your SQL. Both come from using a client-side statement cache against a pooler that does not keep a session.
Why the statement collides
asyncpg prepares statements and caches them per connection, keyed by SQL text, up to statement_cache_size (100 by default). Each prepared statement gets a server-side name like __asyncpg_stmt_1__ .
Under a direct connection that name is unique per session, so the cache is a pure win.β¦