SlideShare a Scribd company logo
1 of 52
PostgreSQL Performance
Problems:
Monitoring and Alerting
Grant Fritchey
DevOps Advocate
Microsoft Data Platform MVP
AWS Community Builder
About me
grant@scarydba.com
scarydba.com
@gfritchey
linkedin.com/in/grant-fritchey
Ryan Booz
PostgreSQL & DevOps
Advocate
@ryanbooz
About me
/in/ryanbooz
www.softwareandbooz.com
youtube.com/@ryanbooz
Introduction
• Understand common
performance problems and how
to spot them
• Develop knowledge about the
tools within PostgreSQL that
show performance issues
• Learn about open source and 3rd
party monitoring solutions
Goals
Most Common Database
Problems
• Performance
• Scalability
• Security
• Multiple platforms
• Data integration & integrity
• Compliance
Today’s Focus, Performance Tuning
#PASSDataCommunitySummit
Easy Performance
Tuning
Actual Performance Tuning
Identify Poorly
Performing
Queries
Examine Code
and Execution
Plans
Implement
Potential Solution
Validate
Improvement
Deploy To
Production
Questions?
Standard Tools For Performance Metrics
Statistics in PostgreSQL
Statistics Views
• There are many. Each release tends to add more,
or more detail
• Most standard views ship with multiple versions:
• "all" view – contains a row for all objects of that type
• "sys" view – contains a row only for system objects of that type
• "user" view – contains a row only for user objects of that type
Standard Go-to Statistics Views
General
• pg_stat_database
• pg_stat_(all/sys/user)_tables
• pg_stat_(all/sys/user)_indexes
• pg_stat_io (PostgreSQL 16+)
For replication
• pg_stat_replication
• pg_stat_replication_slots
• pg_stat_subscription
• pg_stat_bgwriter
• pg_stat_archiver
• pg_stat_wal
pg_stat_database
• High-level statistics about each database
• Transaction counts
• Blocks used from cache or disk
• Tuples (rows) inserted, updated, deleted, etc.
• Deadlocks
• Session stats
• And more!
pg_stat_*_tables
• Some similarity to database statistics, but at the table level
• Tuple inserts, updates, deletes, hot updates, live, dead, etc.
• Time of last:
• table scan
• seq scan
• vacuum/autovacuum
• analyze/autoanalyzer
• And more!
pg_stat_*_indexes
• Number of scans
• Last scan of index
• Tuples read or fetched using the index scan
pg_stat_io
• New in PostgreSQL 16
• Part of the 'contrib' modules
• I/O related statistics based on backend type, object type,
and context
• Helpful for tuning
• 'shared_buffers' (page cache)
• Checkpointer inefficiency
• Issues with background jobs
What is pg_stat_statements?
• An extension included with PostgreSQL 8.4+
• It is part of the contrib module but not enabled by default
• Must be loaded via ‘shared_preload_libraries’ in postgresql.conf
• Tracks aggregated statistics of all queries in the cluster
• Installing the extension in the database creates the necessary views to query the data
• Every dbid, userid, and
queryid
• Stats are grouped based on
query structure and final ID as
determined by an internal
hash calculation
How does it store aggregates?
How does it identify queries?
SELECT id, name FROM table1 WHERE id = 1000;
SELECT id, name FROM table1 WHERE id = $1;
SELECT id, name FROM table1 WHERE id IN
(1000,2000,3000);
SELECT id, name FROM table1 WHERE id IN
($1,$2,$3);
pg_stat_statement statistics
• Execution Time (total/min/max/mean/stddev)
• Planning Time (total/min/max/mean/stddev)
• Calls (total)
• Rows (total)
• Buffers (shared/local/temp)
• read/hit/dirtied/written
• read/write time
• WAL
39 Columns of data
(as of PG16)
Name |Value
-------------------+-----------------------
userid |16422
dbid |16434
queryid |-6155333619461995114
query |SELECT id, name FROM...
plans |0
total_plan_time |0.0
min_plan_time |0.0
max_plan_time |0.0
mean_plan_time |0.0
stddev_plan_time |0.0
calls |151
total_exec_time |8.489053
min_exec_time |0.013751
max_exec_time |1.356096
mean_exec_time |0.056218894039735096
stddev_exec_time |0.11851139585068957
rows |151
shared_blks_hit |450
shared_blks_read |3
All statistics are cumulative
from the last restart*
*or reset by a superuser
Caveats
• PostgreSQL 13
• modified column names to include
planning statistics
• PostgreSQL 14
• Must set “compute_query_id”=true
in postgresql.conf
• Includes informational view for
allocation and “last reset”
information
pg_stat_*_indexes
• Number of scans
• Last scan of index
• Tuples read or fetched using the index scan
pg_stat_io
• New in PostgreSQL 16
• Part of the 'contrib' modules
• I/O related statistics based on backend type, object
type, and context
• Helpful for tuning
• 'shared_buffers' (page cache)
• Checkpointer inefficiency
• Issues with background jobs
pg_stat_kcache
• Open-source extension that provides statistics about real
reads and writes done at the filesystem layer
• Requires pg_stat_statements to be installed
• Must be added to the 'shared_preload_libraries' configuration
parameter
• Query-level filesystem statistics
• *Not often available in hosted environments
• AWS/Azure/GCP
have some limited
tooling to
track/display some
of this data
Postgres Tools for Data Collection
PostgreSQL Performance Tuning
Common Tools
• pgAdmin and other IDEs
• Numerous open-source, Grafana-based tools
• pgWatch
• pgDash
• pg_stat_monitor
• auto_explain
• Some recent Python-based solutions, but they
aren't dynamic
Pros/cons
• Native tools, where available, are used by many, well
documented, and usually easy to get started with (at
least SQL Server)
• However, they can only go as far as the tool allows, and
they rarely have exactly the right documentation to
understand how to act upon the data
• Not all of these tools help you bring correlation between
the graphs and issues.
• Things are (at least slightly) desperate.
• Statistics determine plan choice
• Customizable per server, table, and column
• Asynchronous process maintains statistics
• Manually update with ANALYZE
Cost-based Optimizer
• Histogram of column values
• Defaults to 100 buckets
• Helpful views:
• pg_catalog.pg_stats
• pg_catalog.pg_stat_user_tables
Statistics
• EXPLAIN = Estimated plan
• EXPLAIN ANALYZE = Actual plan
• EXPLAIN (ANALYZE,BUFFERS)
• Query plan with disk IO
• EXPLAIN (ANALYZE,BUFFERS,VERBOSE)
• Additional details on columns, schemas, etc.
EXPLAIN in Practice
• Inverted tree representation
• There is no built-in visual execution plan
• EXPLAIN provides textual plan
• pgAdmin does attempt some visualizations
• Websites for visualizations and suggestions
• https://www.pgmustard.com/
• https://explain.depesz.com/
EXPLAIN
--> Nodes
Read inside-out
Join/Aggregate/Merge/Append at each level
EXPLAIN
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_name, count(*), date_part('year',order_date)::int order_year
FROM sales.orders o
INNER JOIN sales.customers c ON o.customer_id=c.customer_id
WHERE c.customer_id=100
GROUP BY customer_name,order_year
ORDER BY order_year DESC;
GroupAggregate (cost=1827.91..1830.76 rows=102 width=35) (actual time=11.956..13.754 rows=4 loops=1)
Group Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer), c.customer_name
Buffers: shared hit=903
-> Sort (cost=1827.91..1828.18 rows=107 width=27) (actual time=11.700..12.686 rows=107 loops=1)
Sort Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer) DESC, c.customer_name
Sort Method: quicksort Memory: 33kB
Buffers: shared hit=903
-> Nested Loop (cost=0.28..1824.30 rows=107 width=27) (actual time=0.113..10.649 rows=107 loops=1)
Buffers: shared hit=903
-> Index Scan using pk_sales_customers on customers c (cost=0.28..2.49 rows=1 width=27)
Index Cond: (customer_id = 100)
Buffers: shared hit=3
-> Seq Scan on orders o (cost=0.00..1819.94 rows=107 width=8) (actual time=0.053..8.413 rows=107 loops=1)
Filter: (customer_id = 100)
Rows Removed by Filter: 73488
Buffers: shared hit=900
Planning Time: 0.267 ms
Execution Time: 13.934 ms
GroupAggregate (cost=1827.91..1830.76 rows=102 width=35)
(actual time=11.956..13.754 rows=4 loops=1)
Group Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer), c.customer_name
Buffers: shared hit=903
-> Sort (cost=1827.91..1828.18 rows=107 width=27) (actual time=11.700..12.686 rows=107 loops=1)
Sort Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer) DESC, c.customer_name
Sort Method: quicksort Memory: 33kB
Buffers: shared hit=903
-> Nested Loop (cost=0.28..1824.30 rows=107 width=27) (actual time=0.113..10.649 rows=107 loops=1)
Buffers: shared hit=903
-> Index Scan using pk_sales_customers on customers c (cost=0.28..2.49 rows=1 width=27)
Index Cond: (customer_id = 100)
Buffers: shared hit=3
-> Seq Scan on orders o (cost=0.00..1819.94 rows=107 width=8) (actual time=0.053..8.413 rows=107 loops=1)
Filter: (customer_id = 100)
Rows Removed by Filter: 73488
Buffers: shared hit=900
Planning Time: 0.267 ms
Execution Time: 13.934 ms
• Sequence scan (seq scan)
• Index scan
• Index-only scan
Primary Scan nodes
• Nested Loop
• Hash Join
• Merge Join
Primary join nodes
Explain Glossary: https://www.pgmustard.com/docs/explain
Questions?
3rd Party & Open Source Tools
Open Source Monitoring Solutions
Nagios
Core
PgHero pgCluu
pgWatch pgAdmin
Paid Monitoring Solutions
DataDog Better
Stack
pgDash
pgAnalyze Redgate
SQL
Monitor
Wrap-up
• Understand the common
performance problems and how
to spot them
• Develop knowledge about the
tools within PostgreSQL that
show performance issues
• Learn about open source and 3rd
party monitoring solutions
Goals
Grant Fritchey
DevOps Advocate
Microsoft Data Platform MVP
AWS Community Builder
About me
grant@scarydba.com
scarydba.com
@gfritchey
linkedin.com/in/grant-fritchey
Ryan Booz
PostgreSQL & DevOps
Advocate
@ryanbooz
About me
/in/ryanbooz
www.softwareandbooz.com
youtube.com/@ryanbooz
Questions?

