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.21 2002/01/10 03:05:48 momjian Exp $
6
+ # $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_upgrade,v 1.22 2002/01/10 04:58:19 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.
@@ -77,51 +77,60 @@ then echo "Cannot read ./$OLDDIR/PG_VERSION --- something is wrong." 1>&2
77
77
fi
78
78
79
79
# Get the actual versions seen in the data dirs.
80
- DESTVERSION =` cat ./data/PG_VERSION`
81
- SRCVERSION =` cat ./$OLDDIR /PG_VERSION`
80
+ DEST_VERSION =` cat ./data/PG_VERSION`
81
+ SRC_VERSION =` cat ./$OLDDIR /PG_VERSION`
82
82
83
83
# Check for version compatibility.
84
84
# This code will need to be updated/reviewed for each new PostgreSQL release.
85
85
86
- # MYVERSION is the expected output database version
87
- MYVERSION =" 7.1"
86
+ # UPGRADE_VERSION is the expected output database version
87
+ UPGRADE_VERSION =" 7.1"
88
88
89
- if [ " $SRCVERSION " = " 7.1" -a ! " $DATA " ]
90
- then echo " $0 requires a full data dump file to upgrade from version $SRCVERSION ." 1>&2
91
- echo " Use the '-d' parameter to specify the dump file" 1>&2
89
+ if [ " $SRC_VERSION " = " 7.1" -a ! " $DATA " ]
90
+ then echo " $0 requires a full data dump file to upgrade from version $SRC_VERSION ." 1>&2
91
+ echo " Use the '-d' parameter to specify the data dump file" 1>&2
92
+ echo " If you don't have enough disk space to keep a dump file, grep out the '\\ connect' and" 1>&2
93
+ echo " 'SELECT setval' lines from the dump file and pass that file to $0 , e.g:" 1>&2
94
+ echo 1>&2
95
+ echo " pg_dumpall | egrep '^(\\ connect)|SELECT setval \()[^ ]*$' > data.out" 1>&2
92
96
exit 1
93
97
fi
94
98
95
- if [ " $DESTVERSION " != " $MYVERSION " -a " $DESTVERSION " != " $SRCVERSION " ]
96
- then echo " $0 is for PostgreSQL version $MYVERSION , but ./data/PG_VERSION contains $DESTVERSION ." 1>&2
97
- echo " Did you run initdb for version $MYVERSION ?" 1>&2
99
+ if [ " $SRC_VERSION " != " 7.1" -a " $DATA " ]
100
+ then echo " $0 does not require the -d option for this version." 1>&2
101
+ exit 1
102
+ fi
103
+
104
+ if [ " $DEST_VERSION " != " $UPGRADE_VERSION " -a " $DEST_VERSION " != " $SRC_VERSION " ]
105
+ then echo " ` basename $0 ` is for PostgreSQL version $UPGRADE_VERSION , but ./data/PG_VERSION contains $DEST_VERSION ." 1>&2
106
+ echo " Did you run initdb for version $UPGRADE_VERSION ?" 1>&2
98
107
exit 1
99
108
fi
100
109
101
110
# Check that input database is of a compatible version (anything with the same
102
111
# physical layout of user tables and indexes should be OK). I did not write
103
- # something like "$SRCVERSION -ge $MINVERSION " because test(1) isn't bright
112
+ # something like "$SRC_VERSION -ge $UPGRADE_VERSION " because test(1) isn't bright
104
113
# enough to compare dotted version strings properly. Using a case statement
105
114
# looks uglier but is more flexible.
106
115
107
- case " $SRCVERSION " in
116
+ case " $SRC_VERSION " in
108
117
# 7.2) ;;
109
- * ) echo " Sorry, ` basename $0 ` cannot upgrade database version $SRCVERSION to $DESTVERSION ." 1>&2
118
+ * ) echo " Sorry, ` basename $0 ` cannot upgrade database version $SRC_VERSION to $DEST_VERSION ." 1>&2
110
119
echo " The on-disk structure of tables has changed." 1>&2
111
- echo " You will need to dump and restore using pg_dump ." 1>&2
120
+ echo " You will need to dump and restore using pg_dumpall ." 1>&2
112
121
exit 1;;
113
122
esac
114
123
115
124
116
- # OK, ready to proceed.
125
+ # Checking done. Ready to proceed.
117
126
118
127
# Execute the schema script to create everything, except modify any
119
128
# sequences with int4 maximums if we are upgrading from 7.1.
120
129
cat $SCHEMA | awk -F' ' ' {
121
- if ("' " $SRCVERSION " ' " == "7.1" &&
130
+ if ("' " $SRC_VERSION " ' " == "7.1" &&
122
131
$1 == "CREATE" &&
123
132
$2 == "SEQUENCE" &&
124
- ($9 >= 2147483646 && # handle OS round
133
+ ($9 >= 2147483646 && # handle OS rounding
125
134
($9 <= 2147483648))
126
135
{
127
136
for(i=1; i < NF; i++)
@@ -134,32 +143,28 @@ cat $SCHEMA | awk -F' ' '{
134
143
else print $0;
135
144
}' |
136
145
psql " template1"
137
-
138
146
if [ $? -ne 0 ]
139
147
then echo " There were errors in the input script $SCHEMA .
140
148
$0 aborted." 1>&2
141
149
exit 1
142
150
fi
143
151
144
-
145
- if [ " $SRCVERSION " != " 7.1" ]
152
+ # Set sequence values for 7.1-version sequences, which are int4.
153
+ if [ " $SRC_VERSION " != " 7.1" ]
146
154
then echo " Input script $SCHEMA complete, fixing row commit statuses..."
147
155
else echo " Input script $SCHEMA complete, setting int8 sequences..."
148
156
149
- # Set all the sequence counters because they are not brought over
150
- # in the schema dump, and the old 7.1 sequences where int4 in size
151
- # so bringing over the file wouldn't help us anyway.
152
- cat $DATA | awk ' $0 == "\\connect " || "SELECT setval (" \
153
- {print $0;}' |
154
- psql " template1"
155
-
156
- if [ $? -ne 0 ]
157
- then echo " There were errors in the input script $SCHEMA .
157
+ # Set all the sequence counters because they are not brought over
158
+ # in the schema dump.
159
+ cat $DATA | egrep ' ^(\\connect)|SELECT setval \()[^ ]*$' |
160
+ psql " template1"
161
+ if [ $? -ne 0 ]
162
+ then echo " There were errors in setting the sequence values.
158
163
$0 aborted." 1>&2
159
- exit 1
160
- fi
164
+ exit 1
165
+ fi
161
166
162
- echo " Int8 sequences set, fixing row commit statuses..."
167
+ echo " Int8 sequences set, fixing row commit statuses..."
163
168
fi
164
169
165
170
# Now vacuum each result database in case our transaction increase
@@ -174,10 +179,16 @@ $0 aborted." 1>&2
174
179
fi
175
180
done
176
181
177
- # should be pretty small file
182
+ # Used for scans looking for a database/tablename match
183
+ # New oid is looked up
178
184
pg_dumpall -s > $TMPFILE 2> /dev/null
185
+ if [ " $? " -ne 0 ]
186
+ then echo " Unable to dump schema of new database.; exiting" 1>&2
187
+ exit 1
188
+ fi
179
189
180
- # flush buffers to disk
190
+ # we are done with SQL database access
191
+ # shutdown forces buffers to disk
181
192
pg_ctl stop
182
193
if [ " $? " -ne 0 ]
183
194
then echo " Unable to stop database server.; exiting" 1>&2
@@ -188,7 +199,7 @@ echo "Commit fixes complete, moving data files..."
188
199
189
200
cat " $SCHEMA " | while read LINE
190
201
do
191
- if /bin/echo " $LINE " | grep -q " ^\\\\ connect [^ ]*$"
202
+ if /bin/echo " $LINE " | grep -q ' ^\\connect [^ ]*$'
192
203
then OLDDB=" $DB "
193
204
DB=" ` /bin/echo \" $LINE \" | cut -d' ' -f2` "
194
205
if [ " $DB " = " -" ]
208
219
then TABLE=" "
209
220
fi
210
221
fi
222
+ # 7.1 sequences were handled earlier because they were int4.
223
+ if test " $SRC_VERSION " ! = " 7.1" &&
224
+ echo " $LINE " | egrep -q " ^-- Name: [^ ]* Type: SEQUENCE "
225
+ then TABLE=" ` echo \" $LINE \" | cut -d' ' -f3` "
226
+ # skip system tables
227
+ if [ " ` echo \" $TABLE \" | cut -c 1-3` " = " pg_" ]
228
+ then TABLE=" "
229
+ fi
230
+ fi
211
231
if [ " $DB " -a " $OID " -a " $TABLE " ]
212
232
then
213
233
NEWOID=` awk -F' ' '
214
- BEGIN { newdb=""; newoid="";
234
+ BEGIN { newdb=""; newoid="";
215
235
newtable=""; ret=0;}
216
236
$1 == "\\\\connect" && $2 != "-" {newdb=$2;}
217
237
$0 ~ /^-- TOC Entry ID [0-9]* .OID / \
218
238
{ newoid = substr($7, 1, length($7)-1);}
219
239
{print $0 >> "/tmp/x";
220
240
print $3 >> "/tmp/x";
221
241
print newdb," ", newoid >> "/tmp/x"}
222
- ($0 ~ /^-- Name: [^ ]* Type: TABLE / && \
223
- $0 ~ /^-- Name: [^ ]* Type: INDEX /) && \
242
+ ($0 ~ /^-- Name: [^ ]* Type: TABLE / || \
243
+ $0 ~ /^-- Name: [^ ]* Type: INDEX / || \
244
+ $0 ~ /^-- Name: [^ ]* Type: SEQUENCE /) && \
224
245
newdb == "' " $DB " ' " && \
225
246
$3 == "' " $TABLE " ' " \
226
247
{ ret=newoid; exit}
227
248
END { print ret;}' $TMPFILE `
228
249
if [ " $NEWOID " -eq 0 ]
229
- then echo " Move of database $DB , OID $OID , table $TABLE failed.\nNew oid not found; exiting" 1>&2
250
+ then echo " Move of database $DB , OID $OID , table $TABLE failed.
251
+ New oid not found; exiting" 1>&2
230
252
exit 1
231
253
fi
232
254
# We use stars so we don't have to worry about database oids
233
255
if [ ` ls " $OLDDIR " /base/* /" $OID " | wc -l` -eq 0 ]
234
- then echo " Move of database $DB , OID $OID , table $TABLE failed.\nFile not found; exiting" 1>&2
256
+ then echo " Move of database $DB , OID $OID , table $TABLE failed.
257
+ File not found; exiting" 1>&2
235
258
exit 1
236
259
fi
237
260
if [ ` ls " $OLDDIR " /base/* /" $OID " | wc -l` -gt 1 ]
238
- then echo " Move of database $DB , OID $OID , table $TABLE failed.\nToo many found; exiting" 1>&2
261
+ then echo " Move of database $DB , OID $OID , table $TABLE failed.
262
+ Too many found; exiting" 1>&2
239
263
exit 1
240
264
fi
241
265
if [ ` ls data/base/* /" $NEWOID " | wc -l` -eq 0 ]
242
- then echo " Move of database $DB , OID $OID , table $TABLE to $NEWOID failed.\nFile not found; exiting" 1>&2
266
+ then echo " Move of database $DB , OID $OID , table $TABLE to $NEWOID failed.
267
+ File not found; exiting" 1>&2
243
268
exit 1
244
269
fi
245
270
if [ ` ls data/base/* /" $NEWOID " | wc -l` -gt 1 ]
246
- then echo " Move of database $DB , OID $OID , table $TABLE to $NEWOID failed.\nToo many found; exiting" 1>&2
271
+ then echo " Move of database $DB , OID $OID , table $TABLE to $NEWOID failed.
272
+ Too many found; exiting" 1>&2
247
273
exit 1
248
274
fi
249
275
mv -f " $OLDDIR " /base/* /" $OID " data/base/* /" $NEWOID "
250
276
if [ " $? " -ne 0 ]
251
- then echo " Move of database $DB , OID $OID , table $TABLE \n to $NEWOID failed.; exiting" 1>&2
277
+ then echo " Move of database $DB , OID $OID , table $TABLE
278
+ to $NEWOID failed.; exiting" 1>&2
252
279
exit 1
253
280
fi
254
281
TABLE=" "
255
282
fi
256
283
done
257
284
258
285
# 7.1 has non-compressed log file format
259
- if [ " $SRCVERSION " = " 7.1" ]
286
+ if [ " $SRC_VERSION " = " 7.1" ]
260
287
then
261
288
# pg_log is oid 1269 in 7.1
262
289
LOGSIZE=` ls -l " $OLDDIR " /global/1269 " $OLDDIR " /global/1269.* 2> /dev/null |
270
297
# set max transaction id
271
298
272
299
else
273
- # how to handle 7.2?
274
300
rm -r data/pg_clog &&
275
301
mv " $OLDDIR " /data/pg_clog data/pg_clog &&
276
302
mv " $OLDDIR " /data/global/pg_control data/global/pg_control
0 commit comments