head	1.17;
access;
symbols
	REL7_4_29:1.15
	REL8_0_25:1.16
	REL7_4_28:1.15
	REL8_0_24:1.16
	REL7_4_27:1.15
	REL8_0_23:1.16
	REL7_4_26:1.15
	REL8_0_22:1.16
	REL7_4_25:1.15
	REL8_0_21:1.16
	REL7_4_24:1.15
	REL8_0_20:1.16
	REL7_4_23:1.15
	REL8_0_19:1.16
	REL7_4_22:1.15
	REL8_0_18:1.16
	REL7_4_21:1.15
	REL8_0_17:1.16
	REL7_4_20:1.15
	REL8_0_16:1.16
	REL7_3_21:1.15
	REL7_4_19:1.15
	REL8_0_15:1.16
	REL7_3_20:1.15
	REL7_4_18:1.15
	REL8_0_14:1.16
	REL7_3_19:1.15
	REL7_4_17:1.15
	REL8_0_13:1.16
	REL8_0_12:1.16
	REL7_3_18:1.15
	REL7_4_16:1.15
	REL8_0_11:1.16
	REL8_0_10:1.16
	REL7_4_15:1.15
	REL7_3_17:1.15
	REL8_0_9:1.16
	REL7_4_14:1.15
	REL7_3_16:1.15
	REL7_3_15:1.15
	REL7_4_13:1.15
	REL8_0_8:1.16
	REL7_3_14:1.15
	REL7_4_12:1.15
	REL8_0_7:1.16
	REL7_3_13:1.15
	REL7_4_11:1.15
	REL8_0_6:1.16
	REL7_3_12:1.15
	REL7_4_10:1.15
	REL8_0_5:1.16
	REL7_3_11:1.15
	REL7_4_9:1.15
	REL8_0_4:1.16
	REL7_2_8:1.12
	REL7_3_10:1.15
	REL7_4_8:1.15
	REL8_0_3:1.16
	REL8_0_2:1.16
	REL7_2_7:1.12
	REL7_3_9:1.15
	REL7_4_7:1.15
	REL8_0_1:1.16
	REL8_0_STABLE:1.16.0.4
	REL8_0_0:1.16.0.2
	REL8_0_0RC5:1.16
	REL8_0_0RC4:1.16
	REL8_0_0RC3:1.16
	REL8_0_0RC2:1.16
	REL8_0_0RC1:1.16
	REL8_0_0BETA5:1.16
	REL8_0_0BETA4:1.16
	REL7_4_6:1.15
	REL7_3_8:1.15
	REL7_2_6:1.12
	REL8_0_0BETA3:1.16
	REL8_0_0BETA2:1.16
	REL7_2_5:1.12
	REL7_4_5:1.15
	REL7_3_7:1.15
	REL7_4_4:1.15
	REL8_0_0BETA1:1.16
	REL7_4_3:1.15
	REL7_4_2:1.15
	REL7_3_6:1.15
	REL7_4_1:1.15
	REL7_3_5:1.15
	REL7_4:1.15
	REL7_4_RC2:1.15
	REL7_4_STABLE:1.15.0.6
	REL7_4_RC1:1.15
	REL7_4_BETA5:1.15
	REL7_4_BETA4:1.15
	REL7_4_BETA3:1.15
	REL7_4_BETA2:1.15
	WIN32_DEV:1.15.0.4
	REL7_4_BETA1:1.15
	REL7_3_4:1.15
	REL7_3_2:1.15
	REL7_2_4:1.12
	REL7_3_STABLE:1.15.0.2
	REL7_2_3:1.12
	REL7_2_STABLE:1.12.0.2
	REL7_2:1.11
	REL7_2_RC2:1.10
	REL7_2_RC1:1.10;
locks; strict;
comment	@# @;


1.17
date	2005.06.22.22.56.27;	author tgl;	state dead;
branches;
next	1.16;

1.16
date	2003.11.29.19.51.35;	author pgsql;	state Exp;
branches;
next	1.15;

1.15
date	2002.08.29.22.19.03;	author petere;	state Exp;
branches;
next	1.14;

1.14
date	2002.07.03.14.38.19;	author momjian;	state Exp;
branches;
next	1.13;

1.13
date	2002.04.09.18.07.24;	author momjian;	state Exp;
branches;
next	1.12;

1.12
date	2002.02.11.15.19.33;	author momjian;	state Exp;
branches;
next	1.11;

1.11
date	2002.01.31.19.13.53;	author momjian;	state Exp;
branches;
next	1.10;

1.10
date	2002.01.18.04.38.46;	author momjian;	state Exp;
branches;
next	1.9;

1.9
date	2002.01.15.05.41.55;	author momjian;	state Exp;
branches;
next	1.8;

1.8
date	2002.01.15.05.10.57;	author momjian;	state Exp;
branches;
next	1.7;

1.7
date	2002.01.15.05.03.53;	author momjian;	state Exp;
branches;
next	1.6;

1.6
date	2002.01.15.05.03.25;	author momjian;	state Exp;
branches;
next	1.5;

1.5
date	2002.01.15.04.55.26;	author momjian;	state Exp;
branches;
next	1.4;

1.4
date	2002.01.15.04.50.37;	author momjian;	state Exp;
branches;
next	1.3;

1.3
date	2002.01.15.04.05.24;	author momjian;	state Exp;
branches;
next	1.2;

1.2
date	2002.01.14.22.54.44;	author momjian;	state Exp;
branches;
next	1.1;

