SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
VACUUM in
PostgreSQL
Zalando SE
Rafia Sabih
26-09-2018
1
2
Outline
● Why VACUUM
● Things around VACUUM
● VACUUM utility
● Inside VACUUM
● Proposals around VACUUM
2
Why VACUUM?
3
4
Update Tuples in PostgreSQL
T1 T2 T3
T4
T1
Old
T2
T3
T4
Update T2
New
T2
Page Page
Both versions of the
tuples exist in the page
4
5
Delete tuples in PostgreSQL
T1 T2 T3
T4
T1 T2 T3
T4
Delete T3
Page Page
Tuple is marked deleted but
remains in the page and its
space remains unused
5
6
Why VACUUM
● An UPDATE or DELETE of a row does not immediately removes the old version
○ VACUUM removes dead row versions in tables and indexes
○ Does not return the space to the operating system
■ except in the special case where one or more pages at the end of a table
become entirely free and an exclusive table lock can be easily obtained
○ VACUUM FULL minimizes the size of the table by writing a complete new
version of the table
● To overcome this, a new storage engine namely zheap based on undo logs is
proposed
6
7
Why VACUUM: Transaction wraparound
● The value of current txid reaches the maximum allowed value
○ The system remains blocked until old xacts are frozen
○ Frozen_txid
■ To avoid the problem of transaction wraparound
■ Reserved value is 2, meaning always visible
○ Freeze process rewrites the value of Xmin to frozen_txid for the tuples with
too old Xmin
○ The xids are checked against a cutoff -- vacuum_freeze_min_age
7
Around VACUUM!
8
Around VACUUM: CLOG and MultiXacts
● Commit log(CLOG) is a logical array for the status of the xacts
● Stored in shared memory and used for transaction lifecycle
● At every checkpoint or shutdown, flushed to disk in pg_xact directory
● Multixacts store information about row-level locks, updates etc. when multiple
transactions are involved
Committed Committed Aborted In-progress In-progress In-progress
1440 1441 1442 1443 1444 1445XIDs
status
9
Around VACUUM: VisibilityMap
● Introduced to help VACUUM process in identifying which pages of the relation
has dead tuples
● Provides information on if the tuples in the page are frozen
● Can be found in data directory with the suffix _vm
● Up-to date VMs speed-ups index-only scans
○ No need to visit corresponding heap pages
● Check extension pg_visibility for more information
10
Around VACUUM: FreeSpaceMap
● Tells about the free space available in the pages of the relation
○ Associated with every relation whether normal or index
● While inserting, it is required to check through FSM to find out which page has
enough space
● Pg_freespacemap extension can be used for better visualization
11
Around VACUUM: HeapOnlyTuple
● A precursor to the VACUUM utility
○ Performs the defragmentation of a page
● Reuse the space of the index entries of the deleted or obsolete tuples
● Only for the case when updated tuple does not change any indexed
columns
Col1 Col2 Col3 Col4
With HOT
A new tuple with unchanged indexed column
creates no new index entry
Without HOT
Every version of a row has its own index
entries
12
VACUUM utility
13
Tasks of VACUUM
● Prevent the issues of transaction or multi-xact id wraparound
■ Freeze xids when required
● To reclaim the space of dead tuples and indexes
■ Remove clog entries that are not required
■ Defragment pages with live tuples
● Update statistics of tables, it helps in better query plans
● Update visibility and freespace maps
● Remove CLOGs and multixacts
14
What can be vacuumed?
● Tuples or indexes that are not visible to any running transactions
○ Avoid long-running transactions
● Streaming replication with hot_standby_feedback enabled might also limit the
vacuum on the master
● On an internal level vacuum requires special type of lock on the buffer --
BufferLockForCleanup which locks the buffer and checks that no other process
has pinned that buffer
○ Long running select queries can also be blocking
15
VACUUM options
● Standard vacuum
● VACUUM FULL
○ Rewrites the table and indexes
○ Moderately-frequent standard VACUUM is better than infrequent VACUUM
FULL runs for maintaining heavily-updated tables
○ Alternative: pg_repack
● VACUUM FREEZE
○ Aggressively freezes tuples
● Auto-vacuum
○ To run vacuum and analyze commands in the background
● Vacuumdb
○ For multiple databases in a cluster
16
AUTOVACUUM
● Comprises of a launcher and workers
● Priority for each database
○ XID freeze
○ MXID freeze
○ Least recently auto-vacuumed
○ These priorities are not within database
● Multiple workers can work at the same time on a database
● Auto-vacuum can get terminated if other process needs access to tables
○ Workers tries to get lock, if not available skip the table for now
● Auto-vacuum running for xid-wraparound are more aggressive and can not be
canceled
● Temporary tables are ignored 17
Tuning VACUUM
● For autovacuum
○ Autovacuum_max_workers
○ Autovacuum_naptime
○ Autovacuum_vacuum_threshold
○ Autovacuum_vacuum_scale_factor
■ # dead_tup > (reltuples * scale_factor) + threshold
○ Autovacuum_work_mem
■ Default value -1 means take the value of maintenance_work_mem
● Vacuum_cost_delay
18
Take-away
● VACUUM
○ Removes dead tuples and indexes
○ Prevents the problem of transaction wraparound
○ No memory is returned to OS!
○ Autovacuum is your friend
● Obstacles for VACUUM
○ Long running transactions
○ Low autovacuum_work_mem
● Bloated indexes are more difficult than bloated relations
○ Reindex!
● pg_repack!
19
VACUUM internals
20
At a glance
● For each target relation
○ Acquire ShareUpdateExclusiveLock
○ Scan all pages to get all dead tuples, and freeze old tuples if necessary
■ The list of dead tuples and the index tuples pointing to dead tuples is
limited by maintenance_work_mem
■ If the scan is not complete and amount of memory is consumed, it
goes ahead to perform the next work for the listed tuples and
continues the scan later
21
At a glance
○ Remove the index tuples that point to the respective dead tuples if exists
○ For each page of the table
■ Remove the dead tuples and reallocate the live tuples in the page
■ Update FSM and VM
22
At a glance
● Truncate the last page if possible
○ Update both the statistics and system catalogs of the target table
○ Release ShareUpdateExclusiveLock
● Remove both unnecessary files and pages of the clog if possible
23
Important routines
● Vacuum
○ Entry point for vacuum and analyze commands
○ Pgstat_vacuum_stat : gives information about the dead objects - db and
relations
● Vacuum_rel
○ Checks ownership of the table underway, skips if not allowed
○ Checks if the relation is vacuum-able - ! matview, !partition_table,
!temp_table of other backends, etc.
○ Vacuums toast tables if present
○ Calls cluster (for vacuum full) or heap_vacuum_rel as required
24
Important routines
● Heap_vacuum_rel
○ Scans the heap and indexes
○ Calls lazy_truncate_heap as required
■ Deletes the last pages of the relation if empty
○ Freeze xid or mxid if required
● Lazy_scan_heap
○ Skip all-visible pages
■ But only when the number of pages to be skipped passes
SKIP_PAGES_THRESHOLD
■ Skipping a page also means not updating relfrozenxid!
■ In aggressive mode (freeze), skip only if all-frozen
25
Important routines
● Lazy_vacuum_indexes
○ Deletes index entries corresponding to dead tuples
● Lazy_cleanup_index
○ Calls index specific cleanup method
○ Update the index stats
● Lazy_vacuum_heap
○ Deletes the tuples only after corresponding index entries are deleted
○ Actually removes dead tuples and compacts the space
26
Important routines
● Vac_update_datfrozenxid
○ Updates datfrozenxid and datminmxid for the database
○ If one of them got updated, truncate pg_xact and/or pg_multixact
○ Vac_truncate_clog
■ Truncates the clog files and pages
● Analyze_rel
○ Collect the statistics of updated tables
○ Update the pg_statistics, pg_class
○ Collect all unique columns and indexes to be analysed
○ Lower bound for sample size is 100 rows
27
More to come...
● Zheap
○ In-place updates
○ Older versions of tuples are kept in a separate entity - undo logs
● Block level parallel vacuum (and autovacuum)
○ Parallelize vacuum of a table among workers
● Resume vacuum or autovacuum after interruption
○ Save the blocks that were vacuumed previously and resume from there
● Reduce duplicates in BTree indexes
○ For each key that have duplicates, a list is created which contains the
info about the duplicate tuples
○ Improves index bloat
28

