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)

PPT
Oracle archi ppt
Hitesh Kumar Markam
ย 
PDF
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
ย 
PPTX
Oracle architecture with details-yogiji creations
Yogiji Creations
ย 
PPS
Data models
Anuj Modi
ย 
PDF
ฤฐleri Seviye T-SQL Programlama - Chapter 19
Cihan ร–zhan
ย 
PDF
[pgday.Seoul 2022] ์„œ๋น„์Šค๊ฐœํŽธ์‹œ PostgreSQL ๋„์ž…๊ธฐ - ์ง„์†Œ๋ฆฐ & ๊น€ํƒœ์ •
PgDay.Seoul
ย 
PPT
Mysql
TSUBHASHRI
ย 
PDF
MySQL 8.0ใงๆ†ถใˆใฆใŠใ„ใฆใปใ—ใ„ใ“ใจ
yoku0825
ย 
PPTX
Ordered lists
Toni Norman
ย 
PDF
ORACLE ARCHITECTURE
Manohar Tatwawadi
ย 
PPT
Mysql database
Arshikhan08
ย 
PPTX
Database management functions
yhen06
ย 
KEY
Graphdatabases
Henning Rauch
ย 
PPTX
Database basics
prachin514
ย 
PPT
Presentation on dbms(relational calculus)
yourbookworldanil
ย 
PPTX
MySQL Architecture and Engine
Abdul Manaf
ย 
PPTX
Html and css presentation
umesh patil
ย 
DOCX
Keepalived+MaxScale+MariaDB_์šด์˜๋งค๋‰ด์–ผ_1.0.docx
NeoClova
ย 
PPTX
Stored procedure in sql server
baabtra.com - No. 1 supplier of quality freshers
ย 
PDF
PostgreSQLใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซ้ฃ›ใณ่พผใ‚‚ใ†
NTT DATA OSS Professional Services
ย 
Oracle archi ppt
Hitesh Kumar Markam
ย 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
ย 
Oracle architecture with details-yogiji creations
Yogiji Creations
ย 
Data models
Anuj Modi
ย 
ฤฐleri Seviye T-SQL Programlama - Chapter 19
Cihan ร–zhan
ย 
[pgday.Seoul 2022] ์„œ๋น„์Šค๊ฐœํŽธ์‹œ PostgreSQL ๋„์ž…๊ธฐ - ์ง„์†Œ๋ฆฐ & ๊น€ํƒœ์ •
PgDay.Seoul
ย 
Mysql
TSUBHASHRI
ย 
MySQL 8.0ใงๆ†ถใˆใฆใŠใ„ใฆใปใ—ใ„ใ“ใจ
yoku0825
ย 
Ordered lists
Toni Norman
ย 
ORACLE ARCHITECTURE
Manohar Tatwawadi
ย 
Mysql database
Arshikhan08
ย 
Database management functions
yhen06
ย 
Graphdatabases
Henning Rauch
ย 
Database basics
prachin514
ย 
Presentation on dbms(relational calculus)
yourbookworldanil
ย 
MySQL Architecture and Engine
Abdul Manaf
ย 
Html and css presentation
umesh patil
ย 
Keepalived+MaxScale+MariaDB_์šด์˜๋งค๋‰ด์–ผ_1.0.docx
NeoClova
ย 
Stored procedure in sql server
baabtra.com - No. 1 supplier of quality freshers
ย 
PostgreSQLใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซ้ฃ›ใณ่พผใ‚‚ใ†
NTT DATA OSS Professional Services
ย 

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
ย 
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
ย 
PDF
Introducing Azure SQL Data Warehouse
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
ย 
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
ย 
Introducing Azure SQL Data Warehouse
Grant Fritchey
ย 
Ad

Recently uploaded (20)

PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
ย 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
ย 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
ย 
PPTX
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
ย 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
PPTX
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
ย 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
ย 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
ย 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
ย 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
ย 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
ย 
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
ย 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
ย 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
ย 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
ย 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 

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