1.1
date	2002.01.14.17.32.07;	author momjian;	state Exp;
branches;
next	;


desc
@@


1.17
log
@Remove contrib modules that have been agreed to be obsolete.
(There are more that will be removed once they've been copied to
pgfoundry.org.)
@
text
@#!/bin/sh
#
# pg_upgrade: update a database without needing a full dump/reload cycle.
# CAUTION: Read the manual page before trying to use this!

# $PostgreSQL: pgsql/contrib/pg_upgrade/pg_upgrade,v 1.16 2003/11/29 19:51:35 pgsql Exp $
#
# To migrate this to newer versions of PostgreSQL:  
#	1)  Update the version numbers at the top of the file
#	2)  Search for specific version mentions in the script and update
#	    accordingly.
#	3)  Add changes for next version.

#set -x

# UPGRADE_VERSION is the expected old database version
UPGRADE_VERSION="7.3"
CUR_VERSION="7.3"

# Set this to "Y" to enable this program
ENABLE="Y"

if [ "$ENABLE" != "Y" ]
then
	echo "Sorry, $0 cannot upgrade database version $SRC_VERSION to $DST_VERSION." 1>&2
	echo "The on-disk structure of tables has changed." 1>&2
	echo "You will need to dump and restore using pg_dumpall." 1>&2
	exit 1
fi


trap "rm -f /tmp/$$.*" 0 1 2 3 15

BASENAME=`basename "$0"`
PHASE=""

while [ "$#" -ne 0 ]
do
	if [ "X$1" = "X-1" ]
	then	PHASE="1"
		shift
	elif [ "X$1" = "X-2" ]
	then	PHASE="2"
		shift
	elif [ "X$1" = "X-D" ]
	then	PGDATA="$2"
		shift 2
	fi
done

if [ "$PHASE" = "" ]
then	echo "You must run $BASENAME in either mode 1 or mode 2." 1>&2
	echo "Usage:  $BASENAME [-D datadir] -1 | -2" 1>&2
	exit 1
fi

if [ "$PGDATA" = "" ]
then	echo "You must set the PGDATA environment variable or specify it with -D." 1>&2
	echo "Usage:  $BASENAME [-D datadir] -1 | -2" 1>&2
	exit 1
fi

if [ ! -d "$PGDATA" ]
then	echo "$PGDATA does not exist.  Exiting." 1>&2
	if [ "$PHASE" -eq 2 ]
	then	echo "Perhaps you didn't run initdb." 1>&2
	fi
	exit 1
fi

if [ "$USER" = "root" -o ! -r "$PGDATA"/PG_VERSION ]
then	echo "You must run this as the PostgreSQL superuser.  Exiting." 1>&2
	exit 1
fi

# Strip off the trailing directory name and store our data there
# in the hope we are in the same filesystem so 'mv 'works.

INFODIR=`dirname "$PGDATA"`/pg_upgrade_info
SAVEDATA="$INFODIR"/data

make_dbobjoidmap()
{
	psql -d template1 -At -c "SELECT datname FROM pg_database" |
	grep -v '^template0$' |
	while read DB
	do	
		QUERY="SELECT 	relname, oid
			FROM 	pg_class
			WHERE 	relkind = 'r' OR
				relkind = 'i' OR
				relkind = 'S' OR
				relkind = 't';"

		psql -d "$DB" -At -F'	' -c "$QUERY" |
		while read RELNAME_OID
		do	
			echo "$DB	$RELNAME_OID"
		done
	done
}


make_dboidmap()
{
	psql -d template1 -At -F'	' -c \
		'SELECT datname, oid FROM pg_database;' |
	grep -v '^template0$'
}


move_objfiles()
{
	# Test to make sure there is a matching file in each place

	if [ ! -f "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" -a \
	     ! -h "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" ]
	then	echo "Moving of database $DB, OID $SRC_OID, object $OBJ failed." 1>&2
		echo "File not found.  Exiting." 1>&2
		return 1
	fi

	if [ ! -f "$PGDATA"/base/"$DST_DBOID"/"$DST_OID" -a \
	     ! -h "$PGDATA"/base/"$DST_DBOID"/"$DST_OID" ]
	then	echo "Moving of database $DB, OID $DST_OID, object $OBJ failed." 1>&2
		echo "File not found.  Exiting." 1>&2
		return 1
	fi

	# Move files

	mv -f "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" "$PGDATA"/base/"$DST_DBOID"/"$DST_OID"
	if [ "$?" -ne 0 ]
	then	echo "Moving of database $DB, OID $SRC_OID, object $OBJ" 1>&2
		echo "to $DST_OID failed.  Exiting" 1>&2
		return 1
	fi

	# handle table extents

	ls "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID".* 2> /dev/null | while read FILE
	do
		EXT=`basename "$FILE" | sed 's/^.*\.\(.*\)$/\1/'`
		mv -f "$FILE" "$PGDATA"/base/"$DST_DBOID"/"$DST_OID"."$EXT"
		if [ "$?" -ne 0 ]
		then	echo "Moving of database $DB, OID $SRC_OID, object $OBJ" 1>&2
			echo "to $DST_OID failed.  Exiting." 1>&2
			return 1
		fi
	done
}

