head	1.35;
access;
symbols
	REL7_2_BETA5:1.34
	REL7_2_BETA4:1.17
	REL7_2_BETA3:1.17
	REL7_2_BETA2:1.17
	REL7_2_BETA1:1.17
	REL7_1_2:1.16
	REL7_1_STABLE:1.16.0.2
	REL7_1_BETA:1.15
	REL7_1_BETA3:1.16
	REL7_1_BETA2:1.16
	REL7_1:1.16
	REL7_0_PATCHES:1.15.0.2
	REL7_0:1.15
	REL6_5_PATCHES:1.9.0.2
	REL6_5:1.9
	REL6_4:1.6.0.2;
locks; strict;
comment	@# @;


1.35
date	2002.01.15.04.52.33;	author momjian;	state dead;
branches;
next	1.34;

1.34
date	2002.01.13.17.52.00;	author momjian;	state Exp;
branches;
next	1.33;

1.33
date	2002.01.13.05.36.42;	author momjian;	state Exp;
branches;
next	1.32;

1.32
date	2002.01.13.04.55.44;	author momjian;	state Exp;
branches;
next	1.31;

1.31
date	2002.01.13.01.22.27;	author momjian;	state Exp;
branches;
next	1.30;

1.30
date	2002.01.13.01.13.59;	author momjian;	state Exp;
branches;
next	1.29;

1.29
date	2002.01.11.20.34.14;	author momjian;	state Exp;
branches;
next	1.28;

1.28
date	2002.01.11.06.48.41;	author momjian;	state Exp;
branches;
next	1.27;

1.27
date	2002.01.11.06.33.01;	author momjian;	state Exp;
branches;
next	1.26;

1.26
date	2002.01.11.06.08.02;	author momjian;	state Exp;
branches;
next	1.25;

1.25
date	2002.01.11.05.54.59;	author momjian;	state Exp;
branches;
next	1.24;

1.24
date	2002.01.11.04.39.19;	author momjian;	state Exp;
branches;
next	1.23;

1.23
date	2002.01.11.00.27.42;	author momjian;	state Exp;
branches;
next	1.22;

1.22
date	2002.01.10.04.58.19;	author momjian;	state Exp;
branches;
next	1.21;

1.21
date	2002.01.10.03.05.48;	author momjian;	state Exp;
branches;
next	1.20;

1.20
date	2002.01.09.21.50.52;	author momjian;	state Exp;
branches;
next	1.19;

1.19
date	2002.01.09.16.08.54;	author momjian;	state Exp;
branches;
next	1.18;

1.18
date	2002.01.09.16.00.46;	author momjian;	state Exp;
branches;
next	1.17;

1.17
date	2001.08.25.18.52.42;	author tgl;	state Exp;
branches;
next	1.16;

1.16
date	2000.12.18.16.30.07;	author momjian;	state Exp;
branches;
next	1.15;

1.15
date	2000.05.05.03.08.20;	author tgl;	state Exp;
branches;
next	1.14;

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

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

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

1.11
date	99.08.02.22.34.53;	author tgl;	state Exp;
branches;
next	1.10;

1.10
date	99.07.31.22.06.44;	author tgl;	state Exp;
branches;
next	1.9;

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

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

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

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

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

1.4
date	98.10.24.20.09.46;	author tgl;	state Exp;
branches;
next	1.3;

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

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

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


desc
@@


1.35
log
@Remove old pg_upgrade script, from Tom.
@
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!

# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.34 2002/01/13 17:52:00 momjian Exp $
#
# NOTE: we must be sure to update the version-checking code a few dozen lines
# below for each new PostgreSQL release.

#set -x

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

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


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

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

PHASE=""
if [ "$#" -eq 1 ]
then
	if [ "X$1" = "X-1" ]
	then	PHASE="1"
		shift
	elif [ "X$1" = "X-2" ]
	then	PHASE="2"
		shift
	fi
fi

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

if [ ! -e data ]
then	echo "$0 must be run from the directory above your /data directory.
$0 aborted." 1>&2
	if [ "$PHASE" -eq 2 ]
	then	echo "You must run initdb to create a template1 database." 1>&2
	fi
	exit 1
fi

INFODIR="pg_upgrade_info"
OLDDIR="$INFODIR/data"

make_dbobjoidmap()
{
	psql -d template1 -At -c "SELECT datname FROM pg_database" |
	grep -v '^template0$' |
	while read DB
	do	
		QUERY="`echo \"	SELECT 	relname, oid
				FROM 	pg_class
				WHERE 	relkind = 'r' OR
					relkind = 'i' OR
					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

		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 [ ! -e "$OLDDIR"/base/"$SRC_DBOID"/"$SRC_OID" ]
	then	echo "Move of database $DB, OID $SRC_OID, object $OBJ failed.
File not found;  exiting" 1>&2
		exit 1
	fi

	if [ ! -e data/base/"$DST_DBOID"/"$DST_OID" ]
	then	echo "Move of database $DB, OID $DST_OID, object $OBJ failed.
File not found;  exiting" 1>&2
		exit 1
	fi

	# Move files

	mv -f "$OLDDIR"/base/"$SRC_DBOID"/"$SRC_OID" data/base/"$DST_DBOID"/"$DST_OID"
	if [ "$?" -ne 0 ]
	then	echo "Move of database $DB, OID $SRC_OID, object $OBJ
to $DST_OID failed.;  exiting" 1>&2
		exit 1
	fi

	# handle table extents

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

if [ "$PHASE" -eq 1 ]
then

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


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

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

	if [ 	"$SRC_VERSION" != "$CUR_VERSION" -a \
		"$SRC_VERSION" != "$UPGRADE_VERSION" ]
	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
		echo "You will need to dump and restore using pg_dumpall." 1>&2
		exit 1
	fi

	# Start server, if needed, so we can do some work.
	if ! pg_ctl status | head -1 | grep -q "is running"
	then	pg_ctl -w start
		if [ $? -ne 0 ]
		then	echo "Can not start server.
$0 aborted." 1>&2
			exit 1
		fi
	fi

	# create directory for our data
	rm -rf "$INFODIR"
	mkdir "$INFODIR"
	chmod og-rwx "$INFODIR"

	# Dump schema
	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
	if [ $? -ne 0 ]
	then	echo "Can not dump schema.
$0 aborted." 1>&2
		exit 1
	fi
	
	# Generate mappings for database
	make_dboidmap > "$INFODIR"/dboidmap
	make_dbobjoidmap > "$INFODIR"/dbobjoidmap

	# Generate setval() script for 7.1 because it has int4 sequences
	# Sequence XIDs changed from 7.2beta4 to beta5;  we have to recreate them.
	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

	# 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.
$0 aborted." 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.
$0 aborted." 1>&2
		exit 1
	fi

	mv data "$INFODIR"
	if [ $? -ne 0 ]
	then	echo "Can not move old /data out of the way.
$0 aborted." 1>&2
		exit 1
	fi
	echo
	echo
	echo "Plase 1 completed.
Continue with the steps outlined in the $0 manual page."
	exit 0
fi


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

# check things

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

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

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

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

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

# Get the actual versions seen in the data dirs.

SRC_VERSION=`cat "$OLDDIR"/PG_VERSION`
DST_VERSION=`cat data/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 "$0 is for PostgreSQL version $CUR_VERSION
but data/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 | head -1 | grep -q "is running"
then	pg_ctl -w stop
	if [ $? -ne 0 ]
	then	echo "Can no start server.
$0 aborted." 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 path.
Install it from pgsql/contrib/pg_resetxlog and continue.;  exiting" 1>&2
	exit 1
fi

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

# 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.

SRC_XID=`pg_resetxlog -n "$OLDDIR" | grep "NextXID" | awk -F'  *' '{print $4}'`
if [ "$SRC_VERSION" = "7.1" -a "$SRC_XID" -gt 2000000000 ]
then	echo "XID too high for $0.;  exiting" 1>&2
	exit 1
fi
DST_XID=`pg_resetxlog -n data | grep "NextXID" | awk -F'  *' '{print $4}'`

# compare locales to make sure they match

pg_resetxlog -n "$OLDDIR" | grep "^LC_" > /tmp/$$.0
pg_resetxlog -n data | 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.
$0 aborted." 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.
$0 aborted." 1>&2
	exit 1
fi

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

# 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.
$0 aborted." 1>&2
	exit 1
fi

# Generate mappings for database
make_dboidmap > /tmp/$$.dboidmap
make_dbobjoidmap > /tmp/$$.dbobjoidmap

# 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 "$OBJ" : 'pg_'` -eq 3 -a \
	     `expr "$OBJ" : 'pg_largeobject'` -ne 14 ]
	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 -q "^$DB	pg_toast_$SRC_OID	" "$INFODIR"/dbobjoidmap
	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" data
