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/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 $
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.
@@ -30,6 +30,7 @@ CUR_VERSION="7.2"
30
30
trap " rm -f /tmp/$$ .*" 0 1 2 3 15
31
31
32
32
PHASE=" "
33
+
33
34
if [" $# " -eq 1 ]
34
35
then
35
36
if [" X$1 " = " X-1" ]
38
39
elif [" X$1 " = " X-2" ]
39
40
then PHASE=" 2"
40
41
shift
42
+ elif [" X$1 " = " X-D" ]
43
+ then DATADIR=" $2 "
44
+ shift
41
45
fi
42
46
fi
43
47
44
48
if [" $PHASE " = " " ]
45
49
then 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
47
57
exit 1
48
58
fi
49
59
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 .
52
62
$0 aborted." 1>&2
53
63
if [" $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
55
65
fi
56
66
exit 1
57
67
fi
58
68
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
+
59
75
INFODIR=" pg_upgrade_info"
60
- OLDDIR =" $INFODIR /data "
76
+ SAVEDATA =" $INFODIR " / " ` basename \" $DATADIR \" ` "
61
77
62
78
make_dbobjoidmap ()
63
79
{
@@ -99,21 +115,23 @@ move_objfiles()
99
115
{
100
116
# Test to make sure there is a matching file in each place
101
117
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 " ]
103
120
then echo " Move of database$DB , OID$SRC_OID , object$OBJ failed.
104
121
File not found; exiting" 1>&2
105
122
exit 1
106
123
fi
107
124
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 " ]
109
127
then echo " Move of database$DB , OID$DST_OID , object$OBJ failed.
110
128
File not found; exiting" 1>&2
111
129
exit 1
112
130
fi
113
131
114
132
# Move files
115
133
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 "
117
135
if [" $? " -ne 0 ]
118
136
then echo " Move of database$DB , OID$SRC_OID , object$OBJ
119
137
to$DST_OID failed.; exiting" 1>&2
@@ -122,10 +140,10 @@ to $DST_OID failed.; exiting" 1>&2
122
140
123
141
# handle table extents
124
142
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
126
144
do
127
145
EXT=` 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 "
129
147
if [" $? " -ne 0 ]
130
148
then echo " Move of database$DB , OID$SRC_OID , object$OBJ
131
149
to$DST_OID failed.; exiting" 1>&2
@@ -142,15 +160,15 @@ then
142
160
# #########################
143
161
144
162
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
147
165
exit 1
148
166
fi
149
167
150
168
# get version
151
- SRC_VERSION=" ` catdata /PG_VERSION` "
169
+ SRC_VERSION=` cat" $DATADIR " /PG_VERSION`
152
170
if [" $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'.
154
172
$0 aborted." 1>&2
155
173
exit 1
156
174
fi
@@ -258,9 +276,9 @@ $0 aborted." 1>&2
258
276
exit 1
259
277
fi
260
278
261
- mvdata " $INFODIR "
279
+ mv" $DATADIR " " $INFODIR "
262
280
if [$? -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.
264
282
$0 aborted." 1>&2
265
283
exit 1
266
284
fi
@@ -283,38 +301,38 @@ thenecho "There is no '$INFODIR' directory from a phase 1 run of $0." 1>&2
283
301
exit 1
284
302
fi
285
303
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
288
306
exit 1
289
307
fi
290
308
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
293
311
exit 1
294
312
fi
295
313
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
298
316
exit 1
299
317
fi
300
318
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
303
321
echo " Are you running$0 as the postgres superuser?" 1>&2
304
322
exit 1
305
323
fi
306
324
307
325
# Get the actual versions seen in the data dirs.
308
326
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`
311
329
312
330
# Check for version compatibility.
313
331
# This code will need to be updated/reviewed for each new PostgreSQL release.
314
332
315
333
if [" $DST_VERSION " != " $CUR_VERSION " ]
316
334
then 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
318
336
echo " Did you run initdb for version$UPGRADE_VERSION by mistake?" 1>&2
319
337
exit 1
320
338
fi
349
367
# If the XID is > 2 billion, 7.1 database will have non-frozen XID's in
350
368
# low numbers, and 7.2 will think they are in the future --- bad.
351
369
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}' `
353
371
if [" $SRC_VERSION " = " 7.1" -a " $SRC_XID " -gt 2000000000 ]
354
372
then echo " XID too high for$0 .; exiting" 1>&2
355
373
exit 1
356
374
fi
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}' `
358
376
359
377
# compare locales to make sure they match
360
378
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
363
381
if ! diff /tmp/$$ .0 /tmp/$$ .1> /dev/null
364
382
then echo " Locales do not match between the two versions.; exiting" 1>&2
365
383
exit 1
@@ -470,26 +488,26 @@ thenMAX_XID="$SRC_XID"
470
488
else MAX_XID=" $DST_XID "
471
489
fi
472
490
473
- pg_resetxlog -x" $MAX_XID " data
491
+ pg_resetxlog -x" $MAX_XID " " $DATADIR "
474
492
if [" $? " -ne 0 ]
475
493
then echo " Unable to set new XID.; exiting" 1>&2
476
494
exit 1
477
495
fi
478
496
479
497
# Move over old WAL
480
498
481
- rm -rdata /pg_xlog
482
- mv -f" $OLDDIR " /pg_xlogdata
499
+ rm -r" $DATADIR " /pg_xlog
500
+ mv -f" $SAVEDATA " /pg_xlog" $DATADIR "
483
501
484
502
# Set last log file id and segment from old database
485
503
486
- LOG_ID=` pg_resetxlog -n" $OLDDIR " | grep" Current log file id:" |
504
+ LOG_ID=` pg_resetxlog -n" $SAVEDATA " | grep" Current log file id:" |
487
505
awk -F' *' ' {print $5}' `
488
506
if [" $LOG_ID " = " " ]
489
507
then echo " Unable to get old log file id.; exiting" 1>&2
490
508
exit 1
491
509
fi
492
- SEG_ID=` pg_resetxlog -n" $OLDDIR " | grep" Next log file segment:" |
510
+ SEG_ID=` pg_resetxlog -n" $SAVEDATA " | grep" Next log file segment:" |
493
511
awk -F' *' ' {print $5}' `
494
512
if [" $SEG_ID " = " " ]
495
513
then echo " Unable to get old log segment id.; exiting" 1>&2
498
516
499
517
# Set checkpoint location of new database
500
518
501
- pg_resetxlog -l" $LOG_ID " " $SEG_ID " data
519
+ pg_resetxlog -l" $LOG_ID " " $SEG_ID " " $DATADIR "
502
520
if [" $? " -ne 0 ]
503
521
then echo " Unable to set new log file/segment id.; exiting" 1>&2
504
522
exit 1