if [ "$PHASE" -eq 1 ]
then

	##########################
	#  Phase 1 starts here   #
	##########################


	if [ ! -d "$PGDATA"/base/1 ]
	then	echo "There is no database template1 in $PGDATA/base." 1>&2
		exit 1
	fi

	# get version
	SRC_VERSION=`cat "$PGDATA"/PG_VERSION`
	if [ "$SRC_VERSION" = "" ]
	then	echo "$BASENAME can not find the PostgreSQL version file" 1>&2
		echo "'$PGDATA/PG_VERSION'.  Exiting." 1>&2
		exit 1
	fi

	if [ 	"$SRC_VERSION" != "$CUR_VERSION" -a \
		"$SRC_VERSION" != "$UPGRADE_VERSION" ]
	then	echo "$BASENAME supports versions $UPGRADE_VERSION and $CUR_VERSION only." 1>&2
		echo "However, your database is version $SRC_VERSION." 1>&2
		echo "You will need to dump and restore using pg_dumpall.  Exiting." 1>&2
		exit 1
	fi

	# If server is down, start it so we can do some work.
	if ! pg_ctl status | sed -n '1p' | grep "is running" > /dev/null 2>&1
	then	pg_ctl -w start
		if [ $? -ne 0 ]
		then	echo "Can not start server.  Exiting." 1>&2
			exit 1
		fi
	fi

	# create directory for our data
	if ! rm -rf "$INFODIR" 
	then	echo "Deletion of old pg_upgrade_info directory $INFODIR failed." 1>&2
		echo "Exiting." 1>&2
		exit 1
	fi
	if ! mkdir "$INFODIR"
	then	echo "Creation of new pg_upgrade_info directory $INFODIR failed." 1>&2
		echo "Exiting." 1>&2
		exit 1
	fi

	if ! chmod og-rwx "$INFODIR"
 	then	echo "Permission change on new pg_upgrade_info directory $INFODIR failed." 1>&2
		echo "Exiting." 1>&2
		exit 1
	fi


	# Dump schema
	pg_dumpall -s > "$INFODIR"/schema
	if [ $? -ne 0 ]
	then	echo "Can not dump schema.  Exiting." 1>&2
		exit 1
	fi
	
	# Generate mappings for database
	make_dboidmap > "$INFODIR"/dboidmap || exit "$?"
	make_dbobjoidmap > "$INFODIR"/dbobjoidmap || exit "$?"

	# Vacuum all databases to remove exipired rows.
	# We will lose our transaction log file during the upgrade so we
	# have to do this.

	vacuumdb -a
	if [ $? -ne 0 ]
	then	echo "Can not vacuum server.  Exiting." 1>&2
		exit 1
	fi

	# Stop server so we can move the directory.
	pg_ctl -w stop
	if [ $? -ne 0 ]
	then	echo "Can not stop server.  Exiting." 1>&2
		exit 1
	fi
	
	# No matter what the directory name, call it data
	mv "$PGDATA" "$INFODIR"/data
	if [ $? -ne 0 ]
	then	echo "Can not move old $PGDATA out of the way.  Exiting." 1>&2
		exit 1
	fi
	echo
	echo
	echo "$BASENAME phase 1 completed."
	echo "Continue with the steps outlined in the $BASENAME manual page."
	exit 0
fi


	##########################
	#  Phase 2 starts here   #
	##########################

# check things

if [ ! -d "$INFODIR" ]
then	echo "There is no '$INFODIR' directory from the phase 1 run of $BASENAME." 1>&2
	exit 1	
fi

if [ ! -d "$SAVEDATA" ]
then	echo "There is no '$SAVEDATA' directory from the phase 1 run of $BASENAME." 1>&2
	exit 1	
fi

if [ ! -f "$SAVEDATA/PG_VERSION" ]
then	echo "Cannot read '$SAVEDATA/PG_VERSION' --- something is wrong." 1>&2
	exit 1
fi

if [ ! -f "$PGDATA/PG_VERSION" ]
then	echo "Cannot read '$PGDATA/PG_VERSION' --- something is wrong." 1>&2
	exit 1
fi

if [ ! -d "$PGDATA/base/1" ]
then	echo "Cannot find database template1 in '$PGDATA/base'." 1>&2
	echo "Are you running $BASENAME as the postgres superuser?" 1>&2
	exit 1
fi

# Get the actual versions seen in the data dirs.

SRC_VERSION=`cat "$SAVEDATA"/PG_VERSION`
DST_VERSION=`cat "$PGDATA"/PG_VERSION`

# Check for version compatibility.
# This code will need to be updated/reviewed for each new PostgreSQL release.

if [ "$DST_VERSION" != "$CUR_VERSION" ]
then	echo "$BASENAME is for PostgreSQL version $CUR_VERSION" 1>&2
	echo "but $PGDATA/PG_VERSION contains $DST_VERSION." 1>&2
	echo "Did you run initdb for version $UPGRADE_VERSION by mistake?" 1>&2
	exit 1
fi

# Stop server for pg_resetxlog use

if pg_ctl status | sed -n '1p' | grep "is running" > /dev/null 2>&1
then	pg_ctl -w stop
	if [ $? -ne 0 ]
	then	echo "Can not start server.  Exiting." 1>&2
		exit 1
	fi
fi

# check for proper pg_resetxlog version

pg_resetxlog 2> /dev/null
# file not found status is normally 127, not 1
if [ "$?" -ne 1 ]
then	echo "Unable to find pg_resetxlog in your path." 1>&2
	echo "Install it from pgsql/contrib/pg_resetxlog and continue.  Exiting." 1>&2
	exit 1
fi

if ! pg_resetxlog -x 2>&1 | grep 'xid' > /dev/null 2>&1
then	echo "Old version of pg_resetxlog found in path." 1>&2
	echo "Install a newer version of pg_resetxlog from pgsql/contrib/pg_resetxlog." 1>&2
	echo "Exiting." 1>&2
	exit 1
