| 1 |
------------------------------------------------------------------------------ |
| 2 |
PG_UPGRADE: IN-PLACE UPGRADES FOR POSTGRESQL |
| 3 |
------------------------------------------------------------------------------ |
| 4 |
|
| 5 |
Upgrading a PostgreSQL database from one major release to another can be |
| 6 |
an expensive process. For minor upgrades, you can simply install new |
| 7 |
executables and forget about upgrading existing data. But for major |
| 8 |
upgrades, you have to export all of your data using pg_dump, install the |
| 9 |
new release, run initdb to create a new cluster, and then import your |
| 10 |
old data. If you have a lot of data, that can take a considerable amount |
| 11 |
of time. If you have too much data, you may have to buy more storage |
| 12 |
since you need enough room to hold the original data plus the exported |
| 13 |
data. pg_upgrade can reduce the amount of time and disk space required |
| 14 |
for many upgrades. |
| 15 |
|
| 16 |
The URL http://momjian.us/main/writings/pgsql/pg_upgrade.pdf contains a |
| 17 |
presentation about pg_upgrade internals that mirrors the text |
| 18 |
description below. |
| 19 |
|
| 20 |
------------------------------------------------------------------------------ |
| 21 |
WHAT IT DOES |
| 22 |
------------------------------------------------------------------------------ |
| 23 |
|
| 24 |
pg_upgrade is a tool that performs an in-place upgrade of existing |
| 25 |
data. Some upgrades change the on-disk representation of data; |
| 26 |
pg_upgrade cannot help in those upgrades. However, many upgrades do |
| 27 |
not change the on-disk representation of a user-defined table. In those |
| 28 |
cases, pg_upgrade can move existing user-defined tables from the old |
| 29 |
database cluster into the new cluster. |
| 30 |
|
| 31 |
There are two factors that determine whether an in-place upgrade is |
| 32 |
practical. |
| 33 |
|
| 34 |
Every table in a cluster shares the same on-disk representation of the |
| 35 |
table headers and trailers and the on-disk representation of tuple |
| 36 |
headers. If this changes between the old version of PostgreSQL and the |
| 37 |
new version, pg_upgrade cannot move existing tables to the new cluster; |
| 38 |
you will have to pg_dump the old data and then import that data into the |
| 39 |
new cluster. |
| 40 |
|
| 41 |
Second, all data types should have the same binary representation |
| 42 |
between the two major PostgreSQL versions. |
| 43 |
|
| 44 |
------------------------------------------------------------------------------ |
| 45 |
HOW IT WORKS |
| 46 |
------------------------------------------------------------------------------ |
| 47 |
|
| 48 |
To use pg_upgrade during an upgrade, start by installing a fresh |
| 49 |
cluster using the newest version in a new directory. When you've |
| 50 |
finished installation, the new cluster will contain the new executables |
| 51 |
and the usual template0, template1, and postgres databases, but no |
| 52 |
user-defined tables. At this point, you can shut down the old and new |
| 53 |
postmasters and invoke pg_upgrade. |
| 54 |
|
| 55 |
When pg_upgrade starts, it ensures that all required executables are |
| 56 |
present and contain the expected version numbers. The verification |
| 57 |
process also checks the old and new $PGDATA directories to ensure that |
| 58 |
the expected files and subdirectories are in place. If the verification |
| 59 |
process succeeds, pg_upgrade starts the old postmaster and runs |
| 60 |
pg_dumpall --schema-only to capture the metadata contained in the old |
| 61 |
cluster. The script produced by pg_dumpall will be used in a later step |
| 62 |
to recreate all user-defined objects in the new cluster. |
| 63 |
|
| 64 |
Note that the script produced by pg_dumpall will only recreate |
| 65 |
user-defined objects, not system-defined objects. The new cluster will |
| 66 |
contain the system-defined objects created by the latest version of |
| 67 |
PostgreSQL. |
| 68 |
|
| 69 |
Once pg_upgrade has extracted the metadata from the old cluster, it |
| 70 |
performs a number of bookkeeping tasks required to 'sync up' the new |
| 71 |
cluster with the existing data. |
| 72 |
|
| 73 |
First, pg_upgrade copies the commit status information and 'next |
| 74 |
transaction ID' from the old cluster to the new cluster. This step |
| 75 |
ensures that the proper tuples are visible from the new cluster. |
| 76 |
Remember, pg_upgrade does not export/import the content of user-defined |
| 77 |
tables so the transaction IDs in the new cluster must match the |
| 78 |
transaction IDs in the old data. pg_upgrade also copies the starting |
| 79 |
address for write-ahead logs from the old cluster to the new cluster. |
| 80 |
|
| 81 |
Now pg_upgrade begins reconstructing the metadata obtained from the old |
| 82 |
cluster using the first part of the pg_dumpall output. |
| 83 |
|
| 84 |
Next, pg_upgrade executes the remainder of the script produced earlier |
| 85 |
by pg_dumpall --- this script effectively creates the complete |
| 86 |
user-defined metadata from the old cluster to the new cluster. It |
| 87 |
preserves the relfilenode numbers so TOAST and other references |
| 88 |
to relfilenodes in user data is preserved. (See binary-upgrade usage |
| 89 |
in pg_dump). We choose to preserve tablespace and database OIDs as well. |
| 90 |
|
| 91 |
Finally, pg_upgrade links or copies each user-defined table and its |
| 92 |
supporting indexes and toast tables from the old cluster to the new |
| 93 |
cluster. |
| 94 |
|
| 95 |
An important feature of the pg_upgrade design is that it leaves the |
| 96 |
original cluster intact --- if a problem occurs during the upgrade, you |
| 97 |
can still run the previous version, after renaming the tablespaces back |
| 98 |
to the original names. |