SlideShare a Scribd company logo
POSTGRESQL
PERFORMANCE TUNING
Presented by: Majid Tahir
Maven Logix Pvt Ltd
AGENDA
 Introduction
 What is Database Performance Tuning
 Factors affecting database performance
 Tuning PGSQL Database Parameters
 Performance Tips
 PGSQL Tuning Tools
DATABASE PERFORMANCE TUNING
 Database performance tuning encompasses the
steps you can take to optimize performance with
the goal of maximizing the use of system resources
for greater efficiency.
 At a high level, database performance can be
defined as the rate at which a
database management system (DBMS) supplies
information to users.
FACTORS AFFECTING DATABASE
PERFORMANCE
 There are five factors that influence database
performance:
1. Workload
2. Throughput
3. Resources
4. Optimization
5. Contention.
WORKLOAD
 The workload that is requested of the DBMS defines
the demand.
 It is a combination of
 Online transactions
 Batch jobs
 Ad hoc Queries
 Workload can be predicted (such as heavy month-end
processing of payroll, or very light access after 7 pm,
but at other times it is unpredictable.
 The overall workload can have a major impact on
database performance.
THROUGHPUT
 Throughput defines the overall capability of the
computer to process data.
 It is a composite of
 I/O speed
 CPU speed
 Parallel capabilities of the machine
 The efficiency of the operating system and system
software.
RESOURCES
 The hardware and software tools at the disposal of
the system are known as the resources of the
system.
 Examples include
 Memory (such as that allocated to buffer pools)
 Disk
OPTIMIZATION
 Database optimization involves maximizing the
speed and efficiency with which data is retrieved.
 Relational database management systems
(RDBMS) rely on an optimizer (or relational
optimizer) that transforms SQL statements into
executable code.
 Other factors that need to be optimized
 Database Parameters
 System parameters, etc.
CONTENTION
 When the demand (workload) for a particular
resource is high, Contention can result.
 Contention is the condition in which two or more
components of the workload are attempting to use
a single resource in a conflicting way
 (for example, dual updates to the same piece of
data).
 The DBMS uses a locking mechanism
 The DBMS locking strategies permit multiple users
from multiple environments to access and modify
data in the database at the same time.
TUNING IN PGSQL
 First step is to understand lifecycle of a query
 Following are the steps:
1. Transmission of query string to database backend
2. Parsing of query string
3. Planning of query to optimize retrieval of data
4. Retrieval of data from hardware
5. Transmission of results to client
TRANSMISSION OF QUERY STRING TO DATABASE
BACKEND
 The first step is the sending of the query string ( the
actual SQL command you type in or your
application uses ) to the database backend.
 Select * from emp
PARSING OF QUERY STRING
 Once the SQL query is inside the database server it
is parsed into tokens.
 During the parse call, the database performs the
following checks:
 Syntax Check
e.g. SELECT * FORM employees;
SELECT * FORM employees
* ERROR at line 1: FROM keyword not found where
expected
 Semantic Check whether a statement is meaningful)
PLANNING OF QUERY TO OPTIMIZE RETRIEVAL OF
DATA
 The planning of the query is where PostgreSQL
really starts to do some work.
 It also analyzes your SQL to determine what the
most efficient way of retrieving your data is.
 Should we use an index?
 Maybe a hash join on those two tables is appropriate?
RETRIEVAL OF DATA FROM HARDWARE
 Now that PostgreSQL has a plan of what it believes
to be the best way to retrieve the data, it is time to
actually get it.
 This step is mostly effected by your hardware
configuration.
TRANSMISSION OF RESULTS TO CLIENT
 And finally the last step is to transmit the results to
the client.
 All of the data that you are returning is pulled from
the disk and sent over the wire to your client.
 Minimizing the number of rows and columns to only
those that are necessary can often increase your
performance.
DATABASE PARAMETERS
 The default PostgreSQL configuration is not tuned
for any particular workload.
 Default values are set to ensure that PostgreSQL
runs everywhere, with the least resources it can
consume and so that it doesn’t cause any
vulnerabilities.
 It has default settings for all of the database
parameters.
 The parameters can be changed in the
postgresql.conf file located in the installation folder
MAX_CONNECTIONS
 Determines the maximum number of concurrent
connections to the database server. The default is
typically 100 connections
 The change in max_connections require restart
SHARED_BUFFERS
 This parameter sets how much dedicated memory
will be used by PostgreSQL for cache.
 This should be set to roughly 25% of available RAM
on the system.
Editing this option is the simplest way to improve the
performance of your database server.
WAL_BUFFERS
 PostgreSQL writes its WAL (write ahead log) record
into the buffers and then these buffers are flushed
to disk.
 The default size of the buffer, defined