Weitere ähnliche Inhalte

Was ist angesagt?

Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationAlexey Lesovsky
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Alexey Lesovsky
 
Solving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsSolving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsAlexander Korotkov
 
PostgreSQL Replication High Availability Methods
PostgreSQL Replication High Availability MethodsPostgreSQL Replication High Availability Methods
PostgreSQL Replication High Availability MethodsMydbops
 
In-memory OLTP storage with persistence and transaction support
In-memory OLTP storage with persistence and transaction supportIn-memory OLTP storage with persistence and transaction support
In-memory OLTP storage with persistence and transaction supportAlexander Korotkov
 
Introduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparoundIntroduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparoundMasahiko Sawada
 
PostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication CheatsheetPostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication CheatsheetAlexey Lesovsky
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PGConf APAC
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performancePostgreSQL-Consulting
 
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 EDB
 
Postgresql Database Administration Basic - Day1
Postgresql  Database Administration Basic  - Day1Postgresql  Database Administration Basic  - Day1
Postgresql Database Administration Basic - Day1PoguttuezhiniVP
 
Delta: Building Merge on Read
Delta: Building Merge on ReadDelta: Building Merge on Read
Delta: Building Merge on ReadDatabricks
 
PostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldPostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldJignesh Shah
 
