Conversation
8dc395c to
15cb28c
Compare
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.
Contributor
There was a problem hiding this comment.
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 apostgresqltuned profile, and activate it. - Adds a systemd drop-in for
postgresql.serviceto order startup aftertuned.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.
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>
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
Contributor
Author
|
This comment has been minimized.
This comment has been minimized.
za-arthur
approved these changes
Feb 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Summary of Changes
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)