#!/usr/bin/perl -wT

# Upgrade a PostgreSQL cluster to a newer major version.
#
# (C) 2005-2009 Martin Pitt <mpitt@debian.org>
# (C) 2013 Peter Eisentraut <petere@debian.org>
# (C) 2013 Christoph Berg <myon@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

use strict;

use lib '/usr/share/postgresql-common';
use PgCommon;
use Getopt::Long;
use POSIX;

# untaint environment
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

my ($version, $newversion, $cluster);
my (%info, %newinfo);
my ($encoding, $old_lc_ctype, $old_lc_collate);

# do not trip over cwd not being accessible to postgres superuser
chdir '/';

sub adapt_conffiles {
    my %c = read_cluster_conf_file $newversion, $cluster, 'postgresql.conf';

    # Arguments: <ref to conf hash> <name> <comment>
    sub deprecate {
        if (defined ${$_[0]}{$_[1]}) {
            PgCommon::disable_conf_value $newversion, $cluster,
                'postgresql.conf', $_[1], $_[2];
        }
    }

    # Arguments: <ref to conf hash> <old name> <new name>
    sub rename_ {
        my ($conf, $old, $new) = @_;
        if (defined ${$conf}{$old}) {
            PgCommon::replace_conf_value $newversion, $cluster,
                'postgresql.conf', $old, "deprecated in favor of $new", 
                $new, ${$conf}{$old};
        }
    }

    # Arguments: <string>, <from>, <to>
    sub strrepl {
        my ($s, $f, $t) = @_;
        for(;;) {
            my $i = index $s, $f;
            last if $i < 0;
            substr($s, $i, (length $f)) = $t;
        }
        return $s;
    }

    # adapt paths to configuration files
    PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
	'hba_file', strrepl($c{'hba_file'}, $version, $newversion) if $c{'hba_file'};
    PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
	'ident_file', strrepl($c{'ident_file'}, $version, $newversion) if $c{'ident_file'};
    PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
	'external_pid_file', strrepl($c{'external_pid_file'}, $version, $newversion) if $c{'external_pid_file'};
    PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
        'data_directory', $newinfo{'pgdata'};

    if ($newversion >= '8.2') {
        # preload_libraries -> shared_preload_libraries transition
        rename_ \%c, 'preload_libraries', 'shared_preload_libraries';

	# australian_timezones -> timezone_abbreviations transition
	my $australian_timezones = config_bool $c{'australian_timezones'};
	if (defined $australian_timezones) {
	    PgCommon::replace_conf_value $newversion, $cluster, 'postgresql.conf',
		    'australian_timezones', 'deprecated in favor of timezone_abbreviations', 
		    'timezone_abbreviations', ($australian_timezones ?  'Australia' : 'Default');
	}
    }

    if ($newversion >= '8.3') {
        deprecate \%c, 'bgwriter_lru_percent', 'deprecated';
        deprecate \%c, 'bgwriter_all_percent', 'deprecated';
        deprecate \%c, 'bgwriter_all_maxpages', 'deprecated';

        rename_ \%c, 'redirect_stderr', 'logging_collector';

        rename_ \%c, 'stats_command_string', 'track_activities';
        deprecate \%c, 'stats_start_collector', 'deprecated, always on now';
        deprecate \%c, 'stats_reset_on_server_start', 'deprecated';

	# stats_block_level and stats_row_level are merged into track_counts
	if ($c{'stats_block_level'} || $c{'stats_row_level'}) {
	    deprecate \%c, 'stats_block_level', 'deprecated in favor of track_counts';
	    deprecate \%c, 'stats_row_level', 'deprecated in favor of track_counts';
	    PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
		'track_counts', (config_bool $c{'stats_block_level'} || config_bool $c{'stats_row_level'}) ? 'yes' : 'no';
	}

        # archive_command now has to be enabled explicitly
        if ($c{'archive_command'}) {
            PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
                'archive_mode', 'yes';
        }
    }

    if ($newversion >= '8.4') {
        deprecate \%c, 'max_fsm_pages', 'not needed any more';
        deprecate \%c, 'max_fsm_relations', 'not needed any more';
        deprecate \%c, 'krb_server_hostname', 'does not exist any more';
        deprecate \%c, 'krb_realm', 'does not exist any more';
        rename_ \%c, 'explain_pretty_print', 'debug_pretty_print';

	# "ident sameuser" -> "ident"
	my $hba = "$PgCommon::confroot/$newversion/$cluster/pg_hba.conf";
	open O, $hba or error "open $hba: $!";
	open N, ">$hba.new" or error "open $hba.new: $!";
	while (<O>) {
	    s/ident sameuser/ident/;
	    print N $_;
	}
	close O;
	close N;
	chown $newinfo{'owneruid'}, $newinfo{'ownergid'}, "$hba.new";
	chmod 0640, "$hba.new";
	rename "$hba.new", $hba or error "rename: $!";
    }

    if ($newversion >= '9.0') {
	deprecate \%c, 'add_missing_from', 'does not exist any more';
	deprecate \%c, 'regex_flavor', 'does not exist any more';
    }

    if ($newversion >= '9.2') {
	deprecate \%c, 'wal_sender_delay', 'does not exist any more';
	deprecate \%c, 'silent_mode', 'does not exist any more';
	deprecate \%c, 'custom_variable_classes', 'does not exist any more';

        # SSL certificate paths have an explicit option now, older versions use
        # a symlink
	for my $f (['server.crt', 'ssl_cert_file'],
		   ['server.key', 'ssl_key_file'],
		   ['root.crt', 'ssl_ca_file'],
		   ['root.crl', 'ssl_crl_file']) {
	    my $file = "$newinfo{'pgdata'}/$f->[0]";
	    if (-l $file) { # migrate symlink to config entry with link target
		PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
		    $f->[1], (readlink $file);
		unlink $file;
	    } elsif (-e $file) { # plain file in data dir, put in config
		PgCommon::set_conf_value $newversion, $cluster, 'postgresql.conf',
		    $f->[1], $file;
	    }
	}
    }

    if ($newversion >= '9.3') {
        rename_ \%c, 'unix_socket_directory', 'unix_socket_directories';
        rename_ \%c, 'replication_timeout', 'wal_sender_timeout';
    }
}