if [ "$?" -ne 0 ]
then	echo "Unable to set new XID.;  exiting" 1>&2
	exit 1
fi

# Move over old WAL

rm -r data/pg_xlog
mv -f "$OLDDIR"/pg_xlog data

# Set last log file id and segment from old database

LOG_ID=`pg_resetxlog -n "$OLDDIR" | 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 "$OLDDIR" | 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" data
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

# Set sequence values for 7.1-version sequences, which were int4.

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 restore.
$0 aborted." 1>&2
		exit 1
	fi
fi

echo
echo
echo "You may remove the old database files with 'rm -r pg_upgrade'."

exit 0
@


1.34
log
@Fix recreation of sequence files for 7.2 also because of XID change from
beta4 to beta5.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.33 2002/01/13 05:36:42 momjian Exp $
@


1.33
log
@Now works for 7.1 databases.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.32 2002/01/13 04:55:44 momjian Exp $
d73 4
a76 2
		# Don't move over 7.1 sequences;  use setval() file
		if [ "$SRC_VERSION" != "7.1" ]
d218 3
a220 2

	if [ "$SRC_VERSION" = "7.1" ]
d517 3
a519 2
if [ "$SRC_VERSION" = "7.1" ]
then	echo "Set int8 sequence values from 7.1..."
@


1.32
log
@Now tested on 7.2->7.2 migration;  still disabled.  Need 7.1 testing now.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.31 2002/01/13 01:22:27 momjian Exp $
d194 1
a194 1
			for(i=1; i < NF; i++)
d201 1
a201 1
			print;
d264 2
@


1.31
log
@Keep it disabled.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.30 2002/01/13 01:13:59 momjian Exp $
d16 10
d65 1
a65 1
	grep -v '^template0$' | # template1 OK
d91 2
a92 1
		'SELECT datname, oid FROM pg_database;'
a162 13
		exit 1
	fi

	# Check that input database is of a compatible version (anything with the same
	# physical layout of user tables and indexes should be OK).  I did not write
	# something like "$SRC_VERSION -ge $UPGRADE_VERSION" because test(1) isn't bright
	# enough to compare dotted version strings properly.  Using a case statement
	# looks uglier but is more flexible.
	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
a166 1

d220 1
a220 1
		grep -v '^template0$' | # template1 OK
d222 3
a224 2
		do	
			echo "\\connect $DB"
d231 1
a231 1
				VALUE=`psql -d template1 -At -c "SELECT last_value 
d264 2
a265 1
	echo "Plase 1 completed.  Continue with the steps outlined in the $0 manual page."
d522 2
@


1.30
log
@Wow, it actually worked for the regression database.  More testing needed.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.28 2002/01/11 06:48:41 momjian Exp $
d11 1
a11 1
set -x
d14 1
a14 1
ENABLE="Y"
@


1.29
log
@Sync up.
@
text
@d6 6
a11 1
#set -x
d14 1
a14 1
ENABLE="N"
d18 91
d110 1
a110 4
# Hard-wired from pg_class in 7.1.  I wish there was another way.
# Are these fixed values for that release? XXX
SRC_LARGEOBJECT_OID=16948
SRC_LARGEOBJECT_IDX_OID=17148
d112 56
a167 4
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.28 2002/01/11 06:48:41 momjian Exp $
#
# NOTE: we must be sure to update the version-checking code a few dozen lines
# below for each new PostgreSQL release.
a168 1
trap "rm -f /tmp/$$.*" 0 1 2 3 15
d170 6
a175 7
SCHEMA=""
while [ "$#" -gt 1 ]
do
	if [ "X$1" = "X-s" ]
	then	SCHEMA="$2"
		if [ ! -s "$SCHEMA" ]
		then	echo "$SCHEMA does not exist" 1>&2
d178 79
a256 2
		shift 2
	else	echo "Usage:  $0 -s schema_dump old_data_dir" 1>&2
a258 1
done
d260 8
a267 3
if [ "$#" -ne 1 -o ! "$SCHEMA" ]
then	echo "Usage:  $0 -s schema_dump old_data_dir" 1>&2
	exit 1
d270 4
a273 1
OLDDIR="$1"
d277 3
a279 4
if [ ! -d "./data" ]
then	echo "`basename $0` must be run from the directory containing the database directory \`data\' (`dirname $PGDATA`.)" 1>&2
	echo "You must have run initdb to create the template1 database." 1>&2
	exit 1
d282 3
a284 3
if [ ! -d "./$OLDDIR" ]
then	echo "You must rename your old data directory to $OLDDIR and run initdb." 1>&2
	exit 1
d287 2
a288 3
if [ ! -d "./data/base/1" ]
then	echo "Cannot find database template1 in ./data/base." 1>&2
	echo "Are you running $0 as the postgres superuser?" 1>&2
d292 2
a293 2
if [ ! -d "./$OLDDIR/base/1" ]
then	echo "There is no database template1 in ./$OLDDIR/base." 1>&2
d297 3
a299 7
if [ ! -r "./data/PG_VERSION" ]
then	echo "Cannot read ./data/PG_VERSION --- something is wrong." 1>&2
	exit 1
