MAJOR=4
MINOR=50
DEVNAME=tty50
  7   	ˆl–Ãa¾”*i?u8 Y¸î41hß�þ^u¶VqÀ	æ�$®² ?È„e§gÇÅÄ ?÷     	#!/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-2022 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 warnings;
use PgCommon;
use File::Temp qw(tempfile);
use Getopt::Long;
use POSIX qw(lchown);

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

# global variables
my ($version, $newversion, $cluster, $newcluster);
my (%info, %newinfo);
my ($encoding, $old_lc_ctype, $old_lc_collate); # old cluster encoding
my $maintenance_db = 'template1';

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

# update the new cluster's conffiles
sub adapt_conffiles {
    my ($newversion, $newcluster, $configfile) = @_;
    my %c = read_cluster_conf_file $newversion, $newcluster, $configfile;

    # Arguments: <ref to conf hash> <name> <comment>
    my $deprecate = sub {
        my ($conf, $guc, $comment) = @_;
        if (defined $conf->{$guc}) {
            PgCommon::disable_conf_value $newversion, $newcluster,
                $configfile, $guc, $comment;
        }
    };

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

    # Arguments: <config option> <value>
    my $set = sub {
        my ($guc, $val) = @_;
        PgCommon::set_conf_value $newversion, $newcluster, $configfile,
            $guc, $val;
    };

    # adapt paths to configuration files
    if ($configfile eq 'postgresql.conf') {
        $set->('data_directory', $newinfo{'pgdata'});
    } else {
        # fix bug in pg_upgradecluster 200..202
        $deprecate->(\%c, 'data_directory', 'not valid in postgresql.auto.conf');
    }
    for my $guc (qw(hba_file ident_file external_pid_file stats_temp_directory)) {
        next unless (defined $c{$guc});
        my $val = $c{$guc};
        $val =~ s/\b\Q$version\E\b/$newversion/g;
        $val =~ s/\b\Q$cluster\E\b/$newcluster/g if ($cluster ne $newcluster);
        $set->($guc, $val);
    }

    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, $newcluster, $configfile,
		    '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 mer