# Save original pg_hba.conf, replace it with one that only allows local access
# to the owner. Database must be reloaded manually if it is running.
# Arguments: <version> <cluster> <owner> <owneruid>
sub disable_connections {
    my $hba = "$PgCommon::confroot/$_[0]/$_[1]/pg_hba.conf";

    return if (-e "$hba.pg_upgradecluster"); # leftover from a previous upgrade
		# attempt, do not rename again so we do not lose the original
    rename $hba, "$hba.pg_upgradecluster" or error "could not rename $hba: $!";
    unless (open F, ">$hba") {
        rename "$hba.pg_upgradecluster", $hba;
        error "could not create $hba";
    }
    chmod 0400, $hba;
    chown $_[3], 0, $hba;
    if ($_[0] >= '8.4') {
	print F "local all $_[2] ident\n";
    } else {
	print F "local all $_[2] ident sameuser\n";
    }
    close F;
}

# Restore original pg_hba.conf, but do not restart postmaster.
# Arguments: <version> <cluster>
sub enable_connections {
    my $hba = "$PgCommon::confroot/$_[0]/$_[1]/pg_hba.conf";

    rename "$hba.pg_upgradecluster", $hba or error "could not rename $hba.pg_upgradecluster: $!"
	if (-e "$hba.pg_upgradecluster");
}

# Get encoding and locales of a running cluster
# Arguments: <version> <cluster>
sub get_encoding {
    $encoding = get_db_encoding $version, $cluster, 'template1';
    if ($version <= '8.3') {
	($old_lc_ctype, $old_lc_collate) = get_cluster_locales $version, $cluster;
    } else {
	($old_lc_ctype, $old_lc_collate) = get_db_locales $version, $cluster, 'template1';
    }
    unless ($encoding && $old_lc_ctype && $old_lc_collate) {
	error 'could not get cluster locales';
    }
}


