11#! /bin/sh
22#
3- # pg_upgrade: update a database without needing a full dump/reload cycle
3+ # pg_upgrade: update a database without needing a full dump/reload cycle.
44# CAUTION: read the manual page before trying to use this!
55
6- echo " pg_upgrade is disabled in this release because the on-disk structure" 1>&2
7- echo " of the tables has changed compared to previous releases." 1>&2
8- exit 1
6+ # NOTE: we must be sure to update the version-checking code a few dozen lines
7+ # below for each new PostgreSQL release.
98
109trap " rm -f /tmp/$$ " 0 1 2 3 15
1110
@@ -33,37 +32,76 @@ OLDDIR="$1"
3332
3433# check things
3534
36- if [! -f " ./data/PG_VERSION " ]
35+ if [! -d " ./data" ]
3736then echo " ` basename$0 ` must be run from the directory containing
38- the database directory\` data' (` dirname$PGDATA ` .)" 1>&2
37+ the database directory\` data\' (` dirname$PGDATA ` .)" 1>&2
38+ echo " You must have run initdb to create the template1 database." 1>&2
3939exit 1
4040fi
4141
4242if [! -d " ./$OLDDIR " ]
43- then echo " You must rename your old /data directory to /$OLDDIR and run initdb." 1>&2
43+ then echo " You must rename your old data directory to$OLDDIR and run initdb." 1>&2
44+ exit 1
45+ fi
46+
47+ if [! -d " ./data/base/template1" ]
48+ then echo " Cannot find database template1 in ./data/base." 1>&2
49+ echo " Are you running$0 as the postgres superuser?" 1>&2
4450exit 1
4551fi
4652
4753if [! -d " ./$OLDDIR /base/template1" ]
48- then echo " There isnot database template1 in ./$OLDDIR /base." 1>&2
54+ then echo " There isno database template1 in ./$OLDDIR /base." 1>&2
4955exit 1
5056fi
5157
52- if [! -d " ./data" ]
53- then echo " You must run initdb to create the template1 database ." 1>&2
58+ if [! -r " ./data/PG_VERSION " ]
59+ then echo " Cannot read ./data/PG_VERSION --- something is wrong ." 1>&2
5460exit 1
5561fi
5662
57- if [! -d " ./data/base/template1 " ]
58- then echo " $0 must be run as the postgres superuser ." 1>&2
63+ if [! -r " ./$OLDDIR /PG_VERSION " ]
64+ then echo " Cannot read ./ $OLDDIR /PG_VERSION --- something is wrong ." 1>&2
5965exit 1
6066fi
6167
62- # do I need to create a database?
68+ # Get the actual versions seen in the data dirs.
69+ DESTVERSION=` cat ./data/PG_VERSION`
70+ SRCVERSION=` cat ./$OLDDIR /PG_VERSION`
6371
64- # remove any COPY statements
65- # we don't even need pgdump_oid because we are moving pg_variable
66- # then shouldn't be in there anyway
72+ # Check for version compatibility.
73+ # This code will need to be updated/reviewed for each new PostgreSQL release.
74+
75+ # MYVERSION is the expected output database version
76+ MYVERSION=" 6.6"
77+
78+ if [" $DESTVERSION " != " $MYVERSION " ]
79+ then echo " $0 is for PostgreSQL version$MYVERSION , but ./data/PG_VERSION contains$DESTVERSION ." 1>&2
80+ echo " Did you run initdb for version$MYVERSION ?" 1>&2
81+ exit 1
82+ fi
83+
84+ # Check that input database is of a compatible version (anything with the same
85+ # physical layout of user tables and indexes should be OK). I did not write
86+ # something like "$SRCVERSION -ge $MINVERSION" because test(1) isn't bright
87+ # enough to compare dotted version strings properly. Using a case statement
88+ # looks uglier but is more flexible.
89+
90+ case " $SRCVERSION " in
91+ 6.5) ;;
92+ 6.6) ;;
93+ * )echo " Sorry,` basename$0 ` cannot upgrade database version$SRCVERSION to$DESTVERSION ." 1>&2
94+ echo " The on-disk structure of tables has changed." 1>&2
95+ echo " You will need to dump and restore using pg_dump." 1>&2
96+ exit 1;;
97+ esac
98+
99+
100+ # OK, ready to proceed.
101+ # XXX Do I need to create a database?
102+
103+ # remove any COPY statements, except for the one that loads pg_shadow.
104+ # there shouldn't be any others in there anyway...
67105
68106cat$INPUT | awk' {
69107if (toupper($1) == "COPY" && $2 != "pg_shadow")
@@ -82,6 +120,8 @@ $0 aborted." 1>&2
82120exit 1
83121fi
84122
123+ echo " Input script$INPUT complete, moving data files..."
124+
85125for DIR in data/base/*
86126do
87127BASEDIR=" ` basename$DIR ` "
92132BASEFILE=" ` basename$FILE ` "
93133if [` expr" $BASEFILE " : " pg_" ` -ne 3-a \
94134" $BASEFILE " != " PG_VERSION" ]
95- then mv$FILE $DIR
135+ then mv-f $FILE $DIR
96136fi
97137done
98138fi
99139done
100140
101- mv$OLDDIR /pg_log data
102- mv$OLDDIR /pg_variable data
141+ mv-f $OLDDIR /pg_log data
142+ mv-f $OLDDIR /pg_variable data
103143
104144echo " You may remove the$OLDDIR directory with 'rm -r$OLDDIR '."
145+ exit 0