SlideShare a Scribd company logo
Tune Box Linux Anda, bukan hanya PostgreSQL
Ibrar Ahmed
Tune your Linux Box, not just PostgreSQL
Linux Kernel Tuning for PostgreSQL performance
Senior Database Architect - Percona LLC
September 08 – 12, 2019
Database Performance
• Choose better Hardware
• Operating System Selection and Tuning
• Choose Database Depend based on Workload
• Tune your Database.
• Tune your Queries
• Tune your Application
SELECT * FROM foo
PostgreSQL Modules
Clients
Processes
PostgreSQL Memory
Kernel Memory
Disk Management
Background Writer
Shared Buffers
Default = 128MB
WAL Writer
Checkpointer
Archiver
Logger
Postmaster
Port = 5432
Postgres Postgres
Client - psql Client - JDBC Client - ODBC
base/ Datafiles
pg_wal/ WAL files
global
Configuration Files
Table Spaces$PGDATA
/dev/sda /dev/sdb
WAL Buffers
Default = 16MB
work_mem
maintancework_mem
4K 2MB/1GB Huge Pages THP
PostgreSQL Performance Tuning
PostgreSQL Tuning
• shared_buffers
• wal_buffers
• effective_cache_size
• work_mem
• maintenance_work_mem
• synchronous_commit
• checkpoint_timeout
• checkpoint_completion_target
PostgreSQL Memory
Shared Buffers
Default = 128MB
WAL Buffers
Default = 16MB
work_mem
maintancework_mem
Disk Management
base/ Datafiles
pg_wal/ WAL files
global
Configuration Files
Table Spaces$PGDATA
/dev/sda /dev/sdb
shared_buffer
• PostgreSQL uses its own buffer and also uses kernel buffered I/O.
• PostgreSQL does not change the information on disk directly then how?
• Writes the data to shared buffer cache.
• The backend process write that these blocks kernel buffer.
postgresql=# SHOW shared_buffers;
shared_buffers
----------------
128MB
(1 row)
The proper size for the POSTGRESQL shared buffer cache is the largest useful size that does not adversely affect other activity.
—Bruce Momjian
How/When data written to disk actually?
PostgreSQL Tuning / shared_buffer
Configuraing WAL
• Do you have Transaction? Obviously
• WAL – (Write Ahead LOG) Log your transactions
• Size of WAL files 16MB with 8K Block size (can be changed at compile time)
• PostgreSQL writes WAL into the buffers(wal_buffer ) and then these buffers are
flushed to disk.
Tip: Bigger value for wal_buffer in case of lot of concurrent connection gives better performance.
effective_cache_size
• This used by the optimizer to estimate the size of the kernel's disk buffer cache.
• The effective_cache_size provides an estimate of the memory available for disk
caching.
• It is just a guideline, not the exact allocated memory or cache size.
TIP: It should be large enough to hold most accessed tables, but at the same time small enough to avoid swap.
work_mem
This configuration is used for complex sorting.
maintenance_work_mem
maintenance_work_mem is a memory setting used for maintenance tasks.
• The default value is 64MB.
• Setting a large value helps in tasks like VACUUM, RESTORE, CREATE INDEX, ADD FOREIGN
KEY and ALTER TABLE.
synchronous_commit
•This is used to enforce that commit will wait for WAL to be written on disk
before returning a success status to the client.
•This is a trade-off between performance and reliability.
•Increasing reliability decreases performance and vice versa.
checkpoint_timeout
•PostgreSQL writes changes into WAL. The checkpoint process flushes
the data into the data files.
•More checkpoints have a negative impact on performance.
Linux Kernel Tuning
Input Output Handling
• Direct IO, Buffered IO and Double buffering
• PostgreSQL believes that the Operating system (Kernel) knows much better about
storage and IO scheduling.
• PostgreSQL has its own buffering; and also needs the pages cache. Double Buffering
• It Increase the use of memory.
• And different kernel and setting behave differently.
Virtual Memory
https://en.wikipedia.org/wiki/Virtual_memory
https://en.wikipedia.org/wiki/Page_table
• Every process is given the impression that it is working with
large, contiguous sections of memory
• Each process runs in its own dedicated address space
• Pages Table are used to translate the virtual addresses
seen by the application into Physical Address
Translation Lookaside Buffer (TLB)
• Translation Lookaside Buffer is a memory cache
• It reduce the time to access a user memory location
• If a match is found the physical address of the
page is returned → TLB hit
• If match not found scan the page table (walk)
looking for the address mapping (entry) → TLB miss
Small page size bigger TLB size → expensive
Memory Pages
•Different OS has different size of Pages
•Linux has a concept of Classic Huge Pages and Transparent Huge
Pages.
•BSD has Super Pages.
•Windows has Large Pages.
Linux Page sizes
4K 67108864
2M 131072
1G 256
large/huge pages
• Linux Page size is 4K
• Many modern processors support other page sizes
If we consider a server with 256G of RAM:
Classic Huge Pages 1/3
# cat /proc/meminfo
MemTotal: 264041660 kB
...
Hugepagesize: 2048 kB
DirectMap4k: 128116 kB
DirectMap2M: 3956736 kB
DirectMap1G: 266338304 kB
sysctl -w vm.nr_hugepages=256
Classic Huge Pages 2/3 – Setting Huge Page Size
# vi /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="hugepagesz=1GB default_hugepagesz=1G”
# update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-75-generic
Found initrd image: /boot/initrd.img-4.4.0-75-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
Done
# shutdown -r now
Classic Huge Pages 3/3 – Enabling / Disabling in PostgreSQL
# vim /etc/postgresql/10/main/postgresql.conf
huge_pages=ON # default is try
# service postgresql restart
Transparent Huge pages
• The kernel works in the background (khugepaged) trying to:
"create" huge pages.
• Find enough contiguous blocks of memory
• Convert them into a huge page
• Transparently allocate them to processes when there is a "fit"
Disabling Transparent Huge pages
# cat /proc/meminfo | grep AnonHuge
AnonHugePages: 2048 kB
# ps aux | grep huge
root 42 0.0 0.0 0 0 ? SN Jan17 0:00 [khugepaged]
To disable it:
• at runtime:
# echo never > /sys/kernel/mm/transparent_hugepage/enabled
# echo never > /sys/kernel/mm/transparent_hugepage/defrag
• at boot time:
GRUB_CMDLINE_LINUX_DEFAULT="(...) transparent_hugepage=never"
vm.swappiness
• This is another kernel parameter that can affect the performance of the
database.
•Used to control the swappiness (swapping pages to swap memory into
RAM) behavior on a Linux system.
•This parameter's can take anything from “0” to “100”, default is 60.
•Higher value means more aggressively swap.
vm.overcommit_memory and vm.overcommit_ratio
• Applications acquire memory and free that memory when it is no longer
needed.
• But in some cases an application acquires too much memory and does not
release it. This can invoke the OOM killer.
• Heuristic overcommit, Do it intelligently (default); based kernel heuristics
• Allow overcommit anyway
• Don’t over commit beyond the overcommit ratio.
vm.dirty_background_ratio and vm.dirty_background_bytes
•The vm.dirty_background_ratio is the percentage of memory filled with
dirty pages that need to be flushed to disk.
•Flushing is done in the background.
• The value of this parameter ranges from 0 to 100;
vm.dirty_ratio / vm.dirty_bytes
•The vm.dirty_background_ratio is the percentage of memory filled with
dirty pages that need to be flushed to disk.
•Flushing is done in the foreground.
• The value of this parameter ranges from 0 to 100;
Blogs
•Tuning PostgreSQL Database Parameters to Optimise Performance.
▪ https://www.percona.com/blog/2018/08/31/tuning-postgresql-database-parameters-to-opti
mize-performance/
•Tune Linux Kernel Parameters For PostgreSQL Optimisation
▪ https://www.percona.com/blog/2018/08/29/tune-linux-kernel-parameters-for-postgresql-op
timization/
?
“Poor leaders rarely ask questions of themselves or others. Good leaders,
on the other hand, ask many questions. Great leaders ask the great
questions.”
Michael Marquardt author of
Leading with Questions