fi

SRC_XID=`pg_resetxlog -n "$SAVEDATA" | grep "NextXID" | awk -F'  *' '{print $4}'`
DST_XID=`pg_resetxlog -n "$PGDATA" | grep "NextXID" | awk -F'  *' '{print $4}'`

# compare locales to make sure they match

pg_resetxlog -n "$SAVEDATA" | grep "^LC_" > /tmp/$$.0
pg_resetxlog -n "$PGDATA" | grep "^LC_" > /tmp/$$.1
if ! diff /tmp/$$.0 /tmp/$$.1 > /dev/null
then	echo "Locales do not match between the two versions.  Exiting." 1>&2
	exit 1
fi

# Restart postmaster

pg_ctl -w start
if [ $? -ne 0 ]
then	echo "Can not start server.  Exiting." 1>&2
	exit 1
fi


###################################
# Checking done.  Ready to proceed.
###################################


# Execute the schema script to create everything

psql template1 < "$INFODIR"/schema
if [ $? -ne 0 ]
then	echo "There were errors in the input script.  Exiting." 1>&2
	exit 1
fi

echo "Input script completed, fixing row commit statuses..."

# Generate mappings for new database
make_dboidmap > /tmp/$$.dboidmap || exit "$?"
make_dbobjoidmap > /tmp/$$.dbobjoidmap || exit "$?"

# we are done with SQL database access
# shutdown forces buffers to disk

pg_ctl -w stop
if [ "$?" -ne 0 ]
then	echo "Unable to stop database server.  Exiting." 1>&2
	exit 1
fi

echo "Commit fixes complete, moving data files..."

# Move table/index/sequence files

cat "$INFODIR"/dbobjoidmap | while read LINE
do
	DB=`echo "$LINE" | awk '{print $1}'`
	OBJ=`echo "$LINE" | awk '{print $2}'`

	# Skip system tables, except for pg_largeobject
	# pg_toast tables are handled later as part of the
	# base table move
	if [ `expr X"$OBJ" : X'pg_'` -eq 4 -a \
	     `expr X"$OBJ" : X'pg_largeobject'` -ne 15 ]
	then	continue
	fi

	SRC_OID=`echo "$LINE" | awk '{print $3}'`
	SRC_DBOID=`grep "^$DB	" "$INFODIR"/dboidmap | awk '{print $2}'`
	DST_DBOID=`grep "^$DB	" /tmp/$$.dboidmap | awk '{print $2}'`
	DST_OID=`grep "^$DB	$OBJ	" /tmp/$$.dbobjoidmap | awk '{print $3}'`

	move_objfiles

	# Handle TOAST files if they exist
	if grep "^$DB	pg_toast_$SRC_OID	" "$INFODIR"/dbobjoidmap \
		> /dev/null 2>&1
	then	# toast heap
		SAVE_SRC_OID="$SRC_OID"
		SAVE_DST_OID="$DST_OID"
		SRC_OID=`grep "^$DB	pg_toast_$SAVE_SRC_OID	" \
			"$INFODIR"/dbobjoidmap | awk '{print $3}'`
		DST_OID=`grep "^$DB	pg_toast_$SAVE_DST_OID	" \
			/tmp/$$.dbobjoidmap | awk '{print $3}'`
		move_objfiles
		# toast index
		SRC_OID=`grep "^$DB	pg_toast_${SAVE_SRC_OID}_idx	" \
			"$INFODIR"/dbobjoidmap | awk '{print $3}'`
		DST_OID=`grep "^$DB	pg_toast_${SAVE_DST_OID}_idx	" \
			/tmp/$$.dbobjoidmap | awk '{print $3}'`
		move_objfiles
	fi
done
								   

# Set this so future backends don't think these tuples are their own
# because it matches their own XID.
# Commit status already updated by vacuum above
# Set to maximum XID just in case SRC wrapped around recently and
# is lower than DST's database

if [ "$SRC_XID" -gt "$DST_XID" ]
then	MAX_XID="$SRC_XID"
else	MAX_XID="$DST_XID"
fi

pg_resetxlog -x "$MAX_XID" "$PGDATA"
if [ "$?" -ne 0 ]
then	echo "Unable to set new XID.  Exiting." 1>&2
	exit 1
fi

# Move over old WAL

rm -r "$PGDATA"/pg_xlog
mv -f "$SAVEDATA"/pg_xlog "$PGDATA"

# Move over old clog

rm -r "$PGDATA"/pg_clog
mv -f "$SAVEDATA"/pg_clog "$PGDATA"

# Set last log file id and segment from old database

LOG_ID=`pg_resetxlog -n "$SAVEDATA" | grep "Current log file id:" |
	awk -F'  *' '{print $5}'`
if [ "$LOG_ID" = "" ]
then	echo "Unable to get old log file id.  Exiting." 1>&2
	exit 1
fi
SEG_ID=`pg_resetxlog -n "$SAVEDATA" | grep "Next log file segment:" |
	awk -F'  *' '{print $5}'`
if [ "$SEG_ID" = "" ]
then	echo "Unable to get old log segment id.  Exiting." 1>&2
	exit 1
fi

# Set checkpoint location of new database

pg_resetxlog -l "$LOG_ID","$SEG_ID" "$PGDATA"
if [ "$?" -ne 0 ]
then	echo "Unable to set new log file/segment id.  Exiting." 1>&2
	exit 1
fi

# Restart server with moved data

