Sub topic 13
Huge pages for large shared memory
This chapter assumes you can read PostgreSQL settings, edit postgresql.conf, and restart a test server. You should also know that shared_buffers is PostgreSQL’s shared buffer cache and that a restart differs from a configuration reload.
What huge pages change
A huge page is an operating-system memory page that is larger than the usual page size. On many Linux systems, a normal page is 4 KiB. A huge page may be 2 MiB or another platform-specific size.
PostgreSQL stores its shared memory, including shared_buffers, in memory pages. A large shared memory area requires many page-table entries when it uses normal pages. Page tables map virtual addresses used by PostgreSQL to physical RAM. Huge pages reduce the number of entries required. They can also reduce translation lookaside buffer (TLB) misses, which happen when the processor cannot quickly find a recent address mapping.
Huge pages mainly help servers with large shared memory areas. They do not make more RAM available, and they do not increase the memory available to an individual sort, hash, or connection.
PostgreSQL’s huge-page setting
The huge_pages setting controls whether PostgreSQL requests huge pages for its main shared memory area.
offdisables huge pages.tryrequests huge pages but allows PostgreSQL to start with normal pages if they are unavailable.onrequires huge pages. PostgreSQL refuses to start if it cannot allocate them.
try is a safe starting point when testing. Use on only after you have verified the operating-system configuration and startup process.
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name IN ('shared_buffers', 'huge_pages', 'huge_page_size');huge_pages has a postmaster context, so changing it requires a server restart. A reload is not enough. The pending_restart column shows whether a changed value is waiting for that restart.
Configuring Linux huge pages
Linux reserves explicit huge pages before PostgreSQL starts. The required number depends on the shared memory size and the huge-page size. Check the current state first:
grep -E 'HugePages_Total|HugePages_Free|Hugepagesize' /proc/meminfoA result might show 2 MiB huge pages. If PostgreSQL needs 8 GiB of shared memory, the rough minimum is:
8 GiB ÷ 2 MiB = 4096 huge pagesReserve a test amount with root privileges:
sudo sysctl -w vm.nr_hugepages=4096
grep -E 'HugePages_Total|HugePages_Free|Hugepagesize' /proc/meminfoMake the value persistent through your operating-system configuration, such as a systemd sysctl file, according to your distribution’s practice. Leave room for the operating system, PostgreSQL private memory, filesystem cache, and other services.
Set PostgreSQL to try huge pages, then restart it:
ALTER SYSTEM SET huge_pages = 'try';After the restart, verify the active value and inspect the server log. A successful allocation may be logged during startup. If huge_pages = on causes startup failure, change the setting back using a configuration file or ALTER SYSTEM while the server is stopped, then start PostgreSQL again.
Transparent huge pages are different
Linux Transparent Huge Pages (THP) are an operating-system feature that attempts to combine normal pages automatically. They are different from PostgreSQL’s explicit huge_pages setting. THP can introduce unpredictable allocation or compaction pauses on some database workloads. Check your platform’s PostgreSQL guidance and benchmark before enabling it.
Exercise
Which setting is safer for an initial test, huge_pages = on or huge_pages = try, and why?
Show answer
try is safer because PostgreSQL can fall back to normal pages when the operating system has not reserved enough huge pages. on makes startup fail if huge pages cannot be allocated.