Spaces:
Running

axeronis commited on
Commit
a1c68dc
·
1 Parent(s): 83af423

fix: Configure database for Supabase Transaction Pooler (pgbouncer compatibility)

Browse files
backend/alembic/env.py CHANGED
@@ -63,12 +63,22 @@ def run_migrations_online() -> None:
63
 
64
  In this scenario we need to create an Engine
65
  and associate a connection with the context.
66
-
 
67
  """
68
- connectable = engine_from_config(
69
- config.get_section(config.config_ini_section, {}),
70
- prefix="sqlalchemy.",
 
 
 
 
 
 
71
  poolclass=pool.NullPool,
 
 
 
72
  )
73
 
74
  with connectable.connect() as connection:
 
63
 
64
  In this scenario we need to create an Engine
65
  and associate a connection with the context.
66
+
67
+ Configured for Supabase Transaction Pooler (pgbouncer) compatibility.
68
  """
69
+ from sqlalchemy import create_engine
70
+
71
+ # Get the database URL
72
+ database_url = config.get_main_option("sqlalchemy.url")
73
+
74
+ # Create engine with pgbouncer-compatible settings
75
+ # prepare_threshold=0 disables prepared statements (required for transaction pooler)
76
+ connectable = create_engine(
77
+ database_url,
78
  poolclass=pool.NullPool,
79
+ connect_args={
80
+ "prepare_threshold": 0, # Disable prepared statements for pgbouncer
81
+ }
82
  )
83
 
84
  with connectable.connect() as connection:
backend/app/core/database.py CHANGED
@@ -35,10 +35,13 @@ async_engine = create_async_engine(
35
  echo=settings.DEBUG,
36
  future=True,
37
  pool_pre_ping=True,
38
- pool_size=3, # Reduced for Supabase Free (max ~15 connections)
39
- max_overflow=5, # Limited overflow to stay within limits
40
  pool_timeout=30, # Connection timeout
41
- pool_recycle=1800, # Recycle connections every 30 min (faster cleanup)
 
 
 
42
  )
43
 
44
  # Note: pgvector type registration is handled in init_db() startup
 
35
  echo=settings.DEBUG,
36
  future=True,
37
  pool_pre_ping=True,
38
+ pool_size=5, # Optimized for Transaction Pooler (supports more connections)
39
+ max_overflow=10, # More headroom with Transaction Pooler
40
  pool_timeout=30, # Connection timeout
41
+ pool_recycle=1800, # Recycle connections every 30 min
42
+ connect_args={
43
+ "prepare_threshold": 0, # Disable prepared statements for pgbouncer/transaction pooler
44
+ }
45
  )
46
 
47
  # Note: pgvector type registration is handled in init_db() startup