#
# Execution starts here
#

# command line arguments

$newversion = get_newest_version;

my $method = 'dump';
my $link = 0;

my ($locale, $lc_collate, $lc_ctype, $lc_messages, $lc_monetary, $lc_numeric,
    $lc_time, $logfile);
GetOptions ('v|version=s' => \$newversion,
	    'locale=s' => \$locale,
	    'lc-collate=s' => \$lc_collate,
	    'lc-ctype=s' => \$lc_ctype,
	    'lc-messages=s' => \$lc_messages,
	    'lc-monetary=s' => \$lc_monetary,
	    'lc-numeric=s' => \$lc_numeric,
	    'lc-time=s' => \$lc_time,
	    'logfile=s' => \$logfile,
	    'm|method=s' => \$method,
	    'k|link' => \$link,
    ) or exit 1;

$method eq 'dump' or $method eq 'upgrade' or error 'method must be "dump" or "upgrade"';

# untaint
($newversion) = $newversion =~ /^(\d+\.\d+)$/;
($locale) = $locale =~ /^([\w@._-]+)$/ if $locale;
($lc_collate) = $lc_collate =~ /^([\w@._-]+)$/ if $lc_collate;
($lc_ctype) = $lc_ctype =~ /^([\w@._-]+)$/ if $lc_ctype;
($lc_messages) = $lc_messages =~ /^([\w@._-]+)$/ if $lc_messages;
($lc_monetary) = $lc_monetary =~ /^([\w@._-]+)$/ if $lc_monetary;
($lc_numeric) = $lc_numeric =~ /^([\w@._-]+)$/ if $lc_numeric;
($lc_time) = $lc_time =~ /^([\w@._-]+)$/ if $lc_time;
($logfile) = $logfile =~ /^([^\n]+)$/ if $logfile;

if ($#ARGV < 1) {
    print "Usage: $0 [OPTIONS] <old version> <cluster name> [<new data directory>]\n";
    exit 1;
}

($version) = $ARGV[0] =~ /^(\d+\.\d+)$/;
($cluster) = $ARGV[1] =~ /^([-.\w]+)$/;
my $datadir;
($datadir) = $ARGV[2] =~ /(.*)/ if defined $ARGV[2];

error "Cannot upgrade from $version to $newversion. " .
    "You should invoke pg_upgradecluster on a cluster with an old version."
    if ($version eq $newversion);

error 'specified cluster does not exist' unless cluster_exists $version, $cluster;
%info = cluster_info ($version, $cluster);
error 'cluster is disabled' if $info{'start'} eq 'disabled';

if (cluster_exists $newversion, $cluster) {
    error "target cluster $newversion/$cluster already exists";
}

my $oldpsql = get_program_path 'psql', $version;
my $oldsocket = get_cluster_socketdir $version, $cluster;
my $owner = getpwuid $info{'owneruid'};
error 'could not get name of cluster owner' unless $owner;
my $old_hba_modified;

# stopping old cluster, so that we notice early when there are still
# connections
if ($info{'running'}) {
    get_encoding;
    print "Stopping old cluster...\n";
    my @argv = ('pg_ctlcluster', $version, $cluster, 'stop', '--');
    push @argv, ('-t', '5') if $version >= '8.4';
    error "Could not stop old cluster" if system @argv;
}

if ($method eq 'dump' or ($method eq 'upgrade' and not $info{'running'})) {
    # Disable access to cluster during upgrade
    print "Disabling connections to the old cluster during upgrade...\n";
    disable_connections $version, $cluster, $owner, $info{'owneruid'};
    $old_hba_modified = 1;

    print "Restarting old cluster with restricted connections...\n";
    my @argv = ('pg_ctlcluster', $version, $cluster, 'start');
    error "Could not restart old cluster" if system @argv;

    get_encoding unless ($encoding); # if the cluster was not running before, get encoding now

    if ($method eq 'upgrade') {
	print "Stopping old cluster...\n";
	@argv = ('pg_ctlcluster', $version, $cluster, 'stop', '--');
	push @argv, ('-t', '5') if $version >= '8.4';
	error "Could not stop old cluster" if system @argv;
    }
}

