Skip to content

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

Merged
hunleyd merged 13 commits intodevelopfrom
INDATA-459
Feb 26, 2026
Merged

feat(tuned): enable tuned and disable THP#2060
hunleyd merged 13 commits 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.

(I did a ripgrep across all repos for transparent_hugepage and the only references I saw were in AppArmour profiles which were not setting a value)

@hunleyd hunleyd force-pushed the INDATA-459 branch 2 times, most recently from 8dc395c to 15cb28c Compare February 20, 2026 18:51
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.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a PostgreSQL-focused tuned setup intended to disable Transparent Huge Pages (THP) and ensures PostgreSQL starts after tuned so kernel tuning is applied before DB startup.

Changes:

  • Bumps PostgreSQL package version strings in Ansible vars.
  • Adds an Ansible task block to install/configure tuned, create a postgresql tuned profile, and activate it.
  • Adds a systemd drop-in for postgresql.service to order startup after tuned.service.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
ansible/vars.yml Updates Postgres release version strings used by the build.
ansible/tasks/setup-tuned.yml Installs/configures tuned, creates a postgresql profile, and applies it (including THP-related settings).
ansible/tasks/setup-postgres.yml Creates a systemd drop-in to order PostgreSQL startup after tuned.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

hunleyd and others added 8 commits February 25, 2026 15:00
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* 'develop' of github.com:supabase/postgres:
  feat: bump PostgREST to 14.5 (#2057)
  feat: bump oriole to latest (#2028)
  fix: bump AMI version (#2059)
Add test cases to test_ami_nix.py to ensure the 'postgresql' tuned
profile is active and Transparent Huge Pages (THP) are set to 'never',
validating the recent performance optimizations.
…-459

* 'INDATA-459' of github.com:supabase/postgres:
  Update ansible/tasks/setup-tuned.yml
  Update ansible/tasks/setup-tuned.yml
  Update ansible/tasks/setup-postgres.yml
  Update ansible/tasks/setup-postgres.yml
  Update ansible/tasks/setup-postgres.yml
@hunleyd
Copy link
Contributor Author

hunleyd commented Feb 25, 2026

============================================================
Test Summary
============================================================

ENGINE          RESULT
------          ------
15              PASSED (509s)
17              PASSED (514s)
17-oriole       PASSED (478s)

============================================================
Total: 3 | Passed: 3 | Failed: 0
============================================================

[INFO] Postgres commit: 28f4057139b9920125ea39bfd05c1b27e9ad7d47
[INFO] Cache location: /Users/jimchancojr/.cache/supadev/postgres/28f4057139b9920125ea39bfd05c1b27e9ad7d47
[SUCCESS] All tests passed!

@blacksmith-sh

This comment has been minimized.

@hunleyd hunleyd marked this pull request as ready for review February 26, 2026 16:45
@hunleyd hunleyd requested review from a team as code owners February 26, 2026 16:45
@hunleyd hunleyd added this pull request to the merge queue Feb 26, 2026
Merged via the queue into develop with commit 70368d2 Feb 26, 2026
52 of 53 checks passed
@hunleyd hunleyd deleted the INDATA-459 branch February 26, 2026 20:25
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.

3 participants