How to Disable Transparent Huge Pages (THP) for Optimal PostgreSQL 15 Performance on a Linux Server
Steps to Turn Off Transparent Huge Pages (THP) to Boost PostgreSQL 15 on Linux
Disabling Transparent Huge Pages (THP) can help improve the performance of PostgreSQL by preventing potential latency spikes caused by the defragmentation process that THP performs. Here's a step-by-step guide on how to disable Transparent Huge Pages for optimal performance of your PostgreSQL server.
Step 1: Verify the Current THP Status
Before disabling THP, you can check its current status by running the following commands:
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
The output will show the current status of THP and whether it is enabled or disabled.
Step 2: Disable THP at Runtime
To disable THP without rebooting the server, you can run the following commands:
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
These commands will disable THP until the next reboot. To make this change permanent, follow the next steps.
Step 3: Disable THP Permanently
To disable THP permanently, you need to modify the kernel boot parameters. This can be done by editing the GRUB configuration file.
- Edit the GRUB Configuration File
Open the GRUB configuration file in a text editor:
sudo nano /etc/default/grub
- Modify the GRUB_CMDLINE_LINUX_DEFAULT Parameter
Find the line that starts with GRUB_CMDLINE_LINUX_DEFAULT
and add transparent_hugepage=never
to the list of parameters. It should look something like this:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash transparent_hugepage=never"
- Update GRUB
After modifying the GRUB configuration file, update GRUB to apply the changes:
sudo update-grub
Step 4: Reboot the Server
Reboot the server to apply the changes:
sudo reboot
Step 5: Verify THP is Disabled
After the server reboots, verify that THP is disabled by running the following commands again:
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
The output should show that THP is set to never
.
Automate the Process Using a Startup Script (Optional)
If you prefer not to modify GRUB or want to ensure that THP is disabled at every boot, you can create a startup script.
- Create a Script to Disable THP
Create a new script file, for example, /etc/init.d/
disable-thp.sh
:
sudo nano /etc/init.d/disable-thp.sh
Add the following content to the script:
#!/bin/bash
### BEGIN INIT INFO
# Provides: disable-thp
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Disable Transparent Huge Pages (THP)
### END INIT INFO
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
- Make the Script Executable
Make the script executable:
sudo chmod +x /etc/init.d/disable-thp.sh
- Register the Script to Run at Startup
Register the script to run at startup:
sudo update-rc.d disable-thp.sh defaults
Conclusion
Disabling Transparent Huge Pages (THP) can help improve PostgreSQL performance by reducing latency spikes caused by THP's defragmentation process. By following the steps above, you can disable THP both temporarily and permanently. Regularly monitor your PostgreSQL performance to ensure that the changes have a positive impact.