pg_ctl -w start
if [ "$?" -ne 0 ]
then	echo "Unable to restart database server.  Exiting." 1>&2
	exit 1
fi

# Now that we have moved the WAL/transaction log files, vacuum again to
# mark install rows with fixed transaction ids to prevent problems on xid
# wraparound.

vacuumdb -a
if [ $? -ne 0 ]
then	echo "There were errors during VACUUM.  Exiting." 1>&2
	exit 1
fi

echo
echo
echo "$BASENAME phase 2 completed."
echo "You may remove the old database files with 'rm -r $INFODIR'."
exit 0
@


1.16
log
@
$Header: -> $PostgreSQL Changes ...
@
text
@d6 1
a6 1
# $PostgreSQL: /cvsroot/pgsql-server/contrib/pg_upgrade/pg_upgrade,v 1.15 2002/08/29 22:19:03 petere Exp $
@


1.15
log
@Make pg_resetxlog options parsing more standard and prepare messages for
translation.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql-server/contrib/pg_upgrade/pg_upgrade,v 1.14 2002/07/03 14:38:19 momjian Exp $
@


1.14
log
@Disable pg_upgrade for 7.3.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.13 2002/04/09 18:07:24 momjian Exp $
d464 1
a464 1
pg_resetxlog -l "$LOG_ID" "$SEG_ID" "$PGDATA"
@


1.13
log
@Update pg_upgrade for 7.3.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.12 2002/02/11 15:19:33 momjian Exp $
d17 1
a17 1
UPGRADE_VERSION="7.2"
@


1.12
log
@Fix flag handling of pg_upgrade.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.11 2002/01/31 19:13:53 momjian Exp $
d17 2
a18 2
UPGRADE_VERSION="7.1"
CUR_VERSION="7.2"
d78 1
d92 2
a93 8
				relkind = 't'"
		# Don't move over 7.1 int4 sequences;  use setval() file.
		# Sequence XIDs changed from 7.2beta4 to beta5;  don't copy them.
		if [ "$SRC_VERSION" != "7.1" -a \
		     "$SRC_VERSION" != "7.2" ]
		then	QUERY="$QUERY OR relkind = 'S';";
			QUERY="$QUERY;"
		fi
d211 1
a211 22
	pg_dumpall -s |
	awk -F'  *' '
	{
		# Modify sequences with int8 maximums if we are upgrading from 7.1.
		if ("'"$SRC_VERSION"'" == "7.1" &&
		    $1 == "CREATE" &&
		    $2 == "SEQUENCE" &&
		    # handle OS rounding
		    $9 >= 2147483646 &&
		    $9 <= 2147483648)
		{
			for(i=1; i <= NF; i++)
			{
				if (i != 9)
					printf "%s ", $i;
				else
					printf "%s ", "9223372036854775807";
			}
			print "";
		}
		else    print $0;
	}' > "$INFODIR"/schema
a220 26
	# Generate setval() script for 7.1 because it has int4 sequences
	# Sequence XIDs changed from 7.2beta4 to beta5 so we have to recreate them too.
	# Not required for 7.2beta5 and later, but it can't hurt.
	if [ "$SRC_VERSION" = "7.1" -o \
	     "$SRC_VERSION" = "7.2" ]
	then
		psql -d template1 -At -c "SELECT datname FROM pg_database" |
		grep -v '^template0$' |
		while read DB
		do
			# We use awk as a portable way to output a backslash
			awk 'BEGIN {print "\\connect '"$DB"'"}'
			psql -d "$DB" -At -c "
				SELECT 	relname
				FROM 	pg_class
				WHERE 	relkind = 'S';" |
			while read SEQUENCE
			do
				VALUE=`psql -d "$DB" -At -c "SELECT last_value 
								 FROM \"$SEQUENCE\";"`
				echo "SELECT setval ('$SEQUENCE', $VALUE, true);"
			done
		done > "$INFODIR"/setval
	else	rm -f "$INFODIR"/setval
	fi

a325 3
# If the XID is > 2 billion, 7.1 database will have non-frozen XID's in 
# low numbers, and 7.2 will think they are in the future --- bad.

a326 4
if [ "$SRC_VERSION" = "7.1" -a "$SRC_XID" -gt 2000000000 ]
then	echo "XID too high for $BASENAME.  Exiting." 1>&2
	exit 1
fi
a361 10
# XXX do we still need this?
# Now vacuum each result database because our movement of transaction log
# causes some committed transactions to appear as non-committed

vacuumdb -a
if [ $? -ne 0 ]
then	echo "There were errors during VACUUM.  Exiting." 1>&2
	exit 1
fi

d442 5
d478 3
a480 1
# Set sequence values for 7.1-version sequences, which were int4.
d482 4
a485 8
if [ "$SRC_VERSION" = "7.1" -o \
     "$SRC_VERSION" = "7.2" ]
then	echo "Set sequence values..."
	psql -d template1 -At < "$INFODIR"/setval
	if [ $? -ne 0 ]
	then	echo "There were errors during int4 sequence restoration.  Exiting." 1>&2
		exit 1
	fi
@


1.11
log
@Add mention of recovery using pg_upgrade.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.10 2002/01/18 04:38:46 momjian Exp $
d37 2
a38 2
if [ "$#" -eq 1 ]
then
d49 1
a49 1
fi
@


1.10
log
@Enable /contrib/pg_upgrade, mention little testing.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.9 2002/01/15 05:41:55 momjian Exp $
d187 1
a187 1
	# Start server, if needed, so we can do some work.