# in dump mode, old cluster is running now
# in upgrade mode, old cluster is stopped

# create new cluster, preserving encoding and locales
my @argv = ('pg_createcluster', '-u', $info{'owneruid'}, '-g', $info{'ownergid'},
    '--socketdir', $info{'socketdir'}, $newversion,
    $cluster);

push @argv, ('--datadir', $datadir) if $datadir;
push @argv, ('--logfile', $logfile) if $logfile;
push @argv, ('--encoding', $encoding) unless $locale or $lc_ctype;
$lc_ctype ||= $locale || $old_lc_ctype;
$lc_collate ||= $locale || $old_lc_collate;
push @argv, ('--locale', $locale) if $locale;
push @argv, ('--lc-collate', $lc_collate) if $lc_collate;
push @argv, ('--lc-ctype', $lc_ctype) if $lc_ctype;
push @argv, ('--lc-messages', $lc_messages) if $lc_messages;
push @argv, ('--lc-monetary', $lc_monetary) if $lc_monetary;
push @argv, ('--lc-numeric', $lc_numeric) if $lc_numeric;
push @argv, ('--lc-time', $lc_time) if $lc_time;
delete $ENV{'LC_ALL'};
error "Could not create target cluster" if system @argv;

%newinfo = cluster_info($newversion, $cluster);

if ($method eq 'dump') {
    print "Disabling connections to the new cluster during upgrade...\n";
    disable_connections $newversion, $cluster, $owner, $newinfo{'owneruid'};

    @argv = ('pg_ctlcluster', $newversion, $cluster, 'start');
    error "Could not start target cluster" if system @argv;
}

sleep(4);

my $pg_restore = get_program_path 'pg_restore', $newversion;

# check whether upgrade scripts exist; if so, verify that pg_restore supports
# -X no-data-for-failed-tables.
my $upgrade_scripts = (-d "$PgCommon::common_confdir/pg_upgradecluster.d" &&
     `run-parts --test $PgCommon::common_confdir/pg_upgradecluster.d`);

if ($upgrade_scripts) {
    if (`$pg_restore --help` !~ qr/no-data-for-failed-tables/) {
	error "$PgCommon::common_confdir/pg_upgradecluster.d has upgrade scripts, but $pg_restore does not support the \"-X no-data-for-failed-tables\" option."
    }
}

# Run upgrade scripts in init phase
if ($upgrade_scripts) {
    print "Running <init> phase upgrade helper scripts...\n";
    if (!fork) {
	change_ugid $info{'owneruid'}, $info{'ownergid'};

	@argv = ('run-parts', '--lsbsysinit', '-a', $version, '-a', $cluster,
	    '-a', $newversion, '-a', 'init',
	    "$PgCommon::common_confdir/pg_upgradecluster.d");
	error "$PgCommon::common_confdir/pg_upgradecluster.d script failed" if system @argv;
	exit 0;
    }
    wait;
}

# dump cluster; drop to cluster owner privileges