More Related Content

Similar to PostgreSQL Performance Problems: Monitoring and Alerting

Jethro data meetup index base sql on hadoop - oct-2014
Jethro data meetup    index base sql on hadoop - oct-2014Jethro data meetup    index base sql on hadoop - oct-2014
Jethro data meetup index base sql on hadoop - oct-2014Eli Singer
 
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)Gabriele Bartolini
 
Microsoft Azure Big Data Analytics
Microsoft Azure Big Data AnalyticsMicrosoft Azure Big Data Analytics
Microsoft Azure Big Data AnalyticsMark Kromer
 
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineApache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineDataWorks Summit
 
Journey through high performance django application
Journey through high performance django applicationJourney through high performance django application
Journey through high performance django applicationbangaloredjangousergroup
 
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...Gruter
 
PostgreSQL High_Performance_Cheatsheet
PostgreSQL High_Performance_CheatsheetPostgreSQL High_Performance_Cheatsheet
PostgreSQL High_Performance_CheatsheetLucian Oprea
 
Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek PROIDEA
 
Docker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic StackDocker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic StackJakub Hajek
 
Challenges of Implementing an Advanced SQL Engine on Hadoop
Challenges of Implementing an Advanced SQL Engine on HadoopChallenges of Implementing an Advanced SQL Engine on Hadoop
Challenges of Implementing an Advanced SQL Engine on HadoopDataWorks Summit
 
