SlideShare a Scribd company logo
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!
PostgreSQL Performance Problems: Monitoring and Alerting
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

What's hot (20)

PDF
CloudInit Introduction
Publicis Sapient Engineering
 
PDF
BlueStore: a new, faster storage backend for Ceph
Sage Weil
 
PDF
Oracle Enterprise Manager Cloud Control 12c: how to solve 'ERROR: NMO Not Set...
Marco Vigelini
 
PDF
Room 2 - 6 - Đinh Tuấn Phong - Migrate opensource database to Kubernetes easi...
Vietnam Open Infrastructure User Group
 
PDF
Routed Provider Networks on OpenStack
Romana Project
 
PDF
Linux Linux Traffic Control
SUSE Labs Taipei
 
PDF
PostgreSQL HA
haroonm
 
PDF
Room 3 - 2 - Trần Tuấn Anh - Defending Software Supply Chain Security in Bank...
Vietnam Open Infrastructure User Group
 
PDF
Ceph Day Beijing - Ceph All-Flash Array Design Based on NUMA Architecture
Danielle Womboldt
 
PDF
Accelerating Envoy and Istio with Cilium and the Linux Kernel
Thomas Graf
 
ODP
ansible why ?
Yashar Esmaildokht
 
PDF
Open vSwitch - Stateful Connection Tracking & Stateful NAT
Thomas Graf
 
KEY
Non blocking io with netty
Zauber
 
PPTX
Chef vs Puppet vs Ansible vs Saltstack | Configuration Management Tools | Dev...
Simplilearn
 
PDF
H2O - the optimized HTTP server
Kazuho Oku
 
PPTX
HAProxy
Arindam Nayak
 
PDF
PostgreSQL Replication High Availability Methods
Mydbops
 
PDF
SR-IOV ixgbe Driver Limitations and Improvement
LF Events
 
PPT
Linux memory consumption
haish
 
PDF
Linux Synchronization Mechanism: RCU (Read Copy Update)
Adrian Huang
 
CloudInit Introduction
Publicis Sapient Engineering
 
BlueStore: a new, faster storage backend for Ceph
Sage Weil
 
Oracle Enterprise Manager Cloud Control 12c: how to solve 'ERROR: NMO Not Set...
Marco Vigelini
 
Room 2 - 6 - Đinh Tuấn Phong - Migrate opensource database to Kubernetes easi...
Vietnam Open Infrastructure User Group
 
Routed Provider Networks on OpenStack
Romana Project
 
Linux Linux Traffic Control
SUSE Labs Taipei
 
PostgreSQL HA
haroonm
 
Room 3 - 2 - Trần Tuấn Anh - Defending Software Supply Chain Security in Bank...
Vietnam Open Infrastructure User Group
 
Ceph Day Beijing - Ceph All-Flash Array Design Based on NUMA Architecture
Danielle Womboldt
 
Accelerating Envoy and Istio with Cilium and the Linux Kernel
Thomas Graf
 
ansible why ?
Yashar Esmaildokht
 
Open vSwitch - Stateful Connection Tracking & Stateful NAT
Thomas Graf
 
Non blocking io with netty
Zauber
 
Chef vs Puppet vs Ansible vs Saltstack | Configuration Management Tools | Dev...
Simplilearn
 
H2O - the optimized HTTP server
Kazuho Oku
 
HAProxy
Arindam Nayak
 
PostgreSQL Replication High Availability Methods
Mydbops
 
SR-IOV ixgbe Driver Limitations and Improvement
LF Events
 
Linux memory consumption
haish
 
Linux Synchronization Mechanism: RCU (Read Copy Update)
Adrian Huang
 

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

PDF
query_tuning.pdf
ssuserf99076
 
PDF
Advanced Postgres Monitoring
Denish Patel
 
PDF
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
PDF
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
Citus Data
 
PDF
Webinar slides: An Introduction to Performance Monitoring for PostgreSQL
Severalnines
 
PDF
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
PROIDEA
 
PDF
Monitoring Postgres at Scale | PostgresConf US 2018 | Lukas Fittl
Citus Data
 
PDF
Postgres performance for humans
Craig Kerstiens
 
PDF
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
PDF
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Ontico
 
PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
PDF
OSMC 2021 | pg_stat_monitor: A cool extension for better database (PostgreSQL...
NETWAYS
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PDF
PGConf.ASIA 2019 Bali - Performance Analysis at Full Power - Julien Rouhaud
Equnix Business Solutions
 
PDF
Explain this!
Fabio Telles Rodriguez
 
PDF
Postgres Performance for Humans
Citus Data
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PDF
Postgresql Up And Running Regina Obe Leo Hsu
zahidtraaslw
 
query_tuning.pdf
ssuserf99076
 
Advanced Postgres Monitoring
Denish Patel
 
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
Citus Data
 
Webinar slides: An Introduction to Performance Monitoring for PostgreSQL
Severalnines
 
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
PROIDEA
 
Monitoring Postgres at Scale | PostgresConf US 2018 | Lukas Fittl
Citus Data
 
Postgres performance for humans
Craig Kerstiens
 
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Ontico
 
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
OSMC 2021 | pg_stat_monitor: A cool extension for better database (PostgreSQL...
NETWAYS
 
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PGConf.ASIA 2019 Bali - Performance Analysis at Full Power - Julien Rouhaud
Equnix Business Solutions
 
Explain this!
Fabio Telles Rodriguez
 
Postgres Performance for Humans
Citus Data
 
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
Postgresql Up And Running Regina Obe Leo Hsu
zahidtraaslw
 
Ad

More from Grant Fritchey (20)

PDF
You Need a PostgreSQL Restore Plan Presentation
Grant Fritchey
 
PDF
PostgreSQL Query Performance Monitoring for the Absolute Beginner
Grant Fritchey
 
PDF
Leveraging AI for the PostgreSQL DBA #pgconf.eu
Grant Fritchey
 
PDF
Exploring Execution Plans, Learning to Read SQL Server Execution Plans
Grant Fritchey
 
PPTX
SQL Server Performance Tuning: Common Problems, Possible Solutions
Grant Fritchey
 
PDF
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
PPTX
Migrating To PostgreSQL
Grant Fritchey
 
PDF
Automating Database Deployments Using Azure DevOps
Grant Fritchey
 
PDF
Learn To Effectively Use Extended Events_Techorama.pdf
Grant Fritchey
 
PDF
Using Query Store to Understand and Control Query Performance
Grant Fritchey
 
PPTX
You Should Be Standing Here: Learn How To Present a Session
Grant Fritchey
 
PDF
Redgate Community Circle: Tools For SQL Server Performance Tuning
Grant Fritchey
 
PDF
10 Steps To Global Data Compliance
Grant Fritchey
 
PDF
Time to Use the Columnstore Index
Grant Fritchey
 
PDF
Introduction to SQL Server in Containers
Grant Fritchey
 
PDF
DevOps for the DBA
Grant Fritchey
 
PDF
SQL Injection: How It Works, How to Stop It
Grant Fritchey
 
PDF
Privacy and Protection in the World of Database DevOps
Grant Fritchey
 
PDF
SQL Server Tools for Query Tuning
Grant Fritchey
 
PPTX
Extending DevOps to SQL Server
Grant Fritchey
 
You Need a PostgreSQL Restore Plan Presentation
Grant Fritchey
 
PostgreSQL Query Performance Monitoring for the Absolute Beginner
Grant Fritchey
 
Leveraging AI for the PostgreSQL DBA #pgconf.eu
Grant Fritchey
 
Exploring Execution Plans, Learning to Read SQL Server Execution Plans
Grant Fritchey
 
SQL Server Performance Tuning: Common Problems, Possible Solutions
Grant Fritchey
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Migrating To PostgreSQL
Grant Fritchey
 
Automating Database Deployments Using Azure DevOps
Grant Fritchey
 
Learn To Effectively Use Extended Events_Techorama.pdf
Grant Fritchey
 
Using Query Store to Understand and Control Query Performance
Grant Fritchey
 
You Should Be Standing Here: Learn How To Present a Session
Grant Fritchey
 
Redgate Community Circle: Tools For SQL Server Performance Tuning
Grant Fritchey
 
10 Steps To Global Data Compliance
Grant Fritchey
 
Time to Use the Columnstore Index
Grant Fritchey
 
Introduction to SQL Server in Containers
Grant Fritchey
 
DevOps for the DBA
Grant Fritchey
 
SQL Injection: How It Works, How to Stop It
Grant Fritchey
 
Privacy and Protection in the World of Database DevOps
Grant Fritchey
 
SQL Server Tools for Query Tuning
Grant Fritchey
 
Extending DevOps to SQL Server
Grant Fritchey
 
Ad

Recently uploaded (20)

PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 

PostgreSQL Performance Problems: Monitoring and Alerting

  • 2. Grant Fritchey DevOps Advocate Microsoft Data Platform MVP AWS Community Builder About me [email protected] 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!
  • 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 [email protected] 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

  • #5: sunrise-1371391077dmN.jpg (1920×1280) (publicdomainpictures.net)
  • #6: 12.jpg (852×480) (picdn.net)
  • #7: 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)
  • #11: Big question mark | in Ipswich | Benjamin Reay | Flickr
  • #32: www.maxpixel.net | 522: Connection timed out
  • #45: Big question mark | in Ipswich | Benjamin Reay | Flickr
  • #46: tools.jpg (1200×675) (areweconnected.com)
  • #47: circuit board - Bing images
  • #48: Nagios is a log tool with a PostgreSQL add-on. pgWatch, pgHero, pgAdmin, pgCluu are database tools
  • #49: 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
  • #50: moon-lunar-sky-night.jpg (910×683) (wallpaperflare.com)
  • #51: 12.jpg (852×480) (picdn.net)
  • #54: Big question mark | in Ipswich | Benjamin Reay | Flickr