Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/conn-pooling.md
1---2title: Use Connection Pooling for All Applications3impact: CRITICAL4impactDescription: Handle 10-100x more concurrent users5tags: connection-pooling, pgbouncer, performance, scalability6---78## Use Connection Pooling for All Applications910Postgres connections are expensive (1-3MB RAM each). Without pooling, applications exhaust connections under load.1112**Incorrect (new connection per request):**1314```sql15-- Each request creates a new connection16-- Application code: db.connect() per request17-- Result: 500 concurrent users = 500 connections = crashed database1819-- Check current connections20select count(*) from pg_stat_activity; -- 487 connections!21```2223**Correct (connection pooling):**2425```sql26-- Use a pooler like PgBouncer between app and database27-- Application connects to pooler, pooler reuses a small pool to Postgres2829-- Configure pool_size based on: (CPU cores * 2) + spindle_count30-- Example for 4 cores: pool_size = 103132-- Result: 500 concurrent users share 10 actual connections33select count(*) from pg_stat_activity; -- 10 connections34```3536Pool modes:3738- **Transaction mode**: connection returned after each transaction (best for most apps)39- **Session mode**: connection held for entire session (needed for prepared statements, temp tables)4041Reference: [Connection Pooling](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooler)42