Machine Learning with ML.NET and Azure - Andy Cross
Machine Learning with ML.NET and Azure - Andy CrossMachine Learning with ML.NET and Azure - Andy Cross
Machine Learning with ML.NET and Azure - Andy CrossAndrew Flatters
 
2020 07-30 elastic agent + ingest management
2020 07-30 elastic agent + ingest management2020 07-30 elastic agent + ingest management
2020 07-30 elastic agent + ingest managementDaliya Spasova
 
Building a Complex, Real-Time Data Management Application
Building a Complex, Real-Time Data Management ApplicationBuilding a Complex, Real-Time Data Management Application
Building a Complex, Real-Time Data Management ApplicationJonathan Katz
 
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
 
Performance eng prakash.sahu
Performance eng prakash.sahuPerformance eng prakash.sahu
Performance eng prakash.sahuDr. Prakash Sahu
 
Advanced data modeling with apache cassandra
Advanced data modeling with apache cassandraAdvanced data modeling with apache cassandra
Advanced data modeling with apache cassandraPatrick McFadin
 
SOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBSOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBUniFabric
 

Similar to PostgreSQL Performance Problems: Monitoring and Alerting (20)

Jethro data meetup index base sql on hadoop - oct-2014
Jethro data meetup    index base sql on hadoop - oct-2014Jethro data meetup    index base sql on hadoop - oct-2014
Jethro data meetup index base sql on hadoop - oct-2014
 
Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
 
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
 
