1- #! /bin/sh
1+ z #!/bin/sh
22#
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/contrib/pg_upgrade/Attic/pg_upgrade,v 1.3 2002/01/15 04:05:24 momjian Exp $
6+ # $Header: /cvsroot/pgsql/contrib/pg_upgrade/Attic/pg_upgrade,v 1.4 2002/01/15 04:50:37 momjian Exp $
77#
8- # NOTE: we must be sure to update the version-checking code a few dozen lines
9- # below for each new PostgreSQL release.
8+ # To migrate this to newer versions of PostgreSQL:
9+ #1) Update the version numbers at the top of the file
10+ #2) Search for specific version mentions in the script and update
11+ # accordingly.
12+ #3) Add changes for next version.
1013
1114#set -x
1215
@@ -15,8 +18,7 @@ ENABLE="Y"
1518
1619if [ "$ENABLE" != "Y" ]
1720then
18- echo " Sorry,$0 cannot upgrade database
19- version$SRC_VERSION to$DST_VERSION ." 1>&2
21+ echo "Sorry, $0 cannot upgrade database version $SRC_VERSION to $DST_VERSION." 1>&2
2022echo "The on-disk structure of tables has changed." 1>&2
2123echo "You will need to dump and restore using pg_dumpall." 1>&2
2224exit 1
@@ -59,17 +61,15 @@ thenecho "You must set the PGDATA environment variable or specify it with -D."
5961fi
6062
6163if [ ! -d "$PGDATA" ]
62- then echo " $PGDATA does not exist.
63- $BASENAME aborted." 1>&2
64+ thenecho "$PGDATA does not exist. Exiting." 1>&2
6465if [ "$PHASE" -eq 2 ]
6566thenecho "Perhaps you didn't run initdb." 1>&2
6667fi
6768exit 1
6869fi
6970
7071if [ "$USER" = "root" -o ! -r "$PGDATA"/PG_VERSION ]
71- then echo " You must run this as the PostgreSQL superuser.
72- $BASENAME aborted." 1>&2
72+ thenecho "You must run this as the PostgreSQL superuser. Exiting." 1>&2
7373exit 1
7474fi
7575
@@ -120,24 +120,24 @@ move_objfiles()
120120
121121if [ ! -f "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" -a \
122122 ! -h "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" ]
123- then echo " Move of database$DB , OID$SRC_OID , object$OBJ failed.
124- File not found; exiting " 1>&2
123+ thenecho "Moving of database $DB, OID $SRC_OID, object $OBJ failed." 1>&2
124+ echo " File not found. Exiting. " 1>&2
125125return 1
126126fi
127127
128128if [ ! -f "$PGDATA"/base/"$DST_DBOID"/"$DST_OID" -a \
129129 ! -h "$PGDATA"/base/"$DST_DBOID"/"$DST_OID" ]
130- then echo " Move of database$DB , OID$DST_OID , object$OBJ failed.
131- File not found; exiting " 1>&2
130+ thenecho "Moving of database $DB, OID $DST_OID, object $OBJ failed." 1>&2
131+ echo " File not found. Exiting. " 1>&2
132132return 1
133133fi
134134
135135# Move files
136136
137137mv -f "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" "$PGDATA"/base/"$DST_DBOID"/"$DST_OID"
138138if [ "$?" -ne 0 ]
139- then echo " Move of database$DB , OID$SRC_OID , object$OBJ
140- to$DST_OID failed.; exiting " 1>&2
139+ thenecho "Moving of database $DB, OID $SRC_OID, object $OBJ" 1>&2
140+ echo " to $DST_OID failed. Exiting " 1>&2
141141return 1
142142fi
143143
@@ -148,8 +148,8 @@ to $DST_OID failed.; exiting" 1>&2
148148EXT=`basename "$FILE" | sed 's/^.*\.\(.*\)$/\1/'`
149149mv -f "$FILE" "$PGDATA"/base/"$DST_DBOID"/"$DST_OID"."$EXT"
150150if [ "$?" -ne 0 ]
151- then echo " Move of database$DB , OID$SRC_OID , object$OBJ
152- to$DST_OID failed.; exiting " 1>&2
151+ thenecho "Moving of database $DB, OID $SRC_OID, object $OBJ" 1>&2
152+ echo " to $DST_OID failed. Exiting. " 1>&2
153153return 1
154154fi
155155done
@@ -171,45 +171,43 @@ then
171171# get version
172172SRC_VERSION=`cat "$PGDATA"/PG_VERSION`
173173if [ "$SRC_VERSION" = "" ]
174- then echo " $BASENAME can not find PostgreSQL version file ' $PGDATA /PG_VERSION'.
175- $BASENAME aborted ." 1>&2
174+ thenecho "$BASENAME can not findthe PostgreSQL version file" 1>&2
175+ echo "'$PGDATA/PG_VERSION'. Exiting ." 1>&2
176176exit 1
177177fi
178178
179179if [ "$SRC_VERSION" != "$CUR_VERSION" -a \
180180"$SRC_VERSION" != "$UPGRADE_VERSION" ]
181181thenecho "$BASENAME supports versions $UPGRADE_VERSION and $CUR_VERSION only." 1>&2
182- echo " However, your database is version$SRC_VERSION ;
183- $BASENAME aborted." 1>&2
184- echo " You will need to dump and restore using pg_dumpall." 1>&2
182+ echo "However, your database is version $SRC_VERSION." 1>&2
183+ echo "You will need to dump and restore using pg_dumpall. Exiting." 1>&2
185184exit 1
186185fi
187186
188187# Start server, if needed, so we can do some work.
189188if ! pg_ctl status | sed -n '1p' | grep "is running" > /dev/null 2>&1
190189thenpg_ctl -w start
191190if [ $? -ne 0 ]
192- then echo " Can not start server.
193- $BASENAME aborted." 1>&2
191+ thenecho "Can not start server. Exiting." 1>&2
194192exit 1
195193fi
196194fi
197195
198196# create directory for our data
199197if ! rm -rf "$INFODIR"
200- then echo " Deletion of old pg_upgrade_info directoryfailed, $INFODIR .
201- $BASENAME aborted ." 1>&2
198+ thenecho "Deletion of old pg_upgrade_info directory $INFODIR failed." 1>&2
199+ echo "Exiting ." 1>&2
202200exit 1
203201fi
204202if ! mkdir "$INFODIR"
205- then echo " Creation of new pg_upgrade_info directoryfailed, $INFODIR .
206- $BASENAME aborted ." 1>&2
203+ thenecho "Creation of new pg_upgrade_info directory $INFODIR failed." 1>&2
204+ echo "Exiting ." 1>&2
207205exit 1
208206fi
209207
210208if ! chmod og-rwx "$INFODIR"
211- then echo " Permission change on new pg_upgrade_info directoryfailed, $INFODIR .
212- $BASENAME aborted ." 1>&2
209+ thenecho "Permission change on new pg_upgrade_info directory $INFODIR failed." 1>&2
210+ echo "Exiting ." 1>&2
213211exit 1
214212fi
215213
@@ -238,8 +236,7 @@ $BASENAME aborted." 1>&2
238236else print $0;
239237}' > "$INFODIR"/schema
240238if [ $? -ne 0 ]
241- then echo " Can not dump schema.
242- $BASENAME aborted." 1>&2
239+ thenecho "Can not dump schema. Exiting." 1>&2
243240exit 1
244241fi
245242
@@ -278,30 +275,27 @@ $BASENAME aborted." 1>&2
278275
279276vacuumdb -a
280277if [ $? -ne 0 ]
281- then echo " Can not vacuum server.
282- $BASENAME aborted." 1>&2
278+ thenecho "Can not vacuum server. Exiting." 1>&2
283279exit 1
284280fi
285281
286282# Stop server so we can move the directory.
287283pg_ctl -w stop
288284if [ $? -ne 0 ]
289- then echo " Can not stop server.
290- $BASENAME aborted." 1>&2
285+ thenecho "Can not stop server. Exiting." 1>&2
291286exit 1
292287fi
293288
294289# No matter what the directory name, call it data
295290mv "$PGDATA" "$INFODIR"/data
296291if [ $? -ne 0 ]
297- then echo " Can not move old /$PGDATA out of the way.
298- $BASENAME aborted." 1>&2
292+ thenecho "Can not move old $PGDATA out of the way. Exiting." 1>&2
299293exit 1
300294fi
301295echo
302296echo
303- echo " Plase 1 completed.
304- Continue with the steps outlined in the$BASENAME manual page."
297+ echo "Plase 1 completed."
298+ echo " Continue with the steps outlined in the $BASENAME manual page."
305299exit 0
306300fi
307301
313307# check things
314308
315309if [ ! -d "$INFODIR" ]
316- then echo " There is no '$INFODIR ' directory froma phase 1 run of$BASENAME ." 1>&2
310+ thenecho "There is no '$INFODIR' directory fromthe phase 1 run of $BASENAME." 1>&2
317311exit 1
318312fi
319313
@@ -347,8 +341,8 @@ DST_VERSION=`cat "$PGDATA"/PG_VERSION`
347341# This code will need to be updated/reviewed for each new PostgreSQL release.
348342
349343if [ "$DST_VERSION" != "$CUR_VERSION" ]
350- then echo " $BASENAME is for PostgreSQL version$CUR_VERSION
351- but$PGDATA /PG_VERSION contains$DST_VERSION ." 1>&2
344+ thenecho "$BASENAME is for PostgreSQL version $CUR_VERSION" 1>&2
345+ echo " but $PGDATA/PG_VERSION contains $DST_VERSION." 1>&2
352346echo "Did you run initdb for version $UPGRADE_VERSION by mistake?" 1>&2
353347exit 1
354348fi
358352if pg_ctl status | sed -n '1p' | grep "is running" > /dev/null 2>&1
359353thenpg_ctl -w stop
360354if [ $? -ne 0 ]
361- then echo " Can no start server.
362- $BASENAME aborted." 1>&2
355+ thenecho "Can no start server. Exiting." 1>&2
363356exit 1
364357fi
365358fi
369362pg_resetxlog 2> /dev/null
370363# file not found status is normally 127, not 1
371364if [ "$?" -ne 1 ]
372- then echo " Unable to find pg_resetxlog in path.
373- Install it from pgsql/contrib/pg_resetxlog and continue.; exiting " 1>&2
365+ thenecho "Unable to find pg_resetxlog inyour path." 1>&2
366+ echo " Install it from pgsql/contrib/pg_resetxlog and continue. Exiting. " 1>&2
374367exit 1
375368fi
376369
377370if ! pg_resetxlog -x 2>&1 | grep 'xid' > /dev/null 2>&1
378- then echo " Old version of pg_resetxlog found in path.
379- Install a newer version from pgsql/contrib/pg_resetxlog.; exiting" 1>&2
371+ thenecho "Old version of pg_resetxlog found in path." 1>&2
372+ echo "Install a newer version of pg_resetxlog from pgsql/contrib/pg_resetxlog." 1>&2
373+ echo "Exiting." 1>&2
380374exit 1
381375fi
382376
385379
386380SRC_XID=`pg_resetxlog -n "$SAVEDATA" | grep "NextXID" | awk -F' *' '{print $4}'`
387381if [ "$SRC_VERSION" = "7.1" -a "$SRC_XID" -gt 2000000000 ]
388- then echo " XID too high for$BASENAME .; exiting " 1>&2
382+ thenecho "XID too high for $BASENAME. Exiting. " 1>&2
389383exit 1
390384fi
391385DST_XID=`pg_resetxlog -n "$PGDATA" | grep "NextXID" | awk -F' *' '{print $4}'`
@@ -395,16 +389,15 @@ DST_XID=`pg_resetxlog -n "$PGDATA" | grep "NextXID" | awk -F' *' '{print $4}'`
395389pg_resetxlog -n "$SAVEDATA" | grep "^LC_" > /tmp/$$.0
396390pg_resetxlog -n "$PGDATA" | grep "^LC_" > /tmp/$$.1
397391if ! diff /tmp/$$.0 /tmp/$$.1 > /dev/null
398- then echo " Locales do not match between the two versions.; exiting " 1>&2
392+ thenecho "Locales do not match between the two versions. Exiting. " 1>&2
399393exit 1
400394fi
401395
402396# Restart postmaster
403397
404398pg_ctl -w start
405399if [ $? -ne 0 ]
406- then echo " Can not start server.
407- $BASENAME aborted." 1>&2
400+ thenecho "Can not start server. Exiting." 1>&2
408401exit 1
409402fi
410403
418411
419412psql template1 < "$INFODIR"/schema
420413if [ $? -ne 0 ]
421- then echo " There were errors in the input script.
422- $BASENAME aborted." 1>&2
414+ thenecho "There were errors in the input script. Exiting." 1>&2
423415exit 1
424416fi
425417
426- echo " Input scriptcomplete , fixing row commit statuses..."
418+ echo "Input scriptcompleted , fixing row commit statuses..."
427419
428420# XXX do we still need this?
429421# Now vacuum each result database because our movement of transaction log
430422# causes some committed transactions to appear as non-committed
431423
432424vacuumdb -a
433425if [ $? -ne 0 ]
434- then echo " There were errors during VACUUM.
435- $BASENAME aborted." 1>&2
426+ thenecho "There were errors during VACUUM. Exiting." 1>&2
436427exit 1
437428fi
438429
@@ -445,7 +436,7 @@ make_dbobjoidmap > /tmp/$$.dbobjoidmap || exit "$?"
445436
446437pg_ctl -w stop
447438if [ "$?" -ne 0 ]
448- then echo " Unable to stop database server.; exiting " 1>&2
439+ thenecho "Unable to stop database server. Exiting. " 1>&2
449440exit 1
450441fi
451442
507498
508499pg_resetxlog -x "$MAX_XID" "$PGDATA"
509500if [ "$?" -ne 0 ]
510- then echo " Unable to set new XID.; exiting " 1>&2
501+ thenecho "Unable to set new XID. Exiting. " 1>&2
511502exit 1
512503fi
513504
@@ -521,29 +512,29 @@ mv -f "$SAVEDATA"/pg_xlog "$PGDATA"
521512LOG_ID=`pg_resetxlog -n "$SAVEDATA" | grep "Current log file id:" |
522513awk -F' *' '{print $5}'`
523514if [ "$LOG_ID" = "" ]
524- then echo " Unable to get old log file id.; exiting " 1>&2
515+ thenecho "Unable to get old log file id. Exiting. " 1>&2
525516exit 1
526517fi
527518SEG_ID=`pg_resetxlog -n "$SAVEDATA" | grep "Next log file segment:" |
528519awk -F' *' '{print $5}'`
529520if [ "$SEG_ID" = "" ]
530- then echo " Unable to get old log segment id.; exiting " 1>&2
521+ thenecho "Unable to get old log segment id. Exiting. " 1>&2
531522exit 1
532523fi
533524
534525# Set checkpoint location of new database
535526
536527pg_resetxlog -l "$LOG_ID" "$SEG_ID" "$PGDATA"
537528if [ "$?" -ne 0 ]
538- then echo " Unable to set new log file/segment id.; exiting " 1>&2
529+ thenecho "Unable to set new log file/segment id. Exiting. " 1>&2
539530exit 1
540531fi
541532
542533# Restart server with moved data
543534
544535pg_ctl -w start
545536if [ "$?" -ne 0 ]
546- then echo " Unable to restart database server.; exiting " 1>&2
537+ thenecho "Unable to restart database server. Exiting. " 1>&2
547538exit 1
548539fi
549540
@@ -554,14 +545,12 @@ if [ "$SRC_VERSION" = "7.1" -o \
554545thenecho "Set sequence values..."
555546psql -d template1 -At < "$INFODIR"/setval
556547if [ $? -ne 0 ]
557- then echo " There were errors during int4 sequence restore.
558- $BASENAME aborted." 1>&2
548+ thenecho "There were errors during int4 sequence restore. Exiting." 1>&2
559549exit 1
560550fi
561551fi
562552
563553echo
564554echo
565555echo "You may remove the old database files with 'rm -r $INFODIR'."
566-
567556exit 0