Maria db 이중화구성_고민하기
Maria db 이중화구성_고민하기Maria db 이중화구성_고민하기
Maria db 이중화구성_고민하기NeoClova
 
High Availability and Disaster Recovery in PostgreSQL - EQUNIX
High Availability and Disaster Recovery in PostgreSQL - EQUNIXHigh Availability and Disaster Recovery in PostgreSQL - EQUNIX
High Availability and Disaster Recovery in PostgreSQL - EQUNIXJulyanto SUTANDANG
 
MariaDB Galera Cluster
MariaDB Galera ClusterMariaDB Galera Cluster
MariaDB Galera ClusterAbdul Manaf
 

Was ist angesagt? (20)

PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming Replication
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 
Solving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsSolving PostgreSQL wicked problems
Solving PostgreSQL wicked problems
 
PostgreSQL Replication High Availability Methods
PostgreSQL Replication High Availability MethodsPostgreSQL Replication High Availability Methods
PostgreSQL Replication High Availability Methods
 
In-memory OLTP storage with persistence and transaction support
In-memory OLTP storage with persistence and transaction supportIn-memory OLTP storage with persistence and transaction support
In-memory OLTP storage with persistence and transaction support
 
Introduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparoundIntroduction VAUUM, Freezing, XID wraparound
Introduction VAUUM, Freezing, XID wraparound
 
MySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB StatusMySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB Status
 
PostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication CheatsheetPostgreSQL Streaming Replication Cheatsheet
PostgreSQL Streaming Replication Cheatsheet
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
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
 
Postgresql Database Administration Basic - Day1
Postgresql  Database Administration Basic  - Day1Postgresql  Database Administration Basic  - Day1
Postgresql Database Administration Basic - Day1
 
MyRocks Deep Dive
MyRocks Deep DiveMyRocks Deep Dive
MyRocks Deep Dive
 
