SlideShare a Scribd company logo
1 of 6
Download to read offline
The idea is to find large tables that have been used frequently in a
sequential scan. If you are running large sequential scans all the time,
your performance will be heavily impacted.
Detect Missing Indexes
SELECT schemaname, relname, seq_scan,
seq_tup_read,
seq_tup_read / seq_scan AS avg, idx_scan
FROM pg_stat_user_tables
WHERE seq_scan > 0
ORDER BY seq_tup_read DESC
LIMIT 10;
If you have hundreds of gigabytes of pointless indexes the you can
seriously harm your overall performance.
Finding Useless Indexes
SELECT schemaname, relname,
indexrelname, idx_scan,
pg_size_pretty(pg_relation_size
(indexrelid)) AS idx_size
FROM pg_stat_user_indexes;
The output of this statement contains information about how often
an index was used and it also tells us how much space has been
wasted for each index.
Covering Indexes
In an ordinary index scan, each row retrieval requires fetching data from both the index and the heap. The heap-access portion of an
index scan involves a lot of random access into the heap, which can be slow.
To solve this performance problem, PostgreSQL supports index-only scans, which can answer queries from an index alone without any
heap access.
Additionally, you might choose to create a covering index, which is an index specifically designed to include the columns needed by a
type of query that you run frequently.
Since queries typically need to retrieve more columns than just the ones they search on, PostgreSQL allows you to speed up queries
by adding an INCLUDE clause listing the extra columns.
For example, if you frequently run queries the following, then include the name
column in the index:
SELECT name FROM tab WHERE id = 'key’;
CREATE INDEX idx ON tab(id) INCLUDE (name);
PostgreSQL: High-Performance
Cheat Sheet
PostgreSQL
https://distributedsystemsauthority.com/index-optimization-techniques-postgresql-12-high-performance-guide-part-6-12/Find more at:
It is by far the easiest way to track down performance problems.
It helps us figure out which types of queries are slow and how often these queries are called.
Top 10 Time-consuming Queries
SELECT round((100 * total_time / sum(total_time)
OVER ())::numeric, 2) percent,
round(total_time::numeric, 2) AS total,
calls,
round(mean_time::numeric, 2) AS mean,
substring(query, 1, 40)
FROM pg_stat_statements
ORDER BY total_time
DESC LIMIT 10;
This extension is not installed by default. It needs to be installed in each database . Here's how you can install it:
1. In postgresql.conf file, you should search for shared_preload_libraries and if necessary uncomment it. Then add
“pg_stat_statements” as value
2. Restart the database server
3. Run “CREATE EXTENSION pg_stat_statements;” for the database of interest
When you have a feeling that a query is not performing well, EXPLAIN will help you to reveal the real performance problem.
The purpose of EXPLAIN is to see what the planner has come up with to run the query efficiently.
EXPLAIN command
EXPLAIN SELECT * FROM t_test LIMIT 10;
Limit (cost=0.00..3.39 rows=10 width=233)
-> Seq Scan on post (cost=0.00..1562.12 rows=23042 width=233)
estimated total cost
estimated start-up cost
until first row is returned
estimated number of rows
output by this plan node
estimated average width of rows
output by this plan node (in bytes)
You should read the from the plan inner part to outer part, or bottom to top.
PostgreSQ
EXPLAIN ANALYZE SELECT * FROM post LIMIT 50;
Limit (cost=0.00..3.39 rows=50 width=422) (actual time=0.000..0.044 rows=50 loops=1)
-> Seq Scan on post (cost=0.00..15629.12 rows=999 width=422) (actual
time=0.227..0.227 rows=999 loops=1)
“actual time” values are in milliseconds of real time, whereas the cost estimates are expressed in arbitrary units; so they
are unlikely to match up. Estimated cost are needed to compare different ways to execute a query.
EXPLAIN ANALYZE command
With ANALYZE option, EXPLAIN actually executes the query, and then displays the true row counts and true runtime
accumulated within each plan node, along with the same estimates that a plain EXPLAIN shows.
?
Use index
Use bitmap
scan
0.2
0.8
0.5
Cost Estimates
PostgreSQL
https://distributedsystemsauthority.com/spotting-query-problems-postgresql-12-high-performance-guide-part-8-12/Find more at:
VACUUM
Any time you UPDATE or DELETE, you will leave a dead row behind (and potentially a
dead index entry) that needs to be cleaned up later by some form of vacuum.
When tables grow very large because of excessive dead tuples then performance
will tend to decrease. Therefore the VACUUM process should never be avoided.
The VACUUM simply recovers space and makes it available for reuse. Please note
that space is not returned to the operating system (in most cases); it is only
available for reuse within the same table.
SELECT schemaname, relname, n_live_tup, n_dead_tup
FROM pg_stat_user_tables;
Maintain Performance
VACUUM configs
If the VACUUM is taking too much time or resources, it means that we
must do it more frequently, so that each operation has less to clean.
AUTOVACUUM takes care of cleanup works in the background. It
wakes as specified in postgresql.conf
# time betweenautovacuum runs
autovacuum_naptime = 1
# fraction of table size before vacuum
autovacuum_vacuum_scale_factor = 0.2
# min number of row updates before vacuum
autovacuum_vacuum_threshold = 50
# limit the duration of a transaction
# which prevents free space reclaim
old_snapshot_threshold = 1min
VACUUM ANALYZE
VACUUM ANALYZE. In adiition to vacuum, ANALYZE
collects statistics on the contents of the tables and
stores the results in pg_statistic. Subsequently, the
query planner uses these statistics to help
determine the most efficient execution plans for
queries.
https://distributedsystemsauthority.com/configuring-vacuum-for-performance-postgresql-12-high-
performance-guide-part-4-12/
Find more at:
CLOG buffer
Shared buffers
WAL buffer
Other buffers
Shared Memory
work_mem
maintenance_work_mem
Buffer per backend
PostgreSQL Memory System work_mem
Specifies the amount of memory that will be used by the internal operations such as
ORDER BY, DISTINCT, JOIN, and hash tables before writing to the temporary files on
disk.
When configuring this value, we must take into account that several sessions be
executing these operations at the same time, and each session can execute several sort
operations concurrently. Therefore, the total memory used could be many times the
value of work_mem.
maintenance_work_mem
Specifies the maximum amount of memory that maintenance operations will use, such
as VACUUM, CREATE INDEX, and ALTER TABLE ADD FOREIGN KEY. Since only one of
these operations can be executed at the same time by a session, and an installation
usually does not have many of them running simultaneously, it can be larger than the
work_mem.
Larger configurations can improve performance for VACUUM and database restores.
OS cache
When there are thousands of users trying to read or
write data to many different tables, reading from the
files will result in a poor, non-scalable system.
The reads and writes will result in searching for many
files, opening these files, looking for specific data
records, locking, editing, and unlocking.
To make this a lot more scalable and faster, the
concept of shared buffers is introduced.
PostgreSQL default allocation for the shared_buffers is extremely low, and we need to increase it to allow proper shared memory size for
the database.
A reasonable initial value for shared_buffers is 25% of your system's memory. However, if you want to do better than allocating a
percentage to the shared_buffers relative to the OS cache, you need to analyze the buffers cache content to see if a bigger amount would
benefit your use cases.
shared_buffers
PostgreSQL
Configuration parameters
https://distributedsystemsauthority.com/optimizing-postgresql-shared-buffers/(opens in a new tab)Find more at:
Checkpoint Process
max_wal_size
When a user makes changes to the data, it first goes through the buffer. Now that buffer is dirty. The fact that a user has committed a change
does not mean that the modification has been written to the data file. The checkpointer process is responsible to write the change to the
data file.
There is a trade-off between performance (infrequent checkpoints) and time needed for recovery (frequent checkpoints)
There are multiple parameters to control how often the checkpoints are done but the main one is based on size: max_wal_size.
This sets the maximum size the WAL is allowed to grow between the control points.
Check Course on
Udemy
PostgreSQL High-Performance Course

More Related Content

What's hot

Postgresql Database Administration- Day3
Postgresql Database Administration- Day3Postgresql Database Administration- Day3
Postgresql Database Administration- Day3PoguttuezhiniVP
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1Federico Campoli
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PGConf APAC
 
Postgresql Database Administration Basic - Day1
Postgresql  Database Administration Basic  - Day1Postgresql  Database Administration Basic  - Day1
Postgresql Database Administration Basic - Day1PoguttuezhiniVP
 
Improve PostgreSQL replication with Oracle GoldenGate
Improve PostgreSQL replication with Oracle GoldenGateImprove PostgreSQL replication with Oracle GoldenGate
Improve PostgreSQL replication with Oracle GoldenGateBobby Curtis
 
Galera cluster for high availability
Galera cluster for high availability Galera cluster for high availability
Galera cluster for high availability Mydbops
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HAharoonm
 
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오PgDay.Seoul
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresqlbotsplash.com
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsCommand Prompt., Inc
 
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQLPgDay.Seoul
 
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...ScaleGrid.io
 
PostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldPostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldJignesh Shah
 
Patroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easyPatroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easyAlexander Kukushkin
 
Vacuum in PostgreSQL
Vacuum in PostgreSQLVacuum in PostgreSQL
Vacuum in PostgreSQLRafia Sabih
 
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정PgDay.Seoul
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQLJoel Brewer
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Alexey Lesovsky
 

