Skip to content

Comments

feat(tuned): enable tuned and disable THP#2060

Draft
hunleyd wants to merge 1 commit intodevelopfrom
INDATA-459
Draft

feat(tuned): enable tuned and disable THP#2060
hunleyd wants to merge 1 commit intodevelopfrom
INDATA-459

Conversation

@hunleyd
Copy link
Contributor

@hunleyd hunleyd commented Feb 20, 2026

Implement a specialized tuned profile for PostgreSQL to disable Transparent Huge Pages (THP) and ensure proper service ordering.

Background: Transparent Huge Pages (THP)
In Linux, memory is typically managed in 4KB pages. Huge Pages allow the system to manage memory in much larger chunks (e.g., 2MB or 1GB), which reduces the overhead of the Translation Lookaside Buffer (TLB).

Transparent Huge Pages (THP) is a kernel feature that attempts to automate this by dynamically allocating huge pages for processes. While beneficial for some workloads, it is generally detrimental to database systems like PostgreSQL for several reasons:

  1. Memory Bloat & Fragmentation: THP can lead to significant memory waste. If a process only needs a small portion of a 2MB page, the entire 2MB is still allocated. In highly concurrent database environments, this often leads to rapid memory exhaustion.
  2. Latency Spikes (khugepaged): The kernel's khugepaged thread periodically scans memory to "collapse" standard pages into huge pages. This process can cause unpredictable latency spikes and CPU contention, often referred to as "stalls," which are unacceptable for high-performance database transactions.
  3. Inefficient I/O: PostgreSQL manages its own shared buffers and is optimized for 8KB blocks. The mismatch between the kernel's 2MB THP allocations and PostgreSQL's internal memory management can lead to inefficient paging and increased I/O pressure during page faults.

Summary of Changes

  1. PostgreSQL Service Ordering (ansible/tasks/setup-postgres.yml)
  • SystemD Overrides: Created a directory /etc/systemd/system/postgresql.service.d to house service customizations.
  • Dependency Management: Added a Unit dependency (After=tuned.service) via overrides.conf. This ensures that the tuned profile—and the resulting kernel optimizations—are fully applied before the PostgreSQL daemon initializes.
  1. PostgreSQL-Specific Tuned Profile (ansible/tasks/setup-tuned.yml)
  • Profile Definition: Created a new tuned profile directory and configuration at /etc/tuned/postgresql/tuned.conf.
  • THP Deactivation:
    • Runtime: Sets vm.transparent_hugepages=never to disable THP at the kernel level immediately.
    • Boot-time: Appends transparent_hugepages=never to the bootloader command line to ensure THP remains disabled after system reboots.
  • Service Activation: Restarts the tuned service to register the new profile and executes tuned-adm profile postgresql to apply the optimizations.

These changes ensure a stabler and more predictable performance profile for PostgreSQL by preventing kernel-level memory management interference.

Implement a specialized tuned profile for PostgreSQL to disable Transparent Huge Pages (THP) and ensure proper
  service ordering.

  Background: Transparent Huge Pages (THP)
  In Linux, memory is typically managed in 4KB pages. Huge Pages allow the system to manage memory in much larger chunks (e.g., 2MB
  or 1GB), which reduces the overhead of the Translation Lookaside Buffer (TLB).

  Transparent Huge Pages (THP) is a kernel feature that attempts to automate this by dynamically allocating huge pages for processes.
  While beneficial for some workloads, it is generally detrimental to database systems like PostgreSQL for several reasons:

   1. Memory Bloat & Fragmentation: THP can lead to significant memory waste. If a process only needs a small portion of a 2MB page,
      the entire 2MB is still allocated. In highly concurrent database environments, this often leads to rapid memory exhaustion.
   2. Latency Spikes (khugepaged): The kernel's khugepaged thread periodically scans memory to "collapse" standard pages into huge
      pages. This process can cause unpredictable latency spikes and CPU contention, often referred to as "stalls," which are
      unacceptable for high-performance database transactions.
   3. Inefficient I/O: PostgreSQL manages its own shared buffers and is optimized for 8KB blocks. The mismatch between the kernel's
      2MB THP allocations and PostgreSQL's internal memory management can lead to inefficient paging and increased I/O pressure
      during page faults.

  Summary of Changes

  1. PostgreSQL Service Ordering (ansible/tasks/setup-postgres.yml)
   * SystemD Overrides: Created a directory /etc/systemd/system/postgresql.service.d to house service customizations.
   * Dependency Management: Added a Unit dependency (After=tuned.service) via overrides.conf. This ensures that the tuned profile—and
     the resulting kernel optimizations—are fully applied before the PostgreSQL daemon initializes.

  2. PostgreSQL-Specific Tuned Profile (ansible/tasks/setup-tuned.yml)
   * Profile Definition: Created a new tuned profile directory and configuration at /etc/tuned/postgresql/tuned.conf.
   * THP Deactivation:
       * Runtime: Sets vm.transparent_hugepages=never to disable THP at the kernel level immediately.
       * Boot-time: Appends transparent_hugepages=never to the bootloader command line to ensure THP remains disabled after system
         reboots.
   * Service Activation: Restarts the tuned service to register the new profile and executes tuned-adm profile postgresql to apply
     the optimizations.

  These changes ensure a more stable and predictable performance profile for PostgreSQL by preventing kernel-level memory management
  interference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant