Re: huge_pages=on cause could not map anonymous shared memory: Cannot allocate memory - Mailing list pgsql-general

From Alicja Kucharczyk
Subject Re: huge_pages=on cause could not map anonymous shared memory: Cannot allocate memory
Date
Msg-id CAM=sWa5eM2_M_jqdYnU45nOuB_R=V8i8koS_hX0cAFjjSChv+w@mail.gmail.com
Whole thread Raw
In response to huge_pages=on cause could not map anonymous shared memory: Cannot allocate memory  (Bogdan Siara <bogdan.siara@gmail.com>)
List pgsql-general
Hi Bogdan,
The root cause here is that the number of huge pages you've configured (vm.nr_hugepages = 980) is not sufficient.
Each huge page on your system is 2 MB in size, so 980 pages give you roughly 1.96 GB of memory (980 × 2 MB). However, PostgreSQL is clearly requesting about 2.2 GB of shared memory (specifically, 2204106752 bytes as shown in the error message you provided), which exceeds what's available through huge pages.

That’s why PostgreSQL fails to start when huge_pages = on - it requires the entire shared memory segment to come from huge pages and refuses to fall back to regular ones.

Earlier, you had the huge_pages setting commented out, which means PostgreSQL used the default value: huge_pages = try. In that mode, it first attempts to use huge pages, but if that fails (like in your case due to insufficient allocation), it falls back to standard memory pages — which is why the instance started without issues then.

To fix the issue, you should increase vm.nr_hugepages to at least 1100 to fully cover the shared memory request (you can go a bit higher to be safe and then reduce it as described in the article I'm pasting the link to).

Also, a side note: max_connections = 1000 is quite high for an instance with 8 GB of RAM and only 2 vCPUs. Even if huge pages are properly allocated, such a high number of connections can lead to performance issues. You might want to consider lowering it or using a connection pooler like PgBouncer.

If you’d like to understand how huge pages work in PostgreSQL, including how to calculate memory needs and configure the OS properly, I wrote a detailed article some time ago (still valid). It’s in Polish, which I assume is fine for you: https://linuxpolska.com/pl/baza-wiedzy/blog/postgres-pamieci-ram-tipstricks/

best regards,
Alicja Kucharczyk

pgsql-general by date:

Previous
From: Bogdan Siara
Date:
Subject: huge_pages=on cause could not map anonymous shared memory: Cannot allocate memory
Next
From: Greg Sabino Mullane
Date:
Subject: Re: Pgbackrest failure for INCR and DIFF but not FULL backup