Upgrade to Pro — share decks privately, control downloads, hide ads and more …

bloat-postgresql-scale

 bloat-postgresql-scale

https://www.socallinuxexpo.org/scale/20x/presentations/bloat-postgresql-taxonomy

Video of talk: https://www.youtube.com/watch?v=JDG4bMHxCH8

PostgreSQL's approach to transaction management uses MVCC (multi-version concurrency control). Postgres often maintains multiple physical versions of a single logical row. This is used to reconstruct the logical contents of tables at a specific point in time for SQL queries (zero or one row versions should be visible for each logical row). MVCC avoids having readers block writers and writers block readers, a frequent problem with database systems that use traditional 2PL. However, there is a cost to this approach: bloat must eventually be removed and reclaimed, typically by an autovacuum worker process.

Most Postgres DBAs are familiar with bloat, and almost as many will have some experience with tuning autovacuum to better manage it. There have been quite a few talks about the practical aspects of optimizing autovacuum and avoiding bloat; this talk isn't one of them. Instead, the goal of the talk is to show how bloat can accumulate, what that looks like at the page level and at the level of entire tables and indexes, and how that may impact production queries.

The talk covers:

How VACUUM processes each structure, and in what order.

How the HOT optimization works.

How Postgres manages free space.

The design of VACUUM. What space/bloat management tasks are prioritized by VACUUM, and why this makes sense.

Peter Geoghegan

March 10, 2023
Tweet

More Decks by Peter Geoghegan

Other Decks in Programming

