| 1 |
#!/usr/bin/perl -wT |
| 2 |
|
| 3 |
# multiversion/cluster aware pg_ctl wrapper; this also supplies the correct |
| 4 |
# configuration parameters to 'start', and makes sure that postgres really |
| 5 |
# stops on 'stop'. |
| 6 |
# |
| 7 |
# (C) 2005-2009 Martin Pitt <[email protected]> |
| 8 |
# (C) 2009 Cyril Bouthors <[email protected]> |
| 9 |
# (C) 2013-2021 Christoph Berg <[email protected]> |
| 10 |
# |
| 11 |
# This program is free software; you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by |
| 13 |
# the Free Software Foundation; either version 2 of the License, or |
| 14 |
# (at your option) any later version. |
| 15 |
# |
| 16 |
# This program is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
# GNU General Public License for more details. |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
use Getopt::Long; |
| 24 |
use POSIX qw/setsid dup2 :sys_wait_h/; |
| 25 |
use PgCommon; |
| 26 |
use Fcntl qw(SEEK_SET O_RDWR O_CREAT O_EXCL); |
| 27 |
use POSIX qw(lchown); |
| 28 |
|
| 29 |
my ($version, $cluster, $pg_ctl, $force); |
| 30 |
my (@postgres_auxoptions, @pg_ctl_opts_from_cli); |
| 31 |
my (%postgresql_conf, %info); |
| 32 |
my $mode = 'fast'; # default shutdown mode |
| 33 |
my $foreground = 0; # don't daemonize, use postgres instead of pg_ctl |
| 34 |
my $stdlog = 0; # when run in foreground, still log to the default logfile |
| 35 |
|
| 36 |
# untaint environment |
| 37 |
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; |
| 38 |
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; |
| 39 |
chdir '/'; |
| 40 |
|
| 41 |
# If a pid file is already present, delete it if it is stale/invalid, or exit |
| 42 |
# with a notice if it belongs to an already running postgres. |
| 43 |
sub start_check_pid_file { |
| 44 |
my $pidfile = $info{'pgdata'}.'/postmaster.pid'; |
| 45 |
if (PgCommon::check_pidfile_running $pidfile) { |
| 46 |
print "Cluster is already running.\n"; |
| 47 |
exit 2; |
| 48 |
} |
| 49 |
|
| 50 |
# Remove invalid or stale PID file |
| 51 |
if (-e $pidfile) { |
| 52 |
unlink $pidfile; |
| 53 |
print "Removed stale pid file.\n"; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
# Check if a pid file is not present or it is invalid. If so, clean up/abort. |
| 58 |
sub stop_check_pid_file { |
| 59 |
my $pidfile = $info{'pgdata'}.'/postmaster.pid'; |
| 60 |
my $pid = read_pidfile $pidfile; |
| 61 |
return if (defined $pid && PgCommon::check_pidfile_running $pidfile); |
| 62 |
if ($info{'running'}) { |
| 63 |
error 'pid file is invalid, please manually kill the stale server process.'; |
| 64 |
} |
| 65 |
|
| 66 |
# Remove invalid or stale PID file |
| 67 |
if (-e $pidfile) { |
| 68 |
unlink $pidfile; |
| 69 |
print "Removed stale pid file.\n"; |
| 70 |
} |
| 71 |
print "Cluster is not running.\n"; |
| 72 |
exit 2; |
| 73 |
} |
| 74 |
|
| 75 |
# check if a cluster reliably connects or fails |
| 76 |
# Arguments: <version> <cluster> <port> <socket dir> |
| 77 |
sub cluster_port_ready { |
| 78 |
my ($v, $c, $p, $sd) = @_; |
| 79 |
my $psql = get_program_path('psql', $v); |
| 80 |
error 'cluster_port_ready: could not find psql binary' unless $psql; |
| 81 |
my $n = 0; |
| 82 |
my $result = 0; |
| 83 |
|
| 84 |
# probe until we get three successful or failed connections in a row |
| 85 |
my $nopwoption = $v >= 8.4 ? '-w' : ''; |
| 86 |
if ($v < 8.4) { |
| 87 |
$ENV{'PGPASSWORD'} = 'foo'; # prevent hangs if superuser cannot connect without password |
| 88 |
} |
| 89 |
my $out; |
| 90 |
while ($n < ($result ? 10 : 3)) { |
| 91 |
select undef, undef, undef, 0.5; |
| 92 |
$out = `LC_MESSAGES=C $psql -h '$sd' --port $p $nopwoption -Xc '' template1 2>&1 >/dev/null`; |
| 93 |
|
| 94 |
if ($? == $result) { |
| 95 |
$n++; |
| 96 |
} else { |
| 97 |
$n = 0; |
| 98 |
} |
| 99 |
$result = $?; |
| 100 |
} |
| 101 |
|
| 102 |
if ($out =~ /FATAL:|no password supplied/) { |
| 103 |
print STDERR "Warning: connection to the database failed, disabling startup checks:\n$out\n"; |
| 104 |
return cluster_port_running $v, $c, $p; |
| 105 |
} |
| 106 |
return !$result; |
| 107 |
} |
| 108 |
|
| 109 |
sub start { |
| 110 |
start_check_pid_file; |
| 111 |
|
| 112 |
# check conflicting port |
| 113 |
if (cluster_port_running $version, $cluster, $info{'port'}) { |
| 114 |
my $sockdir = get_cluster_socketdir $version, $cluster; |
| 115 |
error("Port conflict: another instance is already running on $sockdir with port $info{'port'}"); |
| 116 |
} |
| 117 |
|
| 118 |
# prepare environment (empty except for content of 'environment', and LANG) |
| 119 |
%ENV = read_cluster_conf_file $version, $cluster, 'environment', 1; |
| 120 |
# set LANG so non-ascii chars in the server log are not replaced by '?' (affected are non-session contexts) |
| 121 |
unless (exists $ENV{LANG}) { |
| 122 |
my $lc_messages = PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'lc_messages'; |
| 123 |
$ENV{LANG} = $lc_messages if $lc_messages; |
| 124 |
} |
| 125 |
# 9.5 configures OOM killer using env vars |
| 126 |
if ($version >= 9.5) { |
| 127 |
$ENV{PG_OOM_ADJUST_FILE} = "/proc/self/oom_score_adj" unless (exists $ENV{PG_OOM_ADJUST_FILE}); |
| 128 |
# PG_OOM_ADJUST_VALUE defaults to 0, but can be overridden here |
| 129 |
} |
| 130 |
|
| 131 |
my $postgres_opts = ''; |
| 132 |
my $usd = $version >= 9.3 ? 'unix_socket_directories' : 'unix_socket_directory'; |
| 133 |
if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', $usd)) { |
| 134 |
$postgres_opts .= "-c $usd=\"$info{'socketdir'}\""; |
| 135 |
} |
| 136 |
|
| 137 |
my $cdir = $info{'configdir'}; |
| 138 |
$postgres_opts .= " -c config_file=\"$cdir/postgresql.conf\""; |
| 139 |
if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'hba_file')) { |
| 140 |
$postgres_opts .= " -c hba_file=\"$cdir/pg_hba.conf\""; |
| 141 |
} |
| 142 |
if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'ident_file')) { |
| 143 |
$postgres_opts .= " -c ident_file=\"$cdir/pg_ident.conf\""; |
| 144 |
} |
| 145 |
|
| 146 |
if ((-d '/var/run/postgresql') && !defined (PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'external_pid_file')) { |
| 147 |
# check whether /var/run/postgresql/ is writable as the cluster owner |
| 148 |
my $vrp_writable; |
| 149 |
if ($> == 0) { |
| 150 |
change_ugid $info{'owneruid'}, $info{'ownergid'}; |
| 151 |
$vrp_writable = -w '/var/run/postgresql'; |
| 152 |
$< = $> = 0; |
| 153 |
$( = $) = 0; |
| 154 |
} else { |
| 155 |
$vrp_writable = -w '/var/run/postgresql'; |
| 156 |
} |
| 157 |
if ($vrp_writable) { |
| 158 |
$postgres_opts .= " -c external_pid_file=\"/var/run/postgresql/$version-$cluster.pid\""; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$postgres_opts .= ' ' . (join ' ', @postgres_auxoptions); |
| 163 |
($postgres_opts) = $postgres_opts =~ /(.*)/; # untaint |
| 164 |
|
| 165 |
if ($foreground) { |
| 166 |
if ($stdlog and $info{'logfile'}) { |
| 167 |
my $fd = POSIX::open($info{logfile}, POSIX::O_WRONLY|POSIX::O_APPEND|POSIX::O_CREAT) or error "Could not open logfile $info{logfile}"; |
| 168 |
dup2($fd, 1); |
| 169 |
dup2($fd, 2); |
| 170 |
POSIX::close($fd) or error "Could not close log fd"; |
| 171 |
} |
| 172 |
my $postgres = get_program_path 'postgres', $version; |
| 173 |
error "Could not find postgres executable for version $version" unless ($postgres); |
| 174 |
exec '/bin/sh', '-c', "exec $postgres $postgres_opts" or |
| 175 |
error "Executing $postgres failed: $!" |
| 176 |
} |
| 177 |
|
| 178 |
# only supply or default logfile path when none is given explicitly in |
| 179 |
# postgresql.conf |
| 180 |
my @options = ($pg_ctl, 'start', '-D', $info{'pgdata'}); |
| 181 |
my $logsize = 0; |
| 182 |
if ($info{'logfile'}) { |
| 183 |
push @options, ('-l', $info{'logfile'}); |
| 184 |
# remember current size of the log |
| 185 |
$logsize = (stat $info{'logfile'})[7] || 0; # ignore stat errors |
| 186 |
} |
| 187 |
|
| 188 |
push @options, @pg_ctl_opts_from_cli if @pg_ctl_opts_from_cli; |
| 189 |
|
| 190 |
my %pg_ctl_opts_from_file = read_cluster_conf_file $version, $cluster, 'pg_ctl.conf', 1; |
| 191 |
push @options, split(' ', $pg_ctl_opts_from_file{'pg_ctl_options'}) |
| 192 |
if defined $pg_ctl_opts_from_file{'pg_ctl_options'} and $pg_ctl_opts_from_file{'pg_ctl_options'} ne ''; |
| 193 |
|
| 194 |
push @options, ('-s', '-o', $postgres_opts); |
| 195 |
|
| 196 |
if (fork) { |
| 197 |
wait; |
| 198 |
if ($?) { |
| 199 |
my $exit = $? >> 8; |
| 200 |
print STDERR "Error: $pg_ctl @options exited with status $exit: $!\n"; |
| 201 |
my $currlogsize = (stat $info{'logfile'})[7] if $info{'logfile'} && -r $info{'logfile'}; |
| 202 |
if ($currlogsize) { |
| 203 |
open LOG, $info{'logfile'} or |
| 204 |
error "Could not open log file " . $info{'logfile'}; |
| 205 |
seek LOG, $logsize, SEEK_SET; |
| 206 |
print STDERR $_ while <LOG>; |
| 207 |
} |
| 208 |
exit $exit; |
| 209 |
} |
| 210 |
} else { |
| 211 |
if (setsid() < 0) { |
| 212 |
error "could not start session: $!"; |
| 213 |
} |
| 214 |
if ($info{'logfile'}) { |
| 215 |
my $fd = POSIX::open($info{'logfile'}, POSIX::O_WRONLY|POSIX::O_APPEND|POSIX::O_CREAT) or error "Could not open logfile $info{'logfile'}"; |
| 216 |
dup2($fd, 1); |
| 217 |
dup2($fd, 2); |
| 218 |
POSIX::close($fd) or error "Could not close log fd"; |
| 219 |
} |
| 220 |
exec $pg_ctl @options or error "could not exec $pg_ctl @options: $!"; |
| 221 |
} |
| 222 |
|
| 223 |
# wait a bit until the socket exists |
| 224 |
my $success = 0; |
| 225 |
my $currlogsize = 0; |
| 226 |
my $pidfile = $info{'pgdata'}.'/postmaster.pid'; |
| 227 |
for (my $attempt = 0; $attempt < 60; $attempt++) { |
| 228 |
select (undef, undef, undef, 0.5); |
| 229 |
$currlogsize = (stat $info{'logfile'})[7] if $info{'logfile'} && -r $info{'logfile'}; |
| 230 |
if (cluster_port_running $version, $cluster, $info{'port'}) { |
| 231 |
$success = 1; |
| 232 |
last; |
| 233 |
} |
| 234 |
|
| 235 |
# if postgres wrote something, but the process does not exist any |
| 236 |
# more, there must be a problem and we can stop immediately |
| 237 |
last if ($currlogsize > $logsize && !PgCommon::check_pidfile_running $pidfile); |
| 238 |
} |
| 239 |
|
| 240 |
# OK, the server runs, now wait until it stabilized |
| 241 |
if ($success) { |
| 242 |
$success = cluster_port_ready $version, $cluster, $info{'port'}, $info{'socketdir'}; |
| 243 |
} |
| 244 |
|
| 245 |
if (!$success) { |
| 246 |
if ($currlogsize) { |
| 247 |
print STDERR "The PostgreSQL server failed to start. Please check the log output:\n"; |
| 248 |
open LOG, $info{'logfile'} or |
| 249 |
error "Could not open log file " . $info{'logfile'}; |
| 250 |
seek LOG, $logsize, SEEK_SET; |
| 251 |
print STDERR $_ while <LOG>; |
| 252 |
} else { |
| 253 |
print STDERR "The PostgreSQL server failed to start. Please check the log output.\n"; |
| 254 |
} |
| 255 |
exit 1; |
| 256 |
} |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
sub stop { |
| 262 |
stop_check_pid_file; |
| 263 |
my $result = 1; |
| 264 |
|
| 265 |
my @options = ('stop', '-s', '-w', '-D', $info{'pgdata'}); |
| 266 |
|
| 267 |
push @options, @pg_ctl_opts_from_cli if @pg_ctl_opts_from_cli; |
| 268 |
|
| 269 |
my %pg_ctl_opts_from_file = read_cluster_conf_file $version, $cluster, 'pg_ctl.conf', 1; |
| 270 |
push @options, split(' ', $pg_ctl_opts_from_file{'pg_ctl_options'}) |
| 271 |
if defined $pg_ctl_opts_from_file{'pg_ctl_options'} and $pg_ctl_opts_from_file{'pg_ctl_options'} ne ''; |
| 272 |
|
| 273 |
if (!fork()) { |
| 274 |
close STDOUT; |
| 275 |
exec $pg_ctl, @options, '-m', ($force ? 'fast' : $mode); |
| 276 |
} else { |
| 277 |
wait; |
| 278 |
$result = $? >> 8; |
| 279 |
} |
| 280 |
|
| 281 |
# try harder if forced and server hasn't stopped yet |
| 282 |
if ($force and -f $info{'pgdata'}.'/postmaster.pid') { |
| 283 |
print "(does not shutdown gracefully, now stopping immediately)\n"; |
| 284 |
$result = system $pg_ctl, @options, '-m', 'immediate'; |
| 285 |
} |
| 286 |
|
| 287 |
# external_pid_file files are currently not removed by postgres itself |
| 288 |
if ($result == 0) { |
| 289 |
unlink "/var/run/postgresql/$version-$cluster.pid"; |
| 290 |
} |
| 291 |
|
| 292 |
return $result; |
| 293 |
} |
| 294 |
|
| 295 |
sub restart { |
| 296 |
my $result; |
| 297 |
|
| 298 |
if ($info{'running'}) { |
| 299 |
$result = stop; |
| 300 |
return $result if $result; |
| 301 |
} |
| 302 |
return start; |
| 303 |
} |
| 304 |
|
| 305 |
sub reload { |
| 306 |
exec $pg_ctl, '-D', $info{'pgdata'}, '-s', @pg_ctl_opts_from_cli, 'reload'; |
| 307 |
} |
| 308 |
|
| 309 |
sub status { |
| 310 |
exec $pg_ctl, '-D', $info{'pgdata'}, 'status'; |
| 311 |
} |
| 312 |
|
| 313 |
sub promote { |
| 314 |
exec $pg_ctl, '-D', $info{'pgdata'}, '-s', @pg_ctl_opts_from_cli, 'promote'; |
| 315 |
} |
| 316 |
|
| 317 |
sub logrotate { |
| 318 |
exec $pg_ctl, '-D', $info{'pgdata'}, '-s', @pg_ctl_opts_from_cli, 'logrotate'; |
| 319 |
} |
| 320 |
|
| 321 |
# |
| 322 |
# main |
| 323 |
# |
| 324 |
|
| 325 |
my ($skip_systemctl_redirect, $bindir); |
| 326 |
|
| 327 |
exit 1 unless GetOptions ('o|options=s' => \@postgres_auxoptions, |
| 328 |
'f|force' => \$force, |
| 329 |
'm|mode=s' => \$mode, |
| 330 |
'foreground' => \$foreground, |
| 331 |
'skip-systemctl-redirect' => \$skip_systemctl_redirect, |
| 332 |
'stdlog' => \$stdlog, |
| 333 |
'bindir=s' => \$bindir, |
| 334 |
); |
| 335 |
|
| 336 |
if ($mode =~ /^(s(mart)?|f(ast)?|i(mmediate)?)$/) { |
| 337 |
$mode = $1; # untaint |
| 338 |
} else { |
| 339 |
print "Invalid -m mode, valid are: smart fast immediate\n"; |
| 340 |
exit 1; |
| 341 |
} |
| 342 |
|
| 343 |
($bindir) = $bindir =~ /^(\/.*)$/ if $bindir; # untaint |
| 344 |
|
| 345 |
# parse command |
| 346 |
my $version_re = qr/(\d+\.?\d)/; |
| 347 |
my $cluster_re = qr/([^'"\s]+)/; |
| 348 |
my $action_re = qr/(start|stop|restart|reload|status|promote|logrotate)/; |
| 349 |
my $action; |
| 350 |
|
| 351 |
if (@ARGV >= 3 and "$ARGV[0] $ARGV[1] $ARGV[2]" =~ /^$version_re $cluster_re $action_re$/) { |
| 352 |
($version, $cluster, $action) = ($1, $2, $3); |
| 353 |
splice @ARGV, 0, 3; |
| 354 |
} elsif (@ARGV >= 3 and "$ARGV[0] $ARGV[1] $ARGV[2]" =~ /^$action_re $version_re $cluster_re$/) { |
| 355 |
($action, $version, $cluster) = ($1, $2, $3); |
| 356 |
splice @ARGV, 0, 3; |
| 357 |
} elsif (@ARGV >= 2 and "$ARGV[0] $ARGV[1]" =~ m!^$version_re(?:[-/])$cluster_re $action_re$!) { |
| 358 |
($version, $cluster, $action) = ($1, $2, $3); |
| 359 |
splice @ARGV, 0, 2; |
| 360 |
} elsif (@ARGV >= 2 and "$ARGV[0] $ARGV[1]" =~ m!^$action_re $version_re(?:[-/])$cluster_re$!) { |
| 361 |
($action, $version, $cluster) = ($1, $2, $3); |
| 362 |
splice @ARGV, 0, 2; |
| 363 |
} else { |
| 364 |
error "Usage: $0 <version> <cluster> <action> [-- <pg_ctl options>]"; |
| 365 |
} |
| 366 |
|
| 367 |
@pg_ctl_opts_from_cli=(); |
| 368 |
foreach my $argv (@ARGV) { |
| 369 |
push @pg_ctl_opts_from_cli, $argv =~ /(.*)/; # untaint |
| 370 |
} |
| 371 |
|
| 372 |
error "specified cluster '$version $cluster' does not exist" unless cluster_exists $version, $cluster; |
| 373 |
%info = cluster_info ($version, $cluster); |
| 374 |
validate_cluster_owner \%info; |
| 375 |
|
| 376 |
unless ($action eq 'stop') { |
| 377 |
# check if cluster is disabled in start.conf |
| 378 |
error 'Cluster is disabled' if $info{'start'} eq 'disabled'; |
| 379 |
} |
| 380 |
|
| 381 |
# redirect the request through systemd |
| 382 |
if (not $skip_systemctl_redirect and getppid() != 1 and # not run from init |
| 383 |
-d '/run/systemd/system' and not exists $ENV{_SYSTEMCTL_SKIP_REDIRECT} and # systemd is running |
| 384 |
not $foreground and # foreground operation not requested |
| 385 |
not exists $ENV{'PG_CLUSTER_CONF_ROOT'} and # not handling user clusters |
| 386 |
$action =~ /^(start|stop|restart)$/ # redirect only these actions |
| 387 |
) { |
| 388 |
$action = $1; # untaint |
| 389 |
system 'systemctl', 'is-active', '-q', "postgresql\@$version-$cluster"; |
| 390 |
my $unit_active = $? == 0; |
| 391 |
|
| 392 |
# if extra options are given, proceed with pg_ctlcluster with a warning |
| 393 |
if (@postgres_auxoptions != 0 or @pg_ctl_opts_from_cli != 0 or $bindir) { # extra options given |
| 394 |
print "Notice: extra pg_ctl/postgres options given, bypassing systemctl for $action operation\n" if (-t 1); |
| 395 |
# if we are root, redirect to systemctl unless stopping a cluster running outside systemd |
| 396 |
} elsif ($> == 0) { |
| 397 |
if ($action eq 'stop' and not $unit_active) { |
| 398 |
# proceed with pg_ctlcluster |
| 399 |
} else { |
| 400 |
#print "Redirecting $action request to systemctl\n" if (-t 1); |
| 401 |
system 'systemctl', $action, "postgresql\@$version-$cluster"; |
| 402 |
exit $? >> 8; |
| 403 |
# program end |
| 404 |
} |
| 405 |
# as non-root, raise an error on restarting a cluster running from systemd |
| 406 |
} elsif ($action eq 'restart' and $unit_active) { |
| 407 |
error "cluster is running from systemd, can only restart it as root. Try instead:\n sudo systemctl $action postgresql\@$version-$cluster"; |
| 408 |
# program end |
| 409 |
# otherwise just raise a warning on start and restart as non-root |
| 410 |
} elsif (-t 1) { |
| 411 |
if ($action =~ /start/) { |
| 412 |
print "Warning: the cluster will not be running as a systemd service. Consider using systemctl:\n"; |
| 413 |
print " sudo systemctl $action postgresql\@$version-$cluster\n"; |
| 414 |
} elsif ($unit_active) { |
| 415 |
print "Warning: stopping the cluster using pg_ctlcluster will mark the systemd unit as failed. Consider using systemctl:\n"; |
| 416 |
print " sudo systemctl $action postgresql\@$version-$cluster\n"; |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
# recreate missing standard log dir |
| 422 |
if ($> == 0 && ! -e '/var/log/postgresql' && |
| 423 |
$info{'logfile'} =~ m!^/var/log/postgresql!) { |
| 424 |
system 'install', '-d', '-m', '1775', '-o', 'root', '-g', 'postgres', '/var/log/postgresql'; |
| 425 |
} |
| 426 |
|
| 427 |
# recreate missing log file |
| 428 |
if ($action ne 'stop' && $info{'logfile'} && ! -e $info{'logfile'}) { |
| 429 |
if ($> == 0) { # drop privileges; this is important if logfile |
| 430 |
# was determined via an /etc/postgresql/.../log symlink |
| 431 |
change_ugid $info{'owneruid'}, $info{'ownergid'}; |
| 432 |
} |
| 433 |
sysopen (L, $info{'logfile'}, O_RDWR|O_CREAT|O_EXCL) or |
| 434 |
error 'Could not create log file ' . $info{'logfile'}; |
| 435 |
close L; |
| 436 |
chmod 0640, $info{'logfile'}; |
| 437 |
$< = $> = 0; # will silently fail if we were not root before, that's intended |
| 438 |
$( = $) = 0; |
| 439 |
if ($info{'owneruid'} < 500) { |
| 440 |
my $g = (getgrnam 'adm')[2]; |
| 441 |
lchown $info{'owneruid'}, $g, $info{'logfile'} if (defined $g); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
if ($action ne 'stop') { |
| 446 |
# recreate /var/run/postgresql while possibly still running as root |
| 447 |
if (! -d '/var/run/postgresql') { |
| 448 |
system 'install', '-d', '-m', 2775, '-o', 'postgres', '-g', 'postgres', '/var/run/postgresql'; |
| 449 |
} |
| 450 |
|
| 451 |
# allow creating socket directories below /var/run/postgresql for any user |
| 452 |
if ($info{socketdir} =~ m!^(/var)/run/postgresql/[\w_.-]+$! and ! -d $info{socketdir}) { |
| 453 |
if (mkdir $info{socketdir}, 02775) { # don't use "install" here as it would allow stealing existing directories |
| 454 |
chown $info{owneruid}, $info{ownergid}, $info{socketdir}; |
| 455 |
} else { |
| 456 |
error "Could not create $info{socketdir}: $!"; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
# allow creating stats_temp_directory below /var/run/postgresql for any user |
| 461 |
if ($info{config}->{stats_temp_directory} and $info{config}->{stats_temp_directory}=~ m!^(/var)/run/postgresql/[\w_.-]+$! and ! -d $info{config}->{stats_temp_directory}) { |
| 462 |
if (mkdir $info{config}->{stats_temp_directory}, 0750) { # don't use "install" here as it would allow stealing existing directories |
| 463 |
chown $info{owneruid}, $info{ownergid}, $info{config}->{stats_temp_directory}; |
| 464 |
} else { |
| 465 |
error "Could not create $info{config}->{stats_temp_directory}: $!"; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
if ($> == 0) { |
| 471 |
# have postgres start with increased OOM killer protection; 9.0 and |
| 472 |
# later has builtin support for resetting the adjustment of child processes |
| 473 |
if ($action eq 'start' and $version >= '9.0' and not $PgCommon::rpm) { |
| 474 |
if (-w '/proc/self/oom_score_adj') { |
| 475 |
open F, '>/proc/self/oom_score_adj'; |
| 476 |
print F "-900\n"; |
| 477 |
close F; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
change_ugid $info{'owneruid'}, $info{'ownergid'}; |
| 482 |
} |
| 483 |
|
| 484 |
# we are running as the cluster owner now |
| 485 |
|
| 486 |
if( $> != $info{'owneruid'} ) { |
| 487 |
error 'You must run this program as the cluster owner ('. |
| 488 |
(getpwuid $info{'owneruid'})[0].') or root'; |
| 489 |
} |
| 490 |
|
| 491 |
# create socket directory (if it wasn't already created in /var/run/postgresql by the code above) |
| 492 |
if ($action ne 'stop' && ! -d $info{socketdir}) { |
| 493 |
system 'install', '-d', '-m', 2775, $info{socketdir}; |
| 494 |
} |
| 495 |
|
| 496 |
# create stats_temp_directory (if it wasn't already created in /var/run/postgresql by the code above) |
| 497 |
if ($action ne 'stop' && $info{config}->{stats_temp_directory} && ! -d $info{config}->{stats_temp_directory}) { |
| 498 |
system 'install', '-d', '-m', 750, $info{config}->{stats_temp_directory}; |
| 499 |
} |
| 500 |
|
| 501 |
$pg_ctl = $bindir ? "$bindir/pg_ctl" : get_program_path ('pg_ctl', $version); |
| 502 |
error "Could not find pg_ctl executable for version $version" unless ($pg_ctl); |
| 503 |
|
| 504 |
# do the action |
| 505 |
no strict 'refs'; |
| 506 |
exit &$action; |
| 507 |
|
| 508 |
__END__ |
| 509 |
|
| 510 |
=head1 NAME |
| 511 |
|
| 512 |
pg_ctlcluster - start/stop/restart/reload a PostgreSQL cluster |
| 513 |
|
| 514 |
=head1 SYNOPSIS |
| 515 |
|
| 516 |
B<pg_ctlcluster> [I<options>] I<cluster-version> I<cluster-name> I<action> [B<--> I<pg_ctl options>] |
| 517 |
|
| 518 |
where I<action> = B<start>|B<stop>|B<restart>|B<reload>|B<status>|B<promote>|B<logrotate> |
| 519 |
|
| 520 |
=head1 DESCRIPTION |
| 521 |
|
| 522 |
This program controls the B<postgres> server for a particular cluster. It |
| 523 |
essentially wraps the L<pg_ctl(1)> command. It determines the cluster version |
| 524 |
and data path and calls the right version of B<pg_ctl> with appropriate |
| 525 |
configuration parameters and paths. |
| 526 |
|
| 527 |
You have to start this program as the user who owns the database cluster or as |
| 528 |
root. |
| 529 |
|
| 530 |
To ease integration with B<systemd> operation, the alternative syntax |
| 531 |
"B<pg_ctlcluster> I<version>B<->I<cluster> I<action>" is also supported, |
| 532 |
as well as putting the action first (matching the ordering used by B<systemctl>). |
| 533 |
|
| 534 |
=head1 ACTIONS |
| 535 |
|
| 536 |
=over 4 |
| 537 |
|
| 538 |
=item B<start> |
| 539 |
|
| 540 |
A log file for this specific cluster is created if it does not exist yet (by |
| 541 |
default, |
| 542 |
C</var/log/postgresql/postgresql->I<cluster-version>C<->I<cluster-name>C<.log>), |
| 543 |
and a PostgreSQL server process (L<postgres(1)>) is started on it. Exits with |
| 544 |
0 on success, with 2 if the server is already running, and with 1 on other |
| 545 |
failure conditions. |
| 546 |
|
| 547 |
=item B<stop> |
| 548 |
|
| 549 |
Stops the L<postgres(1)> server of the given cluster. By default, "fast" |
| 550 |
shutdown mode is used. |
| 551 |
|
| 552 |
=item B<restart> |
| 553 |
|
| 554 |
Stops the server if it is running and starts it (again). |
| 555 |
|
| 556 |
=item B<reload> |
| 557 |
|
| 558 |
Causes the configuration files to be re-read without a full shutdown of the |
| 559 |
server. |
| 560 |
|
| 561 |
=item B<status> |
| 562 |
|
| 563 |
Checks whether a server is running. If it is, the PID and the command line |
| 564 |
options that were used to invoke it are displayed. |
| 565 |
|
| 566 |
=item B<promote> |
| 567 |
|
| 568 |
Commands a running standby server to exit recovery and begin read-write |
| 569 |
operations. |
| 570 |
|
| 571 |
=item B<logrotate> |
| 572 |
|
| 573 |
Requests that the server of a given cluster rotates the log file. |
| 574 |
|
| 575 |
=back |
| 576 |
|
| 577 |
=head1 OPTIONS |
| 578 |
|
| 579 |
=over 4 |
| 580 |
|
| 581 |
=item B<-f>|B<--force> |
| 582 |
|
| 583 |
For B<stop> and B<restart>, the "fast" mode is used which rolls back all active |
| 584 |
transactions, disconnects clients immediately and thus shuts down cleanly. If |
| 585 |
that does not work, shutdown is attempted again in "immediate" mode, which can |
| 586 |
leave the cluster in an inconsistent state and thus will lead to a recovery run |
| 587 |
at the next start. If this still does not help, the B<postgres> process is |
| 588 |
killed. Exits with 0 on success, with 2 if the server is not running, and with |
| 589 |
1 on other failure conditions. This mode should only be used when the machine |
| 590 |
is about to be shut down. |
| 591 |
|
| 592 |
=item B<-m>|B<--mode> [B<smart>|B<fast>|B<immediate>] |
| 593 |
|
| 594 |
Shutdown mode to use for B<stop> and B<restart> actions, default is B<fast>. |
| 595 |
See pg_ctl(1) for documentation. |
| 596 |
|
| 597 |
=item B<--foreground> |
| 598 |
|
| 599 |
Start B<postgres> in foreground, without daemonizing via B<pg_ctl>. |
| 600 |
|
| 601 |
=item B<--stdlog> |
| 602 |
|
| 603 |
When B<--foreground> is in use, redirect stderr to the standard logfile in |
| 604 |
C</var/log/postgresql/>. (Default when not run in foreground.) |
| 605 |
|
| 606 |
=item B<--skip-systemctl-redirect> |
| 607 |
|
| 608 |
When running as root, B<pg_ctlcluster> redirects actions to B<systemctl> so |
| 609 |
running clusters are properly supervised by B<systemd>. This option skips the |
| 610 |
redirect; it is used in the B<[email protected]> unit file. The redirect is |
| 611 |
also skipped if additional B<postgres> or B<pg_ctl> options are provided. |
| 612 |
|
| 613 |
=item B<--bindir> I<directory> |
| 614 |
|
| 615 |
Path to B<pg_ctl>. (Default is C</usr/lib/postgresql/>I<version>C</bin>.) |
| 616 |
|
| 617 |
=item B<-o>|B<--options> I<option> |
| 618 |
|
| 619 |
Pass given I<option> as command line option to the C<postgres> process. It is |
| 620 |
possible to specify B<-o> multiple times. See L<postgres(1)> for a |
| 621 |
description of valid options. |
| 622 |
|
| 623 |
=item I<pg_ctl options> |
| 624 |
|
| 625 |
Pass given I<pg_ctl options> as command line options to B<pg_ctl>. See L<pg_ctl(1)> |
| 626 |
for a description of valid options. |
| 627 |
|
| 628 |
=back |
| 629 |
|
| 630 |
=head1 FILES |
| 631 |
|
| 632 |
=over 4 |
| 633 |
|
| 634 |
=item C</etc/postgresql/>I<cluster-version>C</>I<cluster-name>C</pg_ctl.conf> |
| 635 |
|
| 636 |
This configuration file contains cluster specific options to be passed to |
| 637 |
L<pg_ctl(1)>. |
| 638 |
|
| 639 |
=item C</etc/postgresql/>I<cluster-version>C</>I<cluster-name>C</start.conf> |
| 640 |
|
| 641 |
This configuration file controls the start/stop behavior of the cluster. See |
| 642 |
section "STARTUP CONTROL" in L<pg_createcluster(8)> for details. |
| 643 |
|
| 644 |
=back |
| 645 |
|
| 646 |
=head1 BUGS |
| 647 |
|
| 648 |
Changing the port number on startup using B<-o -p> will not work as it breaks |
| 649 |
the checks for running clusters. |
| 650 |
|
| 651 |
=head1 SEE ALSO |
| 652 |
|
| 653 |
L<pg_createcluster(8)>, L<pg_ctl(1)>, L<pg_wrapper(1)>, L<pg_lsclusters(1)>, |
| 654 |
L<postgres(1)> |
| 655 |
|
| 656 |
=head1 AUTHOR |
| 657 |
|
| 658 |
Martin Pitt L<E<lt>[email protected]<gt>> |
| 659 |
|