Leveraging the Perf Tool for PostgreSQL Performance Optimization
POSTGRES BANGALORE MEETUP 7
Nitin Jadhav
Senior Software Engineer at Microsoft
Speaker: Nitin Jadhav
Agenda
Context and Scope
The path to CPU performance in PostgreSQL
Step | Category | Performance improvement method |
1 | DBA | Tune postgresql.conf parameters (e.g., work_mem, shared_buffers) |
2 | DBA | Optimize queries: add indexes, rewrite queries, update statistics |
3 | PostgreSQL hacker | Modify code: implement better algorithms |
4 | PostgreSQL hacker | Modify code: Make the planner smarter |
5 | PostgreSQL hacker | Modify code: leverage modern CPU architecture |
6 | Compiler hacker | Enhance compiler optimizations |
7 | CPU designer | Design faster CPUs. Utilise more transistors |
The path to CPU performance in PostgreSQL
Step | Category | Performance improvement method |
1 | DBA | Tune postgresql.conf parameters (e.g., work_mem, shared_buffers) |
2 | DBA | Optimize queries: add indexes, rewrite queries, update statistics |
3 | PostgreSQL hacker | Modify code: implement better algorithms |
4 | PostgreSQL hacker | Modify code: Make the planner smarter |
5 | PostgreSQL hacker | Modify code: leverage modern CPU architecture |
6 | Compiler hacker | Enhance compiler optimizations |
7 | CPU designer | Design faster CPUs. Utilise more transistors |
CPU Performance Fundamentals
Cache Locality
Branch Misprediction
Pipelined Execution
Cache Locality
Branch Misprediction
Impact:
Primary Causes:
How to Reduce Mispredictions:
Instruction Execution Stages
Instruction Fetch
Instruction Decode
Execute
Memory Access
Write Back
Pipelined Execution
Pipelined Execution – Data Hazards
Problem: Conflict of operands between instructions
Example: Need result of previous instruction
Pipelined Execution – Control Hazards
Pipelined Execution – Key Insights
The Perf Tool
Perf - Introduction
What is perf?�A powerful Linux performance analysis tool for developers and system administrators.
Purpose:
Capabilities:
Perf - Workflows
1. perf list to find events.
2. perf stat to count the events.
3. perf record to write events to a file.
4. perf report to browse the recorded file.
Perf - List the Available Events
Purpose: Displays all available performance monitoring events.
Event Types Included:
Behavior:
Perf - View CPU Real-Time System Profile
Command: sudo perf top
Purpose: Monitor CPU activity and performance in real time.
What It Shows:
Key Features:
Perf - View CPU Performance Statistics with perf
Purpose: Collect detailed CPU performance metrics for a command or workload.
What It Provides:
Use Cases:
Perf - View CPU Performance for a Command
Purpose: Measure CPU performance metrics while running a specific command.
What It Provides:
Use Cases:
Perf - Record and View Performance Results
Purpose: Capture detailed performance data for later analysis.
What It Does:
Follow-Up Analysis:
Benefits:
Connect CPU Concepts with Perf (with examples)
Example 1
Program1.c
Program2.c
Example 1 – Perf Results
Program1.c
Program2.c
Conclusion: Since Program2.c
allocates and stores data
sequentially, it achieves better
cache utilization, which reduces
CPU cycles and ultimately speeds
up execution.
Example 2
Program3.c
Program4.c
Example 2 – Perf Results
Program3.c
Program4.c
Conclusion: Since Program2.c
enables the CPU to predict
branches accurately, it minimizes
branch mispredictions, which in
turn speeds up execution.
Apply Perf Insights to PostgreSQL
Refactor ExecScan() to allow inlining of its core logic
Problem
Solution
Solution
Solution
Perf Results
Before
After
Conclusion: This leads to fewer instructions and branches, reducing branch misses
and ultimately improving overall performance.
Perf Results - Before
Perf Results - After
Acknowledgements
Thank you