More Related Content

PDF
PGConf.ASIA 2019 Bali - Building PostgreSQL as a Service with Kubernetes - Ta...
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - AppOS: PostgreSQL Extension for Scalable File I/O - K...
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - PostgreSQL on K8S at Zalando - Alexander Kukushkin
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
Equnix Business Solutions
 
PDF
20190909_PGconf.ASIA_KaiGai
Kohei KaiGai
 
PDF
PGConf.ASIA 2019 Bali - Fault Tolerance in PostgreSQL - Muhammad Haroon
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 - High Availability, 10 Seconds Failover - Lucky Haryadi
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - Setup a High-Availability and Load Balancing PostgreS...
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - Building PostgreSQL as a Service with Kubernetes - Ta...
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - AppOS: PostgreSQL Extension for Scalable File I/O - K...
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - PostgreSQL on K8S at Zalando - Alexander Kukushkin
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
Equnix Business Solutions
 
20190909_PGconf.ASIA_KaiGai
Kohei KaiGai
 
PGConf.ASIA 2019 Bali - Fault Tolerance in PostgreSQL - Muhammad Haroon
Equnix Business Solutions
 
PGConf.ASIA 2019 - High Availability, 10 Seconds Failover - Lucky Haryadi
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - Setup a High-Availability and Load Balancing PostgreS...
Equnix Business Solutions
 

