Menu

asyncpg DuplicatePreparedStatement on PgBouncer: Why Supabase Breaks Your Pool
πŸ“°

asyncpg DuplicatePreparedStatement on PgBouncer: Why Supabase Breaks Your Pool

DEV Community: postgresΒ·R (RYUSLOG DEV)Β·18 days ago
#kauw2qsg
#devtopostgresql#devto#dev#pooler#transaction
Long article (906 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.