d248 2
a249 1
	# Sequence XIDs changed from 7.2beta4 to beta5;  we have to recreate them.
d431 1
a431 1
# Generate mappings for database
@


1.9
log
@Manual page improvements.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.8 2002/01/15 05:10:57 momjian Exp $
d16 4
d21 1
a21 1
ENABLE="N"
a30 4

# UPGRADE_VERSION is the expected old database version
UPGRADE_VERSION="7.1"
CUR_VERSION="7.2"
@


1.8
log
@More cleanups.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.7 2002/01/15 05:03:53 momjian Exp $
d87 5
a91 5
		QUERY="`echo \"	SELECT 	relname, oid
				FROM 	pg_class
				WHERE 	relkind = 'r' OR
					relkind = 'i' OR
					relkind = 't'\"`"
@


1.7
log
@Keep pg_upgrade as disabled.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.6 2002/01/15 05:03:25 momjian Exp $
d355 1
a355 1
	then	echo "Can no start server.  Exiting." 1>&2
@


1.6
log
@More wording improvements.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.5 2002/01/15 04:55:26 momjian Exp $
d17 1
a17 1
ENABLE="Y"
@


1.5
log
@More updates.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.4 2002/01/15 04:50:37 momjian Exp $
d34 1
a34 1
BASENAME=`basename "$0`
d297 1
a297 1
	echo "Plase 1 completed."
d548 1
a548 1
	then	echo "There were errors during int4 sequence restore.  Exiting." 1>&2
d555 1
@


1.4
log
@More cleanups with ideas from Peter.
@
text
@d1 1
a1 1
z#!/bin/sh
d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.3 2002/01/15 04:05:24 momjian Exp $
@


1.3
log
@Add Peter's portability and option suggestions.
@
text
@d1 1
a1 1
#!/bin/sh
d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.2 2002/01/14 22:54:44 momjian Exp $
d8 5
a12 2
# NOTE: we must be sure to update the version-checking code a few dozen lines
# below for each new PostgreSQL release.
d21 1
a21 2
	echo "Sorry, $0 cannot upgrade database
version $SRC_VERSION to $DST_VERSION." 1>&2
d64 1
a64 2
then	echo "$PGDATA does not exist.
$BASENAME aborted." 1>&2
d72 1
a72 2
then	echo "You must run this as the PostgreSQL superuser.
$BASENAME aborted." 1>&2
d123 2
a124 2
	then	echo "Move of database $DB, OID $SRC_OID, object $OBJ failed.
File not found;  exiting" 1>&2
d130 2
a131 2
	then	echo "Move of database $DB, OID $DST_OID, object $OBJ failed.
File not found;  exiting" 1>&2
d139 2
a140 2
	then	echo "Move of database $DB, OID $SRC_OID, object $OBJ
to $DST_OID failed.;  exiting" 1>&2
d151 2
a152 2
		then	echo "Move of database $DB, OID $SRC_OID, object $OBJ
to $DST_OID failed.;  exiting" 1>&2
d174 2
a175 2
	then	echo "$BASENAME can not find PostgreSQL version file '$PGDATA/PG_VERSION'.
$BASENAME aborted." 1>&2
d182 2
a183 3
		echo "However, your database is version $SRC_VERSION;
$BASENAME aborted." 1>&2
		echo "You will need to dump and restore using pg_dumpall." 1>&2
d191 1
a191 2
		then	echo "Can not start server.
$BASENAME aborted." 1>&2
d198 2
a199 2
	then	echo "Deletion of old pg_upgrade_info directory failed, $INFODIR.
$BASENAME aborted." 1>&2
d203 2
a204 2
	then	echo "Creation of new pg_upgrade_info directory failed, $INFODIR.
$BASENAME aborted." 1>&2
d209 2
a210 2
 	then	echo "Permission change on new pg_upgrade_info directory failed, $INFODIR.
$BASENAME aborted." 1>&2
d239 1
a239 2
	then	echo "Can not dump schema.
$BASENAME aborted." 1>&2
d278 1
a278 2
	then	echo "Can not vacuum server.
$BASENAME aborted." 1>&2
d285 1
a285 2
	then	echo "Can not stop server.
$BASENAME aborted." 1>&2
d292 1
a292 2
	then	echo "Can not move old /$PGDATA out of the way.
$BASENAME aborted." 1>&2
d297 2
a298 2
	echo "Plase 1 completed.
Continue with the steps outlined in the $BASENAME manual page."
d310 1
a310 1
then	echo "There is no '$INFODIR' directory from a phase 1 run of $BASENAME." 1>&2
d344 2
a345 2
then	echo "$BASENAME is for PostgreSQL version $CUR_VERSION
but $PGDATA/PG_VERSION contains $DST_VERSION." 1>&2
d355 1
a355 2
	then	echo "Can no start server.
