SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Autovacuum, explained for engineers
PGConf.eu 2015, Vienna
Ilya Kosmodemiansky
ik@postgresql-consulting.com
Outline
• What is it and why is it so important?
• Aggressiveness of autovacuum
• What else important can autovacuum daemon do
• Autovacuum and replication
• How to remove bloat
Two most common problems we meet in our practice
• autovacuum = off
• Autovacuum settings are default
Two most common problems we meet in our practice
• autovacuum = off
• Autovacuum settings are default
• That means there is a lot we can do about improving
performance of this particular database
What is autovacuum?
Modern (classical) databases must deal with two
fundamental problems:
• Concurrent operations
For that they can transactions, ACID transactions
• Failures
For that they can recover to the last successful transaction
using WAL
What is autovacuum?
Technically that means
• There is a combination of locking and MVCC algorithms that
provides transactions support
• Undo and Redo information is stored somewhere to make
recovery possible
What is autovacuum?
In PostgreSQL
• Redo - in WAL
• Undo - directly in datafiles
• UPDATE = INSERT + DELETE
• DELETE is just marking tuple as invisible
xmin
tt=# INSERT into test(id) values(5);
INSERT 0 1
tt=# select *,xmin,xmax from test;
id | xmin | xmax
----+------+------
5 | 1266 | 0
(5 rows)
tt=# select txid_current();
txid_current
--------------
1267
(1 row)
xmax
tt=# begin;
BEGIN
tt=# UPDATE test set id=5 where id=4;
UPDATE 1
In another session:
tt=# select *,xmin,xmax from test;
id | xmin | xmax
----+------+------
4 | 1264 | 1270
(3 rows)
Some garbage collection is required
Tuples that are not visible to any running transaction should
be removed
• Otherwise fragmentation increases and you run into bloat aka
Big Data
• autovacuum workers do that, table by table
• Old-fashioned VACUUM is a bad choice
Beside that, autovacuum workers
• Collect statistics for the optimizer
• Perform wraparound for txid
Some garbage collection is required
Tuples that are not visible to any running transaction should
be removed
• Otherwise fragmentation increases and you run into bloat aka
Big Data
• autovacuum workers do that, table by table
• Old-fashioned VACUUM is a bad choice
Beside that, autovacuum workers
• Collect statistics for the optimizer
• Perform wraparound for txid
You do not want to turn autovacuum off!
This sort of work must be finally done
• If your autovacuum process runs for hours and interferes with
some DDL, to simply terminate it is not an option
• Especially for OLTP, autovacuum should be configured
aggressively enough: so it can work with small portions of
data quickly
autovacuum: aggressive enough
postgres=# select name, setting, context from pg_settings
where category ~ ’Autovacuum’;
name | setting | context
-------------------------------------+-----------+------------
autovacuum | on | sighup
autovacuum_analyze_scale_factor | 0.05 | sighup
autovacuum_analyze_threshold | 50 | sighup
autovacuum_freeze_max_age | 200000000 | postmaster
autovacuum_max_workers | 10 | postmaster
autovacuum_multixact_freeze_max_age | 400000000 | postmaster
autovacuum_naptime | 60 | sighup
autovacuum_vacuum_cost_delay | 20 | sighup
autovacuum_vacuum_cost_limit | -1 | sighup
autovacuum_vacuum_scale_factor | 0.01 | sighup
autovacuum_vacuum_threshold | 50 | sighup
(11 rows)
Scale factors
• autovacuum_vacuum_scale_factor = 0.01 means that at
least 1% of rows (% of table size) in the table should be
changed before autovacuum happens
• autovacuum_vacuum_threshold - alternative setting, exact
number of rows
• The idea is to make autovacuum work more frequently,
vacuuming tables in small portions
• Can be set per-table, but that can be some sort of pain
Magic spell
Autovacuum delays autovacuum_naptime seconds, then checks
tables if they need a vacuum. It runs vacuum on a table until
autovacuum_vacuum_cost_limit is reached, then sleeps
autovacuum_vacuum_cost_delay milliseconds.
Sometimes a good idea
in crontab:
* * * * * /usr/bin/pgrep -f ’postgres: autovacuum’ | xargs --no-run-if-empty -I $ renice -n 20 -p $ >/dev/null 2>/dev/null
* * * * * /usr/bin/pgrep -f ’postgres: autovacuum’ | xargs --no-run-if-empty -I $ ionice -c 3 -t -p $
in postgresql.conf:
autovacuum_max_workers → 10-20 and autovacuum_vacuum_cost_delay → 10
As a result
autovacuum_vacuum_cost_delay
• Is a global setting
• In some cases can be an issue (one tablespace on SSD,
another on SAS)
• On fast SSD, autovacuum_vacuum_cost_delay can be
shorter than on slower SAS
ERROR: canceling statement due to conflict with recov
• The tuple, cleaned up by autovacuum on master, is still in use
by some query on hot standby
• hot_standby_feedback = on - The safest way, in spite of
some bloat on master
Before you hurry to reconfigure your PostgreSQL
• autovacuum does not remove existing bloat
• dump/restore can be an option, but...
• http://reorg.github.io/pg_repack/
• https://github.com/PostgreSQL-Consulting/pgcompacttable
Questions?
ik@postgresql-consulting.com
slides will be available on
http://blog.postgresql-consulting.com

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
 
PostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication CheatsheetPostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication Cheatsheet
 
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
 
Parallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDBParallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDB
 
Optimizing Autovacuum: PostgreSQL's vacuum cleaner
Optimizing Autovacuum: PostgreSQL's vacuum cleanerOptimizing Autovacuum: PostgreSQL's vacuum cleaner
Optimizing Autovacuum: PostgreSQL's vacuum cleaner
 
PostgreSQL Replication Tutorial
PostgreSQL Replication TutorialPostgreSQL Replication Tutorial
PostgreSQL Replication Tutorial
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performance
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Postgresql
PostgresqlPostgresql
Postgresql
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Tuning Autovacuum in Postgresql
Tuning Autovacuum in PostgresqlTuning Autovacuum in Postgresql
Tuning Autovacuum in Postgresql
 
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
Ilya Kosmodemiansky - An ultimate guide to upgrading your PostgreSQL installa...
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
Let's turn your PostgreSQL into columnar store with cstore_fdw
Let's turn your PostgreSQL into columnar store with cstore_fdwLet's turn your PostgreSQL into columnar store with cstore_fdw
Let's turn your PostgreSQL into columnar store with cstore_fdw
 
EDB Postgres Replication Server
EDB Postgres Replication ServerEDB Postgres Replication Server
EDB Postgres Replication Server
 
Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA
 

Semelhante a Autovacuum, explained for engineers, new improved version PGConf.eu 2015 Vienna

Understand regression testing
Understand regression testingUnderstand regression testing
Understand regression testing
gaoliang641
 
Highload Perf Tuning
Highload Perf TuningHighload Perf Tuning
Highload Perf Tuning
HighLoad2009
 

Semelhante a Autovacuum, explained for engineers, new improved version PGConf.eu 2015 Vienna (20)

PostgreSQL Meetup Berlin at Zalando HQ
PostgreSQL Meetup Berlin at Zalando HQPostgreSQL Meetup Berlin at Zalando HQ
PostgreSQL Meetup Berlin at Zalando HQ
 
Strategic autovacuum
Strategic autovacuumStrategic autovacuum
Strategic autovacuum
 
Strategic Autovacuum
Strategic AutovacuumStrategic Autovacuum
Strategic Autovacuum
 
Performance tuning Grails applications
Performance tuning Grails applicationsPerformance tuning Grails applications
Performance tuning Grails applications
 
Overview of Postgres Utility Processes
Overview of Postgres Utility ProcessesOverview of Postgres Utility Processes
Overview of Postgres Utility Processes
 
Understanding Autovacuum
Understanding AutovacuumUnderstanding Autovacuum
Understanding Autovacuum
 
Analyze database system using a 3 d method
Analyze database system using a 3 d methodAnalyze database system using a 3 d method
Analyze database system using a 3 d method
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13
 
On The Building Of A PostgreSQL Cluster
On The Building Of A PostgreSQL ClusterOn The Building Of A PostgreSQL Cluster
On The Building Of A PostgreSQL Cluster
 
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
 
Performance tuning Grails applications
 Performance tuning Grails applications Performance tuning Grails applications
Performance tuning Grails applications
 
Backing Up and Recovery
Backing Up and RecoveryBacking Up and Recovery
Backing Up and Recovery
 
PGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 - Tale from TrenchesPGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 - Tale from Trenches
 
Automate Server Mastery by Stack Advisors - Automation Nation 2018
Automate Server Mastery by Stack Advisors - Automation Nation 2018Automate Server Mastery by Stack Advisors - Automation Nation 2018
Automate Server Mastery by Stack Advisors - Automation Nation 2018
 
Understand regression testing
Understand regression testingUnderstand regression testing
Understand regression testing
 
