Categories
big sur osx postgresql

SHMMAX or SHMALL error on Big Sur

When upgrading to Big Sur with Homebrew, you may find errors on starting Postgres where there’s memory errors.

2021-03-30 17:26:07.203 AEST [47951] FATAL: could not create shared memory segment: Cannot allocate memory
2021-03-30 17:26:07.203 AEST [47951] DETAIL: Failed system call was shmget(key=5432001, size=56, 03600).
2021-03-30 17:26:07.203 AEST [47951] HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMALL parameter. You might need to reconfigure the kernel with larger SHMALL.
The PostgreSQL documentation contains more information about shared memory configuration.
2021-03-30 17:26:07.204 AEST [47951] LOG: database system is shut down
stopped waiting
pg_ctl: could not start server

The short term (until reboot) solution lies here:

https://stackoverflow.com/questions/2017004/setting-shmmax-etc-values-on-mac-os-x-10-6-for-postgresql/10629164#10629164

Basically:

sudo sysctl -w kern.sysv.shmmax=12582912
sudo sysctl -w kern.sysv.shmall=12582912

But for a more permanent switch, since Catalina, editing /etc/sysctl.conf doesn’t work.

After further digging, the solution to this problem is to place a plist file into /Library/LaunchDaemons/ which runs and sets the shared memory parameters on boot. On Catalina this has to be preceded by disabling SIP, and remounting the / file system as writable, before installing the plist file.

https://developer.apple.com/forums/thread/669625

so, go to /Library/LaunchDaemons/, create a file (I called mine memory.plist, and make sure the owner is correct!), add the content below, and then run
sudo launchctl load memory.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>Label</key>
 <string>shmemsetup</string>
 <key>UserName</key>
 <string>root</string>
 <key>GroupName</key>
 <string>wheel</string>
 <key>ProgramArguments</key>
 <array>
  <string>/usr/sbin/sysctl</string>
  <string>-w</string>
  <string>kern.sysv.shmmax=268435456</string>
  <string>kern.sysv.shmmni=128</string>
  <string>kern.sysv.shmseg=32</string>
  <string>kern.sysv.shmall=65536</string>
 </array>
 <key>KeepAlive</key>
 <false/>
 <key>RunAtLoad</key>
 <true/>
</dict>
</plist>

Once you’ve rebooted you can confirm by running

$ sysctl -A kern.sysv.shmall
kern.sysv.shmall: 65536

$ sysctl -A kern.sysv.shmmax
kern.sysv.shmmax: 268435456