What's hot (20)

PDF
GPGPU Accelerates PostgreSQL (English)
Kohei KaiGai
 
PDF
A guide of PostgreSQL on Kubernetes
t8kobayashi
 
PDF
Deploying postgre sql on amazon ec2
Denish Patel
 
PDF
Postgres in Amazon RDS
Denish Patel
 
PDF
PGConf.ASIA 2019 Bali - Foreign Data Wrappers - Etsuro Fujita & Tatsuro Yamada
Equnix Business Solutions
 
ODP
Hug Hbase Presentation.
Jack Levin
 
PPT
KSCOPE 2013: Exadata Consolidation Success Story
Kristofferson A
 
PDF
Countdown to PostgreSQL v9.5 - Foriegn Tables can be part of Inheritance Tree
Ashnikbiz
 
PDF
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
Danielle Womboldt
 
PDF
PGConf.ASIA 2019 Bali - Performance Analysis at Full Power - Julien Rouhaud
Equnix Business Solutions
 
PPTX
Latest performance changes by Scylla - Project optimus / Nolimits
ScyllaDB
 
PDF
Gluster.community.day.2013
Udo Seidel
 
PDF
PGConf.ASIA 2019 Bali - Keynote Speech 2 - Ivan Pachenko
Equnix Business Solutions
 
PPTX
MySQL Head-to-Head
Patrick McGarry
 
ODP
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Experts, Inc.
 
PDF
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
PostgreSQL-Consulting
 
PDF
PostgreSQL HA
haroonm
 
PDF
TeraCache: Efficient Caching Over Fast Storage Devices
Databricks
 
PDF
Case Studies on PostgreSQL
InMobi Technology
 
PDF
Elephants in the Cloud
Mike Fowler
 
GPGPU Accelerates PostgreSQL (English)
Kohei KaiGai
 
A guide of PostgreSQL on Kubernetes
t8kobayashi
 
Deploying postgre sql on amazon ec2
Denish Patel
 
Postgres in Amazon RDS
Denish Patel
 
PGConf.ASIA 2019 Bali - Foreign Data Wrappers - Etsuro Fujita & Tatsuro Yamada
Equnix Business Solutions
 
Hug Hbase Presentation.
Jack Levin
 
KSCOPE 2013: Exadata Consolidation Success Story
Kristofferson A
 
Countdown to PostgreSQL v9.5 - Foriegn Tables can be part of Inheritance Tree
Ashnikbiz
 
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
Danielle Womboldt
 
PGConf.ASIA 2019 Bali - Performance Analysis at Full Power - Julien Rouhaud
Equnix Business Solutions
 
Latest performance changes by Scylla - Project optimus / Nolimits
ScyllaDB
 
Gluster.community.day.2013
Udo Seidel
 
