JRehkemper.de

Setup HugePages for PostgreSQL

By configuring HugePages, the memory gets split into larger pages. This allows for a more efficient memory usage, if your process consumes large amounts of memory. This is usually the case with databases.

Enable HugePages on Linux

To enable HugePages we need to modify our Kernel-Parameters.

To calculate the number of HugePages, we need to know how much memory Postgres may use. This is usually the available amount of memory in idle. This value gets divided by the HugePageSize, which is usually 2048 KB.

Example: Our System has 8GB Memory.
Postgres may use 6GB.
HugePageSize is 2MB.

(6 * 1024) / 2 = 3072

Now we can open the sysctl.conf and add this value.

vim /etc/sysctl.conf

# You need to calculate the number for your system
# SharedMemory for Postgres / HugepageSize
vm.nr_hugepages = 3072

Now we need to stop Postgres before applying the kernel parameter.

systemctl stop postgresql

Then we can apply the kernel parameter.

[tux@server]$ sysctl -p

After that we can start Postgres again.

[tux@server]$ systemctl start postgresql

Configure Postgres

By default Postgres tries to use HugePages. So there is no need to change this.
If you want Postgres only to start, if HugePages are available, you can set the following config parameter.

[tux@server]$ vim /var/lib/pgsql/data/postgresql.conf

huge_pages = on

After that you need to restart Postgres.

[tux@server]$ systemctl restart postgresql
profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.