fi

if [ ! -r "./$OLDDIR/PG_VERSION" ]
then	echo "Cannot read ./$OLDDIR/PG_VERSION --- something is wrong." 1>&2
d305 2
a306 2
DEST_VERSION=`cat ./data/PG_VERSION`
SRC_VERSION=`cat ./$OLDDIR/PG_VERSION`
d311 4
a314 3
if [ "$DEST_VERSION" != "$UPGRADE_VERSION" -a "$DEST_VERSION" != "$SRC_VERSION" ]
then	echo "`basename $0` is for PostgreSQL version $UPGRADE_VERSION, but ./data/PG_VERSION contains $DEST_VERSION." 1>&2
	echo "Did you run initdb for version $UPGRADE_VERSION?" 1>&2
d318 1
a318 5
# Check that input database is of a compatible version (anything with the same
# physical layout of user tables and indexes should be OK).  I did not write
# something like "$SRC_VERSION -ge $UPGRADE_VERSION" because test(1) isn't bright
# enough to compare dotted version strings properly.  Using a case statement
# looks uglier but is more flexible.
d320 7
a326 9
if [ "$ENABLE" != "Y" ]
then
	case "$SRC_VERSION" in
#	    7.2) ;;
	    *)	echo "Sorry, `basename $0` cannot upgrade database version $SRC_VERSION to $DEST_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;;
	esac
d339 1
a339 1
if ! pg_resetxlog -x 2>&1 | grep -q XID
d349 1
a349 1
if [ "$SRC_VERSION" = "7.1" -a "$XID" -gt 2000000000 ]
d364 9
d381 1
a381 1
psql "template1" < "$SCHEMA"
d383 1
a383 1
then	echo "There were errors in the input script $SCHEMA.
d388 1
a388 1
echo "Input script $SCHEMA complete, fixing row commit statuses..."
d401 3
a403 22
# Used for scans looking for a database/table name match
# New oid is looked up
pg_dumpall -s > /tmp/$$.3 2>/dev/null
if [ "$?" -ne 0 ]
then	echo "Unable to dump schema of new database.;  exiting" 1>&2
	exit 1
fi

# Get pg_largeobject oids for movement

DST_LARGEOBJECT_OID=`psql -d template1 -At -c "SELECT oid from pg_class where relname = 'pg_largeobject';"`
DST_LARGEOBJECT_IDX_OID=`psql -d template1 -At -c "SELECT oid from pg_class where relname = 'pg_largeobject_loid_pn_index';"`
if [ "$LARGEOBJECT_OID" -eq 0 -o "$LARGEOBJECT_IDX_OID" -eq 0 ]
then	echo "Unable to find large object oid.;  exiting" 1>&2
	exit 1
fi

if [ "$SRC_VERSION" = "$DST_VERSION" ]
then	# Versions are the same so we can get pg_largeobject oid this way
	SRC_LARGEOBJECT_IDX_OID="$DST_LARGEOBJECT_OID"
	SRC_LARGEOBJECT_IDX_OID="$DST_LARGEOBJECT_IDX_OID"
fi
d418 1
a418 1
cat "$SCHEMA" | while read LINE
d420 9
a428 12
	if /bin/echo "$LINE" | grep -q '^\\connect [^	]*$'
	then	OLDDB="$DB"
		DB="`/bin/echo \"$LINE\" | cut -d' ' -f2`"
		if [ "$DB" = "-" ]
		then	DB="$OLDDB"
		fi
		if [ "$DB" = "template1" -o "$DB" = "template0" ]
		then	DB=""
		fi
	fi
	if echo "$LINE" | grep -q "^-- TOC Entry ID [0-9]* (OID "
	then	OID="`echo \"$LINE\" | cut -d' ' -f7 | tr -d ')'`"
a429 57
	if echo "$LINE" | egrep -q "^-- Name: [^ ]* Type: (TABLE|INDEX|SEQUENCE) "
	then	TABLE="`echo \"$LINE\" | cut -d' ' -f3`"
		# skip system tables
		if [ "`echo \"$TABLE\" | cut -c 1-3`" = "pg_" ]
		then	TABLE=""
		fi
	fi
	if [ "$DB" -a "$OID" -a "$TABLE" ]
	then
		NEWOID=`awk -F' ' '
				BEGIN 	{ newdb=""; newoid="";
					  newtable=""; ret=0;}
				$1 == "\\\\connect" && $2 != "-" {newdb=$2;}
				$0 ~ /^-- TOC Entry ID [0-9]* .OID / \
					{ newoid = substr($7, 1, length($7)-1);}
				{print $0 >> "/tmp/x";
				print $3 >> "/tmp/x";
				print newdb," ", newoid >> "/tmp/x"}
				($0 ~ /^-- Name: [^ ]* Type: TABLE / || \
				 $0 ~ /^-- Name: [^ ]* Type: INDEX / || \
				 $0 ~ /^-- Name: [^ ]* Type: SEQUENCE /) && \
				newdb == "'"$DB"'" && \
				$3 == "'"$TABLE"'" \
					{ ret=newoid; exit}
				END { print ret;}' /tmp/$$.3`
		if [ "$NEWOID" -eq 0 ]
		then	echo "Move of database $DB, OID $OID, table $TABLE failed.
New oid not found;  exiting" 1>&2
			exit 1
		fi

		# We use stars so we don't have to worry about database oids

		# Test to make sure there is exactly one matching file on each place

 		if [ `ls "$OLDDIR"/base/*/"$OID" | wc -l` -eq 0 ]
		then	echo "Move of database $DB, OID $OID, table $TABLE failed.
File not found;  exiting" 1>&2
			exit 1
		fi
  		if [ `ls "$OLDDIR"/base/*/"$OID" | wc -l` -gt 1 ]
		then	echo "Move of database $DB, OID $OID, table $TABLE failed.
Too many found;  exiting" 1>&2
			exit 1
		fi
		if [ `ls data/base/*/"$NEWOID" | wc -l` -eq 0 ]
		then	echo "Move of database $DB, OID $OID, table $TABLE to $NEWOID failed.
File not found;  exiting" 1>&2
			exit 1
		fi
		if [ `ls data/base/*/"$NEWOID" | wc -l` -gt 1 ]
		then	echo "Move of database $DB, OID $OID, table $TABLE to $NEWOID failed.
Too many found;  exiting" 1>&2
			exit 1
		fi

		# Move files
d431 23
a453 67
		SRCDB=`basename \`dirname $OLDDIR"/base/*/"$OID"\``
		DSTDB=`basename \'dirname data/base/*/"$NEWOID"\``
		mv -f "$OLDIR"/base/"$SRCDB"/"$OID" data/base/"$DSTDB"/"$NEWOID"
		if [ "$?" -ne 0 ]
		then	echo "Move of database $DB, OID $OID, table $TABLE 
