22
Rapid Upgrades With Pg_Migrator BRUCE MOMJIAN, ENTERPRISEDB August, 2009 Abstract Pg_Migrator allows migration between major releases of Postgres without a data dump/reload. This presentation explains how pg_migrator works. Creative Commons Attribution License http://momjian.us/presentations

Pg migrator

Embed Size (px)

DESCRIPTION

Bruce Momjian Pg_Migrator allows data to be transfered between major Postgres versionswithout a dump/restore. This talk explains the internal workings ofpg_migrator and includes a pg_migrator demonstration

Citation preview

Page 1: Pg migrator

Rapid Upgrades With Pg_Migrator

BRUCE MOMJIAN,ENTERPRISEDB

August, 2009

AbstractPg_Migrator allows migration between major releases of Postgreswithout a data dump/reload. This presentation explains howpg_migrator works.

Creative Commons Attribution License http://momjian.us/presentations

Page 2: Pg migrator

Why Pg_Migrator� Very fast upgrades

� Optionally no additional disk space

Rapid Upgrades With Pg-Migrator 1

Page 3: Pg migrator

Other Upgrade Options� dump/restore

� Slony

Rapid Upgrades With Pg-Migrator 2

Page 4: Pg migrator

How It Works: Initial Setup

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

System Tables

clog

New Cluster

Rapid Upgrades With Pg-Migrator 3

Page 5: Pg migrator

Decouple New Clog Via Freezing

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

4 7

8

9

pg_class

User Tables

System Tables

clog

New Cluster

5Freeze

X X

Rapid Upgrades With Pg-Migrator 4

Page 6: Pg migrator

Transfer Clog and XID

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

System Tables

clog

New Cluster

controldata controldataxid

Rapid Upgrades With Pg-Migrator 5

Page 7: Pg migrator

Get Schema Dump

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

System Tables

clog

New Cluster

pg_dumpall - -schema

Rapid Upgrades With Pg-Migrator 6

Page 8: Pg migrator

Reserve TOAST OIDs Using Relfilenodes

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

26

18

System Tables

clog

New Cluster

This is necessary because heap references to TOAST tables contain theTOAST oids for easy lookup.Rapid Upgrades With Pg-Migrator 7

Page 9: Pg migrator

Restore Schema In New Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

2640

39

31

System Tables

clog

New Cluster

pg_dumpall - -schema

30

32

33

34

35

36

37

41

42

43

44

45

47

18

Rapid Upgrades With Pg-Migrator 8

Page 10: Pg migrator

Connect TOAST Placeholders To the Proper Relations

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

26

39

31

System Tables

clog

New Cluster

30

32

33

34

35

36

37

42

43

44

45

47

18

41

40

X

Rapid Upgrades With Pg-Migrator 9

Page 11: Pg migrator

Copy User Heap/Index Files

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

2640

39

31

System Tables

clog

New Cluster

30

32

33

34

35

36

37

42

43

44

45

47

18

41

Rapid Upgrades With Pg-Migrator 10

Page 12: Pg migrator

Complete

1

2

3 6

5

4 7

8

9

pg_class

User Tables

15 21 27

262014

13 19 25

241812

17 2311

10 16 22

System Tables

clog

Old Cluster

1

2

3 6

5

4 7

8

9

pg_class

User Tables

2640

39

31

System Tables

clog

New Cluster

30

32

33

34

35

36

37

42

43

44

45

47

18

41

Rapid Upgrades With Pg-Migrator 11

Page 13: Pg migrator

How It Works: In Detail� Check for cluster compatability

– locale– encoding– integer datetimes (default changed from 8.3 -> 8.4)

� Use pg_dumpall to dump old cluster schema (no data)

� Freeze all new cluster rows (remove reference to clog entries)

� Rename tablespaces to *_old

� New cluster uses old xid counter value (see freeze above)

– Set system table frozen xids to match the current xid

� Create new users/databasesRapid Upgrades With Pg-Migrator 12

Page 14: Pg migrator

� Collect cluster information

� Install support functions that call internal backend functions

� Create placeholder files to reserve relfilenode file names

� Create schema in new cluster

� Adjust new cluster to use reserved relfilenode names

– Delete placeholder toast relfilenode files– Remove new cluster toast tables– Create new cluster toast table using reserved relfilenode– Assign new toast tables with proper relfilenodes to relations

� Copy or link files from old cluster to new cluster

– Toast tables have the same relfilenodes as in the old cluster

� Warn about any remaining issues, like REINDEX requirements

Rapid Upgrades With Pg-Migrator 13

Page 15: Pg migrator

