Sub topic 13
Huge pages for large shared memory
This chapter assumes you can edit postgresql.conf, know which settings need a restart, and have root access to the database host to change kernel settings. It covers Linux, where most PostgreSQL servers run. Huge pages affect how the operating system maps shared_buffers. They do not change how PostgreSQL uses that memory.
Why page size matters
The CPU does not address RAM directly. Each process uses virtual addresses, and the kernel keeps a page table that maps them to physical memory. The unit of mapping is the page. On x86-64 Linux the default page size is 4 kB.
Two costs grow with the number of pages:
- Page table size. Every process that touches shared memory needs its own page table entries for it.
shared_buffersis shared memory, so each backend maps it separately. With 4 kB pages, an 8 GB region is about 2 million pages. At 8 bytes per entry, that is roughly 16 MB of page table per backend that has touched all of it. With hundreds of connections, this adds up to gigabytes of kernel memory that never shows up inshared_buffersorwork_mem. - TLB misses. The translation lookaside buffer (TLB) is a small CPU cache of recent address translations. When a workload touches many scattered pages, the TLB misses often, and each miss costs a page table walk.
Huge pages are larger pages, typically 2 MB on x86-64 (1 GB is also possible on some hardware). The same 8 GB needs about 4,000 entries instead of 2 million. The page tables shrink by a factor of about 512, and the TLB covers far more memory.
Huge pages are most useful when shared_buffers is several gigabytes or more, when there are many backends, or when the workload churns through buffers. For a small instance with a 512 MB shared_buffers, the gain is usually too small to measure.
How PostgreSQL uses them
PostgreSQL allocates its main shared memory segment with mmap. On Linux it can ask the kernel for explicitly reserved huge pages. These come from a pool you configure with the vm.nr_hugepages sysctl. Pages in that pool are reserved up front, are never swapped out, and cannot be used for ordinary allocations.
The relevant settings:
| Setting | Context | Purpose |
|---|---|---|
| `huge_pages` | postmaster (restart) | `try` (default), `on`, or `off` |
| `huge_page_size` | postmaster (restart) | Page size to request. `0` (default) uses the kernel default. Available since PostgreSQL 14. |
| `shared_memory_size_in_huge_pages` | read-only | Number of huge pages the server needs. Available since PostgreSQL 15. |
| `huge_pages_status` | read-only | Whether huge pages are actually in use. Available since PostgreSQL 17. |
The three values of huge_pages behave differently:
offnever requests huge pages.tryrequests them and silently falls back to normal pages if the kernel cannot supply them. This is the default, and it is why many servers run without huge pages and nobody notices.onrequests them and refuses to start if the request fails.
A change to any of these needs a full restart. A reload is not enough.
Sizing the huge page pool
The pool must cover the whole shared memory segment, not only shared_buffers. The segment also holds WAL buffers, lock tables, and other structures, so it is somewhat larger than shared_buffers.
Since PostgreSQL 15, the server computes the exact number. You can read it from a stopped server, so you can size the pool before the first start with huge pages:
# Run as the OS user that owns the data directory
postgres -C shared_memory_size_in_huge_pages -D /var/lib/postgresql/16/mainThe command prints a number such as 4305. On a running server, the same value is available in SQL:
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name IN ('huge_pages', 'huge_page_size', 'shared_memory_size_in_huge_pages');The value depends on the current shared_buffers, max_connections, wal_buffers, and other settings. If you change those, recompute it.
On older versions, estimate it yourself. Read the VmPeak line from /proc/<postmaster pid>/status while the server runs, divide by the huge page size, and round up. Add a small margin.
Reserving the pages
Set the pool size with a sysctl. Round the computed number up a little, because an exact fit leaves no room for other consumers of the pool:
# /etc/sysctl.d/90-postgresql.conf
vm.nr_hugepages = 4400Apply it without a reboot:
sudo sysctl --system
grep -i huge /proc/meminfoIn the /proc/meminfo output, check Hugepagesize (usually 2048 kB), HugePages_Total (should equal your setting), and HugePages_Free. Reservation can fail on a machine that has been up for a long time. Memory fragmentation means the kernel cannot always find enough contiguous 2 MB blocks. If HugePages_Total is lower than you asked for, set the sysctl at boot, or reserve the pages before PostgreSQL starts.
The reserved memory is taken away from everything else. A pool of 4400 pages is about 8.6 GB that the page cache, work_mem, and backends cannot use. Include it in your memory budget from the earlier chapter on connection budgeting.
Enabling and verifying
- Compute
shared_memory_size_in_huge_pages. - Set
vm.nr_hugepagesslightly above it, and confirmHugePages_Totalin/proc/meminfo. - Set
huge_pages = oninpostgresql.conf. Usingoninstead oftrymakes a sizing mistake fail at startup instead of passing unnoticed. - Restart PostgreSQL.
- Verify the result.
On PostgreSQL 17 and later, the check is one command:
SHOW huge_pages_status;The result is on, off, or unknown. On earlier versions, look at the kernel counters. While the server runs, HugePages_Free should have dropped by roughly the size of the segment, and HugePages_Rsvd and the used count (HugePages_Total minus HugePages_Free) should reflect it:
grep -E 'HugePages_(Total|Free|Rsvd)' /proc/meminfoTransparent huge pages are a different feature
Linux also has transparent huge pages (THP). The kernel tries to merge ordinary 4 kB pages into huge ones in the background, with no reservation. This sounds convenient, but it has caused trouble for database workloads. Background compaction and page merging can add latency spikes. The PostgreSQL documentation notes that THP may cause performance problems on some systems, and many operators set it to madvise or never:
cat /sys/kernel/mm/transparent_hugepage/enabledThe brackets mark the active mode, for example always [madvise] never. Explicit huge pages, as described in this chapter, are not affected by this setting. You can use them while THP is off.
Measuring the benefit
Do not expect large throughput changes in every workload. Typical gains show up in these places:
- Lower kernel memory use. Compare
PageTablesin/proc/meminfobefore and after, with a similar number of connections. It should fall noticeably on a server with a largeshared_buffersand many backends. - Slightly lower CPU time on buffer-heavy read workloads, because of fewer TLB misses.
- More predictable memory: the segment is locked in RAM and cannot be swapped.
Apply the workflow from the last chapter: record the baseline, make this one change, and compare. Because the setting needs a restart, plan it for a maintenance window. Restarting also empties shared_buffers, so wait for the cache to warm before you compare numbers.
Common problems
- Server fails to start with
huge_pages = on. The log reports that it could not map anonymous shared memory. The pool is too small, or fragmentation prevented the reservation. Increasevm.nr_hugepagesor reserve at boot. - Huge pages never used with
try. The pool was zero or too small. CheckHugePages_Total. - Settings changed, pool not resized. After raising
shared_buffersormax_connections, the segment grows. Recompute the pool. - Containers. The container needs access to the host's huge page pool. In Kubernetes this means requesting
hugepages-2Mias a resource. Without it, the server falls back or fails. - Windows. The equivalent is large pages, which requires the "Lock pages in memory" privilege for the service account. The
huge_pagessetting applies there too.
Exercise
A server has sharedbuffers = 16GB and maxconnections = 400. You set huge_pages = try, restart, and see no errors. How do you find out whether huge pages are in use, and what would you change if they are not?
Show answer
On PostgreSQL 17 or later, run SHOW hugepagesstatus. On older versions, compare HugePagesFree in /proc/meminfo before and after startup. If the free count did not drop, the pool is too small or empty, so the server fell back to 4 kB pages. Compute the need with postgres -C sharedmemorysizeinhugepages (PostgreSQL 15 or later), set vm.nrhugepages slightly above it, and confirm HugePagesTotal. Then set huge_pages = on so a future shortfall fails at startup, and restart.
Exercise
Why does hugepages = on make a good safety setting once you have sized the pool, and why does changing sharedbuffers later call for another look at vm.nr_hugepages?
Show answer
With on, the server refuses to start if it cannot get huge pages, so a pool that is too small is noticed immediately. With try, it silently falls back. The shared memory segment includes sharedbuffers plus other structures, so raising sharedbuffers (or max_connections) increases the number of huge pages required. The pool must be recomputed and enlarged, or the server will fail to start under on.