Delta: Building Merge on Read
Delta: Building Merge on ReadDelta: Building Merge on Read
Delta: Building Merge on Read
 
PostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldPostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized World
 
Maria db 이중화구성_고민하기
Maria db 이중화구성_고민하기Maria db 이중화구성_고민하기
Maria db 이중화구성_고민하기
 
Deep Dive on Amazon Aurora
Deep Dive on Amazon AuroraDeep Dive on Amazon Aurora
Deep Dive on Amazon Aurora
 
High Availability and Disaster Recovery in PostgreSQL - EQUNIX
High Availability and Disaster Recovery in PostgreSQL - EQUNIXHigh Availability and Disaster Recovery in PostgreSQL - EQUNIX
High Availability and Disaster Recovery in PostgreSQL - EQUNIX
 
MariaDB Galera Cluster
MariaDB Galera ClusterMariaDB Galera Cluster
MariaDB Galera Cluster
 

Ähnlich wie Vacuum in PostgreSQL

The Future of zHeap
The Future of zHeapThe Future of zHeap
The Future of zHeapEDB
 
OpenTSDB: HBaseCon2017
OpenTSDB: HBaseCon2017OpenTSDB: HBaseCon2017
OpenTSDB: HBaseCon2017HBaseCon
 
Learn how zheap works
 Learn how zheap works Learn how zheap works
Learn how zheap worksEDB
 
Learn how zheap works
 Learn how zheap works Learn how zheap works
Learn how zheap worksEDB
 
NewSQL - The Future of Databases?
NewSQL - The Future of Databases?NewSQL - The Future of Databases?
NewSQL - The Future of Databases?Elvis Saravia
 
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...Equnix Business Solutions
 
Cassandra 2.1 boot camp, Compaction
Cassandra 2.1 boot camp, CompactionCassandra 2.1 boot camp, Compaction
Cassandra 2.1 boot camp, CompactionJoshua McKenzie
 
Mongo nyc nyt + mongodb
Mongo nyc nyt + mongodbMongo nyc nyt + mongodb
Mongo nyc nyt + mongodbDeep Kapadia
 
Monitoring Cassandra With An EYE
Monitoring Cassandra With An EYEMonitoring Cassandra With An EYE
Monitoring Cassandra With An EYEKnoldus Inc.
 
M|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change MethodsM|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change MethodsMariaDB plc
 
How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018
How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018
How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018javier ramirez
 
NoSQL Database Migration Masterclass - Session 3: Migration Logistics
NoSQL Database Migration Masterclass - Session 3: Migration LogisticsNoSQL Database Migration Masterclass - Session 3: Migration Logistics
NoSQL Database Migration Masterclass - Session 3: Migration LogisticsScyllaDB
 
High-availability with Galera Cluster for MySQL
High-availability with Galera Cluster for MySQLHigh-availability with Galera Cluster for MySQL
High-availability with Galera Cluster for MySQLFromDual GmbH
 
Pg chameleon, mysql to postgresql replica made easy
Pg chameleon, mysql to postgresql replica made easyPg chameleon, mysql to postgresql replica made easy
Pg chameleon, mysql to postgresql replica made easyFederico Campoli
 
Troubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer PerspectiveTroubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer PerspectiveMarcelo Altmann
 
Batch Processing at Scale with Flink & Iceberg
Batch Processing at Scale with Flink & IcebergBatch Processing at Scale with Flink & Iceberg
Batch Processing at Scale with Flink & IcebergFlink Forward
 
MySQL always-up with Galera Cluster
MySQL always-up with Galera ClusterMySQL always-up with Galera Cluster
MySQL always-up with Galera ClusterFromDual GmbH
 
Sql architecture
Sql architectureSql architecture
Sql architecturerchakra
 
An Introduction to Apache Cassandra
An Introduction to Apache CassandraAn Introduction to Apache Cassandra
An Introduction to Apache CassandraSaeid Zebardast
 

Ähnlich wie Vacuum in PostgreSQL (20)