Microsoft Azure Big Data Analytics
Microsoft Azure Big Data AnalyticsMicrosoft Azure Big Data Analytics
Microsoft Azure Big Data Analytics
 
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineApache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
 
Journey through high performance django application
Journey through high performance django applicationJourney through high performance django application
Journey through high performance django application
 
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
 
PostgreSQL High_Performance_Cheatsheet
PostgreSQL High_Performance_CheatsheetPostgreSQL High_Performance_Cheatsheet
PostgreSQL High_Performance_Cheatsheet
 
Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek
 
Docker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic StackDocker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic Stack
 
Challenges of Implementing an Advanced SQL Engine on Hadoop
Challenges of Implementing an Advanced SQL Engine on HadoopChallenges of Implementing an Advanced SQL Engine on Hadoop
Challenges of Implementing an Advanced SQL Engine on Hadoop
 
Machine Learning with ML.NET and Azure - Andy Cross
Machine Learning with ML.NET and Azure - Andy CrossMachine Learning with ML.NET and Azure - Andy Cross
Machine Learning with ML.NET and Azure - Andy Cross
 
Taming the shrew Power BI
Taming the shrew Power BITaming the shrew Power BI
Taming the shrew Power BI
 
2020 07-30 elastic agent + ingest management
2020 07-30 elastic agent + ingest management2020 07-30 elastic agent + ingest management
2020 07-30 elastic agent + ingest management
 
Prestogres internals
Prestogres internalsPrestogres internals
Prestogres internals
 
Building a Complex, Real-Time Data Management Application
Building a Complex, Real-Time Data Management ApplicationBuilding a Complex, Real-Time Data Management Application
Building a Complex, Real-Time Data Management Application
 
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
 
Performance eng prakash.sahu
Performance eng prakash.sahuPerformance eng prakash.sahu
Performance eng prakash.sahu
 
Advanced data modeling with apache cassandra
Advanced data modeling with apache cassandraAdvanced data modeling with apache cassandra
Advanced data modeling with apache cassandra
 
SOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBSOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DB
 

More from Grant Fritchey

Automating Database Deployments Using Azure DevOps
Automating Database Deployments Using Azure DevOpsAutomating Database Deployments Using Azure DevOps
Automating Database Deployments Using Azure DevOpsGrant Fritchey
 
Learn To Effectively Use Extended Events_Techorama.pdf
Learn To Effectively Use Extended Events_Techorama.pdfLearn To Effectively Use Extended Events_Techorama.pdf
Learn To Effectively Use Extended Events_Techorama.pdfGrant Fritchey
 
Using Query Store to Understand and Control Query Performance
Using Query Store to Understand and Control Query PerformanceUsing Query Store to Understand and Control Query Performance
Using Query Store to Understand and Control Query PerformanceGrant Fritchey
 
You Should Be Standing Here: Learn How To Present a Session
You Should Be Standing Here: Learn How To Present a SessionYou Should Be Standing Here: Learn How To Present a Session
You Should Be Standing Here: Learn How To Present a SessionGrant Fritchey
 
Redgate Community Circle: Tools For SQL Server Performance Tuning
Redgate Community Circle: Tools For SQL Server Performance TuningRedgate Community Circle: Tools For SQL Server Performance Tuning
Redgate Community Circle: Tools For SQL Server Performance TuningGrant Fritchey
 
10 Steps To Global Data Compliance
10 Steps To Global Data Compliance10 Steps To Global Data Compliance
10 Steps To Global Data ComplianceGrant Fritchey
 
Time to Use the Columnstore Index
Time to Use the Columnstore IndexTime to Use the Columnstore Index
Time to Use the Columnstore IndexGrant Fritchey
 
Introduction to SQL Server in Containers
Introduction to SQL Server in ContainersIntroduction to SQL Server in Containers
Introduction to SQL Server in ContainersGrant Fritchey
 
SQL Injection: How It Works, How to Stop It
SQL Injection: How It Works, How to Stop ItSQL Injection: How It Works, How to Stop It
SQL Injection: How It Works, How to Stop ItGrant Fritchey
 
