Troubleshooting 'CONNECT_TIMEOUT' or hanging queries in Serverless Functions
Last edited: 6/9/2026
Intermittent CONNECT_TIMEOUT errors or queries that hang until reaching the execution limit may occur when using persistent clients, such as postgres-js, within Serverless Functions.
Why Does This Happen?
Serverless environments may freeze the function between requests. This causes TCP sockets to go stale because the connection pooler or NAT may drop inactive connections while the client-side keepalive timers are paused. Upon resuming the function, the client attempts to reuse the dead socket, leading to a hang.
How to Resolve This:
- Use the Data API: Migrate read-heavy workloads to the Supabase Data API (PostgREST). This utilizes stateless HTTP requests and avoids persistent TCP socket issues.
- Liveness Preflight: For transactional paths using
postgres-js, implement a preflight check by racing aSELECT 1query against a short timeout (e.g., a few seconds) before executing the primary query. If the preflight times out, recycle the database client. - Migrate to Vercel Fluid Compute: Use Vercel's Fluid Compute with
attachDatabasePool. This environment supports thewaitUntilhook, which allows the runtime to close idle connections properly before the instance suspends. - Application Retries: Implement application-level retry logic specifically for connection timeout errors to force a new connection attempt.