What's hot (20)

Postgresql Database Administration- Day3
Postgresql Database Administration- Day3Postgresql Database Administration- Day3
Postgresql Database Administration- Day3
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1
 
Backup and-recovery2
Backup and-recovery2Backup and-recovery2
Backup and-recovery2
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
 
Postgresql Database Administration Basic - Day1
Postgresql  Database Administration Basic  - Day1Postgresql  Database Administration Basic  - Day1
Postgresql Database Administration Basic - Day1
 
Improve PostgreSQL replication with Oracle GoldenGate
Improve PostgreSQL replication with Oracle GoldenGateImprove PostgreSQL replication with Oracle GoldenGate
Improve PostgreSQL replication with Oracle GoldenGate
 
Galera cluster for high availability
Galera cluster for high availability Galera cluster for high availability
Galera cluster for high availability
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
 
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresql
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
 
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
What’s the Best PostgreSQL High Availability Framework? PAF vs. repmgr vs. Pa...
 
PostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldPostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized World
 
Patroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easyPatroni - HA PostgreSQL made easy
Patroni - HA PostgreSQL made easy
 
Vacuum in PostgreSQL
Vacuum in PostgreSQLVacuum in PostgreSQL
Vacuum in PostgreSQL
 
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 

Similar to PostgreSQL High_Performance_Cheatsheet

Quick guide to PostgreSQL Performance Tuning
Quick guide to PostgreSQL Performance TuningQuick guide to PostgreSQL Performance Tuning
Quick guide to PostgreSQL Performance TuningRon Morgan
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluationavniS
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuningJugal Shah
 
Apache Cassandra 2.0
Apache Cassandra 2.0Apache Cassandra 2.0
Apache Cassandra 2.0Joe Stein
 
PostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingPostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingAmir Reza Hashemi
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09guest9d79e073
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Mark Ginnebaugh
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingGrant Fritchey
 
Oracle Performance Tuning DE(v1.2)-part2.ppt
Oracle Performance Tuning DE(v1.2)-part2.pptOracle Performance Tuning DE(v1.2)-part2.ppt
Oracle Performance Tuning DE(v1.2)-part2.pptVenugopalChattu1
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화NAVER D2
 
