PostgreSQL & PgBouncer Memory & Connection Pool Calculator
Calculate safe shared_buffers, effective_cache_size, per-sort work_mem, max_connections, and PgBouncer pool limits from your container RAM and peak client concurrency. Grounded in our Docker lab benchmark where PgBouncer 1.23.1 cut 450-connection backend RSS from 739.5 MiB down to 84.8 MiB.
1. Container & Workload Parameters
0.5–4096 GB (or 256–4,194,304 MiB).5–10,000).0.5–256 vCores) to size default_pool_size.Advanced Parameter Overrides (Optional)
Without PgBouncer, 110 direct backends consume ~748.0 MiB in per-process RSS alone (~6.8 MiB/backend). With PgBouncer Transaction pooling, active backend + pooler RSS drops to ~173.7 MiB (verified against our PostgreSQL & PgBouncer Docker connection-pooling benchmark: 739.5 MiB unpooled vs 84.8 MiB pooled).
postgresql.conf
pgbouncer.ini
Why Unpooled PostgreSQL Connections Exhaust Container RAM
PostgreSQL spawns a dedicated OS backend process (postgres: user db [client]) for every client TCP connection instead of multiplexing sockets inside a single thread pool. Even an idle backend initializes local catalog caches, lock tables, and process state totaling roughly 6.5 MiB to 9.5 MiB of private RSS once it touches a few relations. Raising max_connections to 500 on a 1 GB or 2 GB Docker container without a pooler invites two failure modes: immediate FATAL: sorry, too many clients already (SQLSTATE 53300) during webhook bursts, or kernel cgroup OOMKilled (Exit Code 137) when concurrent sorts allocate multiple work_mem buffers simultaneously.
In our PostgreSQL & PgBouncer Docker connection-pooling benchmark, holding 450 direct PostgreSQL connections consumed 739.5 MiB of container RSS. Placing PgBouncer 1.23.1 in front of PostgreSQL in transaction mode served 110 concurrent clients across a 20-backend pool (default_pool_size = 20) at just 84.8 MiB combined RSS with zero dropped queries.
Architecture Matrix: Unpooled Postgres vs PgBouncer vs SQLite WAL
| Database Topology | Observed RAM / Concurrency Behavior | Best Fit Stack |
|---|---|---|
| Direct PostgreSQL (No Pooler) | ~6.5 to 9.5 MiB RSS per backend; 450 backends used 739.5 MiB RSS; hits SQLSTATE 53300 above max_connections. |
Low-client internal jobs (< 30 persistent connections) or session-lock workers. |
PostgreSQL + PgBouncer (transaction) |
84.8 MiB combined RSS serving 110 clients via 20 server backends (~2 KiB per queued client socket). | Production n8n PostgreSQL queue stacks, FastAPI, Next.js serverless functions. |
| SQLite WAL + Application Mutex | Zero separate DB daemon; 200/200 writes succeeded at 129.7 ms p95 under 50-way HTTP burst when serialized in-process. | Single-container embedded apps (see SQLite WAL benchmark). |
PostgreSQL & PgBouncer Sizing FAQ
Why can high work_mem crash PostgreSQL even when shared_buffers is only 25% of RAM?
PostgreSQL allocates shared_buffers once at startup, whereas work_mem can be allocated multiple times per active query for every hash join or sort node. Fifty unpooled backends running two sort nodes each with work_mem = 64MB can demand 6,400 MiB of anonymous memory and trigger a Linux cgroup OOM kill.
Does PgBouncer transaction pooling support PostgreSQL prepared statements?
PgBouncer 1.21 and newer (including 1.23.1) support protocol-level prepared statements in transaction mode when you set max_prepared_statements = 100 (or higher) in pgbouncer.ini. On older PgBouncer releases, clients had to disable prepared statements to avoid prepared statement does not exist errors.
How should max_connections relate to PgBouncer default_pool_size?
Set PostgreSQL max_connections higher than the sum of PgBouncer default_pool_size plus reserve_pool_size across all database pools, leaving 10 to 15 spare slots above superuser_reserved_connections = 3 for pg_dump backups, migrations, and interactive psql debugging.
Deploy Your Tuned Database Stack in Production
PgBouncer Docker Compose blueprints, n8n SQLite-to-PostgreSQL migrations, SQLite WAL benchmarks, and volume backups.
Generate Compose v2 YAML with mem_limit, 127.0.0.1 port bindings, and healthchecks for Postgres.
Align upstream Nginx proxy timeouts (60s–3600s), SSE streaming rules, and PHP-FPM workers with your database pool.
Size GGUF/AWQ weights, GQA KV cache across parallel slots, decode tok/s, and Ollama, vLLM, or llama.cpp flags.
Compute macOS 2x HiDPI framebuffer Unified Memory (MiB), Retina PPI zones, and DP 1.4 / TB4 / TB5 DSC link bandwidth.