3
3
# pg_upgrade: update a database without needing a full dump/reload cycle.
4
4
# CAUTION: read the manual page before trying to use this!
5
5
6
- # $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_upgrade,v 1.17 2001/08/25 18:52:42 tgl Exp $
6
+ # $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_upgrade,v 1.18 2002/01/09 16:00:46 momjian Exp $
7
7
#
8
8
# NOTE: we must be sure to update the version-checking code a few dozen lines
9
9
# below for each new PostgreSQL release.
@@ -49,13 +49,13 @@ thenecho "You must rename your old data directory to $OLDDIR and run initdb." 1
49
49
exit 1
50
50
fi
51
51
52
- if [! -d " ./data/base/template1 " ]
52
+ if [! -d " ./data/base/1 " ]
53
53
then echo " Cannot find database template1 in ./data/base." 1>&2
54
54
echo " Are you running$0 as the postgres superuser?" 1>&2
55
55
exit 1
56
56
fi
57
57
58
- if [! -d " ./$OLDDIR /base/template1 " ]
58
+ if [! -d " ./$OLDDIR /base/1 " ]
59
59
then echo " There is no database template1 in ./$OLDDIR /base." 1>&2
60
60
exit 1
61
61
fi
@@ -78,9 +78,9 @@ SRCVERSION=`cat ./$OLDDIR/PG_VERSION`
78
78
# This code will need to be updated/reviewed for each new PostgreSQL release.
79
79
80
80
# MYVERSION is the expected output database version
81
- MYVERSION=" 7.0 "
81
+ MYVERSION=" 7.1 "
82
82
83
- if [" $DESTVERSION " != " $MYVERSION " ]
83
+ if [" $DESTVERSION " != " $MYVERSION " -a " $DESTVERSION " != " $SRCVERSION " ]
84
84
then echo " $0 is for PostgreSQL version$MYVERSION , but ./data/PG_VERSION contains$DESTVERSION ." 1>&2
85
85
echo " Did you run initdb for version$MYVERSION ?" 1>&2
86
86
exit 1
93
93
# looks uglier but is more flexible.
94
94
95
95
case " $SRCVERSION " in
96
- # 7.0 ) ;;
96
+ # 7.2 ) ;;
97
97
* )echo " Sorry,` basename$0 ` cannot upgrade database version$SRCVERSION to$DESTVERSION ." 1>&2
98
98
echo " The on-disk structure of tables has changed." 1>&2
99
99
echo " You will need to dump and restore using pg_dump." 1>&2
@@ -114,9 +114,9 @@ cat $INPUT | awk '{
114
114
while (getline $0 > 0 && $0 != "\\.")
115
115
;
116
116
elseprint $0;
117
- }' > $TMPFILE
117
+ }' > $TMPFILE
118
118
119
- psql" template1" < $TMPFILE
119
+ psql" template1" < $TMPFILE
120
120
121
121
if [$? -ne 0 ]
122
122
then echo " There were errors in the input script$INPUT .
@@ -143,36 +143,94 @@ cat $INPUT | awk 'BEGIN{ print "VACUUM;" }
143
143
$2 != "-" &&
144
144
$2 != "template1")
145
145
printf "\\connect %s\nVACUUM;\n", $2;
146
- }' > $TMPFILE
146
+ }' > $TMPFILE
147
147
148
- psql" template1" < $TMPFILE
148
+ psql" template1" < $TMPFILE
149
149
150
150
if [$? -ne 0 ]
151
151
then echo " There were errors in the vacuuming step.
152
152
$0 aborted." 1>&2
153
153
exit 1
154
154
fi
155
155
156
+ # should be pretty small file
157
+ pg_dumpall -s> $TMPFILE 2> /dev/null
158
+
159
+ # flush buffers to disk
160
+ pg_ctl stop
161
+
156
162
echo " Commit fixes complete, moving data files..."
157
163
158
- for DIR in data/base/ *
164
+ cat " $INPUT " | while read LINE
159
165
do
160
- BASEDIR=" ` basename$DIR ` "
161
- if [-d " $DIR " -a \
162
- -d " $OLDDIR /base/$BASEDIR " -a \( " $BASEDIR " != " template1" \) ]
163
- then for FILE in $OLDDIR /base/$BASEDIR /*
164
- do
165
- BASEFILE=" ` basename$FILE ` "
166
- if [` expr" $BASEFILE " : " pg_" ` -ne 3-a \
167
- " $BASEFILE " != " PG_VERSION" ]
168
- then mv -f$FILE $DIR
169
- fi
170
- done
166
+ if /bin/echo" $LINE " | grep -q" ^\\\\ connect"
167
+ then OLDDB=" $DB "
168
+ DB=" ` /bin/echo\" $LINE \" | cut -d' ' -f2` "
169
+ if [" $DB " = " -" ]
170
+ then DB=" $OLDDB "
171
+ fi
172
+ if [" $DB " = " template1" -o " $DB " = " template0" ]
173
+ then DB=" "
174
+ fi
175
+ fi
176
+ if echo " $LINE " | grep -q" ^-- TOC Entry ID [0-9]* (OID"
177
+ then OID=" ` echo\" $LINE \" | cut -d' ' -f7| tr -d' )' ` "
178
+ fi
179
+ if echo " $LINE " | grep -q" ^-- Name: [^ ]* Type: TABLE"
180
+ then TABLE=" ` echo\" $LINE \" | cut -d' ' -f3` "
181
+ # skip system tables
182
+ if [" ` echo\" $TABLE \" | cut -c 1-3` " = " pg_" ]
183
+ then TABLE=" "
184
+ fi
185
+ fi
186
+ if [" $DB " -a " $OID " -a " $TABLE " ]
187
+ then
188
+ NEWOID=` awk -F' ' '
189
+ BEGIN { newdb=""; newoid="";
190
+ newtable=""; ret=0;}
191
+ $1 == "\\\\connect" && $2 != "-" {newdb=$2;}
192
+ $0 ~ /^-- TOC Entry ID [0-9]* .OID / \
193
+ { newoid = substr($7, 1, length($7)-1);}
194
+ {print $0 >> "/tmp/x";
195
+ print $3 >> "/tmp/x";
196
+ print newdb," ", newoid >> "/tmp/x"}
197
+ $0 ~ /^-- Name: [^ ]* Type: TABLE / && \
198
+ newdb == "' " $DB " ' " && \
199
+ $3 == "' " $TABLE " ' " \
200
+ { ret=newoid; exit}
201
+ END { print ret;}' $TMPFILE `
202
+ if [" $NEWOID " -eq 0 ]
203
+ then echo " Move of database$DB , OID$OID , table$TABLE failed.\nNew oid not found; exiting" 1>&2
204
+ exit 1
205
+ fi
206
+ # We use stars so we don't have to worry about database oids
207
+ if [` ls" $OLDDIR " /base/* /" $OID " | wc -l` -eq 0 ]
208
+ then echo " Move of database$DB , OID$OID , table$TABLE failed.\nFile not found; exiting" 1>&2
209
+ exit 1
210
+ fi
211
+ if [` ls" $OLDDIR " /base/* /" $OID " | wc -l` -gt 1 ]
212
+ then echo " Move of database$DB , OID$OID , table$TABLE failed.\nToo many found; exiting" 1>&2
213
+ exit 1
214
+ fi
215
+ if [` ls data/base/* /" $NEWOID " | wc -l` -eq 0 ]
216
+ then echo " Move of database$DB , OID$OID , table$TABLE to$NEWOID failed.\nFile not found; exiting" 1>&2
217
+ exit 1
218
+ fi
219
+ if [` ls data/base/* /" $NEWOID " | wc -l` -gt 1 ]
220
+ then echo " Move of database$DB , OID$OID , table$TABLE to$NEWOID failed.\nToo many found; exiting" 1>&2
221
+ exit 1
222
+ fi
223
+ mv -f" $OLDDIR " /base/* /" $OID " data/base/* /" $NEWOID "
224
+ if [" $? " -ne 0 ]
225
+ then echo " Move of database$DB , OID$OID , table$TABLE \n to$NEWOID failed.; exiting" 1>&2
226
+ exit 1
227
+ fi
228
+ TABLE=" "
171
229
fi
172
230
done
173
231
232
+ rm -r data/pg_clog
174
233
mv -f$OLDDIR /pg_clog data
175
- mv -f$OLDDIR /pg_variable data
176
234
177
235
echo " You must stop/start the postmaster before doing anything else."
178
236
echo " You may remove the$OLDDIR directory with 'rm -r$OLDDIR '."