Sample Run: Validation 1

Performing consistency checks

-----------------------------

Checking old data directory /u/pgsql.old/data

checking base ok

checking global ok

checking pg_clog ok

checking pg_multixact ok

checking pg_subtrans ok

checking pg_tblspc ok

checking pg_twophase ok

checking pg_xlog ok

Checking new data directory /u/pgsql/data

checking base ok

checking global ok

checking pg_clog ok

checking pg_multixact ok

checking pg_subtrans ok

checking pg_tblspc ok

checking pg_twophase ok

checking pg_xlog ok

Checking binaries in old cluster (/u/pgsql.old/bin)

checking postgres ok

checking pg_ctl ok

checking pg_dumpall ok

checking psql ok

Rapid Upgrades With Pg-Migrator 14

Page 16: Pg migrator

Sample Run: Validation 2

Checking binaries in new cluster (/u/pgsql/bin)

checking postgres ok

checking pg_ctl ok

checking pg_dumpall ok

checking psql ok

Starting postmaster to service old cluster

waiting for postmaster to start ok

Getting pg_database and pg_largeobject relfilenodes ok

Checking for columns with user-defined composite types ok

Checking for columns with user-defined array types ok

Checking for columns with user-defined enum types ok

Checking for /contrib/isn with bigint-passing mismatch ok

Checking for invalid ’name’ user columns ok

Checking for tsquery user columns ok

Creating script to adjust sequences ok

Creating catalog dump ok

Splitting old dump file ok

Stopping postmaster servicing old cluster ok

Starting postmaster to service new cluster

waiting for postmaster to start ok

Checking for presence of required libraries ok

Stopping postmaster servicing new cluster ok

*Checks complete*

Rapid Upgrades With Pg-Migrator 15

Page 17: Pg migrator

Preparing for Migration

Preparing for migration

-----------------------

Starting postmaster to service new cluster

waiting for postmaster to start ok

Analyzing all rows on the new cluster ok

Freezing all rows on the new cluster ok

Getting pg_database and pg_largeobject relfilenodes ok

Stopping postmaster servicing new cluster ok

| If pg_migrator fails after this point, you must

| re-initdb the new cluster before continuing.

| You will also need to remove the ".old" suffix

| from old $PGDATA/global/pg_control and old

| tablespace directory names before continuing.

Rapid Upgrades With Pg-Migrator 16

Page 18: Pg migrator

Sample Run: Migration 1

Performing migration

--------------------

Adding ".old" suffix to old global/pg_control ok

Renaming tablespaces to *.old ok

Deleting old commit clogs ok

Copying commit clogs ok

Setting next transaction id for new cluster ok

Resetting WAL archives ok

Starting postmaster to service new cluster

waiting for postmaster to start ok

Setting frozenxid counters in new cluster ok

Creating databases in new cluster ok

Stopping postmaster servicing new cluster ok

Creating placeholder relfiles for toast relations

ok

Starting postmaster to service new cluster

waiting for postmaster to start ok

Restoring database schema ok

Adding support functions to new cluster ok

Restoring relations to use fixed toast file names

ok

Rapid Upgrades With Pg-Migrator 17

Page 19: Pg migrator

Sample Run: Migration 2

Removing support functions from new cluster ok

Stopping postmaster servicing new cluster ok

Restoring user relation files

ok

Setting next oid for new cluster ok

Adjusting sequences ok

Checking for tsvector user columns ok

Checking for hash and gin indexes ok

Checking for bpchar_pattern_ops indexes ok

*Upgrade complete*

| Optimizer statistics and free space information

| are not transferred by pg_migrator, so consider

| running:

| vacuumdb --all --analyze

| on the newly-upgraded cluster.

Rapid Upgrades With Pg-Migrator 18

Page 20: Pg migrator

Possible Post-8.4 DataFormat Changes

Change Conversion Method

clog none

heap page header, including bitmask convert to new page format on read

tuple header, including bitmask convert to new page format on read

data value format create old data type in new cluster

index page format reindex, or recreate index methods

TOAST page format convert to new page format on read

Rapid Upgrades With Pg-Migrator 19

Page 21: Pg migrator

Migration Timings

Migration Method Minutesdump/restore 300.0dump with parallel restore 180.0pg_migrator in copy mode 44.0pg_migrator in link mode 0.7

Database size: 150GB, 850 tables

The last duration is 44 seconds.

Timings courtesy of

Stefan Kaltenbrunner

(mastermind on IRC)

Rapid Upgrades With Pg-Migrator 20

Page 22: Pg migrator

Conclusion

Rapid Upgrades With Pg-Migrator 21