33# 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- # $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_upgrade,v 1.14 2000/02/23 15:46:12 momjian Exp $
6+ # $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_upgrade,v 1.15 2000/05/05 03:08:20 tgl Exp $
77#
88# NOTE: we must be sure to update the version-checking code a few dozen lines
99# below for each new PostgreSQL release.
1010
11- trap " rm -f /tmp/$$ " 0 1 2 3 15
11+ TMPFILE=" /tmp/pgupgrade.$$ "
12+
13+ trap " rm -f$TMPFILE " 0 1 2 3 15
1214
1315if [" $# " -eq 0 ]
14- then echo " Usage:$0 [ -f inputfile] old_data_dir" 1>&2
16+ then echo " Usage:$0 -f inputfile old_data_dir" 1>&2
1517exit 1
1618fi
1719
@@ -22,11 +24,12 @@ thenINPUT="$2"
2224then echo " $INPUT does not exist" 1>&2
2325exit 1
2426fi
25- else INPUT=" "
27+ else echo " Usage:$0 -f inputfile old_data_dir" 1>&2
28+ exit 1
2629fi
2730
2831if [" $# " -ne 1 ]
29- then echo " Usage:$0 [ -f inputfile] old_data_dir" 1>&2
32+ then echo " Usage:$0 -f inputfile old_data_dir" 1>&2
3033exit 1
3134fi
3235
@@ -101,36 +104,57 @@ esac
101104
102105# OK, ready to proceed.
103106
104- # Remove any COPY statements, except for the one that loads pg_shadow.
107+ # Execute the input script to create everything, except that we remove
108+ # any COPY statements, except for the ones that load pg_shadow/pg_group.
105109# There shouldn't be any others in there anyway...
106- # Also marks rows as committed because when pg_log is replaced with
107- # the saved version, the transaction statuses may be wrong, so
108- # vacuum sets the on-row status to the proper value.
109110
110111cat$INPUT | awk' {
111- if (toupper($1) == "COPY" && $2 != "pg_shadow")
112+ if (tolower($1) == "copy" &&
113+ $2 != "pg_shadow" &&
114+ $2 != "pg_group")
112115while (getline $0 > 0 && $0 != "\\.")
113116;
114- else if (tolower($1) == "\\connect" &&
115- tolower($2) == "template1")
116- printf "VACUUM;\n%s\n", $0;
117117elseprint $0;
118- }' > /tmp/$$
118+ }' > $TMPFILE
119+
120+ psql" template1" < $TMPFILE
121+
122+ if [$? -ne 0 ]
123+ then echo " There were errors in the input script$INPUT .
124+ $0 aborted." 1>&2
125+ exit 1
126+ fi
119127
120- # We need to vacuum the last database too
121- echo " VACUUM;" >> /tmp/$$
128+ echo " Input script$INPUT complete, fixing row commit statuses..."
122129
123- # Create and vacuum empty tables/indexes
130+ # Now vacuum each result database to mark all system-table rows as committed,
131+ # because when pg_log is replaced with the saved version, the transaction
132+ # statuses will no longer match the data. VACUUM will force the on-row
133+ # status flags to the right value so that pg_log will not matter anymore.
134+ # Note: we used to try to do this as part of the previous step, but that
135+ # risks permissions problems if VACUUM is run as the wrong user.
136+ # Note: the initial VACUUM does template1, then we do everything else.
137+
138+ cat$INPUT | awk' BEGIN{ print "VACUUM;" }
139+ {
140+ if (tolower($1) == "copy")
141+ while (getline $0 > 0 && $0 != "\\.")
142+ ;
143+ else if (tolower($1) == "\\connect" &&
144+ $2 != "-" &&
145+ $2 != "template1")
146+ printf "\\connect %s\nVACUUM;\n", $2;
147+ }' > $TMPFILE
124148
125- psql" template1" < " /tmp/ $$ "
149+ psql" template1" < $TMPFILE
126150
127151if [$? -ne 0 ]
128- then echo " There were errors in theinput script $INPUT .
152+ then echo " There were errors in thevacuuming step .
129153$0 aborted." 1>&2
130154exit 1
131155fi
132156
133- echo " Input script $INPUT complete, moving data files..."
157+ echo " Commit fixes complete, moving data files..."
134158
135159for DIR in data/base/*
136160do
@@ -153,4 +177,5 @@ mv -f $OLDDIR/pg_variable data
153177
154178echo " You must stop/start the postmaster before doing anything else."
155179echo " You may remove the$OLDDIR directory with 'rm -r$OLDDIR '."
180+
156181exit 0