if (!fork) {
    change_ugid $info{'owneruid'}, $info{'ownergid'};
    my $pg_dumpall = get_program_path 'pg_dumpall', $newversion;
    my $pg_dump = get_program_path 'pg_dump', $newversion;
    my $psql = get_program_path 'psql', $newversion;
    my $newsocket = get_cluster_socketdir $newversion, $cluster;
    my $buffer;

    if ($method eq 'dump') {
	# check for tablespaces (not supported)
	open F, '-|', $oldpsql, '-h', $oldsocket, '-d', 'template1', '-Atc', 
		"SELECT count(*) FROM pg_tablespace WHERE spcname <> 'pg_default' AND spcname <> 'pg_global'"
	    or die "Calling $psql: $!";
	$buffer = <F>;
	close F;
	if ($buffer ne "0\n") {
	    error 'automatic upgrade of tablespaces is not supported';
	}

	# get list of databases, owners, and allowed connections
	my %databases;
	open F, '-|', $oldpsql, '-h', $oldsocket, '-p', $info{'port'}, 
	    '-F|', '-d', 'template1', '-Atc', 
	    'SELECT datname, datallowconn, pg_catalog.pg_encoding_to_char(encoding), usename FROM pg_database, pg_user WHERE datdba = usesysid' or 
	    error 'Could not get pg_database list';
	while (<F>) {
	    chomp;
	    my ($n, $a, $e, $o) = split '\|';
	    ($o) = $o =~ /^(.*)$/; # untaint
	    ($e) = $e =~ /^([\w_]+)$/; # untaint
	    $databases{$n} = [$a eq 't', $o, $e];
	}
	close F;
	error 'could not get list of databases' if $?;

	# Temporarily enable access to all DBs, so that we can upgrade them
	for my $db (keys %databases) {
	    next if $db eq 'template0';

	    unless (${$databases{$db}}[0]) {
		print "Temporarily enabling access to database $db\n";
		(system $oldpsql, '-h', $oldsocket, '-p', $info{'port'}, '-q', 
		    '-d', 'template1', '-c', 
		    "BEGIN READ WRITE; UPDATE pg_database SET datallowconn = 't' WHERE datname = '$db'; COMMIT") == 0 or
		    error 'Could not enable access to database';
	    }
	}

	# dump schemas
	print "Roles, databases, schemas, ACLs...\n";
	open SOURCE, '-|', $pg_dumpall, '-h', $oldsocket, '-p', $info{'port'},
	     '-s', '--quote-all-identifiers' or error 'Could not execute pg_dumpall for old cluster';
	my $data = '';
	while (read SOURCE, $buffer, 1048576) {
	    $data .= $buffer;
	}
	close SOURCE;
	($? == 0) or exit 1;

	# remove creation of db superuser role to avoid error message
	$data =~ s/^CREATE (ROLE|USER) "\Q$owner\E";\s*$//m;

	# create global objects in target cluster
	open SINK, '|-', $psql, '-h', $newsocket, '-p', $newinfo{'port'},
	    '-q', '-d', 'template1' or 
	    error 'Could not execute psql for new cluster';

	# ensure that we can upgrade tables for DBs with default read-only
	# transactions
	print SINK "BEGIN READ WRITE; ALTER USER $owner SET default_transaction_read_only to off; COMMIT;\n";

	print SINK $data;

	close SINK;
	($? == 0) or exit 1;

	
	# Upgrade databases
	for my $db (keys %databases) {
	    next if $db eq 'template0';

	    print "Fixing hardcoded library paths for stored procedures...\n";
	    # starting from 9.0, replace() works on strings; for ealier versions it
	    # works on bytea
	    if ($version >= '9.0') {
		(system $psql, '-h', $oldsocket, '-p', $info{'port'}, '-q', '-d',
		    $db, '-c', "BEGIN READ WRITE; \
			UPDATE pg_proc SET probin = replace(\
			replace(probin, '/usr/lib/postgresql/lib', '\$libdir'), \
			'/usr/lib/postgresql/$version/lib', '\$libdir'); COMMIT") == 0 or
		    error 'Could not fix library paths';
	    } else {
		(system $psql, '-h', $oldsocket, '-p', $info{'port'}, '-q', '-d',
		    $db, '-c', "BEGIN READ WRITE; \
			UPDATE pg_proc SET probin = decode(replace(\
		       replace(encode(probin, 'escape'), '/usr/lib/postgresql/lib', '\$libdir'), \
		       '/usr/lib/postgresql/$version/lib', '\$libdir'), 'escape'); COMMIT") == 0 or
		    error 'Could not fix library paths';
	    }

	    print 'Upgrading database ', $db, "...\n";
	    open SOURCE, '-|', $pg_dump, '-h', $oldsocket, '-p', $info{'port'},
		 '-Fc', '--quote-all-identifiers', $db or 
		error 'Could not execute pg_dump for old cluster';

	    # start pg_restore and copy over everything
	    my @restore_argv = ($pg_restore, '-h', $newsocket, '-p',
		$newinfo{'port'}, '--data-only', '-d', $db);

	    if ($newversion >= '8.3') {
		push @restore_argv, '--disable-triggers';
	    }

	    if ($upgrade_scripts) {
		push @restore_argv, '--no-data-for-failed-tables';
	    }
	    open SINK, '|-', @restore_argv or
		error 'Could not execute pg_restore for new cluster';

	    while (read SOURCE, $buffer, 1048576) {
		print SINK $buffer;
	    }
	    close SOURCE;
	    ($? == 0) or exit 1;
	    close SINK;

	    # clean up
	    print 'Analyzing database ', $db, "...\n";
	    (system $psql, '-h', $newsocket, '-p', $newinfo{'port'}, '-q', 
		'-d', $db, '-c', 'ANALYZE') == 0 or
		error 'Could not ANALZYE database';

	    unless (${$databases{$db}}[0]) {
		print "Disabling access to database $db\n";
		(system $oldpsql, '-h', $oldsocket, '-p', $info{'port'}, '-q', 
		    '-d', 'template1', '-c', 
		    "BEGIN READ WRITE; UPDATE pg_database SET datallowconn = 'f' where datname = '$db'; COMMIT") == 0 or
		    error 'Could not disable access to database in old cluster';
		(system $psql, '-h', $newsocket, '-p', $newinfo{'port'}, '-q', 
		    '-d', 'template1', '-c', 
		    "BEGIN READ WRITE; UPDATE pg_database SET datallowconn = 'f' where datname = '$db'; COMMIT") == 0 or
		    error 'Could not disable access to database in new cluster';
	    }
	}

	# reset owner specific override for default read-only transactions
	(system $psql, '-h', $newsocket, '-p', $newinfo{'port'}, '-q', 'template1', '-c', 
	    "BEGIN READ WRITE; ALTER USER $owner RESET default_transaction_read_only; COMMIT;\n") == 0 or
	    error 'Could not reset default_transaction_read_only value for superuser';
    } else {
	# pg_upgrade

	use File::Temp qw(tempdir);

	my $pg_upgrade = get_program_path 'pg_upgrade', $newversion;
	my @argv = ($pg_upgrade,
		 '-b', "/usr/lib/postgresql/$version/bin",
		 '-B', "/usr/lib/postgresql/$newversion/bin",
		 '-d', $info{'pgdata'},
		 '-D', $newinfo{'pgdata'},
		 '-p', $info{'port'},
		 '-P', $newinfo{'port'},
	    );
	push @argv, "--link" if $link;

	# Make a directory for pg_upgrade to store its reports and log
	# files.  It will not be removed.
	my $logdir = tempdir("/var/log/postgresql/pg_upgradecluster-$version-$newversion-$cluster.XXXX");
	chdir $logdir;

	# pg_upgrade doesn't support separate configuration and data
	# directories, so we must fix this up temporarily.  (In 9.2+,
	# this could be done with the pg_upgrade -o/-O options (see
	# man page), but we want to support some earlier versions as
	# well for a while.)
	foreach my $file ('postgresql.conf', 'pg_hba.conf', 'pg_ident.conf') {
	    symlink($info{'configdir'}."/$file", $info{'pgdata'}."/$file") or error 'could not symlink configuration file';
	    symlink($newinfo{'configdir'}."/$file", $newinfo{'pgdata'}."/$file") or error 'could not symlink configuration files';
	}
	# Run pg_upgrade.
	my $status = system @argv;
	# And remove the temporary configuration files again.
	foreach my $file ('postgresql.conf', 'pg_hba.conf', 'pg_ident.conf') {
	    unlink($newinfo{'pgdata'}."/$file");
	}
	# Remove the PID file of the old cluster (normally removed by
	# pg_ctlcluster, but not by pg_upgrade).
	unlink "/var/run/postgresql/$version-$cluster.pid";

	$status == 0 or error 'pg_upgrade run failed';
    }

    exit 0;
}