to $NEWOID failed.;  exiting" 1>&2
			exit 1
		fi
		# handle table extents
		ls "$OLDDIR"/base/"$SRCDB"/"$OID".* | while read FILE
		do
			EXT=`basename "$FILE" | sed 's/[^[^\.]*\.\(.*\)$/\1/'`
			mv -f "$FILE" data/base/"$DSTDB"/"$NEWOID"."$EXT"
			if [ "$?" -ne 0 ]
			then	echo "Move of database $DB, OID $OID, table $TABLE 
to $NEWOID failed.;  exiting" 1>&2
				exit 1
			fi
		done

		# handle pg_largeobject
		# We use the unique oid's to tell use where to move the
		# pg_largeobject files.

		if [ -f "$OLDIR"/base/"$SRCDB"/"$SRC_LARGEOBJECT_OID" ]
		then	mv "$OLDIR"/base/"$SRCDB"/"$SRC_LARGEOBJECT_OID" \
				data/base/"$DSTDB"/"$DST_LARGEOBJECT_OID"
			if [ "$?" -ne 0 ]
			then	echo "Move of large object for database $DB
to $NEWOID failed.;  exiting" 1>&2
				exit 1
			fi
	 		# handle table extents
			ls "$OLDDIR"/base/"$SRCDB"/"$SRC_LARGEOBJECT_OID".* | while read FILE
			do
				EXT=`basename "$FILE" | sed 's/[^[^\.]*\.\(.*\)$/\1/'`
				mv -f "$FILE" data/base/"$DSTDB"/"$DST_LARGEOBJECT_OID"."$EXT"
				if [ "$?" -ne 0 ]
				then	echo "Move of large object for database $DB
to $NEWOID failed.;  exiting" 1>&2
					exit 1
				fi
			done
		fi

		# Handle pg_largeobject_loid_pn_index
		if [ -f "$OLDIR"/base/"$SRCDB"/"$SRC_LARGEOBJECT_IDX_OID" ]
		then	mv "$OLDIR"/base/"$SRCDB"/"$SRC_LARGEOBJECT_IDX_OID" \
				data/base/"$DSTDB"/"$DST_LARGEOBJECT_IDX_OID"
			if [ "$?" -ne 0 ]
			then	echo "Move of large object for database $DB
to $NEWOID failed.;  exiting" 1>&2
				exit 1
			fi
	 		# handle table extents
			ls "$OLDDIR"/base/"$SRCDB"/"$SRC_LARGEOBJECT_IDX_OID".* | while read FILE
			do
				EXT=`basename "$FILE" | sed 's/[^[^\.]*\.\(.*\)$/\1/'`
				mv -f "$FILE" data/base/"$DSTDB"/"$DST_LARGEOBJECT_IDX_OID"."$EXT"
				if [ "$?" -ne 0 ]
				then	echo "Move of large object for database $DB
to $NEWOID failed.;  exiting" 1>&2
					exit 1
				fi
			done
		fi
		TABLE=""
d456 1
a456 1

d463 1
d480 1
a480 1
# Set last checkpoint location from old database
d482 10
a491 4
CHKPOINT=`pg_resetxlog -n "$OLDDIR" | grep "checkpoint location:" |
	awk -F'  *' '{print $4}'`
if [ "$CHKPOINT" = "" ]
then	echo "Unable to get old checkpoint location.;  exiting" 1>&2
d497 1
a497 1
pg_resetxlog -l `echo "$CHKPOINT" | tr '/' ' '` data
d499 1
a499 1
then	echo "Unable to set new checkpoint location.;  exiting" 1>&2
d515 3
a517 80

	psql -d template1 -At -c "SELECT datname FROM pg_database" | 
	grep -v '^template0$' | # template1 OK
	while read DB
	do	
		echo "$DB"
		# XXX is concurrency a problem here?
		psql -d "$DB" -At -c "SELECT relname FROM pg_class where relkind = 'S';" |
		grep -v '^pg_' | # no system tables
		while read SEQUENCE
		do
			psql -d "$DB" -At <<SQL_END

-- This table matches the 7.1 sequence schema
CREATE TABLE pg_upgrade_temp_seq_int4 (
	sequence_name name
	last_value    integer
	increment_by  integer
	max_value     integer
	min_value     integer
	cache_value   integer
	log_cnt       integer
	is_cycled     "char"
	is_called     "char"
);

-- Move int8 columns of sequence out of the way
UPDATE pg_attribute
SET attrelid = 1 -- OID of template1, not used anywhere else  XXX correct?
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '$SEQUENCE');

-- Replace with int4 sequence columns
UPDATE pg_attribute
SET attrelid = 
	(SELECT oid FROM pg_class WHERE relname = '$SEQUENCE')
WHERE attrelid = 
	(SELECT oid FROM pg_class WHERE relname = 'pg_upgrade_temp_seq_int4');

-- Mark sequence as ordinary table so we can do COPY
UPDATE pg_class
SET relkind = 't'
WHERE relname = '$SEQUENCE';

-- COPY sequence out
COPY "$SEQUENCE" TO '/tmp/$$';

-- Delete int4 row from sequence
-- XXX truncate ok?
TRUNCATE "$SEQUENCE";

-- Prepare int4 sequence table for removal and remove it
UPDATE pg_attribute
SET attrelid = 
	(SELECT oid FROM pg_class WHERE relname = 'pg_upgrade_temp_seq_int4')
WHERE attrelid = 
	(SELECT oid FROM pg_class WHERE relname = '$SEQUENCE');

DROP TABLE pg_upgrade_temp_seq_int4;

-- Restore int8 version of sequence
UPDATE pg_attribute
SET attrelid = (SELECT oid FROM pg_class WHERE relname = '$SEQUENCE')
WHERE attrelid = 1;

-- Load new values
COPY "$SEQUENCE" FROM '/tmp/$$';

-- If previous max was int4, make it int8
UPDATE "$SEQUENCE"
SET max_value = 9223372036854775807
WHERE max_value BETWEEN 2147483646 AND 2147483648; -- OS rounding

-- Restore sequence flag
UPDATE pg_class
SET relkind = 'S'
WHERE relname = '$SEQUENCE';

SQL_END
			if [ $? -ne 0 ]
			then	echo "There were errors during int4 sequence restore.
d519 2
a520 4
				exit 1
			fi
		done
	done
d523 1
a523 1
echo "You may remove the $OLDDIR directory with 'rm -r $OLDDIR'."
@


1.28
log
@More testing, more cleanups.
@
text
@d8 12
a19 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.27 2002/01/11 06:33:01 momjian Exp $
d24 1
a24 3
TMPFILE="/tmp/pgupgrade.$$"

trap "rm -f $TMPFILE" 0 1 2 3 15
d83 1
a89 3
# UPGRADE_VERSION is the expected output database version
UPGRADE_VERSION="7.1"

