mvcc and vacuum explained
play

MVCC and Vacuum explained Boriss Mejas Consultant - 2ndQuadrant - PowerPoint PPT Presentation

MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 MVCC and Vacuum explained Boriss Mejas Consultant - 2ndQuadrant Air Guitar Player https://www. 2ndQuadrant.com MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Prelude


  1. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 MVCC and Vacuum explained Boriss Mejías Consultant - 2ndQuadrant Air Guitar Player https://www. 2ndQuadrant.com

  2. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Prelude https://www. 2ndQuadrant.com

  3. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 ACID https://www. 2ndQuadrant.com

  4. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 ACID Atomicity, Consistency, Isolation, Durability BASE Basically Available, Soft state, Eventually consistent https://www. 2ndQuadrant.com

  5. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Wikipedia: Milky Way https://www. 2ndQuadrant.com

  6. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Wikipedia: ALMA https://www. 2ndQuadrant.com

  7. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 MVCC ● Multi-Version Concurrency Control ● Multiple versions of each row ● Only one version is visible to each observer ● Time-consistent view of the whole database is always available for each session: Snapshot https://www. 2ndQuadrant.com

  8. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Everything is a transaction INSERT INTO key_value (key, value) VALUES (42, ‘towel’); https://www. 2ndQuadrant.com

  9. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Everything is a transaction BEGIN ; INSERT INTO key_value (key, value) VALUES (42, ‘towel’); COMMIT ; https://www. 2ndQuadrant.com

  10. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Concurrency BEGIN ; BEGIN ; UPDATE key_value SET value = ‘foo’ SELECT value WHERE key = 42; FROM key_value WHERE key = 42; COMMIT ; COMMIT ; Time https://www. 2ndQuadrant.com

  11. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 MVCC Basic Components ● Each transaction has a full transaction id – SELECT txid_current(); ● Each row has visibility information – xmin: creation txid – xmax: when row was deleted/updated ● Each transaction has a status – Commit log / pg_xact https://www. 2ndQuadrant.com

  12. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 SQL Write Queries ● INSERT : sets xmin ● DELETE : sets xmax ● UPDATE : delete and insert → sets xmax and creates new version with xmin https://www. 2ndQuadrant.com

  13. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 txid 1020 Concurrency BEGIN ; txid 1330 BEGIN ; txid 1368 UPDATE key_value SET value = ‘foo’ SELECT value WHERE key = 42; FROM key_value WHERE key = 42; COMMIT ; COMMIT ; Time https://www. 2ndQuadrant.com

  14. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Multi-Versions of the row xmin xmax key value 1020 0 42 ‘towel’ UPDATE → xmin xmax key value 1020 1368 42 ‘towel’ 1368 0 42 ‘foo’ Commit state of txid 1368 will be “commit” or “rollback” https://www. 2ndQuadrant.com

  15. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Dead Rows ● DELETE : creates a dead row ● UPDATE : delete and insert → dead row ● ROLLBACK of writes: creates dead rows ● Too many dead rows → “bloated table” https://www. 2ndQuadrant.com

  16. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Interlude https://www. 2ndQuadrant.com

  17. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Mind the Transaction Isolation Level ● READ COMMITTED ● REPEATABLE READ ● SERIALIZABLE ● READ UNCOMMITTED https://www. 2ndQuadrant.com

  18. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Concurrency BEGIN ; SELECT value BEGIN ; FROM key_value UPDATE key_value WHERE key = 42; SET value = ‘foo’ WHERE key = 42; COMMIT ; SELECT value FROM key_value WHERE key = 42; COMMIT ; Time https://www. 2ndQuadrant.com

  19. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Vacuum https://www. 2ndQuadrant.com

  20. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Vacuum – Dead Rows Maintenance ● VACUUM table ; ● Remove dead rows ● Unless the dead row is potentially visible for at least one session https://www. 2ndQuadrant.com

  21. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Concurrency BEGIN ; SELECT value BEGIN ; FROM key_value UPDATE key_value WHERE key = 42; SET value = ‘foo’ WHERE key = 42; COMMIT ; SELECT value FROM key_value VACUUM; WHERE key = 42; COMMIT ; Time https://www. 2ndQuadrant.com

  22. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Table pg_stat_user_tables -[ RECORD 1 ]-------+----------- relid | 24652 schemaname | public relname | key_value n_tup_ins | 2789000 n_tup_upd | 6712 n_tup_del | 1789000 n_tup_hot_upd | 0 n_live_tup | 1000000 n_dead_tup | 5666 https://www. 2ndQuadrant.com

  23. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 VACUUM key_value ; -[ RECORD 1 ]-------+----------- relid | 24652 schemaname | public relname | key_value n_tup_ins | 2789000 n_tup_upd | 6712 n_tup_del | 1789000 n_tup_hot_upd | 0 n_live_tup | 1000000 n_dead_tup | 0 https://www. 2ndQuadrant.com

  24. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Vacuum – Dead Rows Maintenance ● VACUUM table ; ● Remove dead rows ● Unless the dead row is potentially visible for at least one session ● Long running transactions can be an issue – Check ‘idle in transaction’ in pg_stat_activity https://www. 2ndQuadrant.com

  25. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Vacuum Command ● VACUUM ; – Vacuums the entire database ● VACUUM tablename; – Vacuums table tablename ● VACUUM ANALYZE tablename; – Vacuums and analyzes the table ● $ vacuumdb --jobs=N – Multiple concurrent vacuums https://www. 2ndQuadrant.com

  26. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Vacuum effects ● VACUUM locks against DDL ● Only one vacuum per table ● INSERT , DELETE , UPDATE can still run https://www. 2ndQuadrant.com

  27. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Vacuum effects ● VACUUM locks against DDL ● Only one vacuum per table ● INSERT , DELETE , UPDATE can still run divertimento or delude ● VACUUM FULL works differently – It locks everything – It creates a new table – Think of a butterfly https://www. 2ndQuadrant.com

  28. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Autovacuum https://www. 2ndQuadrant.com

  29. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Autovacuum ● Runs VACUUM and ANALYZE ● Runs all the time ● No scheduling, just nap times ● It cancels itself to avoid blocking user’s actions https://www. 2ndQuadrant.com

  30. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Autovacuum basic parameters ● autovacuum = on ● autovacuum_naptime = 1min ● autovacuum_max_workers = 3 ● log_autovacuum_min_duration = -1 https://www. 2ndQuadrant.com

  31. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Autovacuum triggered threshold + (rows * scale_factor) ● autovacuum_vacuum_threshold = 50 ● autovacuum_vacuum_scale_factor = 0.2 ● autovacuum_analyze_threshold = 50 ● autovacuum_analyze_scale_factor = 0.1 https://www. 2ndQuadrant.com

  32. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Table pg_stat_user_tables -[ RECORD 1 ]-------+----------- relid | 24652 schemaname | public relname | key_value n_tup_ins | 2789000 n_tup_upd | 6712 n_tup_del | 1789000 n_tup_hot_upd | 0 n_live_tup | 1000000 n_dead_tup | 5666 https://www. 2ndQuadrant.com

  33. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Table pg_stat_user_tables -[ RECORD 1 ]-------+----------- relname | key_value last_vacuum | 2019-02-19 12:58:42 last_autovacuum | 2019-03-16 07:06:06 last_analyze | 2019-02-19 12:58:42 last_autoanalyze | 2019-03-16 07:06:06 vacuum_count | 7 autovacuum_count | 6128 analyze_count | 10676 autoanalyze_count | 7 https://www. 2ndQuadrant.com

  34. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Autovacuum – How much work? ● autovacuum_vacuum_cost_limit = 200 ● vacuum_cost_page_hit = 1 ● vacuum_cost_page_miss = 10 ● vacuum_cost_page_dirty = 20 https://www. 2ndQuadrant.com

  35. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Table pg_stat_progress_vacuum -[ RECORD 1 ]------+----------- pid | 27666 datid | 18613 datname | lamuella relid | 24652 phase | performing final cleanup heap_blks_total | 1 heap_blks_scanned | 1 heap_blks_vacuumed | 1 index_vacuum_count | 0 max_dead_tuples | 291 num_dead_tuples | 0 https://www. 2ndQuadrant.com

  36. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 At the pub https://www. 2ndQuadrant.com

  37. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Freezing ● Transaction ids are 4 byte ints ● Counter wraps every 4 billion xids ● Old rows get “frozen” – Replace xid with the FrozenTransactionId https://www. 2ndQuadrant.com

  38. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Finale https://www. 2ndQuadrant.com

  39. MVCC and Vacuum / Nordic PGDay Copenhagen, 19 March 2019 Wikipedia: Radio telescope image https://www. 2ndQuadrant.com

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend