2016-06-24 14:12:58 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# HostDbTest.pm - Database host
|
|
|
|
####################################################################################################################################
|
2017-05-12 22:43:04 +02:00
|
|
|
package pgBackRestTest::Env::Host::HostDbTest;
|
|
|
|
use parent 'pgBackRestTest::Env::Host::HostDbCommonTest';
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Perl includes
|
|
|
|
####################################################################################################################################
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
2016-08-24 18:39:27 +02:00
|
|
|
use File::Basename qw(basename);
|
2016-06-24 14:12:58 +02:00
|
|
|
|
2020-03-10 21:41:56 +02:00
|
|
|
use pgBackRestDoc::Common::Exception;
|
|
|
|
use pgBackRestDoc::Common::Log;
|
|
|
|
use pgBackRestDoc::Common::String;
|
2020-03-10 23:57:02 +02:00
|
|
|
use pgBackRestDoc::ProjectInfo;
|
2020-03-10 21:12:44 +02:00
|
|
|
|
|
|
|
use pgBackRestTest::Common::ContainerTest;
|
|
|
|
use pgBackRestTest::Common::DbVersion;
|
|
|
|
use pgBackRestTest::Common::RunTest;
|
|
|
|
use pgBackRestTest::Common::StorageRepo;
|
|
|
|
use pgBackRestTest::Common::Wait;
|
2017-05-12 22:43:04 +02:00
|
|
|
use pgBackRestTest::Env::Host::HostBackupTest;
|
|
|
|
use pgBackRestTest::Env::Host::HostBaseTest;
|
|
|
|
use pgBackRestTest::Env::Host::HostDbCommonTest;
|
2020-03-10 21:12:44 +02:00
|
|
|
use pgBackRestTest::Env::Manifest;
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Db defaults
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant HOST_DB_DEFAULT => 'postgres';
|
|
|
|
use constant HOST_DB_TIMEOUT => 30;
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# new
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oParam,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->new', \@_,
|
|
|
|
{name => 'oParam', required => false, trace => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Get db version
|
2016-12-23 15:22:59 +02:00
|
|
|
my $strDbVersion = testRunGet()->pgVersion();
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
my $self = $class->SUPER::new(
|
|
|
|
{
|
2017-06-24 16:59:00 +02:00
|
|
|
strImage => containerRepo() . ':' . testRunGet()->vm() . "-test",
|
2016-08-24 18:39:27 +02:00
|
|
|
strBackupDestination => $$oParam{strBackupDestination},
|
|
|
|
oLogTest => $$oParam{oLogTest},
|
|
|
|
bStandby => $$oParam{bStandby},
|
2017-06-12 16:52:32 +02:00
|
|
|
bRepoLocal => $oParam->{bRepoLocal},
|
2017-11-06 19:51:12 +02:00
|
|
|
bRepoEncrypt => $oParam->{bRepoEncrypt},
|
2016-06-24 14:12:58 +02:00
|
|
|
});
|
|
|
|
bless $self, $class;
|
|
|
|
|
|
|
|
# Set parameters
|
2016-12-23 15:22:59 +02:00
|
|
|
$self->{strPgSocketPath} = $self->dbPath();
|
|
|
|
$self->{iPgPort} = defined($$oParam{bStandby}) && $$oParam{bStandby} ? 6544 : 6543;
|
2016-06-24 14:12:58 +02:00
|
|
|
|
2016-12-23 15:22:59 +02:00
|
|
|
$self->{strPgLogPath} = $self->testPath();
|
|
|
|
$self->{strPgLogFile} = $self->pgLogPath() . '/postgresql.log';
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
# Get Db version
|
|
|
|
if (defined($strDbVersion))
|
|
|
|
{
|
2016-12-23 15:22:59 +02:00
|
|
|
my $strOutLog = $self->executeSimple($self->pgBinPath() . '/postgres --version');
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
my @stryVersionToken = split(/ /, $strOutLog);
|
|
|
|
@stryVersionToken = split(/\./, $stryVersionToken[2]);
|
2017-09-01 18:29:34 +02:00
|
|
|
my $strDbVersionActual =
|
2017-10-05 18:33:13 +02:00
|
|
|
trim($stryVersionToken[0]) .
|
|
|
|
(defined($stryVersionToken[1]) && trim($stryVersionToken[0]) < 10 ? '.' . trim($stryVersionToken[1]) : '');
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
# Warn if this is a devel/alpha/beta version
|
|
|
|
my $strVersionRegExp = '(devel|((alpha|beta|rc)[0-9]+))$';
|
|
|
|
|
|
|
|
if ($strDbVersionActual =~ /$strVersionRegExp/)
|
|
|
|
{
|
|
|
|
my $strDevVersion = $strDbVersionActual;
|
|
|
|
$strDbVersionActual =~ s/$strVersionRegExp//;
|
|
|
|
$strDevVersion = substr($strDevVersion, length($strDbVersionActual));
|
|
|
|
|
2016-08-25 17:25:46 +02:00
|
|
|
if (!defined($$oParam{bStandby}) || !$$oParam{bStandby})
|
|
|
|
{
|
|
|
|
&log(WARN, "Testing against ${strDbVersionActual} ${strDevVersion} version");
|
|
|
|
}
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Don't run unit tests for unsupported versions
|
|
|
|
my @stryVersionSupport = versionSupport();
|
|
|
|
|
|
|
|
if ($strDbVersionActual < $stryVersionSupport[0])
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "only PostgreSQL version $stryVersionSupport[0] and up are supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($strDbVersion ne $strDbVersionActual)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "actual database version ${strDbVersionActual} does not match expected version ${strDbVersion}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 18:29:34 +02:00
|
|
|
# Create wal directory
|
|
|
|
storageTest()->pathCreate($self->dbPath() . '/pg_' . $self->walId(), {strMode => '0700'});
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlConnect
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlConnect
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
|
|
|
my $iTimeout = defined($$hParam{iTimeout}) ? $$hParam{iTimeout} : HOST_DB_TIMEOUT;
|
|
|
|
my $strDb = defined($$hParam{strDb}) ? $$hParam{strDb} : HOST_DB_DEFAULT;
|
|
|
|
|
2017-09-01 18:29:34 +02:00
|
|
|
# If not connected
|
2016-06-24 14:12:58 +02:00
|
|
|
if (!defined($self->{db}{$strDb}{hDb}))
|
|
|
|
{
|
2017-09-01 18:29:34 +02:00
|
|
|
# Retry until connection is successful
|
2016-06-24 14:12:58 +02:00
|
|
|
my $oWait = waitInit($iTimeout);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
# Connect to the db (whether it is local or remote)
|
|
|
|
$self->{db}{$strDb}{hDb} =
|
|
|
|
DBI->connect(
|
2016-12-23 15:22:59 +02:00
|
|
|
"dbi:Pg:dbname=${strDb};port=" . $self->pgPort() . ';host=' . $self->pgSocketPath(),
|
2016-06-24 14:12:58 +02:00
|
|
|
$self->userGet(), undef,
|
|
|
|
{AutoCommit => 0, RaiseError => 0, PrintError => 0});
|
|
|
|
|
|
|
|
return $self->{db}{$strDb}{hDb} if $self->{db}{$strDb}{hDb};
|
|
|
|
}
|
|
|
|
while (!defined($self->{db}{$strDb}{hDb}) && waitMore($oWait));
|
|
|
|
|
2017-09-01 18:29:34 +02:00
|
|
|
# Error if unable to connect
|
2016-06-24 14:12:58 +02:00
|
|
|
if (!defined($self->{db}{$strDb}{hDb}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to connect to PostgreSQL after ${iTimeout} second(s):\n" . $DBI::errstr, ERROR_DB_CONNECT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->{db}{$strDb}{hDb};
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlDisconnect
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlDisconnect
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
foreach my $strDb (keys(%{$self->{db}}))
|
|
|
|
{
|
|
|
|
if (defined($$hParam{$strDb}) && $$hParam{$strDb} ne $strDb)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($self->{db}{$strDb}{hDb}))
|
|
|
|
{
|
|
|
|
$self->{db}{$strDb}{hDb}->disconnect();
|
|
|
|
undef($self->{db}{$strDb}{hDb});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlExecute
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlExecute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSql = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
|
|
|
my $bCheckPoint = defined($$hParam{bCheckPoint}) ? $$hParam{bCheckPoint} : false;
|
|
|
|
my $bCommit = defined($$hParam{bCommit}) ? $$hParam{bCommit} : true;
|
|
|
|
|
|
|
|
# Get the db handle
|
|
|
|
my $hDb = $self->sqlConnect({strDb => $$hParam{strDb}});
|
|
|
|
|
|
|
|
# Set autocommit on/off
|
|
|
|
$hDb->{AutoCommit} = defined($$hParam{bAutoCommit}) ? ($$hParam{bAutoCommit} ? true : false) : false;
|
|
|
|
|
|
|
|
# Log and execute the statement
|
|
|
|
&log(DETAIL, "SQL: ${strSql}");
|
|
|
|
|
|
|
|
my $hStatement = $hDb->prepare($strSql);
|
|
|
|
|
|
|
|
$hStatement->execute() or
|
|
|
|
confess &log(ERROR, "Unable to execute: ${strSql}\n" . $DBI::errstr);
|
|
|
|
$hStatement->finish();
|
|
|
|
|
|
|
|
if ($bCommit && !$hDb->{AutoCommit})
|
|
|
|
{
|
|
|
|
$self->sqlCommit();
|
|
|
|
}
|
|
|
|
|
|
|
|
# Perform a checkpoint if requested
|
|
|
|
if ($bCheckPoint)
|
|
|
|
{
|
|
|
|
$self->sqlExecute('checkpoint', {bCommit => false, bCheckPoint => false});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set autocommit off
|
|
|
|
$hDb->{AutoCommit} = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlSelect
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlSelect
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSql = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Get the db handle
|
|
|
|
my $hDb = $self->sqlConnect({strDb => $$hParam{strDb}});
|
|
|
|
|
|
|
|
# Log and execute the statement
|
2018-01-30 23:13:54 +02:00
|
|
|
&log(DEBUG, (defined($$hParam{strDb}) ? "DB: $$hParam{strDb}, " : "") . "SQL: ${strSql}");
|
2016-06-24 14:12:58 +02:00
|
|
|
my $hStatement = $hDb->prepare($strSql);
|
|
|
|
|
|
|
|
$hStatement = $hDb->prepare($strSql);
|
|
|
|
|
|
|
|
$hStatement->execute() or
|
|
|
|
confess &log(ERROR, "Unable to execute: ${strSql}\n" . $DBI::errstr);
|
|
|
|
|
|
|
|
my @oyRow = $hStatement->fetchrow_array();
|
|
|
|
|
|
|
|
$hStatement->finish();
|
|
|
|
|
|
|
|
return @oyRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlSelectOne
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlSelectOne
|
|
|
|
{
|
2018-01-30 23:13:54 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $strSql = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
return ($self->sqlSelect($strSql, $hParam))[0];
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlSelectOneTest
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlSelectOneTest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSql = shift;
|
|
|
|
my $strExpectedValue = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
|
|
|
my $iTimeout = defined($$hParam{iTimeout}) ? $$hParam{iTimeout} : HOST_DB_TIMEOUT;
|
|
|
|
|
|
|
|
my $lStartTime = time();
|
|
|
|
my $strActualValue;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-01-30 23:13:54 +02:00
|
|
|
$self->sqlConnect($hParam);
|
|
|
|
$strActualValue = $self->sqlSelectOne($strSql, $hParam);
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
if (defined($strActualValue) && $strActualValue eq $strExpectedValue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-09-01 18:29:34 +02:00
|
|
|
|
|
|
|
$self->sqlDisconnect();
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
while (defined($iTimeout) && (time() - $lStartTime) <= $iTimeout);
|
|
|
|
|
|
|
|
confess &log(
|
|
|
|
ERROR, "expected value '${strExpectedValue}' from '${strSql}' but actual was '" .
|
|
|
|
(defined($strActualValue) ? $strActualValue : '[undef]') . "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# sqlCommit
|
|
|
|
####################################################################################################################################
|
|
|
|
sub sqlCommit
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
my $bCheckPoint = defined($$hParam{bCheckPoint}) ? $$hParam{bCheckPoint} : false;
|
|
|
|
|
|
|
|
$self->sqlExecute('commit', {bCommit => false, bCheckPoint => $bCheckPoint});
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-09-01 18:29:34 +02:00
|
|
|
# sqlWalRotate
|
2016-06-24 14:12:58 +02:00
|
|
|
####################################################################################################################################
|
2017-09-01 18:29:34 +02:00
|
|
|
sub sqlWalRotate
|
2016-06-24 14:12:58 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2017-09-01 18:29:34 +02:00
|
|
|
$self->sqlExecute('select pg_switch_' . $self->walId() . '()', {bCommit => false, bCheckPoint => false});
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# clusterCreate
|
|
|
|
#
|
|
|
|
# Create the PostgreSQL cluster and start it.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub clusterCreate
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
2017-09-01 18:29:34 +02:00
|
|
|
my $strWalPath = defined($$hParam{strWalPath}) ? $$hParam{strWalPath} : $self->dbPath() . '/pg_' . $self->walId();
|
2016-06-24 14:12:58 +02:00
|
|
|
|
2017-09-01 18:29:34 +02:00
|
|
|
# Don't link WAL directory for versions < 9.2 because some recovery scenarios won't work
|
2016-06-24 14:12:58 +02:00
|
|
|
$self->executeSimple(
|
2016-12-23 15:22:59 +02:00
|
|
|
$self->pgBinPath() . '/initdb ' .
|
|
|
|
($self->pgVersion() >= PG_VERSION_93 ? ' -k' : '') .
|
2017-09-01 18:29:34 +02:00
|
|
|
($self->pgVersion() >= PG_VERSION_92 ? ' --' . $self->walId() . "dir=${strWalPath}" : '') .
|
2018-09-25 11:24:42 +02:00
|
|
|
($self->pgVersion() >= PG_VERSION_11 ? ' --wal-segsize=1' : '') .
|
2016-06-24 14:12:58 +02:00
|
|
|
' --pgdata=' . $self->dbBasePath() . ' --auth=trust');
|
|
|
|
|
2016-12-23 15:22:59 +02:00
|
|
|
if (!$self->standby() && $self->pgVersion() >= PG_VERSION_HOT_STANDBY)
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
$self->executeSimple(
|
|
|
|
"echo 'host replication replicator db-standby trust' >> " . $self->dbBasePath() . '/pg_hba.conf');
|
|
|
|
}
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
$self->clusterStart(
|
|
|
|
{bHotStandby => $$hParam{bHotStandby}, bArchive => $$hParam{bArchive}, bArchiveAlways => $$hParam{bArchiveAlways},
|
|
|
|
bArchiveInvalid => $$hParam{bArchiveInvalid}});
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2016-12-23 15:22:59 +02:00
|
|
|
if (!$self->standby() && $self->pgVersion() >= PG_VERSION_HOT_STANDBY)
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
$self->sqlExecute("create user replicator replication", {bCommit =>true});
|
|
|
|
}
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# clusterStart
|
|
|
|
#
|
|
|
|
# Start the PostgreSQL cluster with various test options.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub clusterStart
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
|
|
|
my $bHotStandby = defined($$hParam{bHotStandby}) ? $$hParam{bHotStandby} : false;
|
|
|
|
my $bArchive = defined($$hParam{bArchive}) ? $$hParam{bArchive} : true;
|
|
|
|
my $bArchiveAlways = defined($$hParam{bArchiveAlways}) ? $$hParam{bArchiveAlways} : false;
|
|
|
|
my $bArchiveInvalid = defined($$hParam{bArchiveInvalid}) ? $$hParam{bArchiveInvalid} : false;
|
2016-09-29 01:45:33 +02:00
|
|
|
my $bArchiveEnabled = defined($$hParam{bArchiveEnabled}) ? $$hParam{bArchiveEnabled} : true;
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
# Make sure postgres is not running
|
|
|
|
if (-e $self->dbBasePath() . '/postmaster.pid')
|
|
|
|
{
|
|
|
|
confess 'postmaster.pid exists';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create the archive command
|
|
|
|
my $strArchive =
|
|
|
|
$self->backrestExe() . ' --stanza=' . ($bArchiveInvalid ? 'bogus' : $self->stanza()) .
|
|
|
|
' --config=' . $self->backrestConfig() . ' archive-push %p';
|
|
|
|
|
|
|
|
# Start the cluster
|
|
|
|
my $strCommand =
|
2016-12-23 15:22:59 +02:00
|
|
|
$self->pgBinPath() . '/pg_ctl start -o "-c port=' . $self->pgPort() .
|
|
|
|
($self->pgVersion() < PG_VERSION_95 ? ' -c checkpoint_segments=1' : '');
|
2016-06-24 14:12:58 +02:00
|
|
|
|
2016-09-29 01:45:33 +02:00
|
|
|
if ($bArchiveEnabled)
|
2016-06-24 14:12:58 +02:00
|
|
|
{
|
2016-12-23 15:22:59 +02:00
|
|
|
if ($self->pgVersion() >= PG_VERSION_95 && $bArchiveAlways)
|
2016-06-24 14:12:58 +02:00
|
|
|
{
|
|
|
|
$strCommand .= " -c archive_mode=always";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strCommand .= " -c archive_mode=on";
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 01:45:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$strCommand .= " -c archive_mode=off";
|
|
|
|
}
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
if ($bArchive)
|
|
|
|
{
|
|
|
|
$strCommand .= " -c archive_command='${strArchive}'";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strCommand .= " -c archive_command=true";
|
|
|
|
}
|
|
|
|
|
2016-12-23 15:22:59 +02:00
|
|
|
if ($self->pgVersion() >= PG_VERSION_90)
|
2016-06-24 14:12:58 +02:00
|
|
|
{
|
2017-09-01 18:29:34 +02:00
|
|
|
$strCommand .= ' -c wal_level=hot_standby -c hot_standby=' . ($bHotStandby ? 'on' : 'off');
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$strCommand .=
|
2016-12-23 15:22:59 +02:00
|
|
|
($self->pgVersion() >= PG_VERSION_HOT_STANDBY ? ' -c max_wal_senders=3' : '') .
|
2016-08-24 18:39:27 +02:00
|
|
|
' -c listen_addresses=\'*\'' .
|
2016-12-23 15:22:59 +02:00
|
|
|
' -c log_directory=\'' . $self->pgLogPath() . "'" .
|
|
|
|
' -c log_filename=\'' . basename($self->pgLogFile()) . "'" .
|
2016-08-24 18:39:27 +02:00
|
|
|
' -c log_rotation_age=0' .
|
|
|
|
' -c log_rotation_size=0' .
|
|
|
|
' -c log_error_verbosity=verbose' .
|
2016-12-23 15:22:59 +02:00
|
|
|
' -c unix_socket_director' . ($self->pgVersion() < PG_VERSION_93 ? 'y=\'' : 'ies=\'') . $self->dbPath() . '\'"' .
|
|
|
|
' -D ' . $self->dbBasePath() . ' -l ' . $self->pgLogFile() . ' -s';
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
$self->executeSimple($strCommand);
|
|
|
|
|
|
|
|
# Connect user session
|
|
|
|
$self->sqlConnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# clusterStop
|
|
|
|
#
|
|
|
|
# Stop the PostgreSQL cluster and optionally check for errors in the server log.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub clusterStop
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
|
|
|
my $bIgnoreLogError = defined($$hParam{bIgnoreLogError}) ? $$hParam{bIgnoreLogError} : false;
|
|
|
|
|
|
|
|
# Disconnect user session
|
|
|
|
$self->sqlDisconnect();
|
|
|
|
|
2017-08-11 00:11:29 +02:00
|
|
|
# Grep for errors in postgresql.log - this is done first because we want to ignore any errors that happen during shutdown
|
2017-06-09 23:51:41 +02:00
|
|
|
if (!$bIgnoreLogError && storageTest()->exists($self->pgLogFile()))
|
2016-06-24 14:12:58 +02:00
|
|
|
{
|
2017-07-24 13:57:47 +02:00
|
|
|
$self->executeSimple(
|
|
|
|
'grep -v "FATAL\: 57P03\: the database system is starting up" ' . $self->pgLogFile() . ' | grep "ERROR\|FATAL"',
|
|
|
|
{iExpectedExitStatus => 1});
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 00:11:29 +02:00
|
|
|
# If postmaster process is running then stop the cluster
|
|
|
|
if (-e $self->dbBasePath() . '/' . DB_FILE_POSTMASTERPID)
|
|
|
|
{
|
|
|
|
$self->executeSimple($self->pgBinPath() . '/pg_ctl stop -D ' . $self->dbBasePath() . ' -w -s -m fast');
|
|
|
|
}
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
# Remove the log file
|
2017-06-09 23:51:41 +02:00
|
|
|
storageTest()->remove($self->pgLogFile(), {bIgnoreMissing => true});
|
2016-06-24 14:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# clusterRestart
|
|
|
|
#
|
|
|
|
# Restart the PostgreSQL cluster.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub clusterRestart
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $hParam = shift;
|
|
|
|
|
|
|
|
$self->clusterStop($hParam);
|
|
|
|
$self->clusterStart($hParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Getters
|
|
|
|
####################################################################################################################################
|
2017-09-01 18:29:34 +02:00
|
|
|
sub walId {return shift->pgVersion() >= PG_VERSION_10 ? 'wal' : 'xlog'}
|
2016-12-23 15:22:59 +02:00
|
|
|
sub pgBinPath {return testRunGet()->pgBinPath()}
|
|
|
|
sub pgLogFile {return shift->{strPgLogFile}}
|
|
|
|
sub pgLogPath {return shift->{strPgLogPath}}
|
|
|
|
sub pgPort {return shift->{iPgPort}}
|
|
|
|
sub pgSocketPath {return shift->{strPgSocketPath}}
|
|
|
|
sub pgVersion {return testRunGet()->pgVersion()}
|
2016-06-24 14:12:58 +02:00
|
|
|
|
|
|
|
1;
|