Privacy and Protection in the World of Database DevOps
Privacy and Protection in the World of Database DevOpsPrivacy and Protection in the World of Database DevOps
Privacy and Protection in the World of Database DevOpsGrant Fritchey
 
SQL Server Tools for Query Tuning
SQL Server Tools for Query TuningSQL Server Tools for Query Tuning
SQL Server Tools for Query TuningGrant Fritchey
 
Extending DevOps to SQL Server
Extending DevOps to SQL ServerExtending DevOps to SQL Server
Extending DevOps to SQL ServerGrant Fritchey
 
Introducing Azure SQL Data Warehouse
Introducing Azure SQL Data WarehouseIntroducing Azure SQL Data Warehouse
Introducing Azure SQL Data WarehouseGrant Fritchey
 
Introducing Azure Databases
Introducing Azure DatabasesIntroducing Azure Databases
Introducing Azure DatabasesGrant Fritchey
 
Statistis, Row Counts, Execution Plans and Query Tuning
Statistis, Row Counts, Execution Plans and Query TuningStatistis, Row Counts, Execution Plans and Query Tuning
Statistis, Row Counts, Execution Plans and Query TuningGrant Fritchey
 
Understanding Your Servers, All Your Servers
Understanding Your Servers, All Your ServersUnderstanding Your Servers, All Your Servers
Understanding Your Servers, All Your ServersGrant Fritchey
 
Changing Your Habits: Tips to Tune Your T-SQL
Changing Your Habits: Tips to Tune Your T-SQLChanging Your Habits: Tips to Tune Your T-SQL
Changing Your Habits: Tips to Tune Your T-SQLGrant Fritchey
 
Azure SQL Database for the Earthed DBA
Azure SQL Database for the Earthed DBAAzure SQL Database for the Earthed DBA
Azure SQL Database for the Earthed DBAGrant Fritchey
 
The Query Store SQL Tuning
The Query Store SQL TuningThe Query Store SQL Tuning
The Query Store SQL TuningGrant Fritchey
 

More from Grant Fritchey (20)

Automating Database Deployments Using Azure DevOps
Automating Database Deployments Using Azure DevOpsAutomating Database Deployments Using Azure DevOps
Automating Database Deployments Using Azure DevOps
 
Learn To Effectively Use Extended Events_Techorama.pdf
Learn To Effectively Use Extended Events_Techorama.pdfLearn To Effectively Use Extended Events_Techorama.pdf
Learn To Effectively Use Extended Events_Techorama.pdf
 
Using Query Store to Understand and Control Query Performance
Using Query Store to Understand and Control Query PerformanceUsing Query Store to Understand and Control Query Performance
Using Query Store to Understand and Control Query Performance
 
You Should Be Standing Here: Learn How To Present a Session
You Should Be Standing Here: Learn How To Present a SessionYou Should Be Standing Here: Learn How To Present a Session
You Should Be Standing Here: Learn How To Present a Session
 
Redgate Community Circle: Tools For SQL Server Performance Tuning
Redgate Community Circle: Tools For SQL Server Performance TuningRedgate Community Circle: Tools For SQL Server Performance Tuning
Redgate Community Circle: Tools For SQL Server Performance Tuning
 
10 Steps To Global Data Compliance
10 Steps To Global Data Compliance10 Steps To Global Data Compliance
10 Steps To Global Data Compliance
 
Time to Use the Columnstore Index
Time to Use the Columnstore IndexTime to Use the Columnstore Index
Time to Use the Columnstore Index
 
Introduction to SQL Server in Containers
Introduction to SQL Server in ContainersIntroduction to SQL Server in Containers
Introduction to SQL Server in Containers
 
DevOps for the DBA
DevOps for the DBADevOps for the DBA
DevOps for the DBA
 
SQL Injection: How It Works, How to Stop It
SQL Injection: How It Works, How to Stop ItSQL Injection: How It Works, How to Stop It
SQL Injection: How It Works, How to Stop It
 
Privacy and Protection in the World of Database DevOps
Privacy and Protection in the World of Database DevOpsPrivacy and Protection in the World of Database DevOps
Privacy and Protection in the World of Database DevOps
 
SQL Server Tools for Query Tuning
SQL Server Tools for Query TuningSQL Server Tools for Query Tuning
SQL Server Tools for Query Tuning
 