The Future of zHeap
The Future of zHeapThe Future of zHeap
The Future of zHeap
 
OpenTSDB: HBaseCon2017
OpenTSDB: HBaseCon2017OpenTSDB: HBaseCon2017
OpenTSDB: HBaseCon2017
 
MySQL and MariaDB Backups
MySQL and MariaDB BackupsMySQL and MariaDB Backups
MySQL and MariaDB Backups
 
Learn how zheap works
 Learn how zheap works Learn how zheap works
Learn how zheap works
 
Learn how zheap works
 Learn how zheap works Learn how zheap works
Learn how zheap works
 
NewSQL - The Future of Databases?
NewSQL - The Future of Databases?NewSQL - The Future of Databases?
NewSQL - The Future of Databases?
 
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
PGConf.ASIA 2019 Bali - PostgreSQL Database Migration and Maintenance - Koich...
 
Cassandra 2.1 boot camp, Compaction
Cassandra 2.1 boot camp, CompactionCassandra 2.1 boot camp, Compaction
Cassandra 2.1 boot camp, Compaction
 
Mongo nyc nyt + mongodb
Mongo nyc nyt + mongodbMongo nyc nyt + mongodb
Mongo nyc nyt + mongodb
 
Monitoring Cassandra With An EYE
Monitoring Cassandra With An EYEMonitoring Cassandra With An EYE
Monitoring Cassandra With An EYE
 
M|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change MethodsM|18 Battle of the Online Schema Change Methods
M|18 Battle of the Online Schema Change Methods
 
How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018
How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018
How a BEAM runner executes a pipeline. Apache BEAM Summit London 2018
 
NoSQL Database Migration Masterclass - Session 3: Migration Logistics
NoSQL Database Migration Masterclass - Session 3: Migration LogisticsNoSQL Database Migration Masterclass - Session 3: Migration Logistics
NoSQL Database Migration Masterclass - Session 3: Migration Logistics
 
High-availability with Galera Cluster for MySQL
High-availability with Galera Cluster for MySQLHigh-availability with Galera Cluster for MySQL
High-availability with Galera Cluster for MySQL
 
Pg chameleon, mysql to postgresql replica made easy
Pg chameleon, mysql to postgresql replica made easyPg chameleon, mysql to postgresql replica made easy
Pg chameleon, mysql to postgresql replica made easy
 
Troubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer PerspectiveTroubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer Perspective
 
Batch Processing at Scale with Flink & Iceberg
Batch Processing at Scale with Flink & IcebergBatch Processing at Scale with Flink & Iceberg
Batch Processing at Scale with Flink & Iceberg
 
MySQL always-up with Galera Cluster
MySQL always-up with Galera ClusterMySQL always-up with Galera Cluster
MySQL always-up with Galera Cluster
 
Sql architecture
Sql architectureSql architecture
Sql architecture
 
An Introduction to Apache Cassandra
An Introduction to Apache CassandraAn Introduction to Apache Cassandra
An Introduction to Apache Cassandra
 

Kürzlich hochgeladen

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...Amil Baba Dawood bangali
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptxNikhil Raut
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 

Kürzlich hochgeladen (20)

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptx
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 