PGConf.ASIA 2019 Bali - Keynote Speech 2 - Ivan Pachenko
Equnix Business Solutions
 
MySQL Head-to-Head
Patrick McGarry
 
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Experts, Inc.
 
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
PostgreSQL-Consulting
 
PostgreSQL HA
haroonm
 
TeraCache: Efficient Caching Over Fast Storage Devices
Databricks
 
Case Studies on PostgreSQL
InMobi Technology
 
Elephants in the Cloud
Mike Fowler
 
Ad

Similar to PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed (20)

PDF
Linux Huge Pages
Geraldo Netto
 
PDF
Colvin exadata mistakes_ioug_2014
marvin herrera
 
PDF
MariaDB Server Performance Tuning & Optimization
MariaDB plc
 
PDF
Linux tuning to improve PostgreSQL performance
PostgreSQL-Consulting
 
PDF
Optimizing elastic search on google compute engine
Bhuvaneshwaran R
 
PDF
Running ElasticSearch on Google Compute Engine in Production
Searce Inc
 
PPTX
Investigate SQL Server Memory Like Sherlock Holmes
Richard Douglas
 
PPTX
Maximizing performance via tuning and optimization
MariaDB plc
 
PPTX
Maximizing performance via tuning and optimization
MariaDB plc
 
PDF
Webinar slides: Our Guide to MySQL & MariaDB Performance Tuning
Severalnines
 
PDF
Performance tuning in sql server
Antonios Chatzipavlis
 
PPTX
Oracle Performance On Linux X86 systems
Baruch Osoveskiy
 
PDF
MySQL Performance Tuning London Meetup June 2017
Ivan Zoratti
 
PPTX
515689311-Postgresql-DBA-Architecture.pptx
ssuser03ec3c
 
PPTX
Postgresql Database Administration Basic - Day1
PoguttuezhiniVP
 
PDF
Tuning Linux Windows and Firebird for Heavy Workload
Marius Adrian Popa
 
PPTX
Taking Splunk to the Next Level - Architecture Breakout Session
Splunk
 
PPT
MySQL Performance Tuning at COSCUP 2014
Ryusuke Kajiyama
 
PPTX
CPU Caches
shinolajla
 
PDF
Buytaert kris my_sql-pacemaker
kuchinskaya
 
Linux Huge Pages
Geraldo Netto
 
Colvin exadata mistakes_ioug_2014
marvin herrera
 
MariaDB Server Performance Tuning & Optimization
MariaDB plc
 
Linux tuning to improve PostgreSQL performance
PostgreSQL-Consulting
 
Optimizing elastic search on google compute engine
Bhuvaneshwaran R
 
Running ElasticSearch on Google Compute Engine in Production
Searce Inc
 
Investigate SQL Server Memory Like Sherlock Holmes
Richard Douglas
 
Maximizing performance via tuning and optimization
MariaDB plc
 
Maximizing performance via tuning and optimization
MariaDB plc
 
Webinar slides: Our Guide to MySQL & MariaDB Performance Tuning
Severalnines
 
Performance tuning in sql server
Antonios Chatzipavlis
 
Oracle Performance On Linux X86 systems
Baruch Osoveskiy
 
MySQL Performance Tuning London Meetup June 2017
Ivan Zoratti
 
515689311-Postgresql-DBA-Architecture.pptx
ssuser03ec3c
 
Postgresql Database Administration Basic - Day1
PoguttuezhiniVP
 
Tuning Linux Windows and Firebird for Heavy Workload
Marius Adrian Popa
 
Taking Splunk to the Next Level - Architecture Breakout Session
Splunk
 
MySQL Performance Tuning at COSCUP 2014
Ryusuke Kajiyama
 
CPU Caches
shinolajla
 
Buytaert kris my_sql-pacemaker
kuchinskaya
 
Ad

More from Equnix Business Solutions (20)

PDF
Yang perlu kita ketahui Untuk memahami aspek utama IT dalam bisnis_.pdf
Equnix Business Solutions
 
PDF
Kebocoran Data_ Tindakan Hacker atau Kriminal_ Bagaimana kita mengantisipasi...
Equnix Business Solutions
 