by wal_buffers, is 16MB, but if you have a lot of
concurrent connections then a higher value can
give better performance.
 Change in this parameter requires restart
EFFECTIVE_CACHE_SIZE
 This value tells PostgreSQL's optimizer how much
memory PostgreSQL has available for caching data
and helps in determing whether or not it use an
index or not.
 The larger the value increases the likely hood of
using an index.
 This should be set to the amount of memory
allocated to shared_buffers plus the amount of OS
cache available.
 Often this is more than 50% of the total system
memory.
 The larger value is recommended by experts
WORK_MEM
 This configuration is used for complex sorting.
 If you have to do complex sorting then increase the
value of work_mem for good results.
 In-memory sorts are much faster than sorts spilling
to disk.
CONT…
MAX_FSM_PAGES
 This option helps to control the free space map.
 When something is deleted from a table it isn't
removed from the disk immediately, it is simply
marked as "free" in the free space map.
 The space can then be reused for any new
INSERTs that you do on the table.
 If your setup has a high rate of DELETEs and
INSERTs it may be necessary increase this value to
enhance performance.
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 RESTORE,
CREATE INDEX, ADD FOREIGN KEY and ALTER
TABLE.
CONT…
Change size to 256 MB
USEFUL TUNING TOOLS
 EXPLAIN ANALYZE
 POSTGRESQLTUNER
 PGBADGER
 Analyze logs to generate performance reports
 PGTUNE
DB
Performance
Tuning
Approaches
Monitoring
Tools
Optimization
Partition Table
Index
Managment
Table
Reorganization
Parameter
Configuration
PostGreSQL Performance Tuning
PERFORMANCE TIPS
 Use tool that best fit for your environment
 One index per query
 Use Multiple columns in index
 There are more parameters that can be tuned to
gain better performance.
 In the end, we must always keep in mind that not all
parameters are relevant for all applications types.
 Some applications perform better by tuning a
parameter and some don’t.
 Database parameters must be tuned for the specific
needs of an application and the OS it runs on.

More Related Content

PDF
Postgresql database administration volume 1
PDF
Practical Partitioning in Production with Postgres
PDF
Oracle db performance tuning
PPTX
PostgreSQL Database Slides
PDF
Analyzing and Interpreting AWR
PDF
Linux tuning to improve PostgreSQL performance
PPTX
Getting started with postgresql
PPTX
Git 101 for Beginners
Postgresql database administration volume 1
Practical Partitioning in Production with Postgres
Oracle db performance tuning
PostgreSQL Database Slides
Analyzing and Interpreting AWR
Linux tuning to improve PostgreSQL performance
Getting started with postgresql
Git 101 for Beginners

What's hot (20)

PDF
Your first ClickHouse data warehouse
ODP
PostgreSQL Administration for System Administrators
PDF
MyRocks Deep Dive
PDF
Mastering PostgreSQL Administration
 
PDF
PostgreSQL Performance Tuning
PDF
Deep dive into PostgreSQL statistics.
PDF
PostgreSQL WAL for DBAs
PDF
Databricks Delta Lake and Its Benefits
PDF
Common Strategies for Improving Performance on Your Delta Lakehouse
PDF
Delta Lake: Optimizing Merge
PPTX
Enable GoldenGate Monitoring with OEM 12c/JAgent
PDF
5 Steps to PostgreSQL Performance
PPTX
PostgreSQL.pptx
PDF
Backup and-recovery2
PDF
Altinity Quickstart for ClickHouse-2202-09-15.pdf
PDF
Efficient Data Storage for Analytics with Apache Parquet 2.0
PDF
Parquet performance tuning: the missing guide
PDF
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
PDF
Get to know PostgreSQL!
PDF
Using Optimizer Hints to Improve MySQL Query Performance
Your first ClickHouse data warehouse
PostgreSQL Administration for System Administrators
MyRocks Deep Dive
Mastering PostgreSQL Administration
 
PostgreSQL Performance Tuning
Deep dive into PostgreSQL statistics.
PostgreSQL WAL for DBAs
Databricks Delta Lake and Its Benefits
Common Strategies for Improving Performance on Your Delta Lakehouse
Delta Lake: Optimizing Merge
Enable GoldenGate Monitoring with OEM 12c/JAgent
5 Steps to PostgreSQL Performance
PostgreSQL.pptx
Backup and-recovery2
Altinity Quickstart for ClickHouse-2202-09-15.pdf
Efficient Data Storage for Analytics with Apache Parquet 2.0
Parquet performance tuning: the missing guide
ProxySQL and the Tricks Up Its Sleeve - Percona Live 2022.pdf
Get to know PostgreSQL!
Using Optimizer Hints to Improve MySQL Query Performance
Ad

Similar to PostGreSQL Performance Tuning (20)

