DEV Community

Dimas Adiputro
Dimas Adiputro

Posted on • Edited on

Enable Huge Pages in PostgreSQL

PostgreSQL are recommend to enable Huge Pages.
I am using Redhat Family

if run this command HugePages_Total and HugePages_Free value are 0 it means that huge page is not enable.

[postgres@pg1]$  cat /proc/meminfo | grep -i huge
AnonHugePages:     49152 kB
ShmemHugePages:        0 kB
FileHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
Enter fullscreen mode Exit fullscreen mode

I am using Ibrar Ahmed from percona for the formula

#!/bin/bash
pid=`head -1 $PGDATA/postmaster.pid`
echo "Pid:            $pid"
peak=`grep ^VmPeak /proc/$pid/status | awk '{ print $2 }'`
echo "VmPeak:            $peak kB"
hps=`grep ^Hugepagesize /proc/meminfo | awk '{ print $2 }'`
echo "Hugepagesize:   $hps kB"
hp=$((peak/hps))
echo Set Huge Pages:     $hp
Enter fullscreen mode Exit fullscreen mode

we can run and the output like below

[postgres@pg1 ~]$ sh hg.sh
Pid:            1631
VmPeak:            8921740 kB
Hugepagesize:   2048 kB
Set Huge Pages:     4356
Enter fullscreen mode Exit fullscreen mode

we can set in /etc/sysctl.conf to apply we can use sysctl -p using root user

vm.nr_hugepages = 4356
Enter fullscreen mode Exit fullscreen mode

After hugepages is set, however we need to restart the server to get the effect in postgresql
Below is the output once huge page is enable succefully.

cat /proc/meminfo | grep -i huge
AnonHugePages:     30720 kB
ShmemHugePages:        0 kB
FileHugePages:         0 kB
HugePages_Total:    4356
HugePages_Free:     4238
HugePages_Rsvd:     4100
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:         8921088 kB

Enter fullscreen mode Exit fullscreen mode

we can set huge_page to on or keep as try in postgresql.conf

Controls whether huge pages are requested for the main shared memory area. Valid values are try (the default), on, and off. With huge_pages set to try, the server will try to request huge pages, but fall back to the default if that fails. With on, failure to request huge pages will prevent the server from starting up. With off, huge pages will not be requested. The actual state of huge pages is indicated by the server variable huge_pages_status. https://www.postgresql.org/docs/17/runtime-config-resource.html#GUC-HUGE-PAGES

I prefer to set huge_pages to be on;

postgres=# show huge_pages;
 huge_pages
------------
 on
(1 row)

postgres=# \q
[postgres@pg1 ]$ 
[postgres@pg1 ]$ cat /proc/meminfo | grep -i huge
AnonHugePages:     30720 kB
ShmemHugePages:        0 kB
FileHugePages:         0 kB
HugePages_Total:    4356
HugePages_Free:     4247
HugePages_Rsvd:     4109
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:         8921088 kB
[postgres@pg1 ]$

Enter fullscreen mode Exit fullscreen mode

Top comments (0)