d102 12
a113 7
case "$SRC_VERSION" in
#    7.2) ;;
    *)	echo "Sorry, `basename $0` cannot upgrade database version $SRC_VERSION to $DEST_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;;
esac
d132 1
a132 1
XID=`pg_resetxlog -n "$OLDDIR" | grep "NextXID" | awk -F'  *' '{print $4}'`
d137 1
d139 11
d151 2
d178 1
a178 1
pg_dumpall -s > $TMPFILE 2>/dev/null
d184 15
d210 2
d251 1
a251 1
				END { print ret;}' $TMPFILE`
d257 1
d259 3
d282 6
a287 1
		mv -f "$OLDDIR"/base/*/"$OID" data/base/*/"$NEWOID"
d293 58
d356 2
a357 1
# Set this so the next VACUUM sets the old row XID's as "frozen"
d359 6
d366 1
a366 1
pg_resetxlog -x "$XID" data
d372 5
d408 1
a408 2
	grep -v '^template0$' | # no system databases
	grep -v '^template1$' | # no system databases
@


1.27
log
@pg_resetxlog specifies log location in hex;  more pg_upgrade testing
improvments.
@
text
@d8 1
a8 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.26 2002/01/11 06:08:02 momjian Exp $
d161 1
a161 1
pg_ctl stop
d274 1
a274 1
pg_ctl start
d286 2
d293 1
@


1.26
log
@More cleanups.
@
text
@d6 3
a8 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.25 2002/01/11 05:54:59 momjian Exp $
d111 1
a111 1
if ! pg_resetxlog -x | grep -q XID
d266 1
a266 1
pg_resetxlog -l `echo "$CHKPOINT | tr '/' ' '` data
d283 1
a283 1
else	echo "Set int8 sequence values from 7.1..."
d360 5
a364 4
		if [ $? -ne 0 ]
		then	echo "There were errors during int4 sequence restore.
	$0 aborted." 1>&2
			exit 1
@


1.25
log
@More updates for int8 sequence restoration.  Old dump file not required
anymore.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.24 2002/01/11 04:39:19 momjian Exp $
d102 1
d245 2
d253 1
a253 1
# set last checkpoint location from old database
d262 2
d279 1
@


1.24
log
@Handle int4-int8 sequence migration without full data dump.
@
text
@d4 1
a4 1
# CAUTION: read the manual page before trying to use this!
d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.23 2002/01/11 00:27:42 momjian Exp $
d110 1
a110 1
Install a newer version from pgsql/contrib/pg_resetxlog and continue.;  exiting" 1>&2
d125 1
a125 2
# Execute the schema script to create everything, except modify any
# sequences with int4 maximums if we are upgrading from 7.1.
d127 1
a127 17
cat $SCHEMA | awk -F' ' '{
				if ("'"$SRC_VERSION"'" == "7.1" &&
				    $1 == "CREATE" &&
				    $2 == "SEQUENCE" &&
				    ($9 >= 2147483646 && # handle OS rounding
				    ($9 <= 2147483648))
				{
					for(i=1; i < NF; i++)
						if (i != 9)
							printf "%s ", $i;
						else
							printf "%s ", "9223372036854775807";
					print;
				}
				else	print $0;
			}' |
psql "template1"
d140 3
a142 5
psql -d template1 -At -c "SELECT datname FROM pg_database" | while read DB
do
	echo "VACUUM;" | psql "$DB"
	if [ $? -ne 0 ]
	then	echo "There were errors during VACUUM.
d144 2
a145 3
		exit 1
	fi
done
d147 1
a147 1
# Used for scans looking for a database/tablename match
d181 1
a181 10
	if echo "$LINE" | egrep -q "^-- Name: [^ ]* Type: (TABLE|INDEX) "
	then	TABLE="`echo \"$LINE\" | cut -d' ' -f3`"
		# skip system tables
		if [ "`echo \"$TABLE\" | cut -c 1-3`" = "pg_" ]
		then	TABLE=""
		fi
	fi
	# 7.1 sequences were handled earlier because they were int4.
	if test "$SRC_VERSION" != "7.1" &&
	   echo "$LINE" | egrep -q "^-- Name: [^ ]* Type: SEQUENCE "
d252 1
a252 1
CHKPOINT=`pg_resetxlog -n "$OLDDIR" | grep "checkpoint location" | 
d288 1
a288 1
CREATE TABLE temp_seq_int4 (
d300 1
a300 1
-- Move int8 version of sequence out of the way
d305 1
a305 1
-- Replace with int4 sequence schema
d307 12
a318 2
SET attrelid = (SELECT oid FROM pg_class WHERE relname = '$SEQUENCE')
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'temp_seq_int4');
d320 3
a322 4
-- Select sequence value into temp table
CREATE TEMP TABLE hold_sequence AS
SELECT last_value
FROM "$SEQUENCE"
d326 4
a329 2
SET attrelid = (SELECT oid FROM pg_class WHERE relname = 'temp_seq_int4')
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '$SEQUENCE');
d331 1
a331 1
DROP TABLE temp_seq_int4;
d338 2
a339 4
-- Mark sequence as ordinary table and update it
UPDATE pg_class
SET relkind = 't'
WHERE relname = '$SEQUENCE';
d341 1
d343 2
a344 1
SET last_value = (SELECT last_value FROM hold_sequence);
@


1.23
log
@More improvements using pg_resetxlog.  still disabled.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.22 2002/01/10 04:58:19 momjian Exp $
a15 1
DATA=""
d25 1
a25 8
	elif [ "X$1" = "X-d" ]
	then	DATA="$2"
		if [ ! -s "$DATA" ]
		then	echo "$DATA does not exist" 1>&2
			exit 1
		fi
		shift 2
	else	echo "Usage:  $0 -s schema_dump [ -d data_dump ] old_data_dir" 1>&2
d31 1
a31 1
then	echo "Usage:  $0 -s schema_dump [ -d data_dump ] old_data_dir" 1>&2
a80 15
if [ "$SRC_VERSION" = "7.1" -a ! "$DATA" ]
then	echo "$0 requires a full data dump file to upgrade from version $SRC_VERSION." 1>&2
	echo "Use the '-d' parameter to specify the data dump file" 1>&2
	echo "If you don't have enough disk space to keep a dump file, grep out the '\\connect' and" 1>&2
	echo "'SELECT setval' lines from the dump file and pass that file to $0, e.g:" 1>&2
	echo 1>&2
	echo "	pg_dumpall | egrep '^(\\connect)|SELECT setval \()[^	]*$' > data.out" 1>&2
	exit 1
fi

if [ "$SRC_VERSION" != "7.1" -a "$DATA" ]
then	echo "$0 does not require the -d option for this version." 1>&2
	exit 1
fi

d114 2
a115 2
# We need a high XID number so there is 1 gig gap in XID numbers so the
# moved-over rows can be frozen on next VACUUM.
d151 1
a151 14
# Set sequence values for 7.1-version sequences, which are int4.
if [ "$SRC_VERSION" != "7.1" ]
then	echo "Input script $SCHEMA complete, fixing row commit statuses..."
else	echo "Input script $SCHEMA complete, setting int8 sequences..."

	# Set all the sequence counters because they are not brought over
	# in the schema dump.
	cat $DATA | egrep '^(\\connect)|SELECT setval \()[^	]*$' |
	psql "template1"
	if [ $? -ne 0 ]
	then	echo "There were errors in setting the sequence values.
$0 aborted." 1>&2
		exit 1
	fi
d153 3
a155 5
	echo "Int8 sequences set, fixing row commit statuses..."
fi

# Now vacuum each result database in case our transaction increase
# causes all the XID's to be marked with the frozen XID.
d300 76
@


1.22
log
@More pg_upgrade improvements.  Almost done, except for max transaction
setting.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.21 2002/01/10 03:05:48 momjian Exp $
d124 21
d150 1
d194 1
d215 1
d309 21
a329 22
# 7.1 has non-compressed log file format
if [ "$SRC_VERSION" = "7.1" ]
then
	# pg_log is oid 1269 in 7.1
	LOGSIZE=`ls -l "$OLDDIR"/global/1269 "$OLDDIR"/global/1269.* 2>/dev/null |
	awk -F'  *' '
		BEGIN 	{sum=0;}
			{sum += $5;}
		END	{print sum;}'`

