Spaces:
Running
Running
fix: disable asyncpg statement cache for postgres poolers
Browse files- app/security/database.py +6 -0
app/security/database.py
CHANGED
|
@@ -97,9 +97,15 @@ class SecurityDatabase:
|
|
| 97 |
database_url = normalize_async_database_url(database_url)
|
| 98 |
self.database_url = database_url
|
| 99 |
self.auto_migrate = auto_migrate or database_url.startswith("sqlite")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
self.engine: AsyncEngine = create_async_engine(
|
| 101 |
database_url,
|
| 102 |
pool_pre_ping=True,
|
|
|
|
| 103 |
)
|
| 104 |
if database_url.startswith("sqlite"):
|
| 105 |
event.listen(self.engine.sync_engine, "connect", self._configure_sqlite)
|
|
|
|
| 97 |
database_url = normalize_async_database_url(database_url)
|
| 98 |
self.database_url = database_url
|
| 99 |
self.auto_migrate = auto_migrate or database_url.startswith("sqlite")
|
| 100 |
+
# Disable asyncpg's prepared-statement cache for PostgreSQL poolers.
|
| 101 |
+
# Supabase/PgBouncer transaction pooling can otherwise reuse a server
|
| 102 |
+
# connection whose prepared-statement names were created by another
|
| 103 |
+
# client connection, raising DuplicatePreparedStatementError.
|
| 104 |
+
connect_args = {"statement_cache_size": 0} if database_url.startswith(("postgresql", "postgres")) else {}
|
| 105 |
self.engine: AsyncEngine = create_async_engine(
|
| 106 |
database_url,
|
| 107 |
pool_pre_ping=True,
|
| 108 |
+
connect_args=connect_args,
|
| 109 |
)
|
| 110 |
if database_url.startswith("sqlite"):
|
| 111 |
event.listen(self.engine.sync_engine, "connect", self._configure_sqlite)
|