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.1 2002/01/1417:32:07 momjian Exp $
6+ # $Header: /cvsroot/pgsql/contrib/pg_upgrade/Attic/pg_upgrade,v 1.2 2002/01/1422:54:44 momjian 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.
@@ -30,6 +30,7 @@ CUR_VERSION="7.2"
3030trap " rm -f /tmp/$$ .*" 0 1 2 3 15
3131
3232PHASE=" "
33+
3334if [" $# " -eq 1 ]
3435then
3536if [" X$1 " = " X-1" ]
3839elif [" X$1 " = " X-2" ]
3940then PHASE=" 2"
4041shift
42+ elif [" X$1 " = " X-D" ]
43+ then DATADIR=" $2 "
44+ shift
4145fi
4246fi
4347
4448if [" $PHASE " = " " ]
4549then echo " You must run$0 in either mode 1 or mode 2." 1>&2
46- echo " Usage:$0 [ -1 | -2 ]" 1>&2
50+ echo " Usage:$0 [-D datadir] -1 | -2" 1>&2
51+ exit 1
52+ fi
53+
54+ if [" $DATADIR " = " " ]
55+ then echo " You must set the DATADIR environment variable or specify it with -D." 1>&2
56+ echo " Usage:$0 [-D datadir] -1 | -2" 1>&2
4757exit 1
4858fi
4959
50- if [! -e data ]
51- then echo " $0 must be run from the directory above your /data directory .
60+ if [! -d " $DATADIR " ]
61+ then echo " $DATADIR does not exist .
5262$0 aborted." 1>&2
5363if [" $PHASE " -eq 2 ]
54- then echo " You must run initdb to create a template1 database ." 1>&2
64+ then echo " Perhaps you didn't run initdb." 1>&2
5565fi
5666exit 1
5767fi
5868
69+ if [" $USER " = " root" -o ! -r " $DATADIR " /PG_VERSION ]
70+ then echo " You must run this as the PostgreSQL superuser.
71+ $0 aborted." 1>&2
72+ exit 1
73+ fi
74+
5975INFODIR=" pg_upgrade_info"
60- OLDDIR =" $INFODIR /data "
76+ SAVEDATA =" $INFODIR " / " ` basename \" $DATADIR \" ` "
6177
6278make_dbobjoidmap ()
6379{
@@ -99,21 +115,23 @@ move_objfiles()
99115{
100116# Test to make sure there is a matching file in each place
101117
102- if [! -e " $OLDDIR " /base/" $SRC_DBOID " /" $SRC_OID " ]
118+ if [! -f " $SAVEDATA " /base/" $SRC_DBOID " /" $SRC_OID " -a \
119+ ! -h " $SAVEDATA " /base/" $SRC_DBOID " /" $SRC_OID " ]
103120then echo " Move of database$DB , OID$SRC_OID , object$OBJ failed.
104121File not found; exiting" 1>&2
105122exit 1
106123fi
107124
108- if [! -e data/base/" $DST_DBOID " /" $DST_OID " ]
125+ if [! -f " $DATADIR " /base/" $DST_DBOID " /" $DST_OID " -a \
126+ ! -h " $DATADIR " /base/" $DST_DBOID " /" $DST_OID " ]
109127then echo " Move of database$DB , OID$DST_OID , object$OBJ failed.
110128File not found; exiting" 1>&2
111129exit 1
112130fi
113131
114132# Move files
115133
116- mv -f" $OLDDIR " /base/" $SRC_DBOID " /" $SRC_OID " data /base/" $DST_DBOID " /" $DST_OID "
134+ mv -f" $SAVEDATA " /base/" $SRC_DBOID " /" $SRC_OID " " $DATADIR " /base/" $DST_DBOID " /" $DST_OID "
117135if [" $? " -ne 0 ]
118136then echo " Move of database$DB , OID$SRC_OID , object$OBJ
119137to$DST_OID failed.; exiting" 1>&2
@@ -122,10 +140,10 @@ to $DST_OID failed.; exiting" 1>&2
122140
123141# handle table extents
124142
125- ls" $OLDDIR " /base/" $SRC_DBOID " /" $SRC_OID " .* 2> /dev/null| while read FILE
143+ ls" $SAVEDATA " /base/" $SRC_DBOID " /" $SRC_OID " .* 2> /dev/null| while read FILE
126144do
127145EXT=` basename" $FILE " | sed' s/^.*\.\(.*\)$/\1/' `
128- mv -f" $FILE " data /base/" $DST_DBOID " /" $DST_OID " ." $EXT "
146+ mv -f" $FILE " " $DATADIR " /base/" $DST_DBOID " /" $DST_OID " ." $EXT "
129147if [" $? " -ne 0 ]
130148then echo " Move of database$DB , OID$SRC_OID , object$OBJ
131149to$DST_OID failed.; exiting" 1>&2
@@ -142,15 +160,15 @@ then
142160# #########################
143161
144162
145- if [! -d data /base/1 ]
146- then echo " There is no database template1 indata /base." 1>&2
163+ if [! -d " $DATADIR " /base/1 ]
164+ then echo " There is no database template1 in$DATADIR /base." 1>&2
147165exit 1
148166fi
149167
150168# get version
151- SRC_VERSION=" ` catdata /PG_VERSION` "
169+ SRC_VERSION=` cat" $DATADIR " /PG_VERSION`
152170if [" $SRC_VERSION " = " " ]
153- then echo " $0 can not find PostgreSQL version file 'data /PG_VERSION'.
171+ then echo " $0 can not find PostgreSQL version file '$DATADIR /PG_VERSION'.
154172$0 aborted." 1>&2
155173exit 1
156174fi
@@ -258,9 +276,9 @@ $0 aborted." 1>&2
258276exit 1
259277fi
260278
261- mvdata " $INFODIR "
279+ mv" $DATADIR " " $INFODIR "
262280if [$? -ne 0 ]
263- then echo " Can not move old /data out of the way.
281+ then echo " Can not move old /$DATADIR out of the way.
264282$0 aborted." 1>&2
265283exit 1
266284fi
@@ -283,38 +301,38 @@ thenecho "There is no '$INFODIR' directory from a phase 1 run of $0." 1>&2
283301exit 1
284302fi
285303
286- if [! -e " $OLDDIR " ]
287- then echo " There is no '$OLDDIR ' directory from the phase 1 run of$0 ." 1>&2
304+ if [! -d " $SAVEDATA " ]
305+ then echo " There is no '$SAVEDATA ' directory from the phase 1 run of$0 ." 1>&2
288306exit 1
289307fi
290308
291- if [! -f " $OLDDIR /PG_VERSION" ]
292- then echo " Cannot read '$OLDDIR /PG_VERSION' --- something is wrong." 1>&2
309+ if [! -f " $SAVEDATA /PG_VERSION" ]
310+ then echo " Cannot read '$SAVEDATA /PG_VERSION' --- something is wrong." 1>&2
293311exit 1
294312fi
295313
296- if [! -f " data /PG_VERSION" ]
297- then echo " Cannot read 'data /PG_VERSION' --- something is wrong." 1>&2
314+ if [! -f " $DATADIR /PG_VERSION" ]
315+ then echo " Cannot read '$DATADIR /PG_VERSION' --- something is wrong." 1>&2
298316exit 1
299317fi
300318
301- if [! -d " data /base/1" ]
302- then echo " Cannot find database template1 in 'data /base'." 1>&2
319+ if [! -d " $DATADIR /base/1" ]
320+ then echo " Cannot find database template1 in '$DATADIR /base'." 1>&2
303321echo " Are you running$0 as the postgres superuser?" 1>&2
304322exit 1
305323fi
306324
307325# Get the actual versions seen in the data dirs.
308326
309- SRC_VERSION=` cat" $OLDDIR " /PG_VERSION`
310- DST_VERSION=` catdata /PG_VERSION`
327+ SRC_VERSION=` cat" $SAVEDATA " /PG_VERSION`
328+ DST_VERSION=` cat" $DATADIR " /PG_VERSION`
311329
312330# Check for version compatibility.
313331# This code will need to be updated/reviewed for each new PostgreSQL release.
314332
315333if [" $DST_VERSION " != " $CUR_VERSION " ]
316334then echo " $0 is for PostgreSQL version$CUR_VERSION
317- butdata /PG_VERSION contains$DST_VERSION ." 1>&2
335+ but$DATADIR /PG_VERSION contains$DST_VERSION ." 1>&2
318336echo " Did you run initdb for version$UPGRADE_VERSION by mistake?" 1>&2
319337exit 1
320338fi
349367# If the XID is > 2 billion, 7.1 database will have non-frozen XID's in
350368# low numbers, and 7.2 will think they are in the future --- bad.
351369
352- SRC_XID=` pg_resetxlog -n" $OLDDIR " | grep" NextXID" | awk -F' *' ' {print $4}' `
370+ SRC_XID=` pg_resetxlog -n" $SAVEDATA " | grep" NextXID" | awk -F' *' ' {print $4}' `
353371if [" $SRC_VERSION " = " 7.1" -a " $SRC_XID " -gt 2000000000 ]
354372then echo " XID too high for$0 .; exiting" 1>&2
355373exit 1
356374fi
357- DST_XID=` pg_resetxlog -ndata | grep" NextXID" | awk -F' *' ' {print $4}' `
375+ DST_XID=` pg_resetxlog -n" $DATADIR " | grep" NextXID" | awk -F' *' ' {print $4}' `
358376
359377# compare locales to make sure they match
360378
361- pg_resetxlog -n" $OLDDIR " | grep" ^LC_" > /tmp/$$ .0
362- pg_resetxlog -ndata | grep" ^LC_" > /tmp/$$ .1
379+ pg_resetxlog -n" $SAVEDATA " | grep" ^LC_" > /tmp/$$ .0
380+ pg_resetxlog -n" $DATADIR " | grep" ^LC_" > /tmp/$$ .1
363381if ! diff /tmp/$$ .0 /tmp/$$ .1> /dev/null
364382then echo " Locales do not match between the two versions.; exiting" 1>&2
365383exit 1
@@ -470,26 +488,26 @@ thenMAX_XID="$SRC_XID"
470488else MAX_XID=" $DST_XID "
471489fi
472490
473- pg_resetxlog -x" $MAX_XID " data
491+ pg_resetxlog -x" $MAX_XID " " $DATADIR "
474492if [" $? " -ne 0 ]
475493then echo " Unable to set new XID.; exiting" 1>&2
476494exit 1
477495fi
478496
479497# Move over old WAL
480498
481- rm -rdata /pg_xlog
482- mv -f" $OLDDIR " /pg_xlogdata
499+ rm -r" $DATADIR " /pg_xlog
500+ mv -f" $SAVEDATA " /pg_xlog" $DATADIR "
483501
484502# Set last log file id and segment from old database
485503
486- LOG_ID=` pg_resetxlog -n" $OLDDIR " | grep" Current log file id:" |
504+ LOG_ID=` pg_resetxlog -n" $SAVEDATA " | grep" Current log file id:" |
487505awk -F' *' ' {print $5}' `
488506if [" $LOG_ID " = " " ]
489507then echo " Unable to get old log file id.; exiting" 1>&2
490508exit 1
491509fi
492- SEG_ID=` pg_resetxlog -n" $OLDDIR " | grep" Next log file segment:" |
510+ SEG_ID=` pg_resetxlog -n" $SAVEDATA " | grep" Next log file segment:" |
493511awk -F' *' ' {print $5}' `
494512if [" $SEG_ID " = " " ]
495513then echo " Unable to get old log segment id.; exiting" 1>&2
498516
499517# Set checkpoint location of new database
500518
501- pg_resetxlog -l" $LOG_ID " " $SEG_ID " data
519+ pg_resetxlog -l" $LOG_ID " " $SEG_ID " " $DATADIR "
502520if [" $? " -ne 0 ]
503521then echo " Unable to set new log file/segment id.; exiting" 1>&2
504522exit 1