From 15cb28c1600f729b61c95b3f64801d4732839b03 Mon Sep 17 00:00:00 2001 From: Douglas J Hunley Date: Fri, 20 Feb 2026 12:52:17 -0500 Subject: [PATCH] feat(tuned): enable tuned and disable THP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- ansible/tasks/setup-postgres.yml | 23 +++++++++++ ansible/tasks/setup-tuned.yml | 71 ++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/ansible/tasks/setup-postgres.yml b/ansible/tasks/setup-postgres.yml index 9f34bcdbe..5b5e0c447 100644 --- a/ansible/tasks/setup-postgres.yml +++ b/ansible/tasks/setup-postgres.yml @@ -325,6 +325,29 @@ # Reload +- name: Create a SystemD override dir for PostgreSQL + ansible.builtin.file: + group: 'root' + mode: '0755' + owner: 'root' + path: '/etc/systemd/system/postgresql.service.d' + state: 'directory' + become: true + +- name: Ensure PostgrfeSQL starts after tuned + become: true + community.general.ini_file: + create: true + group: 'root' + mode: '0644' + no_extra_spaces: true + option: 'After' + owner: 'root' + path: '/etc/systemd/system/postgresql.service.d/overrides.conf' + section: 'Unit' + state: 'present' + value: 'tuned.service' + - name: System - systemd reload ansible.builtin.systemd_service: daemon_reload: true diff --git a/ansible/tasks/setup-tuned.yml b/ansible/tasks/setup-tuned.yml index 0809eede5..70c7d5313 100644 --- a/ansible/tasks/setup-tuned.yml +++ b/ansible/tasks/setup-tuned.yml @@ -1,10 +1,65 @@ -- name: Install tuned - ansible.builtin.apt: - force_apt_get: true - name: tuned - policy_rc_d: 101 - state: 'present' - update_cache: true - become: true +- name: INstall and configure tuned when stage2_nix when: - stage2_nix + block: + - name: Install tuned + ansible.builtin.apt: + force_apt_get: true + name: 'tuned' + policy_rc_d: 101 + state: 'present' + update_cache: true + become: true + + - name: Create a tuned profile directory + ansible.builtin.file: + group: 'root' + mode: '0755' + owner: 'root' + path: '/etc/tuned/postgresql' + state: 'directory' + become: true + + - name: Create a tuned profile + community.general.ini_file: + create: true + group: 'root' + mode: '0644' + no_extra_spaces: true + option: 'summary' + path: '/etc/tuned/postgresql/tuned.conf' + section: 'main' + state: 'present' + value: 'Tuned profile for PostgreSQL' + become: true + + - name: Disable Transparent Huge Pages (THP) + community.general.ini_file: + create: true + group: 'root' + mode: '0644' + no_extra_spaces: true + option: "{{ thp_item['option'] }}" + path: '/etc/tuned/postgresql/tuned.conf' + section: "{{ thp_item['section'] }}" + state: 'present' + value: "{{ thp_item['value'] }}" + become: true + loop: + - { section: 'bootloader', option: 'cmdline', value: 'transparent_hugepages=never' } + - { section: 'vm', option: 'transparent_hugepages', value: 'never' } + loop_control: + loop_var: 'thp_item' + + - name: Activate the tuned service + ansible.builtin.systemd_service: + daemon_reload: true + enabled: true + name: 'tuned' + state: 'restarted' + become: true + + - name: Activate the PostgreSQL tuned profile + ansible.builtin.command: + cmd: tuned-adm profile postgresql + become: true