SlideShare a Scribd company logo
P!"#r$% P$rf&r'()*$
f&r H+'()%
@*r(,#-$r%",$)%
S.('$/$%% p/+#%
http://www.postgresweekly.com
http://www.craigkerstiens.com
http://www.postgresguide.com
http://www.postgresapp.com
http://postgres.heroku.com
Postgres - TLDR
Postgres - TLDR
Datatypes
Conditional Indexes
Transactional DDL
Foreign Data Wrappers
Concurrent Index Creation
Extensions
Common Table Expressions
Fast Column Addition
Listen/Notify
Table Inheritance
Per Transaction sync replication
Window functions
NoSQL inside SQL
Momentum
OLTP vs OLAP
OLTP
W$b(pp%
PostgresSetup/Config
PostgresSetup/Config
On Amazon
PostgresSetup/Config
On Amazon
Use Heroku OR ‘postgresql when its not your dayjob’
PostgresSetup/Config
On Amazon
Use Heroku OR ‘postgresql when its not your dayjob’
Other clouds
PostgresSetup/Config
On Amazon
Use Heroku OR ‘postgresql when its not your dayjob’
Other clouds
‘postgresql when its not your dayjob’
PostgresSetup/Config
On Amazon
Use Heroku OR ‘postgresql when its not your dayjob’
Other clouds
‘postgresql when its not your dayjob’
Real hardware
PostgresSetup/Config
On Amazon
Use Heroku OR ‘postgresql when its not your dayjob’
Other clouds
‘postgresql when its not your dayjob’
Real hardware
High performance PostgreSQL
PostgresSetup/Config
On Amazon
Use Heroku OR ‘postgresql when its not your dayjob’
Other clouds
‘postgresql when its not your dayjob’
Real hardware
High performance PostgreSQL
http://thebuild.com/blog/2012/06/04/postgresql-when-its-not-your-job-at-djangocon-europe/
C(*.$
80/20 r+/$
Postgres performance for humans
Cache Hit RateSELECT
'index hit rate' as name,
(sum(idx_blks_hit) - sum(idx_blks_read)) /
sum(idx_blks_hit + idx_blks_read) as ratio
FROM pg_statio_user_indexes
union all
SELECT
'cache hit rate' as name,
case sum(idx_blks_hit)
when 0 then 'NaN'::numeric
else to_char((sum(idx_blks_hit) -
sum(idx_blks_read)) / sum(idx_blks_hit + idx_blks_read),
'99.99')::numeric
end as ratio
FROM pg_statio_user_indexes)
Cache Hit Rate
name | ratio
----------------+------------------------
cache hit rate | 0.99
Index Hit Rate
SELECT
relname,
100 * idx_scan / (seq_scan + idx_scan),
n_live_tup
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;
Index Hit Rate
relname | percent_of_times_index_used | rows_in_table
---------------------+-----------------------------+---------------
events | 0 | 669917
app_infos_user_info | 0 | 198218
app_infos | 50 | 175640
user_info | 3 | 46718
rollouts | 0 | 34078
favorites | 0 | 3059
schema_migrations | 0 | 2
authorizations | 0 | 0
delayed_jobs | 23 | 0
S.&r"*+"%
$ cat ~/.psqlrc
set ON_ERROR_ROLLBACK interactive
-- automatically switch between extended and normal
x auto
-- always show how long a query takes
timing
set show_slow_queries
'SELECT
(total_time / 1000 / 60) as total_minutes,
(total_time/calls) as average_time, query
FROM pg_stat_statements
ORDER BY 1 DESC
LIMIT 100;'
psql
$ cat ~/.psqlrc
set ON_ERROR_ROLLBACK interactive
-- automatically switch between extended and normal
x auto
-- always show how long a query takes
timing
set show_slow_queries
'SELECT
(total_time / 1000 / 60) as total_minutes,
(total_time/calls) as average_time, query
FROM pg_stat_statements
ORDER BY 1 DESC
LIMIT 100;'
psql
datascope
https://github.com/will/datascope
U)0$r%"()0,)#
Sp$*,1* Q+$r2
P$rf&r'()*$
Understanding Query Performance
Understanding Query Performance
SELECT last_name
FROM employees
WHERE salary >= 50000;
Explain
# EXPLAIN
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Seq Scan on employees (cost=0.00..35811.00 rows=1
width=6)
Filter: (salary >= 50000)
(3 rows)
Explain
# EXPLAIN
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Seq Scan on employees
width=6)
Filter: (salary >= 50000)
(3 rows)
startup time max time rows return
(cost=0.00..35811.00 rows=1
Explain Analyze
# EXPLAIN ANALYZE
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Seq Scan on employees (cost=0.00..35811.00 rows=1
width=6) (actual time=2.401..295.247 rows=1428
loops=1)
Filter: (salary >= 50000)
Total runtime: 295.379
(3 rows)
Filter: (salary >= 50000)
(3 rows)
startup time max time rows return
actual time
2.401..295.247 rows=1428
295.379
Rough guidelines
Page response times < 100 ms
Common queries < 10ms
Rare queries < 100ms
Explain Analyze
# EXPLAIN ANALYZE
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Seq Scan on employees (cost=0.00..35811.00 rows=1
width=6) (actual time=2.401..295.247 rows=1428
loops=1)
Filter: (salary >= 50000)
Total runtime: 295.379
(3 rows)
Filter: (salary >= 50000)
(3 rows)
startup time max time rows return
actual time
2.401..295.247 rows=1428
295.379
# CREATE INDEX idx_emps ON employees (salary);
Indexes!
Indexes!
EXPLAIN ANALYZE
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Index Scan using idx_emps on employees
(cost=0.00..8.49 rows=1 width=6) (actual time =
0.047..1.603 rows=1428 loops=1)
Index Cond: (salary >= 50000)
Total runtime: 1.771 ms
(3 rows)
Indexes!
EXPLAIN ANALYZE
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Index Scan using idx_emps on employees
(cost=0.00..8.49 rows=1 width=6) (actual time =
0.047..1.603 rows=1428 loops=1)
Index Cond: (salary >= 50000)
Total runtime: 1.771 ms
(3 rows)
Indexes!
EXPLAIN ANALYZE
SELECT last_name
FROM employees
WHERE salary >= 50000;
QUERY PLAN
--------------------------------------------------
Index Scan using idx_emps on employees
(cost=0.00..8.49 rows=1 width=6) (actual time =
0.047..1.603 rows=1428 loops=1)
Index Cond: (salary >= 50000)
Total runtime: 1.771 ms
(3 rows)
pg_stat_statements
pg_stat_statements
$ select * from pg_stat_statements where query ~ 'from users where email';
userid │ 16384
dbid │ 16388
query │ select * from users where email = ?;
calls │ 2
total_time │ 0.000268
rows │ 2
shared_blks_hit │ 16
shared_blks_read │ 0
shared_blks_dirtied │ 0
shared_blks_written │ 0
local_blks_hit │ 0
local_blks_read │ 0
local_blks_dirtied │ 0
local_blks_written │ 0
...
pg_stat_statements
SELECT
(total_time / 1000 / 60) as total,
(total_time/calls) as avg,
query
FROM pg_stat_statements
ORDER BY 1 DESC
LIMIT 100;
pg_stat_statements
pg_stat_statements
total | avg | query
--------+--------+-------------------------
295.76 | 10.13 | SELECT id FROM users...
219.13 | 80.24 | SELECT * FROM ...
(2 rows)
pg_stat_statements
I)03$%
Indexes
B-Tree
Generalized Inverted Index (GIN)
Generalized Search Tree (GIST)
K Nearest Neighbors (KNN)
Space Partitioned GIST (SP-GIST)
Indexes
Which do I use?
BTree
This is what you usually want
Generalized Inverted Index (GIN)
Use with multiple values in 1 column
Array/hStore
Generalized Search Tree (GIST)
Full text search
Shapes
Indexes
B-Tree
Generalized Inverted Index (GIN)
Generalized Search Tree (GIST)
K Nearest Neighbors (KNN)
Space Partitioned GIST (SP-GIST)
M&r$ ,)03$%
Indexes
Conditional
Functional
Concurrent creation
Conditional
> SELECT *
FROM places;
name | population
-----------------------------------
ACMAR | 6055
ARAB | 13650
Conditional
> SELECT *
FROM places
WHERE population > 10000;
name | population
-----------------------------------
ARAB | 13650
Conditional
> SELECT *
FROM places
WHERE population > 10000;
name | population
-----------------------------------
ARAB | 13650
Conditional
> CREATE INDEX idx_large_population ON
places(name) where population > 10000;
Conditional
> CREATE INDEX idx_large_population ON
places(name) where population > 10000;
Functional
> SELECT *
FROM places;
data
-----------------------------------
{"city": "ACMAR", "pop": 6055}
{"city": "ARAB", "pop": 13650}
> SELECT *
FROM places
WHERE get_numeric('pop', data) > 10000;
data
-----------------------------------
{"city": "ARAB", "pop": 13650}
Functional
> SELECT *
FROM places
WHERE get_numeric('pop', data) > 10000;
data
-----------------------------------
{"city": "ARAB", "pop": 13650}
Functional
> CREATE INDEX idx_large_population ON
places(get_numeric('pop', data));
Functional
> CREATE INDEX idx_large_population ON
places(get_numeric('pop', data));
Functional
ConditionalandFunctional
> CREATE INDEX idx_large_population ON
places(data) WHERE
get_numeric('pop', data) > 10000;
ConditionalandFunctional
> CREATE INDEX idx_large_population ON
places(data) WHERE
get_numeric('pop', data) > 10000;
ConditionalandFunctional
> CREATE INDEX idx_large_population ON
places(data) WHERE
get_numeric('pop', data) > 10000;
CREATE INDEX CONCURRENTLY ...
roughly 2-3x slower
Doesn’t lock table
One more thing
D4()#&
Connections
Connections
django-postgrespool
djorm-ext-pool
django-db-pool
django-postgrespool
import dj_database_url
import django_postgrespool
DATABASE = { 'default': dj_database_url.config() }
DATABASES['default']['ENGINE'] = 'django_postgrespool'
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2'
}
django-postgrespool
import dj_database_url
import django_postgrespool
DATABASE = { 'default': dj_database_url.config() }
DATABASES['default']['ENGINE'] = 'django_postgrespool'
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2'
}
pgbouncer
pgpool
Other options
A00,)# C(*.$
Replicationoptions
Replicationoptions
slony
bucardo
pgpool
Replicationoptions
slony
bucardo
pgpool
wal-e
barman
Replicationoptions
slony
bucardo
pgpool
wal-e
barman
Update settings
effective_cache_size
shared_buffers
work_mem
maintenance_work_mem
Update settings
B(*-+p%
Logical
pg_dump
can be human readable, is portable
Physical
The bytes on disk
Base backup
Logical
Good across architectures
Good for portability
Has load on DB
Works < 50 GB
Physical
More initial setup
Less portability
Limited load on system
Use above 50 GB
R$*(p
OLAP
Whole other talk
Disk IO is important
Order on disk is helpful (pg-reorg)
MPP solutions on top of Postgres
Recap
OLTP (webapps)
Ensure bulk of data is cache
Efficient use of indexes
When cache sucks, throw more at it
Recap
Q+$%",&)%

More Related Content

PDF
Advanced pg_stat_statements: Filtering, Regression Testing & more
PDF
Quick reference for Grafana
PDF
PostgreSQL: Advanced indexing
PDF
John Melesky - Federating Queries Using Postgres FDW @ Postgres Open
PDF
PostgreSQL Procedural Languages: Tips, Tricks and Gotchas
DOCX
Spark_Documentation_Template1
PDF
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
PDF
Deep dive to PostgreSQL Indexes
Advanced pg_stat_statements: Filtering, Regression Testing & more
Quick reference for Grafana
PostgreSQL: Advanced indexing
John Melesky - Federating Queries Using Postgres FDW @ Postgres Open
PostgreSQL Procedural Languages: Tips, Tricks and Gotchas
Spark_Documentation_Template1
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Deep dive to PostgreSQL Indexes

What's hot (20)

PPTX
Common Performance Pitfalls in Odoo apps
PDF
ClickHouse Features for Advanced Users, by Aleksei Milovidov
PDF
Tests unitaires pour PostgreSQL avec pgTap
PDF
Neatly folding-a-tree
PPT
Spring data iii
PDF
Py spark cheat sheet by cheatsheetmaker.com
PPTX
Best Practices in Handling Performance Issues
PDF
Transducers in JavaScript
PDF
Testing multi outputformat based mapreduce
PDF
How to teach an elephant to rock'n'roll
PPTX
2017 02-07 - elastic & spark. building a search geo locator
PPTX
Using Cerberus and PySpark to validate semi-structured datasets
PPT
Oracle 10g Performance: chapter 00 sampling
PDF
Full Text Search in PostgreSQL
PDF
PostgreSQL, performance for queries with grouping
PDF
Redis 101
PPTX
Powershell for Log Analysis and Data Crunching
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PDF
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
Common Performance Pitfalls in Odoo apps
ClickHouse Features for Advanced Users, by Aleksei Milovidov
Tests unitaires pour PostgreSQL avec pgTap
Neatly folding-a-tree
Spring data iii
Py spark cheat sheet by cheatsheetmaker.com
Best Practices in Handling Performance Issues
Transducers in JavaScript
Testing multi outputformat based mapreduce
How to teach an elephant to rock'n'roll
2017 02-07 - elastic & spark. building a search geo locator
Using Cerberus and PySpark to validate semi-structured datasets
Oracle 10g Performance: chapter 00 sampling
Full Text Search in PostgreSQL
PostgreSQL, performance for queries with grouping
Redis 101
Powershell for Log Analysis and Data Crunching
pg_proctab: Accessing System Stats in PostgreSQL
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
Psycopg2 - Connect to PostgreSQL using Python Script
Ad

Viewers also liked (20)

PDF
Moving from Django Apps to Services
PPT
Multi Tenancy With Python and Django
PDF
Developing Software As A Service App with Python & Django
PDF
5 Steps to PostgreSQL Performance
PPT
Django multi-tier
PDF
Gerenciamento de Backups PostgreSQL com pgbarman
PDF
Hacking PostgreSQL. Обзор архитектуры.
PDF
Performance improvements in PostgreSQL 9.5 and beyond
PDF
Pagination Done the Right Way
PDF
SQL: Query optimization in practice
PPTX
The Magic of Tuning in PostgreSQL
PDF
SQL Transactions - What they are good for and how they work
PPTX
Driven Development - Closing the Loop on Scrum
PDF
Package and distribute your Python code
PDF
Sofware Fora de Séria 2016 - Implementando realtime no frontend
PDF
PostgreSQL Disaster Recovery with Barman
PPTX
S.O.L.I.D. - Павел Кохан, Python Meetup 26.09.2014
PDF
Como DDD e Strategic Design estão nos ajudando a modernizar um Legado
PDF
ZCA: A component architecture for Python
PDF
Organise a Code Dojo!
Moving from Django Apps to Services
Multi Tenancy With Python and Django
Developing Software As A Service App with Python & Django
5 Steps to PostgreSQL Performance
Django multi-tier
Gerenciamento de Backups PostgreSQL com pgbarman
Hacking PostgreSQL. Обзор архитектуры.
Performance improvements in PostgreSQL 9.5 and beyond
Pagination Done the Right Way
SQL: Query optimization in practice
The Magic of Tuning in PostgreSQL
SQL Transactions - What they are good for and how they work
Driven Development - Closing the Loop on Scrum
Package and distribute your Python code
Sofware Fora de Séria 2016 - Implementando realtime no frontend
PostgreSQL Disaster Recovery with Barman
S.O.L.I.D. - Павел Кохан, Python Meetup 26.09.2014
Como DDD e Strategic Design estão nos ajudando a modernizar um Legado
ZCA: A component architecture for Python
Organise a Code Dojo!
Ad

Similar to Postgres performance for humans (20)

PDF
Postgres Performance for Humans
ODP
Basic Query Tuning Primer
ODP
Basic Query Tuning Primer - Pg West 2009
PDF
PerlApp2Postgresql (2)
PDF
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PDF
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
PDF
How To Control IO Usage using Resource Manager
PDF
Scylla Summit 2016: Analytics Show Time - Spark and Presto Powered by Scylla
PDF
Hadoop Integration in Cassandra
PDF
New Tuning Features in Oracle 11g - How to make your database as boring as po...
PDF
PostgreSQL 9.6 새 기능 소개
PDF
Solr @ Etsy - Apache Lucene Eurocon
PDF
ETL Patterns with Postgres
PPT
Do You Know The 11g Plan?
PPT
Oracle SQL Tuning
PDF
KSQL: Streaming SQL for Kafka
PDF
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
PDF
Materialized views in PostgreSQL
PDF
Spark DataFrames for Data Munging
PDF
OGSA-DAI DQP: A Developer's View
Postgres Performance for Humans
Basic Query Tuning Primer
Basic Query Tuning Primer - Pg West 2009
PerlApp2Postgresql (2)
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
How To Control IO Usage using Resource Manager
Scylla Summit 2016: Analytics Show Time - Spark and Presto Powered by Scylla
Hadoop Integration in Cassandra
New Tuning Features in Oracle 11g - How to make your database as boring as po...
PostgreSQL 9.6 새 기능 소개
Solr @ Etsy - Apache Lucene Eurocon
ETL Patterns with Postgres
Do You Know The 11g Plan?
Oracle SQL Tuning
KSQL: Streaming SQL for Kafka
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Materialized views in PostgreSQL
Spark DataFrames for Data Munging
OGSA-DAI DQP: A Developer's View

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf

Postgres performance for humans