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.12 2002/02/11 15:19:33 momjian Exp $
6+ # $Header: /cvsroot/pgsql/contrib/pg_upgrade/Attic/pg_upgrade,v 1.13 2002/04/09 18:07:24 momjian Exp $
77#
88# To migrate this to newer versions of PostgreSQL:
99# 1) Update the version numbers at the top of the file
1414# set -x
1515
1616# UPGRADE_VERSION is the expected old database version
17- UPGRADE_VERSION=" 7.1 "
18- CUR_VERSION=" 7.2 "
17+ UPGRADE_VERSION=" 7.2 "
18+ CUR_VERSION=" 7.3 "
1919
2020# Set this to "Y" to enable this program
2121ENABLE=" Y"
7575
7676# Strip off the trailing directory name and store our data there
7777# in the hope we are in the same filesystem so 'mv 'works.
78+
7879INFODIR=` dirname" $PGDATA " ` /pg_upgrade_info
7980SAVEDATA=" $INFODIR " /data
8081
@@ -88,14 +89,8 @@ make_dbobjoidmap()
8889FROM pg_class
8990WHERE relkind = 'r' OR
9091relkind = 'i' OR
91- relkind = 't'"
92- # Don't move over 7.1 int4 sequences; use setval() file.
93- # Sequence XIDs changed from 7.2beta4 to beta5; don't copy them.
94- if [" $SRC_VERSION " != " 7.1" -a \
95- " $SRC_VERSION " != " 7.2" ]
96- then QUERY=" $QUERY OR relkind = 'S';" ;
97- QUERY=" $QUERY ;"
98- fi
92+ relkind = 'S' OR
93+ relkind = 't';"
9994
10095psql -d" $DB " -At -F' ' -c" $QUERY " |
10196while read RELNAME_OID
@@ -213,28 +208,7 @@ then
213208
214209
215210# Dump schema
216- pg_dumpall -s|
217- awk -F' *' '
218- {
219- # Modify sequences with int8 maximums if we are upgrading from 7.1.
220- if ("' " $SRC_VERSION " ' " == "7.1" &&
221- $1 == "CREATE" &&
222- $2 == "SEQUENCE" &&
223- # handle OS rounding
224- $9 >= 2147483646 &&
225- $9 <= 2147483648)
226- {
227- for(i=1; i <= NF; i++)
228- {
229- if (i != 9)
230- printf "%s ", $i;
231- else
232- printf "%s ", "9223372036854775807";
233- }
234- print "";
235- }
236- else print $0;
237- }' > " $INFODIR " /schema
211+ pg_dumpall -s> " $INFODIR " /schema
238212if [$? -ne 0 ]
239213then echo " Can not dump schema. Exiting." 1>&2
240214exit 1
@@ -244,32 +218,6 @@ then
244218make_dboidmap> " $INFODIR " /dboidmap|| exit " $? "
245219make_dbobjoidmap> " $INFODIR " /dbobjoidmap|| exit " $? "
246220
247- # Generate setval() script for 7.1 because it has int4 sequences
248- # Sequence XIDs changed from 7.2beta4 to beta5 so we have to recreate them too.
249- # Not required for 7.2beta5 and later, but it can't hurt.
250- if [" $SRC_VERSION " = " 7.1" -o \
251- " $SRC_VERSION " = " 7.2" ]
252- then
253- psql -d template1 -At -c" SELECT datname FROM pg_database" |
254- grep -v' ^template0$' |
255- while read DB
256- do
257- # We use awk as a portable way to output a backslash
258- awk' BEGIN {print "\\connect' " $DB " ' "}'
259- psql -d" $DB " -At -c"
260- SELECT relname
261- FROM pg_class
262- WHERE relkind = 'S';" |
263- while read SEQUENCE
264- do
265- VALUE=` psql -d" $DB " -At -c" SELECT last_value
266- FROM\" $SEQUENCE \" ;" `
267- echo " SELECT setval ('$SEQUENCE ',$VALUE , true);"
268- done
269- done > " $INFODIR " /setval
270- else rm -f" $INFODIR " /setval
271- fi
272-
273221# Vacuum all databases to remove exipired rows.
274222# We will lose our transaction log file during the upgrade so we
275223# have to do this.
@@ -375,14 +323,7 @@ thenecho "Old version of pg_resetxlog found in path." 1>&2
375323exit 1
376324fi
377325
378- # If the XID is > 2 billion, 7.1 database will have non-frozen XID's in
379- # low numbers, and 7.2 will think they are in the future --- bad.
380-
381326SRC_XID=` pg_resetxlog -n" $SAVEDATA " | grep" NextXID" | awk -F' *' ' {print $4}' `
382- if [" $SRC_VERSION " = " 7.1" -a " $SRC_XID " -gt 2000000000 ]
383- then echo " XID too high for$BASENAME . Exiting." 1>&2
384- exit 1
385- fi
386327DST_XID=` pg_resetxlog -n" $PGDATA " | grep" NextXID" | awk -F' *' ' {print $4}' `
387328
388329# compare locales to make sure they match
418359
419360echo " Input script completed, fixing row commit statuses..."
420361
421- # XXX do we still need this?
422- # Now vacuum each result database because our movement of transaction log
423- # causes some committed transactions to appear as non-committed
424-
425- vacuumdb -a
426- if [$? -ne 0 ]
427- then echo " There were errors during VACUUM. Exiting." 1>&2
428- exit 1
429- fi
430-
431362# Generate mappings for new database
432363make_dboidmap> /tmp/$$ .dboidmap|| exit " $? "
433364make_dbobjoidmap> /tmp/$$ .dbobjoidmap|| exit " $? "
508439rm -r" $PGDATA " /pg_xlog
509440mv -f" $SAVEDATA " /pg_xlog" $PGDATA "
510441
442+ # Move over old clog
443+
444+ rm -r" $PGDATA " /pg_clog
445+ mv -f" $SAVEDATA " /pg_clog" $PGDATA "
446+
511447# Set last log file id and segment from old database
512448
513449LOG_ID=` pg_resetxlog -n" $SAVEDATA " | grep" Current log file id:" |
@@ -539,16 +475,14 @@ thenecho "Unable to restart database server. Exiting." 1>&2
539475exit 1
540476fi
541477
542- # Set sequence values for 7.1-version sequences, which were int4.
478+ # Now that we have moved the WAL/transaction log files, vacuum again to
479+ # mark install rows with fixed transaction ids to prevent problems on xid
480+ # wraparound.
543481
544- if [" $SRC_VERSION " = " 7.1" -o \
545- " $SRC_VERSION " = " 7.2" ]
546- then echo " Set sequence values..."
547- psql -d template1 -At< " $INFODIR " /setval
548- if [$? -ne 0 ]
549- then echo " There were errors during int4 sequence restoration. Exiting." 1>&2
550- exit 1
551- fi
482+ vacuumdb -a
483+ if [$? -ne 0 ]
484+ then echo " There were errors during VACUUM. Exiting." 1>&2
485+ exit 1
552486fi
553487
554488echo