wait;
if ($old_hba_modified) {
    print "Re-enabling connections to the old cluster...\n";
    enable_connections $version, $cluster;
}
if ($method eq 'dump') {
    print "Re-enabling connections to the new cluster...\n";
    enable_connections $newversion, $cluster;
}

if ($?) {
    print STDERR "Error during cluster dumping, removing new cluster\n";
    system 'pg_dropcluster', '--stop', $newversion, $cluster;

    # Reload old cluster to allow connections again
    if (system 'pg_ctlcluster', $version, $cluster, 'reload') {
        error 'could not reload old cluster, please do that manually';
    }
    exit 1;
}

# copy configuration files
print "Copying old configuration files...\n";
install_file $info{'configdir'}.'/postgresql.conf', $newinfo{'configdir'},
    $newinfo{'owneruid'}, $newinfo{'ownergid'}, "644";
install_file $info{'configdir'}.'/pg_ident.conf', $newinfo{'configdir'},
    $newinfo{'owneruid'}, $newinfo{'ownergid'}, "640";
install_file $info{'configdir'}.'/pg_hba.conf', $newinfo{'configdir'},
    $newinfo{'owneruid'}, $newinfo{'ownergid'}, "640";
if ( -e $info{'configdir'}.'/start.conf') {
    print "Copying old start.conf...\n";
    install_file $info{'configdir'}.'/start.conf', $newinfo{'configdir'},
	$newinfo{'owneruid'}, $newinfo{'ownergid'}, "644";
}
if ( -e $info{'configdir'}.'/pg_ctl.conf') {
    print "Copying old pg_ctl.conf...\n";
    install_file $info{'configdir'}.'/pg_ctl.conf', $newinfo{'configdir'},
	$newinfo{'owneruid'}, $newinfo{'ownergid'}, "644";
}
# copy SSL files (overwriting any file that pg_createcluster put there)
for my $file (qw/server.crt server.key root.crt/) {
    if ( -e "$info{'pgdata'}/$file") {
	print "Copying old $file...\n";
	if (!fork) { # we don't use install_file because that converts symlinks to files
	    change_ugid $info{'owneruid'}, $info{'ownergid'};
	    system "cp -a $info{'pgdata'}/$file $newinfo{'pgdata'}";
	    exit 0;
	}
	wait;
    }
}