High-Performance Hibernate - JDK.io 2018
High-Performance Hibernate - JDK.io 2018High-Performance Hibernate - JDK.io 2018
High-Performance Hibernate - JDK.io 2018
 
Provisioning and Capacity Planning Workshop (Dogpatch Labs, September 2015)
Provisioning and Capacity Planning Workshop (Dogpatch Labs, September 2015)Provisioning and Capacity Planning Workshop (Dogpatch Labs, September 2015)
Provisioning and Capacity Planning Workshop (Dogpatch Labs, September 2015)
 
Highload Perf Tuning
Highload Perf TuningHighload Perf Tuning
Highload Perf Tuning
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threads
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it Fast
 

Mais de PostgreSQL-Consulting

Mais de PostgreSQL-Consulting (11)

Linux IO internals for database administrators (SCaLE 2017 and PGDay Nordic 2...
Linux IO internals for database administrators (SCaLE 2017 and PGDay Nordic 2...Linux IO internals for database administrators (SCaLE 2017 and PGDay Nordic 2...
Linux IO internals for database administrators (SCaLE 2017 and PGDay Nordic 2...
 
PostgreSQL worst practices, version PGConf.US 2017 by Ilya Kosmodemiansky
PostgreSQL worst practices, version PGConf.US 2017 by Ilya KosmodemianskyPostgreSQL worst practices, version PGConf.US 2017 by Ilya Kosmodemiansky
PostgreSQL worst practices, version PGConf.US 2017 by Ilya Kosmodemiansky
 
PostgreSQL worst practices, version FOSDEM PGDay 2017 by Ilya Kosmodemiansky
PostgreSQL worst practices, version FOSDEM PGDay 2017 by Ilya KosmodemianskyPostgreSQL worst practices, version FOSDEM PGDay 2017 by Ilya Kosmodemiansky
PostgreSQL worst practices, version FOSDEM PGDay 2017 by Ilya Kosmodemiansky
 
Linux internals for Database administrators at Linux Piter 2016
Linux internals for Database administrators at Linux Piter 2016Linux internals for Database administrators at Linux Piter 2016
Linux internals for Database administrators at Linux Piter 2016
 
10 things, an Oracle DBA should care about when moving to PostgreSQL
10 things, an Oracle DBA should care about when moving to PostgreSQL10 things, an Oracle DBA should care about when moving to PostgreSQL
10 things, an Oracle DBA should care about when moving to PostgreSQL
 
Pgconfru 2015 kosmodemiansky
Pgconfru 2015 kosmodemianskyPgconfru 2015 kosmodemiansky
Pgconfru 2015 kosmodemiansky
 
Как PostgreSQL работает с диском
Как PostgreSQL работает с дискомКак PostgreSQL работает с диском
Как PostgreSQL работает с диском
 
Kosmodemiansky wr 2013
Kosmodemiansky wr 2013Kosmodemiansky wr 2013
Kosmodemiansky wr 2013
 
Максим Богук. Postgres-XC
Максим Богук. Postgres-XCМаксим Богук. Postgres-XC
Максим Богук. Postgres-XC
 
Иван Фролков. Tricky SQL
Иван Фролков. Tricky SQLИван Фролков. Tricky SQL
Иван Фролков. Tricky SQL
 
Илья Космодемьянский. Использование очередей асинхронных сообщений с PostgreSQL
Илья Космодемьянский. Использование очередей асинхронных сообщений с PostgreSQLИлья Космодемьянский. Использование очередей асинхронных сообщений с PostgreSQL
Илья Космодемьянский. Использование очередей асинхронных сообщений с PostgreSQL
 

Último

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 

Autovacuum, explained for engineers, new improved version PGConf.eu 2015 Vienna

  • 1. Autovacuum, explained for engineers PGConf.eu 2015, Vienna Ilya Kosmodemiansky ik@postgresql-consulting.com
  • 2. Outline • What is it and why is it so important? • Aggressiveness of autovacuum • What else important can autovacuum daemon do • Autovacuum and replication • How to remove bloat
  • 3. Two most common problems we meet in our practice • autovacuum = off • Autovacuum settings are default
  • 4. Two most common problems we meet in our practice • autovacuum = off • Autovacuum settings are default • That means there is a lot we can do about improving performance of this particular database
  • 5. What is autovacuum? Modern (classical) databases must deal with two fundamental problems: • Concurrent operations For that they can transactions, ACID transactions • Failures For that they can recover to the last successful transaction using WAL
  • 6. What is autovacuum? Technically that means • There is a combination of locking and MVCC algorithms that provides transactions support • Undo and Redo information is stored somewhere to make recovery possible
  • 7. What is autovacuum? In PostgreSQL • Redo - in WAL • Undo - directly in datafiles • UPDATE = INSERT + DELETE • DELETE is just marking tuple as invisible
  • 8. xmin tt=# INSERT into test(id) values(5); INSERT 0 1 tt=# select *,xmin,xmax from test; id | xmin | xmax ----+------+------ 5 | 1266 | 0 (5 rows) tt=# select txid_current(); txid_current -------------- 1267 (1 row)
  • 9. xmax tt=# begin; BEGIN tt=# UPDATE test set id=5 where id=4; UPDATE 1 In another session: tt=# select *,xmin,xmax from test; id | xmin | xmax ----+------+------ 4 | 1264 | 1270 (3 rows)
  • 10. Some garbage collection is required Tuples that are not visible to any running transaction should be removed • Otherwise fragmentation increases and you run into bloat aka Big Data • autovacuum workers do that, table by table • Old-fashioned VACUUM is a bad choice Beside that, autovacuum workers • Collect statistics for the optimizer • Perform wraparound for txid
  • 11. Some garbage collection is required Tuples that are not visible to any running transaction should be removed • Otherwise fragmentation increases and you run into bloat aka Big Data • autovacuum workers do that, table by table • Old-fashioned VACUUM is a bad choice Beside that, autovacuum workers • Collect statistics for the optimizer • Perform wraparound for txid You do not want to turn autovacuum off!
  • 12. This sort of work must be finally done • If your autovacuum process runs for hours and interferes with some DDL, to simply terminate it is not an option • Especially for OLTP, autovacuum should be configured aggressively enough: so it can work with small portions of data quickly
  • 13. autovacuum: aggressive enough postgres=# select name, setting, context from pg_settings where category ~ ’Autovacuum’; name | setting | context -------------------------------------+-----------+------------ autovacuum | on | sighup autovacuum_analyze_scale_factor | 0.05 | sighup autovacuum_analyze_threshold | 50 | sighup autovacuum_freeze_max_age | 200000000 | postmaster autovacuum_max_workers | 10 | postmaster autovacuum_multixact_freeze_max_age | 400000000 | postmaster autovacuum_naptime | 60 | sighup autovacuum_vacuum_cost_delay | 20 | sighup autovacuum_vacuum_cost_limit | -1 | sighup autovacuum_vacuum_scale_factor | 0.01 | sighup autovacuum_vacuum_threshold | 50 | sighup (11 rows)
  • 14. Scale factors • autovacuum_vacuum_scale_factor = 0.01 means that at least 1% of rows (% of table size) in the table should be changed before autovacuum happens • autovacuum_vacuum_threshold - alternative setting, exact number of rows • The idea is to make autovacuum work more frequently, vacuuming tables in small portions • Can be set per-table, but that can be some sort of pain
  • 15. Magic spell Autovacuum delays autovacuum_naptime seconds, then checks tables if they need a vacuum. It runs vacuum on a table until autovacuum_vacuum_cost_limit is reached, then sleeps autovacuum_vacuum_cost_delay milliseconds.
  • 16. Sometimes a good idea in crontab: * * * * * /usr/bin/pgrep -f ’postgres: autovacuum’ | xargs --no-run-if-empty -I $ renice -n 20 -p $ >/dev/null 2>/dev/null * * * * * /usr/bin/pgrep -f ’postgres: autovacuum’ | xargs --no-run-if-empty -I $ ionice -c 3 -t -p $ in postgresql.conf: autovacuum_max_workers → 10-20 and autovacuum_vacuum_cost_delay → 10
  • 18. autovacuum_vacuum_cost_delay • Is a global setting • In some cases can be an issue (one tablespace on SSD, another on SAS) • On fast SSD, autovacuum_vacuum_cost_delay can be shorter than on slower SAS
  • 19. ERROR: canceling statement due to conflict with recov • The tuple, cleaned up by autovacuum on master, is still in use by some query on hot standby • hot_standby_feedback = on - The safest way, in spite of some bloat on master
  • 20. Before you hurry to reconfigure your PostgreSQL • autovacuum does not remove existing bloat • dump/restore can be an option, but... • http://reorg.github.io/pg_repack/ • https://github.com/PostgreSQL-Consulting/pgcompacttable
  • 21. Questions? ik@postgresql-consulting.com slides will be available on http://blog.postgresql-consulting.com