# check < 2gig

# set max transaction id

else
	rm -r data/pg_clog &&
	mv "$OLDDIR"/data/pg_clog data/pg_clog &&
	mv "$OLDDIR"/data/global/pg_control data/global/pg_control
	if [ "$?" -ne 0 ]
	then	echo "Moving of transaction and control files failed.;  exiting" 1>&2
		exit 1
	fi
d331 2
@


1.21
log
@More improvements;  still disabled.  (Don't panic.)
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.20 2002/01/09 21:50:52 momjian Exp $
d80 2
a81 2
DESTVERSION=`cat ./data/PG_VERSION`
SRCVERSION=`cat ./$OLDDIR/PG_VERSION`
d86 2
a87 2
# MYVERSION is the expected output database version
MYVERSION="7.1"
d89 7
a95 3
if [ "$SRCVERSION" = "7.1" -a ! "$DATA" ]
then	echo "$0 requires a full data dump file to upgrade from version $SRCVERSION." 1>&2
	echo "Use the '-d' parameter to specify the dump file" 1>&2
d99 8
a106 3
if [ "$DESTVERSION" != "$MYVERSION" -a "$DESTVERSION" != "$SRCVERSION" ]
then	echo "$0 is for PostgreSQL version $MYVERSION, but ./data/PG_VERSION contains $DESTVERSION." 1>&2
	echo "Did you run initdb for version $MYVERSION?" 1>&2
d112 1
a112 1
# something like "$SRCVERSION -ge $MINVERSION" because test(1) isn't bright
d116 1
a116 1
case "$SRCVERSION" in
d118 1
a118 1
    *)	echo "Sorry, `basename $0` cannot upgrade database version $SRCVERSION to $DESTVERSION." 1>&2
d120 1
a120 1
	echo "You will need to dump and restore using pg_dump." 1>&2