Vacuum in PostgreSQL

  • 2. 2 Outline ● Why VACUUM ● Things around VACUUM ● VACUUM utility ● Inside VACUUM ● Proposals around VACUUM 2
  • 4. 4 Update Tuples in PostgreSQL T1 T2 T3 T4 T1 Old T2 T3 T4 Update T2 New T2 Page Page Both versions of the tuples exist in the page 4
  • 5. 5 Delete tuples in PostgreSQL T1 T2 T3 T4 T1 T2 T3 T4 Delete T3 Page Page Tuple is marked deleted but remains in the page and its space remains unused 5
  • 6. 6 Why VACUUM ● An UPDATE or DELETE of a row does not immediately removes the old version ○ VACUUM removes dead row versions in tables and indexes ○ Does not return the space to the operating system ■ except in the special case where one or more pages at the end of a table become entirely free and an exclusive table lock can be easily obtained ○ VACUUM FULL minimizes the size of the table by writing a complete new version of the table ● To overcome this, a new storage engine namely zheap based on undo logs is proposed 6
  • 7. 7 Why VACUUM: Transaction wraparound ● The value of current txid reaches the maximum allowed value ○ The system remains blocked until old xacts are frozen ○ Frozen_txid ■ To avoid the problem of transaction wraparound ■ Reserved value is 2, meaning always visible ○ Freeze process rewrites the value of Xmin to frozen_txid for the tuples with too old Xmin ○ The xids are checked against a cutoff -- vacuum_freeze_min_age 7
  • 9. Around VACUUM: CLOG and MultiXacts ● Commit log(CLOG) is a logical array for the status of the xacts ● Stored in shared memory and used for transaction lifecycle ● At every checkpoint or shutdown, flushed to disk in pg_xact directory ● Multixacts store information about row-level locks, updates etc. when multiple transactions are involved Committed Committed Aborted In-progress In-progress In-progress 1440 1441 1442 1443 1444 1445XIDs status 9
  • 10. Around VACUUM: VisibilityMap ● Introduced to help VACUUM process in identifying which pages of the relation has dead tuples ● Provides information on if the tuples in the page are frozen ● Can be found in data directory with the suffix _vm ● Up-to date VMs speed-ups index-only scans ○ No need to visit corresponding heap pages ● Check extension pg_visibility for more information 10
  • 11. Around VACUUM: FreeSpaceMap ● Tells about the free space available in the pages of the relation ○ Associated with every relation whether normal or index ● While inserting, it is required to check through FSM to find out which page has enough space ● Pg_freespacemap extension can be used for better visualization 11
  • 12. Around VACUUM: HeapOnlyTuple ● A precursor to the VACUUM utility ○ Performs the defragmentation of a page ● Reuse the space of the index entries of the deleted or obsolete tuples ● Only for the case when updated tuple does not change any indexed columns Col1 Col2 Col3 Col4 With HOT A new tuple with unchanged indexed column creates no new index entry Without HOT Every version of a row has its own index entries 12
  • 14. Tasks of VACUUM ● Prevent the issues of transaction or multi-xact id wraparound ■ Freeze xids when required ● To reclaim the space of dead tuples and indexes ■ Remove clog entries that are not required ■ Defragment pages with live tuples ● Update statistics of tables, it helps in better query plans ● Update visibility and freespace maps ● Remove CLOGs and multixacts 14
  • 15. What can be vacuumed? ● Tuples or indexes that are not visible to any running transactions ○ Avoid long-running transactions ● Streaming replication with hot_standby_feedback enabled might also limit the vacuum on the master ● On an internal level vacuum requires special type of lock on the buffer -- BufferLockForCleanup which locks the buffer and checks that no other process has pinned that buffer ○ Long running select queries can also be blocking 15
  • 16. VACUUM options ● Standard vacuum ● VACUUM FULL ○ Rewrites the table and indexes ○ Moderately-frequent standard VACUUM is better than infrequent VACUUM FULL runs for maintaining heavily-updated tables ○ Alternative: pg_repack ● VACUUM FREEZE ○ Aggressively freezes tuples ● Auto-vacuum ○ To run vacuum and analyze commands in the background ● Vacuumdb ○ For multiple databases in a cluster 16
  • 17. AUTOVACUUM ● Comprises of a launcher and workers ● Priority for each database ○ XID freeze ○ MXID freeze ○ Least recently auto-vacuumed ○ These priorities are not within database ● Multiple workers can work at the same time on a database ● Auto-vacuum can get terminated if other process needs access to tables ○ Workers tries to get lock, if not available skip the table for now ● Auto-vacuum running for xid-wraparound are more aggressive and can not be canceled ● Temporary tables are ignored 17
  • 18. Tuning VACUUM ● For autovacuum ○ Autovacuum_max_workers ○ Autovacuum_naptime ○ Autovacuum_vacuum_threshold ○ Autovacuum_vacuum_scale_factor ■ # dead_tup > (reltuples * scale_factor) + threshold ○ Autovacuum_work_mem ■ Default value -1 means take the value of maintenance_work_mem ● Vacuum_cost_delay 18
  • 19. Take-away ● VACUUM ○ Removes dead tuples and indexes ○ Prevents the problem of transaction wraparound ○ No memory is returned to OS! ○ Autovacuum is your friend ● Obstacles for VACUUM ○ Long running transactions ○ Low autovacuum_work_mem ● Bloated indexes are more difficult than bloated relations ○ Reindex! ● pg_repack! 19
  • 21. At a glance ● For each target relation ○ Acquire ShareUpdateExclusiveLock ○ Scan all pages to get all dead tuples, and freeze old tuples if necessary ■ The list of dead tuples and the index tuples pointing to dead tuples is limited by maintenance_work_mem ■ If the scan is not complete and amount of memory is consumed, it goes ahead to perform the next work for the listed tuples and continues the scan later 21
  • 22. At a glance ○ Remove the index tuples that point to the respective dead tuples if exists ○ For each page of the table ■ Remove the dead tuples and reallocate the live tuples in the page ■ Update FSM and VM 22
  • 23. At a glance ● Truncate the last page if possible ○ Update both the statistics and system catalogs of the target table ○ Release ShareUpdateExclusiveLock ● Remove both unnecessary files and pages of the clog if possible 23
  • 24. Important routines ● Vacuum ○ Entry point for vacuum and analyze commands ○ Pgstat_vacuum_stat : gives information about the dead objects - db and relations ● Vacuum_rel ○ Checks ownership of the table underway, skips if not allowed ○ Checks if the relation is vacuum-able - ! matview, !partition_table, !temp_table of other backends, etc. ○ Vacuums toast tables if present ○ Calls cluster (for vacuum full) or heap_vacuum_rel as required 24
  • 25. Important routines ● Heap_vacuum_rel ○ Scans the heap and indexes ○ Calls lazy_truncate_heap as required ■ Deletes the last pages of the relation if empty ○ Freeze xid or mxid if required ● Lazy_scan_heap ○ Skip all-visible pages ■ But only when the number of pages to be skipped passes SKIP_PAGES_THRESHOLD ■ Skipping a page also means not updating relfrozenxid! ■ In aggressive mode (freeze), skip only if all-frozen 25
  • 26. Important routines ● Lazy_vacuum_indexes ○ Deletes index entries corresponding to dead tuples ● Lazy_cleanup_index ○ Calls index specific cleanup method ○ Update the index stats ● Lazy_vacuum_heap ○ Deletes the tuples only after corresponding index entries are deleted ○ Actually removes dead tuples and compacts the space 26
  • 27. Important routines ● Vac_update_datfrozenxid ○ Updates datfrozenxid and datminmxid for the database ○ If one of them got updated, truncate pg_xact and/or pg_multixact ○ Vac_truncate_clog ■ Truncates the clog files and pages ● Analyze_rel ○ Collect the statistics of updated tables ○ Update the pg_statistics, pg_class ○ Collect all unique columns and indexes to be analysed ○ Lower bound for sample size is 100 rows 27
  • 28. More to come... ● Zheap ○ In-place updates ○ Older versions of tuples are kept in a separate entity - undo logs ● Block level parallel vacuum (and autovacuum) ○ Parallelize vacuum of a table among workers ● Resume vacuum or autovacuum after interruption ○ Save the blocks that were vacuumed previously and resume from there ● Reduce duplicates in BTree indexes ○ For each key that have duplicates, a list is created which contains the info about the duplicate tuples ○ Improves index bloat 28