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-limits.md
1---2title: Set Appropriate Connection Limits3impact: CRITICAL4impactDescription: Prevent database crashes and memory exhaustion5tags: connections, max-connections, limits, stability6---78## Set Appropriate Connection Limits910Too many connections exhaust memory and degrade performance. Set limits based on available resources.1112**Incorrect (unlimited or excessive connections):**1314```sql15-- Default max_connections = 100, but often increased blindly16show max_connections; -- 500 (way too high for 4GB RAM)1718-- Each connection uses 1-3MB RAM19-- 500 connections * 2MB = 1GB just for connections!20-- Out of memory errors under load21```2223**Correct (calculate based on resources):**2425```sql26-- Formula: max_connections = (RAM in MB / 5MB per connection) - reserved27-- For 4GB RAM: (4096 / 5) - 10 = ~800 theoretical max28-- But practically, 100-200 is better for query performance2930-- Recommended settings for 4GB RAM31alter system set max_connections = 100;3233-- Also set work_mem appropriately34-- work_mem * max_connections should not exceed 25% of RAM35alter system set work_mem = '8MB'; -- 8MB * 100 = 800MB max36```3738Monitor connection usage:3940```sql41select count(*), state from pg_stat_activity group by state;42```4344Reference: [Database Connections](https://supabase.com/docs/guides/platform/performance#connection-management)45