Transcript

  1. https://speakerdeck.com/peterg/bloat-postgresql-scale Outline Most discussions of VACUUM/bloat take a practical approach

    - Starting point is VACUUM itself, and the impact to the user application - Can recommend “Managing Your Tuple Graveyard” talk from Chelsea Dole, which is on at 3:30 today in Ballroom A I’m going to take a bottom-up approach instead - Starting point is bloat itself, and effects that tend to naturally emerge from the user application - Might help you to develop a mental model that holds together existing knowledge of how these things work - Theoretical focus, but grounded in practicalities 3
  2. https://speakerdeck.com/peterg/bloat-postgresql-scale Overview 1. Structure Logical vs. Physical structures, TIDs as

    “physiological” identi fi ers 2. A bottom-up take on bloat Page level view of bloat, VACUUM, and opportunistic cleanup 3. VACUUM’s priorities Space reclamation, query response time 4. Conclusions Summary of the central ideas from 1 - 3 4
  3. https://speakerdeck.com/peterg/bloat-postgresql-scale Logical vs. Physical Database systems like Postgres use access

    methods to abstract away physical representation. MVCC more or less versions entries in objects (relations). - “Readers don’t block writers, writers don’t block readers” - Baked into everything, necessitates cleaning up old versions Postgres heap relations (tables) generally store newly inserted tuples in whatever order is convenient. Index relations often have multiple versions, too. 6
  4. https://speakerdeck.com/peterg/bloat-postgresql-scale Heap (table) structure Heap structure is optimized for sequential

    access, and access by index scans, which lookup entries using tuple identi fi ers (TIDs). Tuples are identi fi ed by TID (e.g., '(2,32)', '(43,89)') , which must be stable so that index scans won’t break. TID is a “physiological” identi fi er. - Physical across pages/blocks — block number. - Logical within pages/blocks — item identi fi er. “Hybrid” of logical and physical that retains many of the advantages of strictly logical and strictly physical identi fi ers 7
  5. https://speakerdeck.com/peterg/bloat-postgresql-scale Index structure Indexes make access to speci fi c

    records ef fi cient through key- based searching by SQL queries. B-Tree indexes have strict rules about which key values go on which “logical node” - Unlike the heap, where there are no “built in” rules governing where newly inserted heap tuples can be placed - “Strictly logical” B-Tree indexes do not have rules about the physical location of any given key value - A page split can change the physical location of some of the entries for a given logical node 8
  6. https://speakerdeck.com/peterg/bloat-postgresql-scale Index structure (cont. 1) Clearly the only kind of

    index lookup that can ever work reliably is a key search — using the whole key (or at least a pre fi x column) Going the other way (from heap entry to index entry) is harder - Pruning of dead heap tuples in heap pages destroys the information required to look up corresponding dead entries in indexes (by freeing the tuples that contain the indexed key) - VACUUM can only clean up indexes in bulk through a linear scan of each and every index, which matches on TID only - No “retail deletion” of individual entries in indexes takes place (not obvious how VACUUM could ever do this) 9
  7. https://speakerdeck.com/peterg/bloat-postgresql-scale Index structure (cont. 2) These dependencies have important consequences

    for VACUUM They make VACUUM an inherently bulk operation, that must work at the level of the whole table and its indexes collectively Postgres uses opportunistic cleanup techniques to make up for this - These work at the level of individual pages, incrementally and on-demand, during query execution - Complements VACUUM — makes up for its weaknesses, and vice-versa 10
  8. https://speakerdeck.com/peterg/bloat-postgresql-scale Overview 1. Structure Logical vs. Physical structures, TIDs as

    “physiological” identi fi ers 2. A bottom-up take on bloat Page level view of bloat, VACUUM, and opportunistic cleanup 3. VACUUM’s priorities Space reclamation, query response time 4. Conclusions Summary of the central ideas from 1 - 3 11
  9. https://speakerdeck.com/peterg/bloat-postgresql-scale Database pages as “cells” PostgreSQL storage consists of 8KiB

    pages “Page model” - Individual page modi fi cations can be made atomic with low-level techniques - High level atomic operations (transactions) can be composed from simpler atomic operations (WAL-logged atomic page operations) 13
  10. https://speakerdeck.com/peterg/bloat-postgresql-scale “Linux is evolution, not intelligent design” “Bad programmers worry

    about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds
  11. https://speakerdeck.com/peterg/bloat-postgresql-scale Evolutionary pressure Like cells in living organisms, the structure

    of pages shares a lot across disparate access methods (e.g., heap, B-Tree, …) The high level structures are very dissimilar, but the structure of individual pages is nevertheless much more similar than different Too much complexity to manage without breaking down into manageable pieces with commonality - What else could possibly work? 15
  12. https://speakerdeck.com/peterg/bloat-postgresql-scale Bloat at the page level Bloat at the level

    of individual pages looks similar across index and heap pages - Opportunistic cleanup techniques are fairly similar across heap and index pages, despite the differences that exist at the highest level (the level of whole tables) This is not the view that “drives” VACUUM, though - VACUUM is an operation that works at the level of a whole table (including its indexes) — so the “high level view” is more relevant 18
  13. https://speakerdeck.com/peterg/bloat-postgresql-scale VACUUM “Top-down” structure - Autovacuum (which is how VACUUM

    is typically run) is typically triggered by table-level threshold having been exceeded. - VACUUM is good at “putting a fl oor under” the problem of bloat at the table/system level by making sure that every table gets a “clean sweep” at some point - But VACUUM has no direct understanding of how bloat can become concentrated in individual pages, impacting queries disproportionately VACUUM generally processes each page once (sometimes twice), based on fi xed rules - VACUUM from recent Postgres versions can bypass index vacuuming when it turns out that there are only very few entries to delete from indexes - But VACUUM cannot reorder work at runtime, nor can it add extra work at runtime 20
  14. https://speakerdeck.com/peterg/bloat-postgresql-scale VACUUM - processing order 1. Visits heap, performing pruning,

    collecting dead TID references needed by step 2. Mechanically similar to opportunistic pruning, but directed at all pages that might need to be pruned, including pages that have very little bloat. Pruning item identi fi ers as dead/LP_DEAD, since index scans still need these until after step 2 (as “tombstones”). These are the TIDs to be removed in indexes later. 2. Visits indexes, deleting index tuples that match TID list collected in step 1. 3. Second pass over heap, to mark dead/LP_DEAD item identi fi ers reusable/ LP_UNUSED. Step 3 is more like step 2 than step 3 — same dead items TID array indicates which TIDs are safe to make reusable Dead item TIDs (collected in step 1 and reliably removed from indexes in step 2) can now fi nally be marked reusable 21
  15. https://speakerdeck.com/peterg/bloat-postgresql-scale VACUUM - processing order (cont.) 22 Processing order makes

    sense when you think about basic rules around lookups Indexes resolve key values in heap using heap TIDs, which must be stable over time (within a VACUUM cycle). Index scans must always land on the correct heap tuple — at the very least there needs to be a “stub” 4 byte dead/LP_DEAD item identi fi er that serves as a tombstone to avoid total chaos. Cannot allow index tuples/TIDs to point to who-knows-what by allowing premature recycling of TID/item identi fi er In other words, VACUUM steps 1, 2, and 3 need to happen in a fi xed order
  16. https://speakerdeck.com/peterg/bloat-postgresql-scale postgres=# create table deltable (delcol int primary key); CREATE

    TABLE postgres=# insert into deltable select generate_series(1, 1000); INSERT 0 1000 postgres=# delete from deltable where delcol % 5 = 0; DELETE 200 postgres=# vacuum (index_cleanup off, verbose) deltable; INFO: vacuuming "postgres.public.deltable" INFO: finished vacuuming "postgres.public.deltable": index scans: 0 pages: 0 removed, 5 remain, 5 scanned (100.00% of total) tuples: 200 removed, 800 remain, 0 are dead but not yet removable removable cutoff: 275362920, which was 0 XIDs old when operation ended new relfrozenxid: 275362918, which is 1 XIDs ahead of previous value frozen: 0 pages from table (0.00% of total) had 0 tuples frozen index scan bypassed: 5 pages from table (100.00% of total) have 200 dead item identifiers avg read rate: 134.698 MB/s, avg write rate: 202.047 MB/s buffer usage: 14 hits, 2 misses, 3 dirtied WAL usage: 6 records, 0 full page images, 858 bytes system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s VACUUM VACUUM without steps 2 and 3
  17. https://speakerdeck.com/peterg/bloat-postgresql-scale postgres=# vacuum (index_cleanup on, verbose) deltable; INFO: vacuuming "postgres.public.deltable"

    INFO: finished vacuuming "postgres.public.deltable": index scans: 1 pages: 0 removed, 5 remain, 5 scanned (100.00% of total) tuples: 0 removed, 800 remain, 0 are dead but not yet removable removable cutoff: 275362920, which was 0 XIDs old when operation ended frozen: 0 pages from table (0.00% of total) had 0 tuples frozen index scan needed: 5 pages from table (100.00% of total) had 200 dead item identifiers removed index "deltable_pkey": pages: 5 in total, 0 newly deleted, 0 currently deleted, 0 reusable avg read rate: 0.000 MB/s, avg write rate: 70.383 MB/s buffer usage: 31 hits, 0 misses, 1 dirtied WAL usage: 15 records, 1 full page images, 10058 bytes system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s VACUUM Finishing off the steps we skipped (steps 2 and 3)
  18. https://speakerdeck.com/peterg/bloat-postgresql-scale HOT UPDATEs 25 Heap-only tuple optimization avoids creating new

    index entries during updates Chains together heap tuples in heap page — existing TIDs in indexes can fi nd the version of interesting by traversing the HOT chain (version chain) from the heap page Avoids adding successor versions to indexes in the fi rst place (hence the name “heap-only tuple”). Optimization only applies when no indexed columns are modi fi ed by UPDATE statements. “All or nothing” — successor versions in indexes either avoided entirely, or required in each and every index
  19. https://speakerdeck.com/peterg/bloat-postgresql-scale Opportunistic HOT pruning “Prunes away” heap tuples during query

    execution (not during VACUUM), which is important for many workloads - Including workloads that don’t manage any HOT updates! “HOT pruning” is a historical misnomer that caught on — all heap tuples can be pruned Cannot reclaim dead item identi fi ers in the heap page, except with heap-only tuples - Heap-only tuples are not directly referenced from indexes/TIDs (only indirectly), so there is no “3 steps of VACUUM” style dependency to worry about when pruning HOT chains - Pruning will free a little more space in affected heap pages when there is only ever “heap-only tuple bloat” (left by HOT updates) - But this may not be very signi fi cant at the level of the heap page itself (indexes are another matter). Far more space used for heap tuples than dead stub item identi fi ers, which take up only 4 bytes — far less than the tens or even hundreds of bytes it takes to store tuples themselves. 26
  20. https://speakerdeck.com/peterg/bloat-postgresql-scale Opportunistic index deletion B-Tree also independently cleans up bloat

    opportunistically, at the level of individual pages Postgres 14 greatly improved this mechanism, by making it speci fi cally target non-HOT update bloat in indexes - Limits build-up of bloat in individual index pages - “All or nothing” nature of HOT update optimization is still a problem — but the worst case is vastly improved - Can perfectly preserve the size of indexes affected by many non-HOT updates - Index deduplication (added in Postgres 13) also helps by “soaking up” bursts of duplicates needed for versioning purposes 27
  21. https://speakerdeck.com/peterg/bloat-postgresql-scale “Top-down” VACUUM versus “bottom-up” opportunistic cleanup VACUUM works at

    the level of a whole table and its indexes, collectively - Top-down, global - “Puts a fl oor under” level of bloat in table and indexes collectively Opportunistic techniques work at the level of individual pages - Bottom-up, local - Limits the concentration of bloat in individual pages - Runs during query processing, as often as required - “Holds the line” for VACUUM, since VACUUM doesn’t “understand” the ways in which different pages (from the same table/index) sometimes have dramatically different requirements 28
  22. https://speakerdeck.com/peterg/bloat-postgresql-scale “Deleting a million rows once” versus “deleting one row

    a million times” There is not too much difference…in theory The practical differences are far greater than you might guess A “problem within a table” versus a “problem within a page” - VACUUM/autovacuum is typically much more effective at cleaning up after a bulk delete - Opportunistic techniques require…opportunities! In general, there may not be any SQL queries that try to read deleted data. - Opportunistic techniques enable reuse of space for new versions of nearby, related rows — which avoids fragmentation 29
  23. https://speakerdeck.com/peterg/bloat-postgresql-scale Overview 1. Structure Logical vs. Physical structures, TIDs as

    “physiological” identi fi ers 2. A bottom-up take on bloat Page level view of bloat, VACUUM, and opportunistic cleanup 3. VACUUM’s priorities Space reclamation, query response time 4. Conclusions Summary of the central ideas from 1 - 3 30
  24. https://speakerdeck.com/peterg/bloat-postgresql-scale VACUUM design goals VACUUM is designed to be non-disruptive.

    Heavyweight lock strength doesn’t block user queries, including INSERTs, UPDATEs, and DELETEs. Indexes are scanned in physical order during VACUUM, not logical order (B-Tree and GiST only). Preserving worst case query response time is arguably the primary goal. - Not impacting response time while VACUUM runs matters almost as much - Reclaiming space is only a secondary goal. 32
  25. https://speakerdeck.com/peterg/bloat-postgresql-scale Space reclamation Space reclamation is important in extreme cases

    Modest amounts of free space can be reclaimed eventually in more common cases, where query response time matters most. VACUUM occasionally truncates heap tables, giving space back to the operating system Indexes have their own unique restrictions on space reuse - Only whole index pages can be reclaimed by the free space map — undersized pages cannot be merged together. - Look out for “pages deleted” for indexes in autovacuum log output (on Postgres 14+) 33
  26. https://speakerdeck.com/peterg/bloat-postgresql-scale VACUUM and table size Big tables vs. small tables

    Big tables (however you de fi ne them) aren’t processed any differently than small tables by VACUUM - But they should be thought of as qualitatively different things in practice Bigger tables must receive fewer individual VACUUM operations, each of which will be longer and more expensive (compared to a similar table with far fewer rows) - The table doesn’t stop accumulating garbage while VACUUM runs - But VACUUM only removes tuples that were already garbage when the VACUUM operation began - “Too big to fail” dynamics may come into play (e.g., autovacuum cancellation can hurt a lot more with larger tables) 34
  27. https://speakerdeck.com/peterg/bloat-postgresql-scale VACUUM and table size (cont.) Breaking big VACUUMs down

    into smaller VACUUMs is a good strategy Table partitioning can help with this - Individual partitions/child tables are processed as independent tables by VACUUM More frequent VACUUMs by autovacuum may also help - Works best with append-mostly tables with few or no garbage tuples - Number of pages scanned by each VACUUM (as opposed to skipped using the visibility map) is important — heap pages set all- visible must remain all-visible rather than being processed/scanned again and again. - Postgres 15 was the fi rst version that instrumented “pages scanned” in autovacuum log reports (as well as in VACUUM VERBOSE) 35
  28. https://speakerdeck.com/peterg/bloat-postgresql-scale automatic vacuum of table "postgres.public.order_line": index scans: 0 pages:

    0 removed, 7385017 remain, 2091215 scanned (28.32% of total) tuples: 4964150 removed, 451619205 remain, 2728993 are dead but not yet removable removable cutoff: 76733064, which was 2311556 XIDs old when operation ended frozen: 406579 pages from table (5.51% of total) had 24215117 tuples frozen index scan bypassed: 124263 pages from table (1.68% of total) have 406397 dead item identifiers avg read rate: 28.617 MB/s, avg write rate: 19.705 MB/s buffer usage: 2115525 hits, 1945133 misses, 1339404 dirtied WAL usage: 2042719 records, 405011 full page images, 2881770710 bytes system usage: CPU: user: 14.81 s, system: 18.83 s, elapsed: 531.03 s Incremental autovacuum of a large and continually growing table, with updates
  29. https://speakerdeck.com/peterg/bloat-postgresql-scale Overview 1. Structure Logical vs. Physical structures, TIDs as

    “physiological” identi fi ers 2. A bottom-up take on bloat Page level view of bloat, VACUUM, and opportunistic cleanup 3. VACUUM’s priorities Space reclamation, query response time 4. Conclusions Summary of the central ideas from 1 - 3 37
  30. https://speakerdeck.com/peterg/bloat-postgresql-scale Conclusions about bloat in PostgreSQL VACUUM is tasked with

    removing old garbage tuples that are obsolete to all possible transactions. - Recent Postgres versions are much better at showing you what’s really going on Opportunistic techniques (HOT Pruning, deletion in B-Tree indexes) also exist. Garbage collection usually happens both ways. - Even recent Postgres versions make it hard to tell how much of this has happened - But can be inferred from VACUUM instrumentation (particularly autovacuum logging), to a degree 38
  31. https://speakerdeck.com/peterg/bloat-postgresql-scale Conclusions about bloat in PostgreSQL (cont.) Opportunistic techniques are

    restricted by the same ordering requirements that dictate high-level steps VACUUM performs. - Index scans cannot be allowed to land on an unrelated tuple due to heap TID recycling. - So even workloads/tables that do a great deal of cleanup opportunistically are bound to eventually require vacuuming to mark dead item identi fi ers for reuse There is an important complementary relationship between VACUUM and opportunistic cleanup 39