Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e7010ce

Browse files
committed
pg_ctl: Add wait option to promote action
When waiting is selected for the promote action, look into pg_control until the state changes, then use the PQping-based waiting until the server is reachable. Reviewed-by: Michael Paquier <michael.paquier@gmail.com>
1 parent ebdf5bf commit e7010ce

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed

doc/src/sgml/ref/pg_ctl-ref.sgml

+23-6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ PostgreSQL documentation
9191
<cmdsynopsis>
9292
<command>pg_ctl</command>
9393
<arg choice="plain"><option>promote</option></arg>
94+
<arg choice="opt"><option>-w</option></arg>
95+
<arg choice="opt"><option>-t</option> <replaceable>seconds</replaceable></arg>
9496
<arg choice="opt"><option>-s</option></arg>
9597
<arg choice="opt"><option>-D</option> <replaceable>datadir</replaceable></arg>
9698
</cmdsynopsis>
@@ -361,8 +363,8 @@ PostgreSQL documentation
361363
<term><option>--timeout</option></term>
362364
<listitem>
363365
<para>
364-
The maximum number of seconds to wait when waiting for startup or
365-
shutdown to complete. Defaults to the value of the
366+
The maximum number of seconds to wait when waiting for an operation
367+
to complete (see option <option>-w</option>). Defaults to the value of the
366368
<envar>PGCTLTIMEOUT</> environment variable or, if not set, to 60
367369
seconds.
368370
</para>
@@ -383,8 +385,23 @@ PostgreSQL documentation
383385
<term><option>-w</option></term>
384386
<listitem>
385387
<para>
386-
Wait for the startup or shutdown to complete.
387-
Waiting is the default option for shutdowns, but not startups.
388+
Wait for an operation to complete. This is supported for the
389+
modes <literal>start</literal>, <literal>stop</literal>,
390+
<literal>restart</literal>, <literal>promote</literal>,
391+
and <literal>register</literal>.
392+
</para>
393+
394+
<para>
395+
Waiting is the default option for shutdowns, but not startups,
396+
restarts, or promotions. This is mainly for historical reasons; the
397+
waiting option is almost always preferable. If waiting is not
398+
selected, the requested action is triggered, but there is no feedback
399+
about its success. In that case, the server log file or an external
400+
monitoring system would have to be used to check the progress and
401+
success of the operation.
402+
</para>
403+
404+
<para>
388405
When waiting for startup, <command>pg_ctl</command> repeatedly
389406
attempts to connect to the server.
390407
When waiting for shutdown, <command>pg_ctl</command> waits for
@@ -400,8 +417,8 @@ PostgreSQL documentation
400417
<term><option>-W</option></term>
401418
<listitem>
402419
<para>
403-
Do not wait for startup or shutdown to complete. This is the
404-
default for start and restart modes.
420+
Do not wait for an operation to complete. This is the opposite of the
421+
option <option>-w</option>.
405422
</para>
406423
</listitem>
407424
</varlistentry>

src/bin/pg_ctl/pg_ctl.c

+32-13
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,34 @@ do_promote(void)
12281228
exit(1);
12291229
}
12301230

1231-
print_msg(_("server promoting\n"));
1231+
if (do_wait)
1232+
{
1233+
DBState state = DB_STARTUP;
1234+
1235+
print_msg(_("waiting for server to promote..."));
1236+
while (wait_seconds > 0)
1237+
{
1238+
state = get_control_dbstate();
1239+
if (state == DB_IN_PRODUCTION)
1240+
break;
1241+
1242+
print_msg(".");
1243+
pg_usleep(1000000); /* 1 sec */
1244+
wait_seconds--;
1245+
}
1246+
if (state == DB_IN_PRODUCTION)
1247+
{
1248+
print_msg(_(" done\n"));
1249+
print_msg(_("server promoted\n"));
1250+
}
1251+
else
1252+
{
1253+
print_msg(_(" stopped waiting\n"));
1254+
print_msg(_("server is still promoting\n"));
1255+
}
1256+
}
1257+
else
1258+
print_msg(_("server promoting\n"));
12321259
}
12331260

12341261

@@ -2405,18 +2432,10 @@ main(int argc, char **argv)
24052432

24062433
if (!wait_set)
24072434
{
2408-
switch (ctl_command)
2409-
{
2410-
case RESTART_COMMAND:
2411-
case START_COMMAND:
2412-
do_wait = false;
2413-
break;
2414-
case STOP_COMMAND:
2415-
do_wait = true;
2416-
break;
2417-
default:
2418-
break;
2419-
}
2435+
if (ctl_command == STOP_COMMAND)
2436+
do_wait = true;
2437+
else
2438+
do_wait = false;
24202439
}
24212440

24222441
if (ctl_command == RELOAD_COMMAND)

src/bin/pg_ctl/t/003_promote.pl

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 9;
6+
use Test::More tests => 12;
77

88
my $tempdir = TestLib::tempdir;
99

@@ -37,3 +37,19 @@
3737

3838
ok($node_standby->poll_query_until('postgres', 'SELECT NOT pg_is_in_recovery()'),
3939
'promoted standby is not in recovery');
40+
41+
# same again with wait option
42+
$node_standby = get_new_node('standby2');
43+
$node_standby->init_from_backup($node_primary, 'my_backup', has_streaming => 1);
44+
$node_standby->start;
45+
46+
is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
47+
't', 'standby is in recovery');
48+
49+
command_ok([ 'pg_ctl', '-D', $node_standby->data_dir, '-w', 'promote' ],
50+
'pg_ctl -w promote of standby runs');
51+
52+
# no wait here
53+
54+
is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
55+
'f', 'promoted standby is not in recovery');

0 commit comments

Comments
 (0)