The Vercel deployment was failing with error:
remaining connection slots are reserved for roles with the SUPERUSER attribute
This occurred because:
- Table creation at import time -
Base.metadata.create_all()was running every time a serverless function started - Connection pooling misconfiguration - Not using NullPool properly for serverless
- Connection leaks - Connections weren't being disposed properly
- ✅ Removed
Base.metadata.create_all(bind=engine)from module-level code - ✅ Added shutdown event to dispose of connections properly
- ✅ Enhanced health check endpoint to test database connectivity
⚠️ Important: Tables must exist before deployment (use migrations)
- ✅ Explicitly imported and used
NullPoolfor serverless environments - ✅ Added detailed comments about serverless connection management
- ✅ Proper configuration for connection timeout and app identification
- Tables are created via Alembic migrations:
alembic upgrade head - No behavior change for local development
- CRITICAL: Database tables MUST exist before deployment
- Each serverless function creates a fresh connection
- Connections are disposed after each request
- No connection pool = no connection exhaustion
Before deploying to Vercel:
-
Ensure Database is Migrated
# Run migrations on your production database alembic upgrade head -
Verify Environment Variables Make sure Vercel has:
DATABASE_URL- Your Aiven PostgreSQL connection stringENVIRONMENT- Set to "production"- Any other required environment variables
-
Test Locally First
# Activate virtual environment source .venv/bin/activate # Test with production-like settings export DATABASE_URL="your-aiven-database-url" python api/main.py # Visit http://localhost:8000/health # Should show: {"database": "connected"}
-
Deploy to Vercel
# Commit changes git add . git commit -m "Fix: Remove table creation at startup for Vercel deployment" git push # Vercel will auto-deploy if connected to GitHub
-
Verify Deployment
- Check Vercel logs for any errors
- Visit
https://your-app.vercel.app/health - Should return database status: "connected"
- Test GraphQL endpoint:
https://your-app.vercel.app/graphql
Check your Aiven plan's connection limits:
- Hobbyist: 25 connections
- Startup: 100 connections
- Business: 200+ connections
Connect to your Aiven database and run:
-- Check current connections
SELECT count(*) FROM pg_stat_activity;
-- Check connection limit
SELECT setting FROM pg_settings WHERE name = 'max_connections';
-- See active connections by application
SELECT application_name, count(*)
FROM pg_stat_activity
GROUP BY application_name;-
Check Vercel Logs
vercel logs
-
Verify Database Accessibility
- Ensure Aiven database allows connections from Vercel IPs
- Check database SSL requirements
- Verify connection string format
-
Test Connection Locally
from sqlalchemy import create_engine, text from sqlalchemy.pool import NullPool engine = create_engine( "your-database-url", poolclass=NullPool ) with engine.connect() as conn: result = conn.execute(text("SELECT 1")) print("Connection successful!")
-
Check for Lingering Connections
- Old deployments might still hold connections
- Wait 5-10 minutes for connections to time out
- Or manually kill connections in Aiven console
-
Consider Connection Pooling Service
- Use PgBouncer or Aiven's connection pooling
- Reduces database connection load
- Better for high-traffic applications
-
Implement Query Timeout
connect_args = { 'connect_timeout': 10, 'command_timeout': 30, # Query timeout }
-
Add Request Timeout to Vercel
- Configure in
vercel.jsonif needed - Prevents long-running connections
- Configure in
-
Add Application Monitoring
- Sentry for error tracking
- DataDog or New Relic for APM
-
Database Monitoring
- Enable Aiven metrics
- Set up alerts for connection count
- Monitor query performance
- ✅
api/main.py- Removed table creation, added connection disposal - ✅
app/database/database.py- Explicit NullPool configuration - 📝
VERCEL_FIX.md- This documentation
- Run migrations on production database
- Commit and push these changes
- Deploy to Vercel
- Monitor logs and health endpoint
- Test GraphQL functionality
Date: October 13, 2025
Issue: Connection pool exhaustion on Vercel
Status: ✅ Fixed - Ready for deployment