supabase-postgres.git / summary / log / commit / refs

commit    70368d2e84af129d191eca20348be213911cf700
Author:   Douglas J Hunley <doug.hunley@gmail.com>
Commit:   GitHub <noreply@github.com>
Date:     Thu Feb 26 19:53:00 2026 +0000

    feat(tuned): enable tuned and disable THP (#2060)
    
    * feat(tuned): enable tuned and disable THP
    
    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.
    
    * Update ansible/tasks/setup-postgres.yml
    
    Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
    
    * Update ansible/tasks/setup-postgres.yml
    
    Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
    
    * Update ansible/tasks/setup-postgres.yml
    
    Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
    
    * Update ansible/tasks/setup-tuned.yml
    
    Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
    
    * Update ansible/tasks/setup-tuned.yml
    
    Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
    
    * test(tuned): verify tuned profile and THP settings
    
    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.
    
    * Update test_ami_nix.py
    
    ---------
    
    Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>


ansible/tasks/setup-postgres.yml | 36 ++++++++++++++++++++ ansible/tasks/setup-tuned.yml | 73 +++++++++++++++++++++++++++++++++++----- 2 files changed, 100 insertions(+), 9 deletions(-) diff --git a/ansible/tasks/setup-postgres.yml b/ansible/tasks/setup-postgres.yml index 9f34bcdb..be6fa084 100644 --- a/ansible/tasks/setup-postgres.yml +++ b/ansible/tasks/setup-postgres.yml @@ -325,6 +325,42 @@ # 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 PostgreSQL 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/override.conf' + section: 'Unit' + state: 'present' + value: 'tuned.service' + +- name: Ensure PostgreSQL wants tuned + become: true + community.general.ini_file: + create: true + group: 'root' + mode: '0644' + no_extra_spaces: true + option: 'Wants' + 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 0809eede..78c7557e 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 + - (stage2_nix or nixpkg_mode) + 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_hugepage=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 [parent: 578022e930fb]