Catch various corner cases when restarting a PostgreSQL instance

This commit is contained in:
Ian Barwick
2018-02-14 11:28:38 +09:00
parent c9eb1bfcc0
commit de65afdac8
3 changed files with 101 additions and 45 deletions

View File

@@ -2078,28 +2078,88 @@ do_standby_follow_internal(PGconn *primary_conn, t_node_info *primary_node_recor
if (server_up == true) if (server_up == true)
{ {
action = "restart"; /* no "service_restart_command" defined - stop and start using pg_ctl*/
get_server_action(ACTION_RESTART, server_command, config_file_options.data_directory); if (config_file_options.service_restart_command[0] == '\0')
{
action = "stopp"; /* sic */
get_server_action(ACTION_STOP_WAIT, server_command, config_file_options.data_directory);
/* if translation needed, generate messages in the preceding if/else */
log_notice(_("%sing server using \"%s\""),
action,
server_command);
success = local_command(server_command, &output_buf);
if (success == false)
{
log_error(_("unable to %s server"), action);
*error_code = ERR_NO_RESTART;
return false;
}
action = "start";
get_server_action(ACTION_START, server_command, config_file_options.data_directory);
/* if translation needed, generate messages in the preceding if/else */
log_notice(_("%sing server using \"%s\""),
action,
server_command);
success = local_command(server_command, &output_buf);
if (success == false)
{
log_error(_("unable to %s server"), action);
*error_code = ERR_NO_RESTART;
return false;
}
}
else
{
action = "restart";
get_server_action(ACTION_RESTART, server_command, config_file_options.data_directory);
/* if translation needed, generate messages in the preceding if/else */
log_notice(_("%sing server using \"%s\""),
action,
server_command);
success = local_command(server_command, &output_buf);
if (success == false)
{
log_error(_("unable to %s server"), action);
*error_code = ERR_NO_RESTART;
return false;
}
}
} }
else else
{ {
action = "start"; action = "start";
get_server_action(ACTION_START, server_command, config_file_options.data_directory); get_server_action(ACTION_START, server_command, config_file_options.data_directory);
}
/* if translation needed, generate messages in the preceding if/else */ /* if translation needed, generate messages in the preceding if/else */
log_notice(_("%sing server using \"%s\""), log_notice(_("%sing server using \"%s\""),
action, action,
server_command); server_command);
success = local_command(server_command, &output_buf); success = local_command(server_command, &output_buf);
if (success == false) if (success == false)
{ {
log_error(_("unable to %s server"), action); log_error(_("unable to %s server"), action);
*error_code = ERR_NO_RESTART; *error_code = ERR_NO_RESTART;
return false; return false;
}
} }
} }

View File

@@ -181,6 +181,7 @@ typedef enum
ACTION_NONE, ACTION_NONE,
ACTION_START, ACTION_START,
ACTION_STOP, ACTION_STOP,
ACTION_STOP_WAIT,
ACTION_RESTART, ACTION_RESTART,
ACTION_RELOAD, ACTION_RELOAD,
ACTION_PROMOTE ACTION_PROMOTE

View File

@@ -2137,7 +2137,8 @@ local_command(const char *command, PQExpBufferData *outputbuf)
retval = pclose(fp); retval = pclose(fp);
success = (WEXITSTATUS(retval) == 0) ? true : false; /* */
success = (WEXITSTATUS(retval) == 0 || WEXITSTATUS(retval) == 141) ? true : false;
log_verbose(LOG_DEBUG, "result of command was %i (%i)", WEXITSTATUS(retval), retval); log_verbose(LOG_DEBUG, "result of command was %i (%i)", WEXITSTATUS(retval), retval);
@@ -2459,18 +2460,15 @@ get_server_action(t_server_action action, char *script, char *data_dir)
{ {
initPQExpBuffer(&command); initPQExpBuffer(&command);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
"%s %s -w -D ", "%s %s -w -D ",
make_pg_path("pg_ctl"), make_pg_path("pg_ctl"),
config_file_options.pg_ctl_options); config_file_options.pg_ctl_options);
appendShellString( appendShellString(&command,
&command,
data_dir); data_dir);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
" start"); " start");
strncpy(script, command.data, MAXLEN); strncpy(script, command.data, MAXLEN);
@@ -2482,6 +2480,7 @@ get_server_action(t_server_action action, char *script, char *data_dir)
} }
case ACTION_STOP: case ACTION_STOP:
case ACTION_STOP_WAIT:
{ {
if (config_file_options.service_stop_command[0] != '\0') if (config_file_options.service_stop_command[0] != '\0')
{ {
@@ -2491,19 +2490,23 @@ get_server_action(t_server_action action, char *script, char *data_dir)
else else
{ {
initPQExpBuffer(&command); initPQExpBuffer(&command);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
"%s %s -D ", "%s %s -D ",
make_pg_path("pg_ctl"), make_pg_path("pg_ctl"),
config_file_options.pg_ctl_options); config_file_options.pg_ctl_options);
appendShellString( appendShellString(&command,
&command,
data_dir); data_dir);
appendPQExpBuffer( if (action == ACTION_STOP_WAIT)
&command, appendPQExpBuffer(&command,
" -m fast -W stop"); " -w");
else
appendPQExpBuffer(&command,
" -W");
appendPQExpBuffer(&command,
" -m fast stop");
strncpy(script, command.data, MAXLEN); strncpy(script, command.data, MAXLEN);
@@ -2522,18 +2525,15 @@ get_server_action(t_server_action action, char *script, char *data_dir)
else else
{ {
initPQExpBuffer(&command); initPQExpBuffer(&command);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
"%s %s -w -D ", "%s %s -w -D ",
make_pg_path("pg_ctl"), make_pg_path("pg_ctl"),
config_file_options.pg_ctl_options); config_file_options.pg_ctl_options);
appendShellString( appendShellString(&command,
&command,
data_dir); data_dir);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
" restart"); " restart");
strncpy(script, command.data, MAXLEN); strncpy(script, command.data, MAXLEN);
@@ -2553,18 +2553,15 @@ get_server_action(t_server_action action, char *script, char *data_dir)
else else
{ {
initPQExpBuffer(&command); initPQExpBuffer(&command);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
"%s %s -w -D ", "%s %s -w -D ",
make_pg_path("pg_ctl"), make_pg_path("pg_ctl"),
config_file_options.pg_ctl_options); config_file_options.pg_ctl_options);
appendShellString( appendShellString(&command,
&command,
data_dir); data_dir);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
" reload"); " reload");
strncpy(script, command.data, MAXLEN); strncpy(script, command.data, MAXLEN);
@@ -2585,18 +2582,15 @@ get_server_action(t_server_action action, char *script, char *data_dir)
else else
{ {
initPQExpBuffer(&command); initPQExpBuffer(&command);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
"%s %s -w -D ", "%s %s -w -D ",
make_pg_path("pg_ctl"), make_pg_path("pg_ctl"),
config_file_options.pg_ctl_options); config_file_options.pg_ctl_options);
appendShellString( appendShellString(&command,
&command,
data_dir); data_dir);
appendPQExpBuffer( appendPQExpBuffer(&command,
&command,
" promote"); " promote");
strncpy(script, command.data, MAXLEN); strncpy(script, command.data, MAXLEN);
@@ -2630,6 +2624,7 @@ data_dir_required_for_action(t_server_action action)
return true; return true;
case ACTION_STOP: case ACTION_STOP:
case ACTION_STOP_WAIT:
if (config_file_options.service_stop_command[0] != '\0') if (config_file_options.service_stop_command[0] != '\0')
{ {
return false; return false;