$BASENAME aborted." 1>&2
d365 2
a366 2
then	echo "Unable to find pg_resetxlog in path.
Install it from pgsql/contrib/pg_resetxlog and continue.;  exiting" 1>&2
d371 3
a373 2
then	echo "Old version of pg_resetxlog found in path.
Install a newer version from pgsql/contrib/pg_resetxlog.;  exiting" 1>&2
d382 1
a382 1
then	echo "XID too high for $BASENAME.;  exiting" 1>&2
d392 1
a392 1
then	echo "Locales do not match between the two versions.;  exiting" 1>&2
d400 1
a400 2
then	echo "Can not start server.
$BASENAME aborted." 1>&2
d414 1
a414 2
then	echo "There were errors in the input script.
$BASENAME aborted." 1>&2
d418 1
a418 1
echo "Input script complete, fixing row commit statuses..."
d426 1
a426 2
then	echo "There were errors during VACUUM.
$BASENAME aborted." 1>&2
d439 1
a439 1
then	echo "Unable to stop database server.;  exiting" 1>&2
d501 1
a501 1
then	echo "Unable to set new XID.;  exiting" 1>&2
d515 1
a515 1
then	echo "Unable to get old log file id.;  exiting" 1>&2
d521 1
a521 1
then	echo "Unable to get old log segment id.;  exiting" 1>&2
d529 1
a529 1
then	echo "Unable to set new log file/segment id.;  exiting" 1>&2
d537 1
a537 1
then	echo "Unable to restart database server.;  exiting" 1>&2
d548 1
a548 2
	then	echo "There were errors during int4 sequence restore.
$BASENAME aborted." 1>&2
a555 1

@


1.2
log
@Update permissions?
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/contrib/pg_upgrade/pg_upgrade,v 1.1 2002/01/14 17:32:07 momjian Exp $
d14 1
a14 1
ENABLE="N"
d32 1
d44 2
a45 2
	then	DATADIR="$2"
		shift
d50 2
a51 2
then	echo "You must run $0 in either mode 1 or mode 2." 1>&2
	echo "Usage:  $0 [-D datadir] -1 | -2" 1>&2
d55 3
a57 3
if [ "$DATADIR" = "" ]
then	echo "You must set the DATADIR environment variable or specify it with -D." 1>&2
	echo "Usage:  $0 [-D datadir] -1 | -2" 1>&2
d61 3
a63 3
if [ ! -d "$DATADIR" ]
then	echo "$DATADIR does not exist.
$0 aborted." 1>&2
d70 1
a70 1
if [ "$USER" = "root" -o ! -r "$DATADIR"/PG_VERSION ]
d72 1
a72 1
$0 aborted." 1>&2
d76 4
a79 2
INFODIR="pg_upgrade_info"
SAVEDATA="$INFODIR"/"`basename \"$DATADIR\"`"
d125 1
a125 1
		exit 1
d128 2
a129 2
	if [ ! -f "$DATADIR"/base/"$DST_DBOID"/"$DST_OID" -a \
	     ! -h "$DATADIR"/base/"$DST_DBOID"/"$DST_OID" ]
d132 1
a132 1
		exit 1
d137 1
a137 1
	mv -f "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID" "$DATADIR"/base/"$DST_DBOID"/"$DST_OID"
d141 1
a141 1
		exit 1
d146 1
a146 1
	ls "$SAVEDATA"/base/"$SRC_DBOID"/"$SRC_OID".* 2>/dev/null | while read FILE
d149 1
a149 1
		mv -f "$FILE" "$DATADIR"/base/"$DST_DBOID"/"$DST_OID"."$EXT"
d153 1
a153 1
			exit 1
d166 2
a167 2
	if [ ! -d "$DATADIR"/base/1 ]
	then	echo "There is no database template1 in $DATADIR/base." 1>&2
d172 1
a172 1
	SRC_VERSION=`cat "$DATADIR"/PG_VERSION`
d174 2
a175 2
	then	echo "$0 can not find PostgreSQL version file '$DATADIR/PG_VERSION'.
$0 aborted." 1>&2
d181 3
a183 5
	then	echo 
	"$0 supports versions $UPGRADE_VERSION and $CUR_VERSION only." 1>&2
		echo 
	"However, your database is version $SRC_VERSION;
$0 aborted." 1>&2
d189 1
a189 1
	if ! pg_ctl status | head -1 | grep -q "is running"
d193 1
a193 1
$0 aborted." 1>&2
d199 17
a215 3
	rm -rf "$INFODIR"
	mkdir "$INFODIR"
	chmod og-rwx "$INFODIR"
d242 1
a242 1
$0 aborted." 1>&2
d247 2
a248 2
	make_dboidmap > "$INFODIR"/dboidmap
	make_dbobjoidmap > "$INFODIR"/dbobjoidmap
d282 1
a282 1
$0 aborted." 1>&2
d290 1
a290 1
$0 aborted." 1>&2
d293 3
a295 2

	mv "$DATADIR" "$INFODIR"
d297 2
a298 2
	then	echo "Can not move old /$DATADIR out of the way.
$0 aborted." 1>&2
d304 1
a304 1
Continue with the steps outlined in the $0 manual page."
d316 1
a316 1
then	echo "There is no '$INFODIR' directory from a phase 1 run of $0." 1>&2
d321 1
a321 1
then	echo "There is no '$SAVEDATA' directory from the phase 1 run of $0." 1>&2
d330 2
a331 2
if [ ! -f "$DATADIR/PG_VERSION" ]
then	echo "Cannot read '$DATADIR/PG_VERSION' --- something is wrong." 1>&2
d335 3
a337 3
if [ ! -d "$DATADIR/base/1" ]
then	echo "Cannot find database template1 in '$DATADIR/base'." 1>&2
	echo "Are you running $0 as the postgres superuser?" 1>&2
