postgresql_16:4.4.4.5 Linux Huge Pages
PostgreSQL 16 > Server Administration > Server Setup and Operation > Managing Kernel Resources > Linux Huge Pages
Using huge pages reduces overhead when using large contiguous chunks of memory, as PostgreSQL does, particularly when using large values of (see guc-shared-buffers). To use this feature in PostgreSQL you need a kernel with CONFIG_HUGETLBFS=y and CONFIG_HUGETLB_PAGE=y. You will also have to configure the operating system to provide enough huge pages of the desired size. The runtime-computed parameter (see guc-shared-memory-size-in-huge-pages) reports the number of huge pages required. This parameter can be viewed before starting the server with a postgres command like: $ postgres -D $PGDATA -C shared_memory_size_in_huge_pages 3170 $ grep ^Hugepagesize /proc/meminfo Hugepagesize: 2048 kB $ ls /sys/kernel/mm/hugepages hugepages-1048576kB hugepages-2048kB In this example the default is 2MB, but you can also explicitly request either 2MB or 1GB with (see guc-huge-page-size) to adapt the number of pages calculated by shared_memory_size_in_huge_pages. While we need at least 3170 huge pages in this example, a larger setting would be appropriate if other programs on the machine also need huge pages. We can set this with: # sysctl -w vm.nr_hugepages=3170 Don't forget to add this setting to /etc/sysctl.conf so that it is reapplied after reboots. For non-default huge page sizes, we can instead use: # echo 3170 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages It is also possible to provide these settings at boot time using kernel parameters such as hugepagesz=2M hugepages=3170.
Sometimes the kernel is not able to allocate the desired number of huge pages immediately due to fragmentation, so it might be necessary to repeat the command or to reboot. (Immediately after a reboot, most of the machine's memory should be available to convert into huge pages.) To verify the huge page allocation situation for a given size, use: $ cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
It may also be necessary to give the database server's operating system user permission to use huge pages by setting vm.hugetlb_shm_group via sysctl, and/or give permission to lock memory with ulimit -l.
The default behavior for huge pages in PostgreSQL is to use them when possible, with the system's default huge page size, and to fall back to normal pages on failure. To enforce the use of huge pages, you can set (see guc-huge-pages) to on in postgresql.conf. Note that with this setting PostgreSQL will fail to start if not enough huge pages are available.
For a detailed description of the Linux huge pages feature have a look at https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt.