Friday, November 19, 2010

Postgres unable to start with shmget. SHMMAX too small

Error Starting PostgreSQL, Could not create shared memory segment errors



If you have a large PostgreSQL configuration that supports many concurrent requests, you may find yourself starting it as you have increased the shared_buffers parameter or max_connections parameter, and it fails to start.

The error message will say something like could not create shared memory segment, Failed system call was shmget. This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. SHMMAX too small.

Here is the exact error message I got:


debian-testing:~# /etc/init.d/postgresql-8.3 start
Starting PostgreSQL 8.3 database server: mainThe PostgreSQL server failed to start. Please check the log output: 2010-11-17 13:53:34 EST FATAL: could not create shared memory segment: Invalid argument 2010-11-17 13:53:34 EST DETAIL: Failed system call was shmget(key=5432001, size=147382272, 03600). 2010-11-17 13:53:34 EST HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. To reduce the request size (currently 147382272 bytes), reduce PostgreSQL's shared_buffers parameter (currently 16384) and/or its max_connections parameter (currently 503). If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for. The PostgreSQL documentation contains more information about shared memory configuration. failed!
failed!


PostgreSQL Debian increase SHMMAX



In order to increase the maximum amount of shared memory a linux program can access, we need to adjust kernel parameters. Since we are using Debian they are available in /etc/sysctl.conf. This should be similar for many linux distributions.

We simple edit this file:

sudo vim /etc/sysctl.conf


or if you have GNOME running

sudo gedit /etc/sysctl.conf


And either search for the line or add the line (most likely will have to add) :


kernel.shmmax = 147382272


Note the number here is the same as "size" in this system call, shown in my error message above: shmget(key=5432001, size=147382272, 03600)

Now we have to put these settings into effect by running the command


sysctl -p


And now we start PostgreSQL up without the error:


debian-testing:~# /etc/init.d/postgresql-8.3 start
Starting PostgreSQL 8.3 database server: main.
debian-testing:~#

2 comments:

  1. I would like to know why the total parameter 147382272 is not an exact multiple of 1024 x 1024: like 140 x1024 x1024.

    ReplyDelete
  2. As I said in the post, I used 147382272, because that is the size postgres attempted to "get", or reserve, and failed, as shown in the error message:

    debian-testing:~# /etc/init.d/postgresql-8.3 start
    Starting PostgreSQL 8.3 database server: mainThe PostgreSQL server failed to start. Please check the log output: 2010-11-17 13:53:34 EST FATAL: could not create shared memory segment: Invalid argument 2010-11-17 13:53:34 EST DETAIL: Failed system call was shmget(key=5432001, size=147382272, 03600). 2010-11-17 13:53:34 EST HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. To reduce the request size (currently 147382272 bytes), reduce PostgreSQL's shared_buffers parameter (currently 16384) and/or its max_connections parameter (currently 503). If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for. The PostgreSQL documentation contains more information about shared memory configuration. failed!

    ReplyDelete