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

mvcc and vacuum explained
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Boriss Mejías Consultant - 2ndQuadrant Air Guitar Player

MVCC and Vacuum explained

slide-2
SLIDE 2

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Prelude

slide-3
SLIDE 3

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

ACID

slide-4
SLIDE 4

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

ACID

Atomicity, Consistency, Isolation, Durability BASE Basically Available, Soft state, Eventually consistent

slide-5
SLIDE 5

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019 Wikipedia: Milky Way

slide-6
SLIDE 6

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019 Wikipedia: ALMA

slide-7
SLIDE 7

https://www.2ndQuadrant.com

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

slide-8
SLIDE 8

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Everything is a transaction

INSERT INTO key_value (key, value) VALUES (42, ‘towel’);

slide-9
SLIDE 9

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Everything is a transaction

BEGIN; INSERT INTO key_value (key, value) VALUES (42, ‘towel’); COMMIT;

slide-10
SLIDE 10

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Concurrency

BEGIN; SELECT value FROM key_value WHERE key = 42; COMMIT; BEGIN; UPDATE key_value SET value = ‘foo’ WHERE key = 42; COMMIT;

Time

slide-11
SLIDE 11

https://www.2ndQuadrant.com

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

slide-12
SLIDE 12

https://www.2ndQuadrant.com

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

slide-13
SLIDE 13

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Concurrency

BEGIN; SELECT value FROM key_value WHERE key = 42; COMMIT; BEGIN; UPDATE key_value SET value = ‘foo’ WHERE key = 42; COMMIT;

Time txid 1020 txid 1330 txid 1368

slide-14
SLIDE 14

https://www.2ndQuadrant.com

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”

slide-15
SLIDE 15

https://www.2ndQuadrant.com

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”
slide-16
SLIDE 16

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Interlude

slide-17
SLIDE 17

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Mind the Transaction Isolation Level

  • READ COMMITTED
  • REPEATABLE READ
  • SERIALIZABLE
  • READ UNCOMMITTED
slide-18
SLIDE 18

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Concurrency

BEGIN; SELECT value FROM key_value WHERE key = 42; SELECT value FROM key_value WHERE key = 42; COMMIT; BEGIN; UPDATE key_value SET value = ‘foo’ WHERE key = 42; COMMIT;

Time

slide-19
SLIDE 19

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Vacuum

slide-20
SLIDE 20

https://www.2ndQuadrant.com

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

slide-21
SLIDE 21

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Concurrency

BEGIN; SELECT value FROM key_value WHERE key = 42; SELECT value FROM key_value WHERE key = 42; COMMIT; BEGIN; UPDATE key_value SET value = ‘foo’ WHERE key = 42; COMMIT; VACUUM;

Time

slide-22
SLIDE 22

https://www.2ndQuadrant.com

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

slide-23
SLIDE 23

https://www.2ndQuadrant.com

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

slide-24
SLIDE 24

https://www.2ndQuadrant.com

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

slide-25
SLIDE 25

https://www.2ndQuadrant.com

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

slide-26
SLIDE 26

https://www.2ndQuadrant.com

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
slide-27
SLIDE 27

https://www.2ndQuadrant.com

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

slide-28
SLIDE 28

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Autovacuum

slide-29
SLIDE 29

https://www.2ndQuadrant.com

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
slide-30
SLIDE 30

https://www.2ndQuadrant.com

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
slide-31
SLIDE 31

https://www.2ndQuadrant.com

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
slide-32
SLIDE 32

https://www.2ndQuadrant.com

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

slide-33
SLIDE 33

https://www.2ndQuadrant.com

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

slide-34
SLIDE 34

https://www.2ndQuadrant.com

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
slide-35
SLIDE 35

https://www.2ndQuadrant.com

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

slide-36
SLIDE 36

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

At the pub

slide-37
SLIDE 37

https://www.2ndQuadrant.com

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

slide-38
SLIDE 38

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Finale

slide-39
SLIDE 39

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019 Wikipedia: Radio telescope image

slide-40
SLIDE 40

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

MVCC and Vacuum

  • MVCC crucial to provide ACID properties
  • It creates dead rows → needs maintenance
  • VACUUM remove dead rows
  • Autovacuum does it for you
slide-41
SLIDE 41

https://www.2ndQuadrant.com

MVCC and Vacuum / Nordic PGDay

Copenhagen, 19 March 2019

Thanks and Remember Rule #6

Boriss Mejias boriss.mejias@2ndquadrant.com @tchorix