Extending DevOps to SQL Server
Extending DevOps to SQL ServerExtending DevOps to SQL Server
Extending DevOps to SQL Server
 
Introducing Azure SQL Data Warehouse
Introducing Azure SQL Data WarehouseIntroducing Azure SQL Data Warehouse
Introducing Azure SQL Data Warehouse
 
Introducing Azure Databases
Introducing Azure DatabasesIntroducing Azure Databases
Introducing Azure Databases
 
Statistis, Row Counts, Execution Plans and Query Tuning
Statistis, Row Counts, Execution Plans and Query TuningStatistis, Row Counts, Execution Plans and Query Tuning
Statistis, Row Counts, Execution Plans and Query Tuning
 
Understanding Your Servers, All Your Servers
Understanding Your Servers, All Your ServersUnderstanding Your Servers, All Your Servers
Understanding Your Servers, All Your Servers
 
Changing Your Habits: Tips to Tune Your T-SQL
Changing Your Habits: Tips to Tune Your T-SQLChanging Your Habits: Tips to Tune Your T-SQL
Changing Your Habits: Tips to Tune Your T-SQL
 
Azure SQL Database for the Earthed DBA
Azure SQL Database for the Earthed DBAAzure SQL Database for the Earthed DBA
Azure SQL Database for the Earthed DBA
 
The Query Store SQL Tuning
The Query Store SQL TuningThe Query Store SQL Tuning
The Query Store SQL Tuning
 

Recently uploaded

Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
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
 
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
 
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
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
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
 
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
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 

Recently uploaded (20)

Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
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
 
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
 
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
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
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...
 
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
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 

PostgreSQL Performance Problems: Monitoring and Alerting

  • 2. Grant Fritchey DevOps Advocate Microsoft Data Platform MVP AWS Community Builder About me grant@scarydba.com scarydba.com @gfritchey linkedin.com/in/grant-fritchey
  • 3. Ryan Booz PostgreSQL & DevOps Advocate @ryanbooz About me /in/ryanbooz www.softwareandbooz.com youtube.com/@ryanbooz
  • 5. • Understand common performance problems and how to spot them • Develop knowledge about the tools within PostgreSQL that show performance issues • Learn about open source and 3rd party monitoring solutions Goals
  • 6. Most Common Database Problems • Performance • Scalability • Security • Multiple platforms • Data integration & integrity • Compliance
  • 9. Actual Performance Tuning Identify Poorly Performing Queries Examine Code and Execution Plans Implement Potential Solution Validate Improvement Deploy To Production
  • 11. Standard Tools For Performance Metrics
  • 13. Statistics Views • There are many. Each release tends to add more, or more detail • Most standard views ship with multiple versions: • "all" view – contains a row for all objects of that type • "sys" view – contains a row only for system objects of that type • "user" view – contains a row only for user objects of that type
  • 14. Standard Go-to Statistics Views General • pg_stat_database • pg_stat_(all/sys/user)_tables • pg_stat_(all/sys/user)_indexes • pg_stat_io (PostgreSQL 16+) For replication • pg_stat_replication • pg_stat_replication_slots • pg_stat_subscription • pg_stat_bgwriter • pg_stat_archiver • pg_stat_wal
  • 15. pg_stat_database • High-level statistics about each database • Transaction counts • Blocks used from cache or disk • Tuples (rows) inserted, updated, deleted, etc. • Deadlocks • Session stats • And more!
  • 16. pg_stat_*_tables • Some similarity to database statistics, but at the table level • Tuple inserts, updates, deletes, hot updates, live, dead, etc. • Time of last: • table scan • seq scan • vacuum/autovacuum • analyze/autoanalyzer • And more!
  • 17.
  • 18. pg_stat_*_indexes • Number of scans • Last scan of index • Tuples read or fetched using the index scan
  • 19. pg_stat_io • New in PostgreSQL 16 • Part of the 'contrib' modules • I/O related statistics based on backend type, object type, and context • Helpful for tuning • 'shared_buffers' (page cache) • Checkpointer inefficiency • Issues with background jobs
  • 20. What is pg_stat_statements? • An extension included with PostgreSQL 8.4+ • It is part of the contrib module but not enabled by default • Must be loaded via ‘shared_preload_libraries’ in postgresql.conf • Tracks aggregated statistics of all queries in the cluster • Installing the extension in the database creates the necessary views to query the data
  • 21. • Every dbid, userid, and queryid • Stats are grouped based on query structure and final ID as determined by an internal hash calculation How does it store aggregates?
  • 22. How does it identify queries? SELECT id, name FROM table1 WHERE id = 1000; SELECT id, name FROM table1 WHERE id = $1; SELECT id, name FROM table1 WHERE id IN (1000,2000,3000); SELECT id, name FROM table1 WHERE id IN ($1,$2,$3);
  • 23. pg_stat_statement statistics • Execution Time (total/min/max/mean/stddev) • Planning Time (total/min/max/mean/stddev) • Calls (total) • Rows (total) • Buffers (shared/local/temp) • read/hit/dirtied/written • read/write time • WAL
  • 24. 39 Columns of data (as of PG16)
  • 25. Name |Value -------------------+----------------------- userid |16422 dbid |16434 queryid |-6155333619461995114 query |SELECT id, name FROM... plans |0 total_plan_time |0.0 min_plan_time |0.0 max_plan_time |0.0 mean_plan_time |0.0 stddev_plan_time |0.0 calls |151 total_exec_time |8.489053 min_exec_time |0.013751 max_exec_time |1.356096 mean_exec_time |0.056218894039735096 stddev_exec_time |0.11851139585068957 rows |151 shared_blks_hit |450 shared_blks_read |3
  • 26. All statistics are cumulative from the last restart* *or reset by a superuser
  • 27. Caveats • PostgreSQL 13 • modified column names to include planning statistics • PostgreSQL 14 • Must set “compute_query_id”=true in postgresql.conf • Includes informational view for allocation and “last reset” information
  • 28. pg_stat_*_indexes • Number of scans • Last scan of index • Tuples read or fetched using the index scan
  • 29. pg_stat_io • New in PostgreSQL 16 • Part of the 'contrib' modules • I/O related statistics based on backend type, object type, and context • Helpful for tuning • 'shared_buffers' (page cache) • Checkpointer inefficiency • Issues with background jobs
  • 30. pg_stat_kcache • Open-source extension that provides statistics about real reads and writes done at the filesystem layer • Requires pg_stat_statements to be installed • Must be added to the 'shared_preload_libraries' configuration parameter • Query-level filesystem statistics • *Not often available in hosted environments
  • 31. • AWS/Azure/GCP have some limited tooling to track/display some of this data Postgres Tools for Data Collection
  • 33. Common Tools • pgAdmin and other IDEs • Numerous open-source, Grafana-based tools • pgWatch • pgDash • pg_stat_monitor • auto_explain • Some recent Python-based solutions, but they aren't dynamic
  • 34. Pros/cons • Native tools, where available, are used by many, well documented, and usually easy to get started with (at least SQL Server) • However, they can only go as far as the tool allows, and they rarely have exactly the right documentation to understand how to act upon the data • Not all of these tools help you bring correlation between the graphs and issues. • Things are (at least slightly) desperate.
  • 35. • Statistics determine plan choice • Customizable per server, table, and column • Asynchronous process maintains statistics • Manually update with ANALYZE Cost-based Optimizer
  • 36. • Histogram of column values • Defaults to 100 buckets • Helpful views: • pg_catalog.pg_stats • pg_catalog.pg_stat_user_tables Statistics
  • 37. • EXPLAIN = Estimated plan • EXPLAIN ANALYZE = Actual plan • EXPLAIN (ANALYZE,BUFFERS) • Query plan with disk IO • EXPLAIN (ANALYZE,BUFFERS,VERBOSE) • Additional details on columns, schemas, etc. EXPLAIN in Practice
  • 38. • Inverted tree representation • There is no built-in visual execution plan • EXPLAIN provides textual plan • pgAdmin does attempt some visualizations • Websites for visualizations and suggestions • https://www.pgmustard.com/ • https://explain.depesz.com/ EXPLAIN
  • 40. EXPLAIN (ANALYZE, BUFFERS) SELECT customer_name, count(*), date_part('year',order_date)::int order_year FROM sales.orders o INNER JOIN sales.customers c ON o.customer_id=c.customer_id WHERE c.customer_id=100 GROUP BY customer_name,order_year ORDER BY order_year DESC; GroupAggregate (cost=1827.91..1830.76 rows=102 width=35) (actual time=11.956..13.754 rows=4 loops=1) Group Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer), c.customer_name Buffers: shared hit=903 -> Sort (cost=1827.91..1828.18 rows=107 width=27) (actual time=11.700..12.686 rows=107 loops=1) Sort Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer) DESC, c.customer_name Sort Method: quicksort Memory: 33kB Buffers: shared hit=903 -> Nested Loop (cost=0.28..1824.30 rows=107 width=27) (actual time=0.113..10.649 rows=107 loops=1) Buffers: shared hit=903 -> Index Scan using pk_sales_customers on customers c (cost=0.28..2.49 rows=1 width=27) Index Cond: (customer_id = 100) Buffers: shared hit=3 -> Seq Scan on orders o (cost=0.00..1819.94 rows=107 width=8) (actual time=0.053..8.413 rows=107 loops=1) Filter: (customer_id = 100) Rows Removed by Filter: 73488 Buffers: shared hit=900 Planning Time: 0.267 ms Execution Time: 13.934 ms
  • 41. GroupAggregate (cost=1827.91..1830.76 rows=102 width=35) (actual time=11.956..13.754 rows=4 loops=1) Group Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer), c.customer_name Buffers: shared hit=903 -> Sort (cost=1827.91..1828.18 rows=107 width=27) (actual time=11.700..12.686 rows=107 loops=1) Sort Key: ((date_part('year'::text, (o.order_date)::timestamp without time zone))::integer) DESC, c.customer_name Sort Method: quicksort Memory: 33kB Buffers: shared hit=903 -> Nested Loop (cost=0.28..1824.30 rows=107 width=27) (actual time=0.113..10.649 rows=107 loops=1) Buffers: shared hit=903 -> Index Scan using pk_sales_customers on customers c (cost=0.28..2.49 rows=1 width=27) Index Cond: (customer_id = 100) Buffers: shared hit=3 -> Seq Scan on orders o (cost=0.00..1819.94 rows=107 width=8) (actual time=0.053..8.413 rows=107 loops=1) Filter: (customer_id = 100) Rows Removed by Filter: 73488 Buffers: shared hit=900 Planning Time: 0.267 ms Execution Time: 13.934 ms
  • 42. • Sequence scan (seq scan) • Index scan • Index-only scan Primary Scan nodes
  • 43. • Nested Loop • Hash Join • Merge Join Primary join nodes Explain Glossary: https://www.pgmustard.com/docs/explain
  • 45. 3rd Party & Open Source Tools
  • 46. Open Source Monitoring Solutions Nagios Core PgHero pgCluu pgWatch pgAdmin
  • 47. Paid Monitoring Solutions DataDog Better Stack pgDash pgAnalyze Redgate SQL Monitor
  • 49. • Understand the common performance problems and how to spot them • Develop knowledge about the tools within PostgreSQL that show performance issues • Learn about open source and 3rd party monitoring solutions Goals
  • 50. Grant Fritchey DevOps Advocate Microsoft Data Platform MVP AWS Community Builder About me grant@scarydba.com scarydba.com @gfritchey linkedin.com/in/grant-fritchey
  • 51. Ryan Booz PostgreSQL & DevOps Advocate @ryanbooz About me /in/ryanbooz www.softwareandbooz.com youtube.com/@ryanbooz