DOC
Quick guide to PostgreSQL Performance Tuning
PPTX
Performance Tuning
PPTX
Tuning database performance
PDF
PostgreSQL Table Partitioning / Sharding
PDF
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
PDF
Performance tuning and optimization (ppt)
PPTX
שבוע אורקל 2016
PDF
Weblogic performance tuning1
PDF
Weblogic Cluster performance tuning
PPTX
PARALLEL DATABASE SYSTEM in Computer Science.pptx
PPTX
Presentación Oracle Database Migración consideraciones 10g/11g/12c
PDF
PostgreSQL High_Performance_Cheatsheet
PPT
Dbms 3 sem
PPTX
Sql server performance tuning
PPTX
PDF
What is Scalability and How can affect on overall system performance of database
PPTX
Database management system by Neeraj Bhandari ( Surkhet.Nepal )
PPTX
Design and development of oracle database system
PPT
Database performance tuning and query optimization
DOCX
MongoDB Replication and Sharding
Quick guide to PostgreSQL Performance Tuning
Performance Tuning
Tuning database performance
PostgreSQL Table Partitioning / Sharding
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Performance tuning and optimization (ppt)
שבוע אורקל 2016
Weblogic performance tuning1
Weblogic Cluster performance tuning
PARALLEL DATABASE SYSTEM in Computer Science.pptx
Presentación Oracle Database Migración consideraciones 10g/11g/12c
PostgreSQL High_Performance_Cheatsheet
Dbms 3 sem
Sql server performance tuning
What is Scalability and How can affect on overall system performance of database
Database management system by Neeraj Bhandari ( Surkhet.Nepal )
Design and development of oracle database system
Database performance tuning and query optimization
MongoDB Replication and Sharding
Ad

More from Maven Logix (6)

PPTX
Transforming Healthcare Through Telemedicine
PPTX
Concepts and Rights About Data Privacy and Security
PDF
How to Response Cyber Data Breaches at Pakistan
PPTX
Identity Theft and How to Prevent Them in the Digital Age
PPTX
Capability Building for Cyber Defense: Software Walk through and Screening
PPTX
Information Technology in Renewable Energy
Transforming Healthcare Through Telemedicine
Concepts and Rights About Data Privacy and Security
How to Response Cyber Data Breaches at Pakistan
Identity Theft and How to Prevent Them in the Digital Age
Capability Building for Cyber Defense: Software Walk through and Screening
Information Technology in Renewable Energy

Recently uploaded (20)

PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PDF
Mega Projects Data Mega Projects Data
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Computer network topology notes for revision
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Business Analytics and business intelligence.pdf
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Mega Projects Data Mega Projects Data
Business Ppt On Nestle.pptx huunnnhhgfvu
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
climate analysis of Dhaka ,Banglades.pptx
ISS -ESG Data flows What is ESG and HowHow
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Galatica Smart Energy Infrastructure Startup Pitch Deck
IBA_Chapter_11_Slides_Final_Accessible.pptx
IB Computer Science - Internal Assessment.pptx
Computer network topology notes for revision
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Business Analytics and business intelligence.pdf
Reliability_Chapter_ presentation 1221.5784
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb

PostGreSQL Performance Tuning

  • 1. POSTGRESQL PERFORMANCE TUNING Presented by: Majid Tahir Maven Logix Pvt Ltd
  • 2. AGENDA  Introduction  What is Database Performance Tuning  Factors affecting database performance  Tuning PGSQL Database Parameters  Performance Tips  PGSQL Tuning Tools
  • 3. DATABASE PERFORMANCE TUNING  Database performance tuning encompasses the steps you can take to optimize performance with the goal of maximizing the use of system resources for greater efficiency.  At a high level, database performance can be defined as the rate at which a database management system (DBMS) supplies information to users.
  • 4. FACTORS AFFECTING DATABASE PERFORMANCE  There are five factors that influence database performance: 1. Workload 2. Throughput 3. Resources 4. Optimization 5. Contention.
  • 5. WORKLOAD  The workload that is requested of the DBMS defines the demand.  It is a combination of  Online transactions  Batch jobs  Ad hoc Queries  Workload can be predicted (such as heavy month-end processing of payroll, or very light access after 7 pm, but at other times it is unpredictable.  The overall workload can have a major impact on database performance.
  • 6. THROUGHPUT  Throughput defines the overall capability of the computer to process data.  It is a composite of  I/O speed  CPU speed  Parallel capabilities of the machine  The efficiency of the operating system and system software.
  • 7. RESOURCES  The hardware and software tools at the disposal of the system are known as the resources of the system.  Examples include  Memory (such as that allocated to buffer pools)  Disk
  • 8. OPTIMIZATION  Database optimization involves maximizing the speed and efficiency with which data is retrieved.  Relational database management systems (RDBMS) rely on an optimizer (or relational optimizer) that transforms SQL statements into executable code.  Other factors that need to be optimized  Database Parameters  System parameters, etc.
  • 9. CONTENTION  When the demand (workload) for a particular resource is high, Contention can result.  Contention is the condition in which two or more components of the workload are attempting to use a single resource in a conflicting way  (for example, dual updates to the same piece of data).  The DBMS uses a locking mechanism  The DBMS locking strategies permit multiple users from multiple environments to access and modify data in the database at the same time.
  • 10. TUNING IN PGSQL  First step is to understand lifecycle of a query  Following are the steps: 1. Transmission of query string to database backend 2. Parsing of query string 3. Planning of query to optimize retrieval of data 4. Retrieval of data from hardware 5. Transmission of results to client
  • 11. TRANSMISSION OF QUERY STRING TO DATABASE BACKEND  The first step is the sending of the query string ( the actual SQL command you type in or your application uses ) to the database backend.  Select * from emp
  • 12. PARSING OF QUERY STRING  Once the SQL query is inside the database server it is parsed into tokens.  During the parse call, the database performs the following checks:  Syntax Check e.g. SELECT * FORM employees; SELECT * FORM employees * ERROR at line 1: FROM keyword not found where expected  Semantic Check whether a statement is meaningful)
  • 13. PLANNING OF QUERY TO OPTIMIZE RETRIEVAL OF DATA  The planning of the query is where PostgreSQL really starts to do some work.  It also analyzes your SQL to determine what the most efficient way of retrieving your data is.  Should we use an index?  Maybe a hash join on those two tables is appropriate?
  • 14. RETRIEVAL OF DATA FROM HARDWARE  Now that PostgreSQL has a plan of what it believes to be the best way to retrieve the data, it is time to actually get it.  This step is mostly effected by your hardware configuration.
  • 15. TRANSMISSION OF RESULTS TO CLIENT  And finally the last step is to transmit the results to the client.  All of the data that you are returning is pulled from the disk and sent over the wire to your client.  Minimizing the number of rows and columns to only those that are necessary can often increase your performance.
  • 16. DATABASE PARAMETERS  The default PostgreSQL configuration is not tuned for any particular workload.  Default values are set to ensure that PostgreSQL runs everywhere, with the least resources it can consume and so that it doesn’t cause any vulnerabilities.  It has default settings for all of the database parameters.  The parameters can be changed in the postgresql.conf file located in the installation folder
  • 17. MAX_CONNECTIONS  Determines the maximum number of concurrent connections to the database server. The default is typically 100 connections  The change in max_connections require restart
  • 18. SHARED_BUFFERS  This parameter sets how much dedicated memory will be used by PostgreSQL for cache.  This should be set to roughly 25% of available RAM on the system. Editing this option is the simplest way to improve the performance of your database server.
  • 19. WAL_BUFFERS  PostgreSQL writes its WAL (write ahead log) record into the buffers and then these buffers are flushed to disk.  The default size of the buffer, defined by wal_buffers, is 16MB, but if you have a lot of concurrent connections then a higher value can give better performance.  Change in this parameter requires restart
  • 20. EFFECTIVE_CACHE_SIZE  This value tells PostgreSQL's optimizer how much memory PostgreSQL has available for caching data and helps in determing whether or not it use an index or not.  The larger the value increases the likely hood of using an index.  This should be set to the amount of memory allocated to shared_buffers plus the amount of OS cache available.  Often this is more than 50% of the total system memory.  The larger value is recommended by experts
  • 21. WORK_MEM  This configuration is used for complex sorting.  If you have to do complex sorting then increase the value of work_mem for good results.  In-memory sorts are much faster than sorts spilling to disk.
  • 23. MAX_FSM_PAGES  This option helps to control the free space map.  When something is deleted from a table it isn't removed from the disk immediately, it is simply marked as "free" in the free space map.  The space can then be reused for any new INSERTs that you do on the table.  If your setup has a high rate of DELETEs and INSERTs it may be necessary increase this value to enhance performance.
  • 24. 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 RESTORE, CREATE INDEX, ADD FOREIGN KEY and ALTER TABLE.
  • 26. USEFUL TUNING TOOLS  EXPLAIN ANALYZE  POSTGRESQLTUNER  PGBADGER  Analyze logs to generate performance reports  PGTUNE
  • 29. PERFORMANCE TIPS  Use tool that best fit for your environment  One index per query  Use Multiple columns in index  There are more parameters that can be tuned to gain better performance.  In the end, we must always keep in mind that not all parameters are relevant for all applications types.  Some applications perform better by tuning a parameter and some don’t.  Database parameters must be tuned for the specific needs of an application and the OS it runs on.