d344 1
a344 1
DST_VERSION=`cat "$DATADIR"/PG_VERSION`
d350 2
a351 2
then	echo "$0 is for PostgreSQL version $CUR_VERSION
but $DATADIR/PG_VERSION contains $DST_VERSION." 1>&2
d358 1
a358 1
if pg_ctl status | head -1 | grep -q "is running"
d362 1
a362 1
$0 aborted." 1>&2
d369 1
a369 1
pg_resetxlog 2>/dev/null
d377 1
a377 1
if ! pg_resetxlog -x 2>&1 | grep -q 'xid'
d388 1
a388 1
then	echo "XID too high for $0.;  exiting" 1>&2
d391 1
a391 1
DST_XID=`pg_resetxlog -n "$DATADIR" | grep "NextXID" | awk -F'  *' '{print $4}'`
d396 2
a397 2
pg_resetxlog -n "$DATADIR" | grep "^LC_" > /tmp/$$.1
if ! diff /tmp/$$.0 /tmp/$$.1 >/dev/null
d407 1
a407 1
$0 aborted." 1>&2
d422 1
a422 1
$0 aborted." 1>&2
d435 1
a435 1
$0 aborted." 1>&2
d440 2
a441 2
make_dboidmap > /tmp/$$.dboidmap
make_dbobjoidmap > /tmp/$$.dbobjoidmap
d464 2
a465 2
	if [ `expr "$OBJ" : 'pg_'` -eq 3 -a \
	     `expr "$OBJ" : 'pg_largeobject'` -ne 14 ]
d477 2
a478 1
	if grep -q "^$DB	pg_toast_$SRC_OID	" "$INFODIR"/dbobjoidmap
d508 1
a508 1
pg_resetxlog -x "$MAX_XID" "$DATADIR"
d516 2
a517 2
rm -r "$DATADIR"/pg_xlog
mv -f "$SAVEDATA"/pg_xlog "$DATADIR"
d536 1
a536 1
pg_resetxlog -l "$LOG_ID" "$SEG_ID" "$DATADIR"
d558 1
a558 1
$0 aborted." 1>&2
d565 1
a565 1
echo "You may remove the old database files with 'rm -r pg_upgrade'."
@


1.1
log
@Move pg_upgrade to /contrib.  Still need to make Peter's portability
changes.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.34 2002/01/13 17:52:00 momjian Exp $
d33 1
d42 3
d50 7
a56 1
	echo "Usage:  $0 [ -1 | -2 ]" 1>&2
d60 2
a61 2
if [ ! -e data ]
then	echo "$0 must be run from the directory above your /data directory.
d64 1
a64 1
	then	echo "You must run initdb to create a template1 database." 1>&2
d69 6
d76 1
a76 1
OLDDIR="$INFODIR/data"
d118 2
a119 1
	if [ ! -e "$OLDDIR"/base/"$SRC_DBOID"/"$SRC_OID" ]
d125 2
a126 1
	if [ ! -e data/base/"$DST_DBOID"/"$DST_OID" ]
d134 1
a134 1
	mv -f "$OLDDIR"/base/"$SRC_DBOID"/"$SRC_OID" data/base/"$DST_DBOID"/"$DST_OID"
d143 1
a143 1
	ls "$OLDDIR"/base/"$SRC_DBOID"/"$SRC_OID".* 2>/dev/null | while read FILE
d146 1
a146 1
		mv -f "$FILE" data/base/"$DST_DBOID"/"$DST_OID"."$EXT"
d163 2
a164 2
	if [ ! -d data/base/1 ]
	then	echo "There is no database template1 in data/base." 1>&2
d169 1
a169 1
	SRC_VERSION="`cat data/PG_VERSION`"
d171 1
a171 1
	then	echo "$0 can not find PostgreSQL version file 'data/PG_VERSION'.
d279 1
a279 1
	mv data "$INFODIR"
d281 1
a281 1
	then	echo "Can not move old /data out of the way.
d304 2
a305 2
if [ ! -e "$OLDDIR" ]
then	echo "There is no '$OLDDIR' directory from the phase 1 run of $0." 1>&2
d309 2
a310 2
if [ ! -f "$OLDDIR/PG_VERSION" ]
then	echo "Cannot read '$OLDDIR/PG_VERSION' --- something is wrong." 1>&2
d314 2
a315 2
if [ ! -f "data/PG_VERSION" ]
then	echo "Cannot read 'data/PG_VERSION' --- something is wrong." 1>&2
d319 2
a320 2
if [ ! -d "data/base/1" ]
then	echo "Cannot find database template1 in 'data/base'." 1>&2
d327 2
a328 2
SRC_VERSION=`cat "$OLDDIR"/PG_VERSION`
DST_VERSION=`cat data/PG_VERSION`
d335 1
a335 1
but data/PG_VERSION contains $DST_VERSION." 1>&2
d370 1
a370 1
SRC_XID=`pg_resetxlog -n "$OLDDIR" | grep "NextXID" | awk -F'  *' '{print $4}'`
d375 1
a375 1
DST_XID=`pg_resetxlog -n data | grep "NextXID" | awk -F'  *' '{print $4}'`
d379 2
a380 2
pg_resetxlog -n "$OLDDIR" | grep "^LC_" > /tmp/$$.0
pg_resetxlog -n data | grep "^LC_" > /tmp/$$.1
d491 1
a491 1
pg_resetxlog -x "$MAX_XID" data
d499 2
a500 2
rm -r data/pg_xlog
mv -f "$OLDDIR"/pg_xlog data
d504 1
a504 1
LOG_ID=`pg_resetxlog -n "$OLDDIR" | grep "Current log file id:" |
d510 1
a510 1
SEG_ID=`pg_resetxlog -n "$OLDDIR" | grep "Next log file segment:" |
d519 1
a519 1
pg_resetxlog -l "$LOG_ID" "$SEG_ID" data
@