adapt_conffiles;

if ($method eq 'dump') {
    print "Stopping target cluster...\n";
    @argv = ('pg_ctlcluster', $newversion, $cluster, 'stop');
    error "Could not stop target cluster" if system @argv;

    print "Stopping old cluster...\n";
    @argv = ('pg_ctlcluster', $version, $cluster, 'stop');
    error "Could not stop old cluster" if system @argv;
}

print "Disabling automatic startup of old cluster...\n";
my $startconf = $info{'configdir'}.'/start.conf';
if (open F, ">$startconf") {
    print F "# This cluster was upgraded to a newer major version. The old
# cluster has been preserved for backup purposes, but is not started
# automatically.

manual
";
    close F;
} else {
    error "could not create $startconf: $!";
}

my $oldport = next_free_port;
print "Configuring old cluster to use a different port ($oldport)...\n";
set_cluster_port $version, $cluster, $oldport;

print "Starting target cluster on the original port...\n";
@argv = ('pg_ctlcluster', $newversion, $cluster, 'start');
error "Could not start target cluster; please check configuration and log files" if system @argv;

# Run upgrade scripts in finish phase
if ($upgrade_scripts) {
    print "Running <finish> phase upgrade helper scripts...\n";
    if (!fork) {
	change_ugid $info{'owneruid'}, $info{'ownergid'};

	@argv = ('run-parts', '--lsbsysinit', '-a', $version, '-a', $cluster,
	    '-a', $newversion, '-a', 'finish',
	    "$PgCommon::common_confdir/pg_upgradecluster.d");
	error "$PgCommon::common_confdir/pg_upgradecluster.d script failed" if system @argv;
	exit 0;
    }
    wait;
}

