tk0miya / testing.postgresql

Apache License 2.0
290 stars 46 forks source link

FATAL: could not create shared memory segment: Cannot allocate memory #43

Open caminale opened 1 year ago

caminale commented 1 year ago

testing.postgresql Version

testing.postgresql==1.3.0
python 3.10
on MacOs M1

Debug Output

python3.10/site-packages/testing/common/database.py:40: in __init__
    self.cache = self.target_class(**self.settings)
python3.10/site-packages/testing/common/database.py:96: in __init__
    self.setup()
python3.10/site-packages/testing/common/database.py:125: in setup
    self.initialize_database()
python3.10/site-packages/testing/postgresql.py:104: in initialize_database
    raise RuntimeError("initdb failed: %r" % err)
E   RuntimeError: initdb failed: b'2023-02-03 09:50:29.610 CET [20851] FATAL:  could not create shared memory segment: Cannot allocate memory\n2023-02-03 09:50:29.610 CET [20851] DETAIL:  Failed system call was shmget(key=40827474, size=56, 03600).\n2023-02-03 09:50:29.610 CET [20851] 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.\n\tThe PostgreSQL documentation contains more information about shared memory configuration.\nchild process exited with exit code 1\ninitdb: removing contents of data directory "/var/folders/j6/d40048xdd118w7q8jz_7f2cc0000gn/T/tmpiyy6vqob/data"\n'

Expected Behavior

As a developper I expect no error about memory.

Actual Behavior

Several times a day I have a memory shared error, the lib cannot allocate memory. The only solution who solve my problem is to restart my computer.

Steps to Reproduce

I launch in debug mode and I kill the process then I have the problem. Randomly sometime when a test in development crash then I have the problem

nickgieschen commented 1 year ago

Same problem with the same specs. It had been running fine for two years.

petroslamb commented 11 months ago

This has been a perplexing problem for me on a Mac as well. Restarting the laptop is one way around it.

The error message indicates that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMALL parameter. You will need to reconfigure the kernel with a larger SHMALL to resolve this issue.

To increase the SHMALL parameter on your Mac, you can use the sysctl command.

You can first check the current values of SHMALL and SHMMAX using the following commands:

sysctl kern.sysv.shmall
sysctl kern.sysv.shmmax

To temporarily (until the next restart) increase these values, you can use the -w option with sysctl:

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

These commands increase the SHMALL and SHMMAX parameters to 1GB. Please note that these changes will be lost after a system reboot.

To make these changes permanent, you can add these settings to the /etc/sysctl.conf file:

echo 'kern.sysv.shmall=1073741824' | sudo tee -a /etc/sysctl.conf
echo 'kern.sysv.shmmax=1073741824' | sudo tee -a /etc/sysctl.conf

After modifying these settings, you will need to restart your PostgreSQL server for the changes to take effect. Please note that increasing these parameters will consume more system memory. You should adjust these values based on the available memory on your system.

If you continue to have issues after increasing these parameters, you might also need to increase the shared_buffers setting in your postgresql.conf file. This setting determines the amount of memory allocated to PostgreSQL for caching data. You can find this file in the PostgreSQL data directory, which is typically /usr/local/var/postgres for a Homebrew installation.

# in postgresql.conf
shared_buffers = 128MB

Again, you will need to restart the PostgreSQL server for changes to take effect.

Remember that these changes can affect the overall performance of your system. You should monitor your system's performance and adjust these values as needed.