d125 1
a125 1
# OK, ready to proceed.
d130 1
a130 1
				if ("'"$SRCVERSION"'" == "7.1" &&
d133 1
a133 1
				    ($9 >= 2147483646 && # handle OS round
a145 1

d152 2
a153 2

if [ "$SRCVERSION" != "7.1" ]
d157 6
a162 9
# Set all the sequence counters because they are not brought over
# in the schema dump, and the old 7.1 sequences where int4 in size
# so bringing over the file wouldn't help us anyway.
cat $DATA | awk '$0 == "\\connect " || "SELECT setval (" \
			{print $0;}' |
psql "template1"

if [ $? -ne 0 ]
then	echo "There were errors in the input script $SCHEMA.
d164 2
a165 2
	exit 1
fi
d167 1
a167 1
echo "Int8 sequences set, fixing row commit statuses..."
d182 2
a183 1
# should be pretty small file
d185 4
d190 2
a191 1
# flush buffers to disk
d202 1
a202 1
	if /bin/echo "$LINE" | grep -q "^\\\\connect [^	]*$"
d222 9
d234 1
a234 1
				BEGIN 	{ newdb=""; newoid=""; 
d242 3
a244 2
				($0 ~ /^-- Name: [^ ]* Type: TABLE / && \
				 $0 ~ /^-- Name: [^ ]* Type: INDEX /) && \
d250 2
a251 1
		then	echo "Move of database $DB, OID $OID, table $TABLE failed.\nNew oid not found;  exiting" 1>&2
d256 2
a257 1
		then	echo "Move of database $DB, OID $OID, table $TABLE failed.\nFile not found;  exiting" 1>&2
d261 2
a262 1
		then	echo "Move of database $DB, OID $OID, table $TABLE failed.\nToo many found;  exiting" 1>&2
d266 2
a267 1
		then	echo "Move of database $DB, OID $OID, table $TABLE to $NEWOID failed.\nFile not found;  exiting" 1>&2
d271 2
a272 1
		then	echo "Move of database $DB, OID $OID, table $TABLE to $NEWOID failed.\nToo many found;  exiting" 1>&2
d277 2
a278 1
		then	echo "Move of database $DB, OID $OID, table $TABLE \n to $NEWOID failed.;  exiting" 1>&2
d286 1
a286 1
if [ "$SRCVERSION" = "7.1" ]
a299 1
	# how to handle 7.2?
@


1.20
log
@Update with new features.  Still disabled.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.19 2002/01/09 16:08:54 momjian Exp $
d167 1
a167 1
psql -l | while read DB
d182 4
d191 1
a191 1
	if /bin/echo "$LINE" | grep -q "^\\\\connect "
a257 2
# set max transaction id, check < 2gig

d260 27
a286 6
# pg_log is oid 1269 in 7.1
LOGSIZE=`ls -l "$OLDDIR"/global/1269 "$OLDDIR"/global/1269.* 2>/dev/null |
awk -F'  *' '
	BEGIN 	{sum=0;}
		{sum += $5;}
	END	{print sum;}'`
a288 1
echo "You must stop/start the postmaster before doing anything else."
@


1.19
log
@Remove clog copy.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.18 2002/01/09 16:00:46 momjian Exp $
d15 19
a33 10
if [ "$#" -eq 0 ]
then	echo "Usage:  $0 -f inputfile old_data_dir" 1>&2
	exit 1
fi

if [ "X$1" = "X-f" ]
then	INPUT="$2"
	shift 2
	if [ ! -f "$INPUT" ]
	then	echo "$INPUT does not exist" 1>&2
d36 1
a36 3
else	echo "Usage:  $0 -f inputfile old_data_dir" 1>&2
	exit 1
fi
d38 2
a39 2
if [ "$#" -ne 1 ]
then	echo "Usage:  $0 -f inputfile old_data_dir" 1>&2
d48 1
a48 2
then	echo "`basename $0` must be run from the directory containing
the database directory \`data\' (`dirname $PGDATA`.)" 1>&2
d89 6
d118 16
a133 10
# Execute the input script to create everything, except that we remove
# any COPY statements, except for the ones that load pg_shadow/pg_group.
# There shouldn't be any others in there anyway...

cat $INPUT | awk '	{
				if (tolower($1) == "copy" &&
				    $2 != "pg_shadow" &&
				    $2 != "pg_group")
					while (getline $0 > 0 && $0 != "\\.")
						;
d135 2
a136 3
			}' > $TMPFILE

psql "template1" < $TMPFILE
d139 1
a139 1
then	echo "There were errors in the input script $INPUT.
a143 20
echo "Input script $INPUT complete, fixing row commit statuses..."

# Now vacuum each result database to mark all system-table rows as committed,
# because when pg_clog is replaced with the saved version, the transaction
# statuses will no longer match the data.  VACUUM will force the on-row
# status flags to the right value so that pg_clog will not matter anymore.
# Note: we used to try to do this as part of the previous step, but that
# risks permissions problems if VACUUM is run as the wrong user.
# Note: the initial VACUUM does template1, then we do everything else.

cat $INPUT | awk 'BEGIN	{ print "VACUUM;" }
			{
				if (tolower($1) == "copy")
					while (getline $0 > 0 && $0 != "\\.")
						;
				else if (tolower($1) == "\\connect" && 
					 $2 != "-" &&
					 $2 != "template1")
					printf "\\connect %s\nVACUUM;\n", $2;
			}' > $TMPFILE
d145 10
a154 1
psql "template1" < $TMPFILE
d157 1
a157 1
then	echo "There were errors in the vacuuming step.
d162 15
d185 1
a185 1
cat "$INPUT" | while read LINE
d200 1
a200 1
	if echo "$LINE" | grep -q "^-- Name: [^ ]* Type: TABLE "
d218 2
a219 1
				$0 ~ /^-- Name: [^ ]* Type: TABLE / && \
d253 12
@


1.18
log
@Make improvements to pg_upgrade; is still disabled.
@
text
@d6 1
a6 1
# $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.17 2001/08/25 18:52:42 tgl Exp $
a230 3

rm -r data/pg_clog
mv -f $OLDDIR/pg_clog data
@


1.17
log
@Replace implementation of pg_log as a relation accessed through the
buffer manager with 'pg_clog', a specialized access method modeled
on pg_xlog.  This simplifies startup (don't need to play games to
open pg_log; among other things, OverrideTransactionSystem goes away),
should improve performance a little, and opens the door to recycling
commit log space by removing no-longer-needed segments of the commit
log.  Actual recycling is not there yet, but I felt I should commit
this part separately since it'd still be useful if we chose not to
do transaction ID wraparound.
@
text
@d6 1
a6 1
# $Header: /home/projects/pgsql/cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.16 2000/12/18 16:30:07 momjian Exp $
d52 1
a52 1
if [ ! -d "./data/base/template1" ]
d58 1
a58 1
if [ ! -d "./$OLDDIR/base/template1" ]
d81 1
a81 1
MYVERSION="7.0"
d83 1
a83 1
if [ "$DESTVERSION" != "$MYVERSION" ]
d96 1
a96 1
#    7.0) ;;
d117 1
a117 1
			}' >$TMPFILE
d119 1
a119 1
psql "template1" <$TMPFILE
d146 1
a146 1
			}' >$TMPFILE
d148 1
a148 1
psql "template1" <$TMPFILE
d156 6
d164 1
a164 1
for DIR in data/base/*
d166 63
a228 11
	BASEDIR="`basename $DIR`"
	if [ -d "$DIR" -a \
	     -d "$OLDDIR/base/$BASEDIR" -a \( "$BASEDIR" != "template1" \) ]
	then	for FILE in $OLDDIR/base/$BASEDIR/*
		do
			BASEFILE="`basename $FILE`"
			if [ `expr "$BASEFILE" : "pg_"` -ne 3 -a \
				"$BASEFILE" != "PG_VERSION" ]
			then	mv -f $FILE $DIR
			fi
		done
d232 1
a233 1
mv -f $OLDDIR/pg_variable data
@


1.16
log
@Updates for 7.1 branding.
@
text
@d6 1
a6 1
# $Header: /home/projects/pgsql/cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.15 2000/05/05 03:08:20 tgl Exp $
d130 1
a130 1
# because when pg_log is replaced with the saved version, the transaction
d132 1
a132 1
# status flags to the right value so that pg_log will not matter anymore.
d174 1
a174 1
mv -f $OLDDIR/pg_log data
@


1.15
log
@Accept pg_group as well as pg_shadow data from dumpall script.
Rearrange handling of VACUUMs so that they are certain to be executed
as superuser not some random user; also, do not forget to vacuum
template1 itself.
@
text
@d6 1
a6 1
# $Header: /usr/local/cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.14 2000/02/23 15:46:12 momjian Exp $
d96 1
a96 2
    6.5) ;;
    7.0) ;;
@


1.14
log
@1. miscadmin.h needs to include sys/types.h for a definition of pid_t

2. Regression tests fail for types int2 and int4 (which can easily be
fixed by adding entries to resultmap) aswell as float8 and geometry,
where floating point numbers appear to be rounded a little differently
than in your expected results (besides that I also need the positive
zeros file). I'm including a patch for the first 2, but I don't know
whether the latter two are actually a bug in postgres or a bug in the
OS or even allowed difference. I'm including my results for reference.

Rolf Grossmann
@
text
@d6 1
a6 1
# $Header: /usr/local/cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.13 1999/09/28 18:04:18 momjian Exp $
d11 3
a13 1
trap "rm -f /tmp/$$" 0 1 2 3 15
d16 1
a16 1
then	echo "Usage:  $0 [-f inputfile] old_data_dir" 1>&2
d27 2
a28 1
else	INPUT=""
d32 1
a32 1
then	echo "Usage:  $0 [-f inputfile] old_data_dir" 1>&2
d107 2
a108 1
# Remove any COPY statements, except for the one that loads pg_shadow.
a109 3
# Also marks rows as committed because when pg_log is replaced with
# the saved version, the transaction statuses may be wrong, so
# vacuum sets the on-row status to the proper value.
d112 3
a114 1
				if (toupper($1) == "COPY" && $2 != "pg_shadow")
a116 3
				else if (tolower($1) == "\\connect" && 
					 tolower($2) == "template1")
					printf "VACUUM;\n%s\n", $0;
d118 1
a118 1
			}' >/tmp/$$
d120 1
a120 2
# We need to vacuum the last database too
echo "VACUUM;" >>/tmp/$$
d122 5
a126 1
# Create and vacuum empty tables/indexes
d128 22
a149 1
psql "template1" <"/tmp/$$"
d152 1
a152 1
then	echo "There were errors in the input script $INPUT.
d157 1
a157 1
echo "Input script $INPUT complete, moving data files..."
d180 1
@


1.13
log
@Fix pg_upgrade so it vacuums all databases.
@
text
@d6 1
a6 1
# $Header: /usr/local/cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.12 1999/09/28 16:02:28 momjian Exp $
d78 1
a78 1
MYVERSION="6.6"
d94 1
a94 1
    6.6) ;;
@


1.12
log
@pg_upgrade reminder.
@
text
@d6 1
a6 1
# $Header: /usr/local/cvsroot/pgsql/src/bin/pg_dump/pg_upgrade,v 1.11 1999/08/02 22:34:53 tgl Exp $
a102 1
# XXX Do I need to create a database?
d104 5
a108 2
# remove any COPY statements, except for the one that loads pg_shadow.
# there shouldn't be any others in there anyway...
d114 3
d120 1
a120 5
# Add a VACUUM command to the end of the pg_dump script.  With MVCC,
# this is necessary to ensure that all the rows in the new database's
# system tables will still be considered committed after we overwrite
# pg_log with the old database's commit log...

@


1.11
log
@Add a VACUUM command in hopes of making pg_upgrade usable again
in MVCC environment.  I do not trust this until Vadim says it's OK...
@
text
@d6 1
a6 1
# $Header$
d153 1
@


1.10
log
@Re-enable pg_upgrade, after adding checks that the source
and target databases are of versions it knows about.
@
text
@d6 2
d114 9
a122 2
 
#create empty tables/indexes
@


1.9
log
@Cleanup
@
text
@d3 1
a3 1
# pg_upgrade: update a database without needing a full dump/reload cycle
d6 2
a7 3
echo "pg_upgrade is disabled in this release because the on-disk structure" 1>&2
echo "of the tables has changed compared to previous releases." 1>&2
exit 1
d35 1
a35 1
if [ ! -f "./data/PG_VERSION" ]
d37 2
a38 1
the database directory \`data' (`dirname $PGDATA`.)" 1>&2
d43 7
a49 1
then	echo "You must rename your old /data directory to /$OLDDIR and run initdb." 1>&2
d54 1
a54 1
then	echo "There is not database template1 in ./$OLDDIR/base." 1>&2
d58 7
a64 2
if [ ! -d "./data" ]
then	echo "You must run initdb to create the template1 database." 1>&2
d68 13
a80 2
if [ ! -d "./data/base/template1" ]
then	echo "$0 must be run as the postgres superuser." 1>&2
d84 5
a88 1
# do I need to create a database?
d90 15
a104 3
# remove any COPY statements
# we don't even need pgdump_oid because we are moving pg_variable
# then shouldn't be in there anyway
d123 2
d135 1
a135 1
			then	mv $FILE $DIR
d141 2
a142 2
mv $OLDDIR/pg_log data
mv $OLDDIR/pg_variable data
d145 1
@


1.8
log
@Disable pg_upgrade for 6.5.
@
text
@d5 1
@


1.7
log
@I attach a patch for pg_upgrade.  This does two things:

1. check whether the program is being executed in $PGDATA/..  This is
   necessary if the data tree is not in the standard place, as is the
   case with the Debian distribution (because of Debian policy).

2. give a clearer error message if the dumped data structure fails to
   be loaded.

Oliver Elphick
@
text
@d5 3
@


1.6
log
@cleanup
@
text
@d32 3
a34 2
if [ ! -f "./lib/global1.bki.source" ]
then	echo "$0 must be run from the top of the postgres directory tree." 1>&2
d76 2
a77 1
then	echo "psql failed to execute $INPUT script." 1>&2
@


1.5
log
@Fix from Tom Lane for pg_shadow in pg_upgrade
@
text
@d64 1
a64 1
				if (toupper($1) != "COPY" && $2 != "pg_shadow")
@


1.4
log
@Fix pg_upgrade to notice if psql fails to execute the given
script, and not proceed with the update if so.
Also add #!/bin/sh header line.
@
text
@d64 1
a64 1
				if (toupper($0) ~ /^COPY /)
@


1.3
log
@Preserver pg_log and pg_variable in upgrade.  Cleanups.
@
text
@d1 5
a5 1
:
d74 5
d98 1
a98 1
echo "You may removed the $OLDDIR directory with 'rm -r $OLDDIR'."
@


1.2
log
@Make old db directory configurable.
@
text
@d5 1
a5 1
then	echo "Usage:  $0 [-f inputfile] old_data_dir database" 1>&2
d19 2
a20 2
if [ "$#" -ne 2 ]
then	echo "Usage:  $0 [-f input_file] old_data_dir database" 1>&2
a24 1
DATABASE="$2"
d38 2
a39 2
if [ ! -d "./$OLDDIR/data/base/$DATABASE" ]
then	echo "There is not database $DATABASE in ./$OLDDIR/data/base." 1>&2
d55 3
a57 7
if [ "$DATABASE" != "template1" ]
then	echo "Dropping and recreating database $DATABASE." 1>&2
	destroydb "$DATABASE" >/dev/null 2>&1
	createdb "$DATABASE"
fi

# remove any COPY statements, preserve pgdump_oid setting from pg_dumpall
d60 1
a60 2
				if (toupper($0) ~ /^COPY / &&
				    toupper($0) !~ /^COPY[ 	]*PGDUMP_OID/ )
d68 1
a68 2
psql "$DATABASE" <"/tmp/$$"
set -x
d74 2
a75 3
	     -d "$OLDDIR/$DIR" -a \
		\( "$DATABASE" = "$BASEDIR" -o "$DATABASE" = "template1" \) ]
	then	for FILE in $OLDDIR/$DIR/*
d85 3
@


1.1
log
@New pg_upgrade command.
@
text
@d5 1
a5 1
then	echo "Usage:  $0 [-f inputfile] database" 1>&2
d19 2
a20 2
if [ "$#" -ne 1 ]
then	echo "Usage:  $0 [-f input_file] database" 1>&2
d24 2
a25 1
DATABASE="$1"
d34 7
a40 2
if [ ! -d "./data.upgrade" ]
then	echo "You must rename your old /data directory to /data.upgrade and run initdb." 1>&2
d57 2
a58 1
then	destroydb "$DATABASE"
d62 1
a62 1
# remove COPY statements, preserve pgdump_oid setting from pg_dumpall
d81 1
a81 1
	     -d "data.upgrade/$DIR" -a \
d83 1
a83 1
	then	for FILE in data.upgrade/$DIR/*
d94 1
a94 1
echo "You may removed the data.upgrade directory with 'rm -r data.upgrade'."
@