print "Success. Please check that the upgraded cluster works. If it does,
you can remove the old cluster with

  pg_dropcluster $version $cluster
"

__END__

=head1 NAME

pg_upgradecluster - upgrade an existing PostgreSQL cluster to a new major version.

=head1 SYNOPSIS

B<pg_upgradecluster> [B<-v> I<newversion>] I<oldversion> I<name> [I<newdatadir>]

=head1 DESCRIPTION

B<pg_upgradecluster> upgrades an existing PostgreSQL server cluster (i. e. a
collection of databases served by a B<postmaster> instance) to a new version
specified by I<newversion> (default: latest available version).  The
configuration files of the old version are copied to the new cluster.

The cluster of the old version will be configured to use a previously unused
port since the upgraded one will use the original port. The old cluster is not
automatically removed. After upgrading, please verify that the new cluster
indeed works as expected; if so, you should remove the old cluster with
L<pg_dropcluster(8)>. Please note that the old cluster is set to "manual"
startup mode, in order to avoid inadvertently changing it; this means that it
will not be started automatically on system boot, and you have to use
L<pg_ctlcluster(8)> to start/stop it. See section "STARTUP CONTROL" in
L<pg_createcluster(8)> for details.

The I<newdatadir> argument can be used to specify a non-default data directory
of the upgraded cluster. It is passed to B<pg_createcluster>. If not specified,
this defaults to /var/lib/postgresql/I<newversion>/I<name>.

Please note that this program cannot upgrade clusters which use tablespaces. If
you use those, you have to upgrade manually.

=head1 OPTIONS

=over 4

=item B<-v> I<newversion>

Set the version to upgrade to (default: latest available).

=item B<--logfile> I<filel>

Set a custom log file path for the upgraded database cluster.

=item B<--locale=>I<locale>

Set the default locale for the upgraded database cluster. If this option is not
specified, the locale is inherited from the old cluster.

=item B<--lc-collate=>I<locale>

=item B<--lc-ctype=>I<locale>

=item B<--lc-messages=>I<locale>

=item B<--lc-monetary=>I<locale>

=item B<--lc-numeric=>I<locale>

=item B<--lc-time=>I<locale>

Like B<--locale>, but only sets the locale in the specified category.

=item B<-m>, B<--method=>dump|upgrade

Specify the upgrade method.  "dump" uses L<pg_dump(1)> and
L<pg_restore(1)>, "upgrade" uses L<pg_upgrade(1)>.  The default is
"dump".

=item B<-k>, B<--link>

In pg_upgrade mode, use hard links instead of copying files to the new
cluster.  This option is merely passed on to pg_upgrade.  See
L<pg_upgrade(1)> for details.

=back

=head1 HOOK SCRIPTS

Some PostgreSQL extensions like PostGIS need metadata in auxiliary tables which
must not be upgraded from the old version, but rather initialized for the new
version before copying the table data. For this purpose, extensions (as well as
administrators, of course) can drop upgrade hook scripts into 
C</etc/postgresql-common/pg_upgradecluster.d/>. Script file names must consist
entirely of upper and lower case letters, digits, underscores, and hyphens; in
particular, dots (i. e. file extensions) are not allowed.

Scripts in that directory will be called with the following arguments:

<old version> <cluster name> <new version> <phase> 

Phases:

=over

=item B<init>

A virgin cluster of version I<new version> has been created, i. e.  this new
cluster will already have B<template1>, but no user databases. Please note that
you should not create tables in this phase, since they will be overwritten by
the dump/restore operation.

=item B<finish>

All data from the old version cluster has been dumped/reloaded into the new
one. The old cluster still exists.

=back

The scripts are called as the user who owns the database.

=head1 SEE ALSO

L<pg_createcluster(8)>, L<pg_dropcluster(8)>, L<pg_lsclusters(1)>, L<pg_wrapper(1)>

=head1 AUTHOR

Martin Pitt L<E<lt>mpitt@debian.orgE<gt>>