Editor's Notes

  1. sunrise-1371391077dmN.jpg (1920×1280) (publicdomainpictures.net)
  2. 12.jpg (852×480) (picdn.net)
  3. Study Identifies Top Five Most Challenging Database Management Issues (datavail.com) 5 Common Database Management Challenges & How to Solve Them (hackread.com) Blog: 9 Common Database Management Challenges and How to Fix Them | Tudip Top 10 Big Data Challenges and How to Address Them (techtarget.com) dumpster+fire.jpg (400×281) (bp.blogspot.com)
  4. Big question mark | in Ipswich | Benjamin Reay | Flickr
  5. www.maxpixel.net | 522: Connection timed out
  6. Big question mark | in Ipswich | Benjamin Reay | Flickr
  7. tools.jpg (1200×675) (areweconnected.com)
  8. circuit board - Bing images
  9. Nagios is a log tool with a PostgreSQL add-on. pgWatch, pgHero, pgAdmin, pgCluu are database tools
  10. DataDog and Better Stack are log monitor tools that add additional metrics. The other three are all focused database tools, but of the three, only SQL Monitor works on more than just PostgreSQL
  11. moon-lunar-sky-night.jpg (910×683) (wallpaperflare.com)
  12. 12.jpg (852×480) (picdn.net)
  13. Big question mark | in Ipswich | Benjamin Reay | Flickr