Draft
Conversation
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.
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.