2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# DB MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Db;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
use strict;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-07-28 01:13:23 +03:00
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
use DBD::Pg ':async';
|
2015-08-05 18:32:12 +02:00
|
|
|
use DBI;
|
2015-06-14 00:25:49 +02:00
|
|
|
use Exporter qw(import);
|
2015-08-08 23:11:20 +02:00
|
|
|
our @EXPORT = qw();
|
2015-06-14 00:25:49 +02:00
|
|
|
use Fcntl qw(O_RDONLY);
|
2015-06-30 04:07:42 +02:00
|
|
|
use File::Basename qw(dirname);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
use lib dirname($0);
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Common::Wait;
|
|
|
|
use pgBackRest::Config::Config;
|
|
|
|
use pgBackRest::File;
|
2016-05-11 15:21:39 +02:00
|
|
|
use pgBackRest::Manifest;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Version;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2015-06-14 00:25:49 +02:00
|
|
|
# Operation constants
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
use constant OP_DB => 'Db';
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2015-08-05 18:32:12 +02:00
|
|
|
use constant OP_DB_NEW => OP_DB . "->new";
|
2015-08-08 23:11:20 +02:00
|
|
|
use constant OP_DB_BACKUP_START => OP_DB . "->backupStart";
|
|
|
|
use constant OP_DB_BACKUP_STOP => OP_DB . "->backupStop";
|
2015-08-29 20:20:46 +02:00
|
|
|
use constant OP_DB_DESTROY => OP_DB . "->DESTROY";
|
2015-06-30 04:07:42 +02:00
|
|
|
use constant OP_DB_EXECUTE_SQL => OP_DB . "->executeSql";
|
|
|
|
push @EXPORT, qw(OP_DB_EXECUTE_SQL);
|
2015-08-29 20:20:46 +02:00
|
|
|
use constant OP_DB_EXECUTE_SQL_ONE => OP_DB . "->executeSqlOne";
|
|
|
|
use constant OP_DB_EXECUTE_SQL_ROW => OP_DB . "->executeSqlRow";
|
2015-08-08 23:11:20 +02:00
|
|
|
use constant OP_DB_INFO => OP_DB . "->info";
|
|
|
|
push @EXPORT, qw(OP_DB_INFO);
|
|
|
|
use constant OP_DB_TABLESPACE_MAP_GET => OP_DB . "->tablespaceMapGet";
|
2015-06-30 04:07:42 +02:00
|
|
|
use constant OP_DB_VERSION_GET => OP_DB . "->versionGet";
|
2015-08-29 20:20:46 +02:00
|
|
|
use constant OP_DB_VERSION_SUPPORT => OP_DB . "->versionSupport";
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
# Backup advisory lock
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
use constant DB_BACKUP_ADVISORY_LOCK => '12340078987004321';
|
|
|
|
push @EXPORT, qw(DB_BACKUP_ADVISORY_LOCK);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# PostgreSQL version numbers
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant PG_VERSION_83 => '8.3';
|
|
|
|
push @EXPORT, qw(PG_VERSION_83);
|
|
|
|
use constant PG_VERSION_84 => '8.4';
|
|
|
|
push @EXPORT, qw(PG_VERSION_84);
|
|
|
|
use constant PG_VERSION_90 => '9.0';
|
|
|
|
push @EXPORT, qw(PG_VERSION_90);
|
|
|
|
use constant PG_VERSION_91 => '9.1';
|
|
|
|
push @EXPORT, qw(PG_VERSION_91);
|
|
|
|
use constant PG_VERSION_92 => '9.2';
|
|
|
|
push @EXPORT, qw(PG_VERSION_92);
|
|
|
|
use constant PG_VERSION_93 => '9.3';
|
|
|
|
push @EXPORT, qw(PG_VERSION_93);
|
|
|
|
use constant PG_VERSION_94 => '9.4';
|
|
|
|
push @EXPORT, qw(PG_VERSION_94);
|
|
|
|
use constant PG_VERSION_95 => '9.5';
|
|
|
|
push @EXPORT, qw(PG_VERSION_95);
|
2016-05-16 23:59:26 +02:00
|
|
|
use constant PG_VERSION_96 => '9.6';
|
|
|
|
push @EXPORT, qw(PG_VERSION_96);
|
2016-02-06 06:03:29 +02:00
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
# Map the control and catalog versions to PostgreSQL version.
|
|
|
|
#
|
|
|
|
# The control version can be found in src/include/catalog/pg_control.h and may not change with every version of PostgreSQL but is
|
|
|
|
# still checked to detect custom builds which change the structure. The catalog version can be found in
|
|
|
|
# src/include/catalog/catversion.h and should change with every release.
|
2015-08-08 23:11:20 +02:00
|
|
|
####################################################################################################################################
|
2016-05-14 16:33:12 +02:00
|
|
|
my $oPgControlVersionHash =
|
|
|
|
{
|
|
|
|
# iControlVersion => {iCatalogVersion => strDbVersion}
|
|
|
|
833 => {200711281 => PG_VERSION_83},
|
|
|
|
843 => {200904091 => PG_VERSION_84},
|
|
|
|
903 =>
|
|
|
|
{
|
|
|
|
201008051 => PG_VERSION_90,
|
|
|
|
201105231 => PG_VERSION_91,
|
|
|
|
},
|
|
|
|
922 => {201204301 => PG_VERSION_92},
|
|
|
|
937 => {201306121 => PG_VERSION_93},
|
|
|
|
942 =>
|
|
|
|
{
|
|
|
|
201409291 => PG_VERSION_94,
|
|
|
|
201510051 => PG_VERSION_95,
|
2016-05-16 23:59:26 +02:00
|
|
|
201605051 => PG_VERSION_96,
|
2016-05-14 16:33:12 +02:00
|
|
|
},
|
|
|
|
};
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
2014-10-10 23:03:33 +03:00
|
|
|
sub new
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-01 21:58:33 +02:00
|
|
|
my $class = shift; # Class name
|
2014-10-10 23:03:33 +03:00
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
|
|
|
my $strOperation
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_NEW
|
|
|
|
);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-05 18:32:12 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# DESTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub DESTROY
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_DESTROY
|
|
|
|
);
|
|
|
|
|
2015-08-05 18:32:12 +02:00
|
|
|
if (defined($self->{hDb}))
|
|
|
|
{
|
|
|
|
$self->{hDb}->disconnect();
|
|
|
|
undef($self->{hDb});
|
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
);
|
2015-08-05 18:32:12 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 04:07:23 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# versionSupport
|
|
|
|
#
|
|
|
|
# Returns an array of the supported Postgres versions.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub versionSupport
|
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_VERSION_SUPPORT
|
|
|
|
);
|
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
my @strySupportVersion = (PG_VERSION_83, PG_VERSION_84, PG_VERSION_90, PG_VERSION_91, PG_VERSION_92, PG_VERSION_93,
|
2016-05-16 23:59:26 +02:00
|
|
|
PG_VERSION_94, PG_VERSION_95, PG_VERSION_96);
|
2015-04-03 04:07:23 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strySupportVersion', value => \@strySupportVersion}
|
|
|
|
);
|
2015-04-03 04:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(versionSupport);
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
# executeSql
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
sub executeSql
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-06-30 04:07:42 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSql,
|
|
|
|
$bIgnoreError
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_EXECUTE_SQL, \@_,
|
|
|
|
{name => 'strSql'},
|
|
|
|
{name => 'bIgnoreError', default => false}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Get the user-defined command for psql
|
|
|
|
my $strResult;
|
|
|
|
|
|
|
|
# Run remotely
|
2015-06-30 04:07:42 +02:00
|
|
|
if (optionRemoteTypeTest(DB))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-06-30 04:07:42 +02:00
|
|
|
# Build param hash
|
|
|
|
my %oParamHash;
|
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
$oParamHash{'script'} = $strSql;
|
2015-08-29 20:20:46 +02:00
|
|
|
$oParamHash{'ignore-error'} = $bIgnoreError;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-06-30 04:07:42 +02:00
|
|
|
# Execute the command
|
2015-08-05 14:43:41 +02:00
|
|
|
$strResult = protocolGet()->cmdExecute(OP_DB_EXECUTE_SQL, \%oParamHash, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
# Else run locally
|
|
|
|
else
|
|
|
|
{
|
2015-08-05 18:32:12 +02:00
|
|
|
if (!defined($self->{hDb}))
|
|
|
|
{
|
|
|
|
# Connect to the db
|
|
|
|
my $strDbName = 'postgres';
|
|
|
|
my $strDbUser = getpwuid($<);
|
2015-08-06 04:05:45 +02:00
|
|
|
my $strDbSocketPath = optionGet(OPTION_DB_SOCKET_PATH, false);
|
|
|
|
|
|
|
|
if (defined($strDbSocketPath) && $strDbSocketPath !~ /^\//)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strDbSocketPath}' is not valid for '" . OPTION_DB_SOCKET_PATH . "' option:" .
|
|
|
|
" path must be absolute", ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
|
2015-08-05 18:32:12 +02:00
|
|
|
my $strDbUri = "dbi:Pg:dbname=${strDbName};port=" . optionGet(OPTION_DB_PORT) .
|
|
|
|
(optionTest(OPTION_DB_SOCKET_PATH) ? ';host=' . optionGet(OPTION_DB_SOCKET_PATH) : '');
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
logDebugMisc
|
|
|
|
(
|
|
|
|
$strOperation, 'db connect',
|
|
|
|
{name => 'strDbUri', value => $strDbUri},
|
|
|
|
{name => 'strDbUser', value => $strDbUser}
|
|
|
|
);
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-09-08 20:01:19 +02:00
|
|
|
$self->{hDb} = DBI->connect($strDbUri, $strDbUser, undef,
|
|
|
|
{AutoCommit => 1, RaiseError => 0, PrintError => 0, Warn => 0});
|
2015-08-05 18:32:12 +02:00
|
|
|
|
|
|
|
if (!$self->{hDb})
|
|
|
|
{
|
|
|
|
confess &log(ERROR, $DBI::errstr, ERROR_DB_CONNECT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
# Prepare the query
|
2015-08-09 00:22:38 +02:00
|
|
|
my $hStatement = $self->{hDb}->prepare($strSql, {pg_async => PG_ASYNC})
|
2015-08-05 18:32:12 +02:00
|
|
|
or confess &log(ERROR, $DBI::errstr, ERROR_DB_QUERY);
|
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
# Execute the query
|
2015-08-09 00:22:38 +02:00
|
|
|
$hStatement->execute();
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
# Wait for the query to return
|
2015-08-29 20:20:46 +02:00
|
|
|
my $oWait = waitInit(optionGet(OPTION_DB_TIMEOUT));
|
2015-08-09 00:22:38 +02:00
|
|
|
my $bTimeout = true;
|
2015-08-05 18:32:12 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-08-09 00:22:38 +02:00
|
|
|
# Is the statement done?
|
|
|
|
if ($hStatement->pg_ready())
|
2015-08-05 18:32:12 +02:00
|
|
|
{
|
2015-08-09 00:22:38 +02:00
|
|
|
if (!$hStatement->pg_result())
|
|
|
|
{
|
|
|
|
# Return if the error should be ignored
|
2015-08-29 20:20:46 +02:00
|
|
|
if ($bIgnoreError)
|
2015-08-09 00:22:38 +02:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Else report it
|
|
|
|
confess &log(ERROR, $DBI::errstr . ":\n${strSql}", ERROR_DB_QUERY);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get rows and return them
|
2016-05-14 16:29:35 +02:00
|
|
|
my @stryRow;
|
2015-08-09 00:22:38 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-05-14 16:29:35 +02:00
|
|
|
# Get next row
|
|
|
|
@stryRow = $hStatement->fetchrow_array;
|
2015-08-09 00:22:38 +02:00
|
|
|
|
2016-05-14 16:29:35 +02:00
|
|
|
# If the row has data then add it to the result
|
|
|
|
if (@stryRow)
|
|
|
|
{
|
|
|
|
# Add an LF after the first row
|
|
|
|
$strResult .= (defined($strResult) ? "\n" : '');
|
|
|
|
|
|
|
|
# Add row to result
|
|
|
|
for (my $iColumnIdx = 0; $iColumnIdx < @stryRow; $iColumnIdx++)
|
|
|
|
{
|
|
|
|
# Add tab between columns
|
|
|
|
$strResult .= $iColumnIdx == 0 ? '' : "\t";
|
|
|
|
|
|
|
|
# Add column data
|
|
|
|
$strResult .= defined($stryRow[$iColumnIdx]) ? $stryRow[$iColumnIdx] : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else check for error
|
|
|
|
elsif ($hStatement->err)
|
2015-08-09 00:22:38 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, $DBI::errstr . ":\n${strSql}", ERROR_DB_QUERY);
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 16:29:35 +02:00
|
|
|
while (@stryRow);
|
2015-08-09 00:22:38 +02:00
|
|
|
|
|
|
|
$bTimeout = false;
|
2015-08-05 18:32:12 +02:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
} while ($bTimeout && waitMore($oWait));
|
2015-08-05 18:32:12 +02:00
|
|
|
|
2015-08-09 00:22:38 +02:00
|
|
|
# If timeout then cancel the query and confess
|
|
|
|
if ($bTimeout)
|
|
|
|
{
|
|
|
|
$hStatement->pg_cancel();
|
2015-08-29 20:20:46 +02:00
|
|
|
confess &log(ERROR, 'statement timed out after ' . waitInterval($oWait) .
|
|
|
|
" second(s):\n${strSql}", ERROR_DB_TIMEOUT);
|
2015-08-05 18:32:12 +02:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strResult', value => $strResult}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
# executeSqlRow
|
|
|
|
####################################################################################################################################
|
|
|
|
sub executeSqlRow
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSql
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_EXECUTE_SQL_ROW, \@_,
|
|
|
|
{name => 'strSql', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2016-05-14 16:29:35 +02:00
|
|
|
my @stryResult = split("\t", $self->executeSql($strSql));
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strResult', value => \@stryResult}
|
|
|
|
);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# executeSqlOne
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
sub executeSqlOne
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSql
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_EXECUTE_SQL_ONE, \@_,
|
|
|
|
{name => 'strSql', trace => true}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strResult', value => ($self->executeSqlRow($strSql))[0], trace => true}
|
|
|
|
);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# tablespaceMapGet
|
|
|
|
#
|
|
|
|
# Get the mapping between oid and tablespace name.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub tablespaceMapGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_TABLESPACE_MAP_GET
|
|
|
|
);
|
|
|
|
|
|
|
|
dataHashBuild(my $oTablespaceMapRef = {}, "oid\tname\n" . $self->executeSql(
|
|
|
|
'select oid, spcname from pg_tablespace'), "\t");
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oTablespaceMapRef', value => $oTablespaceMapRef}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# databaseMapGet
|
|
|
|
#
|
|
|
|
# Get the mapping between oid and database name.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub databaseMapGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my ($strOperation) = logDebugParam(__PACKAGE__ . '->databaseMapGet');
|
|
|
|
|
|
|
|
dataHashBuild(my $oDatabaseMapRef = {}, "name\t" . MANIFEST_KEY_DB_ID . "\t" . MANIFEST_KEY_DB_LAST_SYSTEM_ID . "\n" .
|
|
|
|
$self->executeSql('select datname, oid, datlastsysoid from pg_database'), "\t");
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oDatabaseMapRef', value => $oDatabaseMapRef}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# info
|
|
|
|
####################################################################################################################################
|
|
|
|
sub info
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oFile,
|
|
|
|
$strDbPath
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_INFO, \@_,
|
|
|
|
{name => 'oFile'},
|
|
|
|
{name => 'strDbPath'}
|
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Get info if it is not cached
|
|
|
|
#-------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
if (!defined($self->{info}{$strDbPath}))
|
2015-08-06 04:05:45 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
# Get info from remote
|
|
|
|
#---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
if ($oFile->isRemote(PATH_DB_ABSOLUTE))
|
|
|
|
{
|
|
|
|
# Build param hash
|
|
|
|
my %oParamHash;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
$oParamHash{'db-path'} = ${strDbPath};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Output remote trace info
|
|
|
|
&log(TRACE, OP_DB_INFO . ": remote (" . $oFile->{oProtocol}->commandParamString(\%oParamHash) . ')');
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Execute the command
|
|
|
|
my $strResult = $oFile->{oProtocol}->cmdExecute(OP_DB_INFO, \%oParamHash, true);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Split the result into return values
|
|
|
|
my @stryToken = split(/\t/, $strResult);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Cache info so it does not need to read again for the same database
|
|
|
|
$self->{info}{$strDbPath}{strDbVersion} = $stryToken[0];
|
|
|
|
$self->{info}{$strDbPath}{iControlVersion} = $stryToken[1];
|
|
|
|
$self->{info}{$strDbPath}{iCatalogVersion} = $stryToken[2];
|
|
|
|
$self->{info}{$strDbPath}{ullDbSysId} = $stryToken[3];
|
|
|
|
}
|
|
|
|
# Get info locally
|
|
|
|
#---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Open the control file and read system id and versions
|
|
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
my $strControlFile = "${strDbPath}/" . DB_FILE_PGCONTROL;
|
|
|
|
my $hFile;
|
|
|
|
my $tBlock;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
sysopen($hFile, $strControlFile, O_RDONLY)
|
|
|
|
or confess &log(ERROR, "unable to open ${strControlFile}", ERROR_FILE_OPEN);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Read system identifier
|
|
|
|
sysread($hFile, $tBlock, 8) == 8
|
|
|
|
or confess &log(ERROR, "unable to read database system identifier");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
$self->{info}{$strDbPath}{ullDbSysId} = unpack('Q', $tBlock);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Read control version
|
|
|
|
sysread($hFile, $tBlock, 4) == 4
|
|
|
|
or confess &log(ERROR, "unable to read control version");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
$self->{info}{$strDbPath}{iControlVersion} = unpack('L', $tBlock);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Read catalog version
|
|
|
|
sysread($hFile, $tBlock, 4) == 4
|
|
|
|
or confess &log(ERROR, "unable to read catalog version");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
$self->{info}{$strDbPath}{iCatalogVersion} = unpack('L', $tBlock);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Close the control file
|
|
|
|
close($hFile);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
# Get PostgreSQL version
|
|
|
|
$self->{info}{$strDbPath}{strDbVersion} =
|
|
|
|
$$oPgControlVersionHash{$self->{info}{$strDbPath}{iControlVersion}}{$self->{info}{$strDbPath}{iCatalogVersion}};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
if (!defined($self->{info}{$strDbPath}{strDbVersion}))
|
|
|
|
{
|
|
|
|
confess &log(
|
|
|
|
ERROR,
|
|
|
|
'unexpected control version = ' . $self->{info}{$strDbPath}{iControlVersion} .
|
|
|
|
' and catalog version = ' . $self->{info}{$strDbPath}{iCatalogVersion} . "\n" .
|
2016-05-14 16:36:35 +02:00
|
|
|
'HINT: is this version of PostgreSQL supported?',
|
2016-05-14 16:33:12 +02:00
|
|
|
ERROR_VERSION_NOT_SUPPORTED);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-05-14 16:33:12 +02:00
|
|
|
{name => 'strDbVersion', value => $self->{info}{$strDbPath}{strDbVersion}},
|
|
|
|
{name => 'iControlVersion', value => $self->{info}{$strDbPath}{iControlVersion}},
|
|
|
|
{name => 'iCatalogVersion', value => $self->{info}{$strDbPath}{iCatalogVersion}},
|
|
|
|
{name => 'ullDbSysId', value => $self->{info}{$strDbPath}{ullDbSysId}}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
# versionGet
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-06-30 04:07:42 +02:00
|
|
|
sub versionGet
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_VERSION_GET
|
|
|
|
);
|
|
|
|
|
2015-08-06 04:05:45 +02:00
|
|
|
# Get data from the cache if possible
|
2016-05-14 16:33:12 +02:00
|
|
|
if (defined($self->{strDbVersion}) && defined($self->{strDbPath}))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
return $self->{strDbVersion}, $self->{strDbPath};
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 04:05:45 +02:00
|
|
|
# Get version and db-path from
|
2016-05-14 16:33:12 +02:00
|
|
|
($self->{strDbVersion}, $self->{strDbPath}) =
|
2015-08-08 23:11:20 +02:00
|
|
|
$self->executeSqlRow("select (regexp_matches(split_part(version(), ' ', 2), '^[0-9]+\.[0-9]+'))[1], setting" .
|
|
|
|
" from pg_settings where name = 'data_directory'");
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
my @stryVersionSupport = versionSupport();
|
2015-04-03 04:07:23 +02:00
|
|
|
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{strDbVersion} < $stryVersionSupport[0])
|
2015-04-03 04:07:23 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
confess &log(ERROR, 'unsupported Postgres version' . $self->{strDbVersion}, ERROR_VERSION_NOT_SUPPORTED);
|
2015-04-03 04:07:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-05-14 16:33:12 +02:00
|
|
|
{name => 'strDbVersion', value => $self->{strDbVersion}},
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strDbPath', value => $self->{strDbPath}}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
# backupStart
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
sub backupStart
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oFile,
|
|
|
|
$strDbPath,
|
|
|
|
$strLabel,
|
|
|
|
$bStartFast
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_BACKUP_START, \@_,
|
|
|
|
{name => 'oFile'},
|
|
|
|
{name => 'strDbPath'},
|
|
|
|
{name => 'strLabel'},
|
|
|
|
{name => 'bStartFast'}
|
|
|
|
);
|
2015-08-08 23:11:20 +02:00
|
|
|
|
2015-08-06 04:05:45 +02:00
|
|
|
# Get the version from the control file
|
2016-05-14 16:33:12 +02:00
|
|
|
my ($strDbVersion) = $self->info($oFile, $strDbPath);
|
2015-08-06 04:05:45 +02:00
|
|
|
|
|
|
|
# Get version and db path from the database
|
|
|
|
my ($fCompareDbVersion, $strCompareDbPath) = $self->versionGet();
|
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Error if the version from the control file and the configured db-path do not match the values obtained from the database
|
2016-05-14 16:33:12 +02:00
|
|
|
if (!($strDbVersion == $fCompareDbVersion && $strDbPath eq $strCompareDbPath))
|
2015-08-06 04:05:45 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
confess &log(ERROR,
|
|
|
|
"version '${fCompareDbVersion}' and db-path '${strCompareDbPath}' queried from cluster does not match" .
|
|
|
|
" version '${strDbVersion}' and db-path '${strDbPath}' read from '${strDbPath}/" . DB_FILE_PGCONTROL . "'\n" .
|
|
|
|
"HINT: the db-path and db-port settings likely reference different clusters", ERROR_DB_MISMATCH);
|
2015-08-06 04:05:45 +02:00
|
|
|
}
|
2015-04-01 21:58:33 +02:00
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
# Only allow start-fast option for version >= 8.4
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{strDbVersion} < PG_VERSION_84 && $bStartFast)
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
&log(WARN, OPTION_START_FAST . ' option is only available in PostgreSQL >= ' . PG_VERSION_84);
|
2015-04-01 21:58:33 +02:00
|
|
|
$bStartFast = false;
|
|
|
|
}
|
|
|
|
|
2016-01-10 06:08:54 +02:00
|
|
|
# Error if archive_mode = always (support has not been added yet)
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->executeSql('show archive_mode') eq 'always')
|
2016-01-10 06:08:54 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "archive_mode=always not supported", ERROR_FEATURE_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
2016-01-11 16:50:45 +02:00
|
|
|
# Check if archive_command is set
|
|
|
|
my $strArchiveCommand = $self->executeSql('show archive_command');
|
|
|
|
|
|
|
|
if (index($strArchiveCommand, BACKREST_EXE) == -1)
|
|
|
|
{
|
2016-04-14 15:30:54 +02:00
|
|
|
confess &log(ERROR, 'archive_command must contain \'' . BACKREST_EXE . '\'', ERROR_ARCHIVE_COMMAND_INVALID);
|
2016-01-11 16:50:45 +02:00
|
|
|
}
|
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Acquire the backup advisory lock to make sure that backups are not running from multiple backup servers against the same
|
|
|
|
# database cluster. This lock helps make the stop-auto option safe.
|
2015-08-08 23:11:20 +02:00
|
|
|
if (!$self->executeSqlOne('select pg_try_advisory_lock(' . DB_BACKUP_ADVISORY_LOCK . ')'))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to acquire backup lock\n" .
|
|
|
|
'HINT: is another backup already running on this cluster?', ERROR_LOCK_ACQUIRE);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:59:26 +02:00
|
|
|
# If stop-auto is enabled check for a running backup. This feature is not supported for PostgreSQL >= 9.6 since backups are
|
|
|
|
# run in non-exclusive mode.
|
|
|
|
if (optionGet(OPTION_STOP_AUTO) && $self->{strDbVersion} < PG_VERSION_96)
|
2015-08-08 23:11:20 +02:00
|
|
|
{
|
2015-09-16 22:23:19 +02:00
|
|
|
# Running backups can only be detected in PostgreSQL >= 9.3
|
2016-05-14 16:33:12 +02:00
|
|
|
if ($self->{strDbVersion} >= PG_VERSION_93)
|
2015-08-08 23:11:20 +02:00
|
|
|
{
|
2015-09-16 22:23:19 +02:00
|
|
|
# If a backup is currently in progress emit a warning and then stop it
|
2015-08-08 23:11:20 +02:00
|
|
|
if ($self->executeSqlOne('select pg_is_in_backup()'))
|
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
&log(WARN, 'the cluster is already in backup mode but no backup process is running.' .
|
|
|
|
' pg_stop_backup() will be called so a new backup can be started.');
|
2015-08-08 23:11:20 +02:00
|
|
|
$self->backupStop();
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 22:23:19 +02:00
|
|
|
# Else emit a warning that the feature is not supported and continue. If a backup is running then an error will be
|
|
|
|
# generated later on.
|
2015-08-08 23:11:20 +02:00
|
|
|
else
|
|
|
|
{
|
2016-05-14 16:33:12 +02:00
|
|
|
&log(WARN, OPTION_STOP_AUTO . 'option is only available in PostgreSQL >= ' . PG_VERSION_93);
|
2015-08-08 23:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Start the backup
|
2016-05-16 23:59:26 +02:00
|
|
|
&log(INFO, 'execute ' . ($self->{strDbVersion} >= PG_VERSION_96 ? 'non-' : '') .
|
|
|
|
"exclusive pg_start_backup() with label \"${strLabel}\": backup begins after " .
|
2015-05-26 18:26:59 +02:00
|
|
|
($bStartFast ? "the requested immediate checkpoint" : "the next regular checkpoint") . " completes");
|
|
|
|
|
2015-08-08 23:11:20 +02:00
|
|
|
my ($strTimestampDbStart, $strArchiveStart) =
|
|
|
|
$self->executeSqlRow("select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS.US TZ'), " .
|
2016-05-16 23:59:26 +02:00
|
|
|
"pg_xlogfile_name(lsn) from pg_start_backup('${strLabel}'" .
|
|
|
|
($bStartFast ? ', true' : '') .
|
|
|
|
($self->{strDbVersion} >= PG_VERSION_96 ? (!$bStartFast ? ', false' : '') . ', false' : '') .
|
|
|
|
') as lsn');
|
2015-01-03 23:49:26 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strArchiveStart', value => $strArchiveStart},
|
|
|
|
{name => 'strTimestampDbStart', value => $strTimestampDbStart}
|
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
# backupStop
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2015-08-08 23:11:20 +02:00
|
|
|
sub backupStop
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_DB_BACKUP_STOP
|
|
|
|
);
|
|
|
|
|
2015-09-16 22:23:19 +02:00
|
|
|
# Stop the backup
|
2016-05-16 23:59:26 +02:00
|
|
|
&log(INFO, 'execute ' . ($self->{strDbVersion} >= PG_VERSION_96 ? 'non-' : '') .
|
|
|
|
'exclusive pg_stop_backup() and wait for all WAL segments to archive');
|
|
|
|
|
|
|
|
my ($strTimestampDbStop, $strArchiveStop, $strLabel, $strTablespaceMap) =
|
|
|
|
$self->executeSqlRow(
|
|
|
|
"select to_char(clock_timestamp(), 'YYYY-MM-DD HH24:MI:SS.US TZ'), pg_xlogfile_name(lsn), " .
|
|
|
|
($self->{strDbVersion} >= PG_VERSION_96 ? 'labelfile, spcmapfile' : "null as labelfile, null as spcmapfile") .
|
|
|
|
' from pg_stop_backup(' .
|
|
|
|
($self->{strDbVersion} >= PG_VERSION_96 ? 'false)' : ') as lsn'));
|
|
|
|
|
|
|
|
# Build a hash of the files that need to be written to the backup
|
|
|
|
my $oFileHash =
|
|
|
|
{
|
|
|
|
&MANIFEST_FILE_BACKUPLABEL => $strLabel,
|
|
|
|
&MANIFEST_FILE_TABLESPACEMAP => $strTablespaceMap
|
|
|
|
};
|
2015-01-03 23:49:26 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strArchiveStop', value => $strArchiveStop},
|
2016-05-16 23:59:26 +02:00
|
|
|
{name => 'strTimestampDbStop', value => $strTimestampDbStop},
|
|
|
|
{name => 'oFileHash', value => $oFileHash}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:03:33 +03:00
|
|
|
1;
|