Production Sizing Summary (PGSizer — Why max_connections=500 Killed Our DB): shared_buffers = 25% RAM | Pool via PgBouncer
- Legacy Bottleneck: Managed RDS + OOM Spikes ($265/mo (3,000 IOPS Cap))
- Right-Sized Target: Tuned KVM + PgBouncer ($19.99/mo (32GB NVMe))
- Interactive Browser Calculator & 1-Click Config: PGSizer — Why max_connections=500 Killed Our DB
1. Real-World Incident Post-Mortem
At 02:14 UTC during a payroll batch, a 16GB PostgreSQL 17 primary vanished with kernel SIGKILL after max_connections=500 collided with work_mem=64MB across parallel hash joins.
Each analytical query spawned four parallel workers with three hash and sort nodes, so 65 active sessions allocated over 15GB of private heap and forced OOM-Killer onto postmaster.
Capping max_connections at 80 behind PgBouncer transaction pooling and lowering work_mem to 16MB stabilized p99 latency at 8ms on a $19.99/mo Hostinger KVM 8 NVMe server.
2. Audit Live Postgres Connection Heap, Temp Spills & Buffer Hit Ratio
Before resizing instances or migrating production workloads, run this baseline diagnostic sequence on your Linux host:
psql -U postgres -d app_prod -c "SELECT state, count(*) FROM pg_stat_activity GROUP BY 1;" psql -U postgres -d app_prod -c "SHOW shared_buffers; SHOW work_mem; SHOW effective_cache_size;" psql -U postgres -d app_prod -c "SELECT datname, temp_files, pg_size_pretty(temp_bytes) FROM pg_stat_database;" psql -U postgres -d app_prod -c "SELECT round(100.0*sum(blks_hit)/nullif(sum(blks_hit+blks_read),0),2) AS hit_pct FROM pg_stat_database;" grep -E "Out of memory|Killed process" /var/log/kern.log | tail -n 5 iostat -xz 1 3 Start by grouping pg_stat_activity by state: if idle sessions outnumber active queries ten-to-one, unpooled backend processes are wasting roughly 9MB of RSS memory apiece.
Next, check temp_bytes alongside iostat await: climbing temp_files means work_mem is spilling sorts onto NVMe, whereas kernel OOM entries prove per-node work_mem was set too high.
3. Production Reference Configuration & Guardrails
Core deterministic sizing equation used by PGSizer:
shared_buffers = 25% RAM | Pool via PgBouncer 01. work_mem multiplies per sort & worker 02. Set random_page_cost = 1.1 on NVMe 03. Cap max_connections at 80 behind pooler Reference deployment snippet (pgsizer-audit.sh):
# Interactive vCPU/RAM Sizer & Config Generator: https://pgsizer.pages.dev/en/?src=auto_devto_pgsizer_c1 #!/usr/bin/env bash # PostgreSQL 17 Memory, Temp Spill & IOPS Quick Audit — https://pgsizer.pages.dev/ DB_NAME="${1:-postgres}" psql -X -U postgres -d "$DB_NAME" <<'SQL' SELECT name, setting, unit FROM pg_settings WHERE name IN ('max_connections','shared_buffers','work_mem','hash_mem_multiplier','effective_cache_size','random_page_cost'); SELECT state, count(*) AS backends FROM pg_stat_activity GROUP BY 1 ORDER BY 2 DESC; SELECT datname, temp_files, pg_size_pretty(temp_bytes) AS spilled_to_nvme, round(100.0 * blks_hit / nullif(blks_hit + blks_read, 0), 2) AS cache_hit_pct FROM pg_stat_database WHERE datname = current_database(); SQL 4. Four Critical Production Gotchas to Avoid
- PostgreSQL allocates work_mem per sort or hash node per parallel worker, and PG 17 defaults hash_mem_multiplier to 2.0, doubling hash table memory silently.
- Leaving random_page_cost at the legacy 4.0 spinning-disk default tricks the query planner into full sequential scans on fast NVMe; lower it to 1.1 immediately.
- PgBouncer pool_mode=transaction breaks session-level SET variables, LISTEN/NOTIFY channels, and unnamed prepared statements unless client drivers enable protocol-level support.
- Pushing shared_buffers past 40% of physical RAM creates double-buffering against Linux page cache and causes nasty checkpoint I/O stalls without checkpoint_completion_target=0.9.
5. Architecture Perspective: Vertical Headroom vs Premature Complexity
Paying $260/mo for managed RDS db.m6i.xlarge still caps baseline gp3 storage at 3,000 IOPS, whereas local AMD EPYC NVMe on Hostinger KVM or UltaHost VDS delivers raw 4K IOPS.
Doubling VPS RAM before deploying PgBouncer rarely fixes connection exhaustion; it simply gives hundreds of idle Postgres forks more memory to burn during context switches.
6. Zero-Downtime Cutover Checklist
- [ ] Deploy PgBouncer in transaction mode on port 6432 and cap PostgreSQL max_connections between 60 and 120 based on physical vCPU cores.
- [ ] Set shared_buffers to 25% of RAM, effective_cache_size to 75% of RAM, random_page_cost=1.1, and wal_compression=zstd in postgresql.conf.
- [ ] Enable Linux huge_pages=try with explicit vm.nr_hugepages sizing so large shared_buffers avoid multi-gigabyte page-table TLB miss overhead.
- [ ] Stream continuous WAL archives plus daily pgBackRest snapshots to off-box S3 object storage and rehearse point-in-time recovery before cutover.
7. Frequently Asked Engineering Questions
Why not set shared_buffers to 80% of RAM like MySQL InnoDB?
PostgreSQL relies heavily on the Linux OS page cache for compressed filesystem reads and sort spill files. Keeping shared_buffers near 25% avoids double-caching pages while leaving safe headroom for work_mem.
When should we step up from Hostinger KVM 8 to UltaHost VDS?
Once hot B-tree indexes exceed 24GB or sustained WAL write throughput demands zero CPU steal under 24/7 OLTP load, move to dedicated physical RAM and NVMe RAID on a 32GB or 64GB UltaHost VDS.
Model your exact workload concurrency, RAM headroom, and 36-month infrastructure TCO directly in the browser at PGSizer (pgsizer.pages.dev).