[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화Henry Jeong
 
SQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVsSQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVsFranklin Yamamoto
 
PostGreSQL Performance Tuning
PostGreSQL Performance TuningPostGreSQL Performance Tuning
PostGreSQL Performance TuningMaven Logix
 
At the core you will have KUSTO
At the core you will have KUSTOAt the core you will have KUSTO
At the core you will have KUSTORiccardo Zamana
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning Arno Huetter
 
Netezza workload management
Netezza workload managementNetezza workload management
Netezza workload managementBiju Nair
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersAdam Hutson
 
You are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdfYou are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdfgardenvarelianand
 

Similar to PostgreSQL High_Performance_Cheatsheet (20)

Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
 
Quick guide to PostgreSQL Performance Tuning
Quick guide to PostgreSQL Performance TuningQuick guide to PostgreSQL Performance Tuning
Quick guide to PostgreSQL Performance Tuning
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluation
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuning
 
Apache Cassandra 2.0
Apache Cassandra 2.0Apache Cassandra 2.0
Apache Cassandra 2.0
 
PostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingPostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / Sharding
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and Alerting
 
Oracle Performance Tuning DE(v1.2)-part2.ppt
Oracle Performance Tuning DE(v1.2)-part2.pptOracle Performance Tuning DE(v1.2)-part2.ppt
Oracle Performance Tuning DE(v1.2)-part2.ppt
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화
 
[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화[2 d1] elasticsearch 성능 최적화
[2 d1] elasticsearch 성능 최적화
 
SQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVsSQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVs
 
PostGreSQL Performance Tuning
PostGreSQL Performance TuningPostGreSQL Performance Tuning
PostGreSQL Performance Tuning
 
At the core you will have KUSTO
At the core you will have KUSTOAt the core you will have KUSTO
At the core you will have KUSTO
 
Database Performance Tuning
Database Performance Tuning Database Performance Tuning
Database Performance Tuning
 
Netezza workload management
Netezza workload managementNetezza workload management
Netezza workload management
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
 
You are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdfYou are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdf
 

Recently uploaded

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptrcbcrtm
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 

Recently uploaded (20)

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.ppt
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 

PostgreSQL High_Performance_Cheatsheet

  • 1. The idea is to find large tables that have been used frequently in a sequential scan. If you are running large sequential scans all the time, your performance will be heavily impacted. Detect Missing Indexes SELECT schemaname, relname, seq_scan, seq_tup_read, seq_tup_read / seq_scan AS avg, idx_scan FROM pg_stat_user_tables WHERE seq_scan > 0 ORDER BY seq_tup_read DESC LIMIT 10; If you have hundreds of gigabytes of pointless indexes the you can seriously harm your overall performance. Finding Useless Indexes SELECT schemaname, relname, indexrelname, idx_scan, pg_size_pretty(pg_relation_size (indexrelid)) AS idx_size FROM pg_stat_user_indexes; The output of this statement contains information about how often an index was used and it also tells us how much space has been wasted for each index. Covering Indexes In an ordinary index scan, each row retrieval requires fetching data from both the index and the heap. The heap-access portion of an index scan involves a lot of random access into the heap, which can be slow. To solve this performance problem, PostgreSQL supports index-only scans, which can answer queries from an index alone without any heap access. Additionally, you might choose to create a covering index, which is an index specifically designed to include the columns needed by a type of query that you run frequently. Since queries typically need to retrieve more columns than just the ones they search on, PostgreSQL allows you to speed up queries by adding an INCLUDE clause listing the extra columns. For example, if you frequently run queries the following, then include the name column in the index: SELECT name FROM tab WHERE id = 'key’; CREATE INDEX idx ON tab(id) INCLUDE (name); PostgreSQL: High-Performance Cheat Sheet PostgreSQL https://distributedsystemsauthority.com/index-optimization-techniques-postgresql-12-high-performance-guide-part-6-12/Find more at:
  • 2. It is by far the easiest way to track down performance problems. It helps us figure out which types of queries are slow and how often these queries are called. Top 10 Time-consuming Queries SELECT round((100 * total_time / sum(total_time) OVER ())::numeric, 2) percent, round(total_time::numeric, 2) AS total, calls, round(mean_time::numeric, 2) AS mean, substring(query, 1, 40) FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10; This extension is not installed by default. It needs to be installed in each database . Here's how you can install it: 1. In postgresql.conf file, you should search for shared_preload_libraries and if necessary uncomment it. Then add “pg_stat_statements” as value 2. Restart the database server 3. Run “CREATE EXTENSION pg_stat_statements;” for the database of interest When you have a feeling that a query is not performing well, EXPLAIN will help you to reveal the real performance problem. The purpose of EXPLAIN is to see what the planner has come up with to run the query efficiently. EXPLAIN command EXPLAIN SELECT * FROM t_test LIMIT 10; Limit (cost=0.00..3.39 rows=10 width=233) -> Seq Scan on post (cost=0.00..1562.12 rows=23042 width=233) estimated total cost estimated start-up cost until first row is returned estimated number of rows output by this plan node estimated average width of rows output by this plan node (in bytes) You should read the from the plan inner part to outer part, or bottom to top. PostgreSQ
  • 3. EXPLAIN ANALYZE SELECT * FROM post LIMIT 50; Limit (cost=0.00..3.39 rows=50 width=422) (actual time=0.000..0.044 rows=50 loops=1) -> Seq Scan on post (cost=0.00..15629.12 rows=999 width=422) (actual time=0.227..0.227 rows=999 loops=1) “actual time” values are in milliseconds of real time, whereas the cost estimates are expressed in arbitrary units; so they are unlikely to match up. Estimated cost are needed to compare different ways to execute a query. EXPLAIN ANALYZE command With ANALYZE option, EXPLAIN actually executes the query, and then displays the true row counts and true runtime accumulated within each plan node, along with the same estimates that a plain EXPLAIN shows. ? Use index Use bitmap scan 0.2 0.8 0.5 Cost Estimates PostgreSQL https://distributedsystemsauthority.com/spotting-query-problems-postgresql-12-high-performance-guide-part-8-12/Find more at:
  • 4. VACUUM Any time you UPDATE or DELETE, you will leave a dead row behind (and potentially a dead index entry) that needs to be cleaned up later by some form of vacuum. When tables grow very large because of excessive dead tuples then performance will tend to decrease. Therefore the VACUUM process should never be avoided. The VACUUM simply recovers space and makes it available for reuse. Please note that space is not returned to the operating system (in most cases); it is only available for reuse within the same table. SELECT schemaname, relname, n_live_tup, n_dead_tup FROM pg_stat_user_tables; Maintain Performance VACUUM configs If the VACUUM is taking too much time or resources, it means that we must do it more frequently, so that each operation has less to clean. AUTOVACUUM takes care of cleanup works in the background. It wakes as specified in postgresql.conf # time betweenautovacuum runs autovacuum_naptime = 1 # fraction of table size before vacuum autovacuum_vacuum_scale_factor = 0.2 # min number of row updates before vacuum autovacuum_vacuum_threshold = 50 # limit the duration of a transaction # which prevents free space reclaim old_snapshot_threshold = 1min VACUUM ANALYZE VACUUM ANALYZE. In adiition to vacuum, ANALYZE collects statistics on the contents of the tables and stores the results in pg_statistic. Subsequently, the query planner uses these statistics to help determine the most efficient execution plans for queries. https://distributedsystemsauthority.com/configuring-vacuum-for-performance-postgresql-12-high- performance-guide-part-4-12/ Find more at:
  • 5. CLOG buffer Shared buffers WAL buffer Other buffers Shared Memory work_mem maintenance_work_mem Buffer per backend PostgreSQL Memory System work_mem Specifies the amount of memory that will be used by the internal operations such as ORDER BY, DISTINCT, JOIN, and hash tables before writing to the temporary files on disk. When configuring this value, we must take into account that several sessions be executing these operations at the same time, and each session can execute several sort operations concurrently. Therefore, the total memory used could be many times the value of work_mem. maintenance_work_mem Specifies the maximum amount of memory that maintenance operations will use, such as VACUUM, CREATE INDEX, and ALTER TABLE ADD FOREIGN KEY. Since only one of these operations can be executed at the same time by a session, and an installation usually does not have many of them running simultaneously, it can be larger than the work_mem. Larger configurations can improve performance for VACUUM and database restores. OS cache When there are thousands of users trying to read or write data to many different tables, reading from the files will result in a poor, non-scalable system. The reads and writes will result in searching for many files, opening these files, looking for specific data records, locking, editing, and unlocking. To make this a lot more scalable and faster, the concept of shared buffers is introduced. PostgreSQL default allocation for the shared_buffers is extremely low, and we need to increase it to allow proper shared memory size for the database. A reasonable initial value for shared_buffers is 25% of your system's memory. However, if you want to do better than allocating a percentage to the shared_buffers relative to the OS cache, you need to analyze the buffers cache content to see if a bigger amount would benefit your use cases. shared_buffers PostgreSQL Configuration parameters https://distributedsystemsauthority.com/optimizing-postgresql-shared-buffers/(opens in a new tab)Find more at:
  • 6. Checkpoint Process max_wal_size When a user makes changes to the data, it first goes through the buffer. Now that buffer is dirty. The fact that a user has committed a change does not mean that the modification has been written to the data file. The checkpointer process is responsible to write the change to the data file. There is a trade-off between performance (infrequent checkpoints) and time needed for recovery (frequent checkpoints) There are multiple parameters to control how often the checkpoints are done but the main one is based on size: max_wal_size. This sets the maximum size the WAL is allowed to grow between the control points. Check Course on Udemy PostgreSQL High-Performance Course