PDF
Kuliah Tamu - Dari Proses Bisnis Menuju Struktur Data.pdf
Equnix Business Solutions
 
PDF
EWTT22_ Apakah Open Source Cocok digunakan dalam Korporasi_.pdf
Equnix Business Solutions
 
PDF
Oracle to PostgreSQL, Challenges to Opportunity.pdf
Equnix Business Solutions
 
PDF
[EWTT2022] Strategi Implementasi Database dalam Microservice Architecture.pdf
Equnix Business Solutions
 
PDF
PostgreSQL as Enterprise Solution v1.1.pdf
Equnix Business Solutions
 
PDF
Webinar2021 - Does HA Can Help You Balance Your Load-.pdf
Equnix Business Solutions
 
PDF
Webinar2021 - In-Memory Database, is it really faster-.pdf
Equnix Business Solutions
 
PDF
EQUNIX - PPT 11DB-Postgres™.pdf
Equnix Business Solutions
 
PPTX
equpos - General Presentation v20230420.pptx
Equnix Business Solutions
 
PDF
Equnix Appliance- Jawaban terbaik untuk kebutuhan komputasi yang mumpuni.pdf
Equnix Business Solutions
 
PDF
OSPX - Professional PostgreSQL Certification Scheme v20201111.pdf
Equnix Business Solutions
 
PDF
Equnix Company Profile v20230329.pdf
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 - The Future of TDEforPG - Taiki Kondo
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 - PGSpider High Performance Cluster Engine - Shigeo Hirose
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - Mission Critical Production High Availability Postgre...
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - Keynote Speech 3 - Kohei KaiGai
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - Keynote Speech 1 - Bruce Momjian
Equnix Business Solutions
 
PDF
PGConf.ASIA 2019 Bali - Modern PostgreSQL Monitoring & Diagnostics - Mahadeva...
Equnix Business Solutions
 
Yang perlu kita ketahui Untuk memahami aspek utama IT dalam bisnis_.pdf
Equnix Business Solutions
 
Kebocoran Data_ Tindakan Hacker atau Kriminal_ Bagaimana kita mengantisipasi...
Equnix Business Solutions
 
Kuliah Tamu - Dari Proses Bisnis Menuju Struktur Data.pdf
Equnix Business Solutions
 
EWTT22_ Apakah Open Source Cocok digunakan dalam Korporasi_.pdf
Equnix Business Solutions
 
Oracle to PostgreSQL, Challenges to Opportunity.pdf
Equnix Business Solutions
 
[EWTT2022] Strategi Implementasi Database dalam Microservice Architecture.pdf
Equnix Business Solutions
 
PostgreSQL as Enterprise Solution v1.1.pdf
Equnix Business Solutions
 
Webinar2021 - Does HA Can Help You Balance Your Load-.pdf
Equnix Business Solutions
 
Webinar2021 - In-Memory Database, is it really faster-.pdf
Equnix Business Solutions
 
EQUNIX - PPT 11DB-Postgres™.pdf
Equnix Business Solutions
 
equpos - General Presentation v20230420.pptx
Equnix Business Solutions
 
Equnix Appliance- Jawaban terbaik untuk kebutuhan komputasi yang mumpuni.pdf
Equnix Business Solutions
 
OSPX - Professional PostgreSQL Certification Scheme v20201111.pdf
Equnix Business Solutions
 
Equnix Company Profile v20230329.pdf
Equnix Business Solutions
 
PGConf.ASIA 2019 - The Future of TDEforPG - Taiki Kondo
Equnix Business Solutions
 
PGConf.ASIA 2019 - PGSpider High Performance Cluster Engine - Shigeo Hirose
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - Mission Critical Production High Availability Postgre...
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - Keynote Speech 3 - Kohei KaiGai
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - Keynote Speech 1 - Bruce Momjian
Equnix Business Solutions
 
PGConf.ASIA 2019 Bali - Modern PostgreSQL Monitoring & Diagnostics - Mahadeva...
Equnix Business Solutions
 

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Doc9.....................................
SofiaCollazos
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Software Development Methodologies in 2025
KodekX
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 

PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed

  • 1. Tune Box Linux Anda, bukan hanya PostgreSQL
  • 2. Ibrar Ahmed Tune your Linux Box, not just PostgreSQL Linux Kernel Tuning for PostgreSQL performance Senior Database Architect - Percona LLC September 08 – 12, 2019
  • 3. Database Performance • Choose better Hardware • Operating System Selection and Tuning • Choose Database Depend based on Workload • Tune your Database. • Tune your Queries • Tune your Application SELECT * FROM foo
  • 4. PostgreSQL Modules Clients Processes PostgreSQL Memory Kernel Memory Disk Management Background Writer Shared Buffers Default = 128MB WAL Writer Checkpointer Archiver Logger Postmaster Port = 5432 Postgres Postgres Client - psql Client - JDBC Client - ODBC base/ Datafiles pg_wal/ WAL files global Configuration Files Table Spaces$PGDATA /dev/sda /dev/sdb WAL Buffers Default = 16MB work_mem maintancework_mem 4K 2MB/1GB Huge Pages THP
  • 6. PostgreSQL Tuning • shared_buffers • wal_buffers • effective_cache_size • work_mem • maintenance_work_mem • synchronous_commit • checkpoint_timeout • checkpoint_completion_target PostgreSQL Memory Shared Buffers Default = 128MB WAL Buffers Default = 16MB work_mem maintancework_mem Disk Management base/ Datafiles pg_wal/ WAL files global Configuration Files Table Spaces$PGDATA /dev/sda /dev/sdb
  • 7. shared_buffer • PostgreSQL uses its own buffer and also uses kernel buffered I/O. • PostgreSQL does not change the information on disk directly then how? • Writes the data to shared buffer cache. • The backend process write that these blocks kernel buffer. postgresql=# SHOW shared_buffers; shared_buffers ---------------- 128MB (1 row) The proper size for the POSTGRESQL shared buffer cache is the largest useful size that does not adversely affect other activity. —Bruce Momjian How/When data written to disk actually?
  • 8. PostgreSQL Tuning / shared_buffer
  • 9. Configuraing WAL • Do you have Transaction? Obviously • WAL – (Write Ahead LOG) Log your transactions • Size of WAL files 16MB with 8K Block size (can be changed at compile time) • PostgreSQL writes WAL into the buffers(wal_buffer ) and then these buffers are flushed to disk. Tip: Bigger value for wal_buffer in case of lot of concurrent connection gives better performance.
  • 10. effective_cache_size • This used by the optimizer to estimate the size of the kernel's disk buffer cache. • The effective_cache_size provides an estimate of the memory available for disk caching. • It is just a guideline, not the exact allocated memory or cache size. TIP: It should be large enough to hold most accessed tables, but at the same time small enough to avoid swap.
  • 11. work_mem This configuration is used for complex sorting.
  • 12. maintenance_work_mem maintenance_work_mem is a memory setting used for maintenance tasks. • The default value is 64MB. • Setting a large value helps in tasks like VACUUM, RESTORE, CREATE INDEX, ADD FOREIGN KEY and ALTER TABLE.
  • 13. synchronous_commit •This is used to enforce that commit will wait for WAL to be written on disk before returning a success status to the client. •This is a trade-off between performance and reliability. •Increasing reliability decreases performance and vice versa.
  • 14. checkpoint_timeout •PostgreSQL writes changes into WAL. The checkpoint process flushes the data into the data files. •More checkpoints have a negative impact on performance.
  • 16. Input Output Handling • Direct IO, Buffered IO and Double buffering • PostgreSQL believes that the Operating system (Kernel) knows much better about storage and IO scheduling. • PostgreSQL has its own buffering; and also needs the pages cache. Double Buffering • It Increase the use of memory. • And different kernel and setting behave differently.
  • 17. Virtual Memory https://en.wikipedia.org/wiki/Virtual_memory https://en.wikipedia.org/wiki/Page_table • Every process is given the impression that it is working with large, contiguous sections of memory • Each process runs in its own dedicated address space • Pages Table are used to translate the virtual addresses seen by the application into Physical Address
  • 18. Translation Lookaside Buffer (TLB) • Translation Lookaside Buffer is a memory cache • It reduce the time to access a user memory location • If a match is found the physical address of the page is returned → TLB hit • If match not found scan the page table (walk) looking for the address mapping (entry) → TLB miss Small page size bigger TLB size → expensive
  • 19. Memory Pages •Different OS has different size of Pages •Linux has a concept of Classic Huge Pages and Transparent Huge Pages. •BSD has Super Pages. •Windows has Large Pages.
  • 20. Linux Page sizes 4K 67108864 2M 131072 1G 256 large/huge pages • Linux Page size is 4K • Many modern processors support other page sizes If we consider a server with 256G of RAM:
  • 21. Classic Huge Pages 1/3 # cat /proc/meminfo MemTotal: 264041660 kB ... Hugepagesize: 2048 kB DirectMap4k: 128116 kB DirectMap2M: 3956736 kB DirectMap1G: 266338304 kB sysctl -w vm.nr_hugepages=256
  • 22. Classic Huge Pages 2/3 – Setting Huge Page Size # vi /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT="hugepagesz=1GB default_hugepagesz=1G” # update-grub Generating grub configuration file ... Found linux image: /boot/vmlinuz-4.4.0-75-generic Found initrd image: /boot/initrd.img-4.4.0-75-generic Found memtest86+ image: /memtest86+.elf Found memtest86+ image: /memtest86+.bin Done # shutdown -r now
  • 23. Classic Huge Pages 3/3 – Enabling / Disabling in PostgreSQL # vim /etc/postgresql/10/main/postgresql.conf huge_pages=ON # default is try # service postgresql restart
  • 24. Transparent Huge pages • The kernel works in the background (khugepaged) trying to: "create" huge pages. • Find enough contiguous blocks of memory • Convert them into a huge page • Transparently allocate them to processes when there is a "fit"
  • 25. Disabling Transparent Huge pages # cat /proc/meminfo | grep AnonHuge AnonHugePages: 2048 kB # ps aux | grep huge root 42 0.0 0.0 0 0 ? SN Jan17 0:00 [khugepaged] To disable it: • at runtime: # echo never > /sys/kernel/mm/transparent_hugepage/enabled # echo never > /sys/kernel/mm/transparent_hugepage/defrag • at boot time: GRUB_CMDLINE_LINUX_DEFAULT="(...) transparent_hugepage=never"
  • 26. vm.swappiness • This is another kernel parameter that can affect the performance of the database. •Used to control the swappiness (swapping pages to swap memory into RAM) behavior on a Linux system. •This parameter's can take anything from “0” to “100”, default is 60. •Higher value means more aggressively swap.
  • 27. vm.overcommit_memory and vm.overcommit_ratio • Applications acquire memory and free that memory when it is no longer needed. • But in some cases an application acquires too much memory and does not release it. This can invoke the OOM killer. • Heuristic overcommit, Do it intelligently (default); based kernel heuristics • Allow overcommit anyway • Don’t over commit beyond the overcommit ratio.
  • 28. vm.dirty_background_ratio and vm.dirty_background_bytes •The vm.dirty_background_ratio is the percentage of memory filled with dirty pages that need to be flushed to disk. •Flushing is done in the background. • The value of this parameter ranges from 0 to 100;
  • 29. vm.dirty_ratio / vm.dirty_bytes •The vm.dirty_background_ratio is the percentage of memory filled with dirty pages that need to be flushed to disk. •Flushing is done in the foreground. • The value of this parameter ranges from 0 to 100;
  • 30. Blogs •Tuning PostgreSQL Database Parameters to Optimise Performance. ▪ https://www.percona.com/blog/2018/08/31/tuning-postgresql-database-parameters-to-opti mize-performance/ •Tune Linux Kernel Parameters For PostgreSQL Optimisation ▪ https://www.percona.com/blog/2018/08/29/tune-linux-kernel-parameters-for-postgresql-op timization/
  • 31. ? “Poor leaders rarely ask questions of themselves or others. Good leaders, on the other hand, ask many questions. Great leaders ask the great questions.” Michael Marquardt author of Leading with Questions