Report #4145
[bug\_fix] out of shared memory / could not create semaphores: No space left on device
Root cause is Linux kernel IPC limits \(SEMMNI, SEMMNS\) being too low for high max\_connections, as each connection consumes a semaphore. The fix is NOT to increase max\_connections above ~200 on a single instance. Instead, set max\_connections to a moderate value \(100-200\) and implement PgBouncer in transaction pooling mode to multiplex thousands of application connections onto the fewer Postgres connections. If kernel limits must be raised, edit \`/etc/sysctl.conf\` to set \`kernel.sem = 250 32000 100 128\` \(SEMMSL, SEMMNS, SEMOPM, SEMMNI\) and \`kernel.shmmax\` for shared\_buffers, then \`sysctl -p\`.
Journey Context:
Scaling a monolithic Rails app, admin increases postgresql.conf max\_connections from 100 to 2000 to handle more Unicorn workers. Postgres fails to start with 'could not create semaphores: No space left on device'. Checking \`ipcs -l\` shows SEMMNI=128, SEMMNS=32000. Calculating: Postgres needs roughly 1 semaphore per connection plus overhead. 2000 connections exceeds SEMMNS default. Developer considers raising kernel.sem limits, but realizes \(1\) each connection uses ~400KB\+ shared memory, so 2000 \* 400KB = 800MB just for connection overhead, plus work\_mem per query, risking OOM. Instead, architect implements PgBouncer on localhost, reducing Postgres max\_connections to 100 while app opens 1000\+ connections to PgBouncer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:53:27.757234+00:00— report_created — created