|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * shell_restore.c |
| 4 | + * Recovery functions for a user-specified shell command. |
| 5 | + * |
| 6 | + * These recovery functions use a user-specified shell command (e.g. based |
| 7 | + * on the GUC restore_command). |
| 8 | + * |
| 9 | + * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group |
| 10 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 11 | + * |
| 12 | + * src/backend/access/transam/shell_restore.c |
| 13 | + * |
| 14 | + *------------------------------------------------------------------------- |
| 15 | + */ |
| 16 | + |
| 17 | +#include "postgres.h" |
| 18 | + |
| 19 | +#include <signal.h> |
| 20 | + |
| 21 | +#include "access/xlogarchive.h" |
| 22 | +#include "access/xlogrecovery.h" |
| 23 | +#include "common/archive.h" |
| 24 | +#include "common/percentrepl.h" |
| 25 | +#include "storage/ipc.h" |
| 26 | +#include "utils/wait_event.h" |
| 27 | + |
| 28 | +static void ExecuteRecoveryCommand(const char *command, |
| 29 | + const char *commandName, |
| 30 | + bool failOnSignal, |
| 31 | + uint32 wait_event_info, |
| 32 | + const char *lastRestartPointFileName); |
| 33 | + |
| 34 | +/* |
| 35 | + * Attempt to execute a shell-based restore command. |
| 36 | + * |
| 37 | + * Returns true if the command has succeeded, false otherwise. |
| 38 | + */ |
| 39 | +bool |
| 40 | +shell_restore(const char *file, const char *path, |
| 41 | + const char *lastRestartPointFileName) |
| 42 | +{ |
| 43 | + char *cmd; |
| 44 | + int rc; |
| 45 | + |
| 46 | + /* Build the restore command to execute */ |
| 47 | + cmd = BuildRestoreCommand(recoveryRestoreCommand, path, file, |
| 48 | + lastRestartPointFileName); |
| 49 | + |
| 50 | + ereport(DEBUG3, |
| 51 | + (errmsg_internal("executing restore command \"%s\"", cmd))); |
| 52 | + |
| 53 | + /* |
| 54 | + * Copy xlog from archival storage to XLOGDIR |
| 55 | + */ |
| 56 | + fflush(NULL); |
| 57 | + pgstat_report_wait_start(WAIT_EVENT_RESTORE_COMMAND); |
| 58 | + rc = system(cmd); |
| 59 | + pgstat_report_wait_end(); |
| 60 | + |
| 61 | + pfree(cmd); |
| 62 | + |
| 63 | + /* |
| 64 | + * Remember, we rollforward UNTIL the restore fails so failure here is |
| 65 | + * just part of the process... that makes it difficult to determine |
| 66 | + * whether the restore failed because there isn't an archive to restore, |
| 67 | + * or because the administrator has specified the restore program |
| 68 | + * incorrectly. We have to assume the former. |
| 69 | + * |
| 70 | + * However, if the failure was due to any sort of signal, it's best to |
| 71 | + * punt and abort recovery. (If we "return false" here, upper levels will |
| 72 | + * assume that recovery is complete and start up the database!) It's |
| 73 | + * essential to abort on child SIGINT and SIGQUIT, because per spec |
| 74 | + * system() ignores SIGINT and SIGQUIT while waiting; if we see one of |
| 75 | + * those it's a good bet we should have gotten it too. |
| 76 | + * |
| 77 | + * On SIGTERM, assume we have received a fast shutdown request, and exit |
| 78 | + * cleanly. It's pure chance whether we receive the SIGTERM first, or the |
| 79 | + * child process. If we receive it first, the signal handler will call |
| 80 | + * proc_exit, otherwise we do it here. If we or the child process received |
| 81 | + * SIGTERM for any other reason than a fast shutdown request, postmaster |
| 82 | + * will perform an immediate shutdown when it sees us exiting |
| 83 | + * unexpectedly. |
| 84 | + * |
| 85 | + * We treat hard shell errors such as "command not found" as fatal, too. |
| 86 | + */ |
| 87 | + if (rc != 0) |
| 88 | + { |
| 89 | + if (wait_result_is_signal(rc, SIGTERM)) |
| 90 | + proc_exit(1); |
| 91 | + |
| 92 | + ereport(wait_result_is_any_signal(rc, true) ? FATAL : DEBUG2, |
| 93 | + (errmsg("could not restore file \"%s\" from archive: %s", |
| 94 | + file, wait_result_to_str(rc)))); |
| 95 | + } |
| 96 | + |
| 97 | + return (rc == 0); |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | + * Attempt to execute a shell-based archive cleanup command. |
| 102 | + */ |
| 103 | +void |
| 104 | +shell_archive_cleanup(const char *lastRestartPointFileName) |
| 105 | +{ |
| 106 | + ExecuteRecoveryCommand(archiveCleanupCommand, "archive_cleanup_command", |
| 107 | + false, WAIT_EVENT_ARCHIVE_CLEANUP_COMMAND, |
| 108 | + lastRestartPointFileName); |
| 109 | +} |
| 110 | + |
| 111 | +/* |
| 112 | + * Attempt to execute a shell-based end-of-recovery command. |
| 113 | + */ |
| 114 | +void |
| 115 | +shell_recovery_end(const char *lastRestartPointFileName) |
| 116 | +{ |
| 117 | + ExecuteRecoveryCommand(recoveryEndCommand, "recovery_end_command", true, |
| 118 | + WAIT_EVENT_RECOVERY_END_COMMAND, |
| 119 | + lastRestartPointFileName); |
| 120 | +} |
| 121 | + |
| 122 | +/* |
| 123 | + * Attempt to execute an external shell command during recovery. |
| 124 | + * |
| 125 | + * 'command' is the shell command to be executed, 'commandName' is a |
| 126 | + * human-readable name describing the command emitted in the logs. If |
| 127 | + * 'failOnSignal' is true and the command is killed by a signal, a FATAL |
| 128 | + * error is thrown. Otherwise a WARNING is emitted. |
| 129 | + * |
| 130 | + * This is currently used for recovery_end_command and archive_cleanup_command. |
| 131 | + */ |
| 132 | +static void |
| 133 | +ExecuteRecoveryCommand(const char *command, const char *commandName, |
| 134 | + bool failOnSignal, uint32 wait_event_info, |
| 135 | + const char *lastRestartPointFileName) |
| 136 | +{ |
| 137 | + char *xlogRecoveryCmd; |
| 138 | + int rc; |
| 139 | + |
| 140 | + Assert(command && commandName); |
| 141 | + |
| 142 | + /* |
| 143 | + * construct the command to be executed |
| 144 | + */ |
| 145 | + xlogRecoveryCmd = replace_percent_placeholders(command, commandName, "r", |
| 146 | + lastRestartPointFileName); |
| 147 | + |
| 148 | + ereport(DEBUG3, |
| 149 | + (errmsg_internal("executing %s \"%s\"", commandName, command))); |
| 150 | + |
| 151 | + /* |
| 152 | + * execute the constructed command |
| 153 | + */ |
| 154 | + fflush(NULL); |
| 155 | + pgstat_report_wait_start(wait_event_info); |
| 156 | + rc = system(xlogRecoveryCmd); |
| 157 | + pgstat_report_wait_end(); |
| 158 | + |
| 159 | + pfree(xlogRecoveryCmd); |
| 160 | + |
| 161 | + if (rc != 0) |
| 162 | + { |
| 163 | + /* |
| 164 | + * If the failure was due to any sort of signal, it's best to punt and |
| 165 | + * abort recovery. See comments in shell_restore(). |
| 166 | + */ |
| 167 | + ereport((failOnSignal && wait_result_is_any_signal(rc, true)) ? FATAL : WARNING, |
| 168 | + /*------ |
| 169 | + translator: First %s represents a postgresql.conf parameter name like |
| 170 | + "recovery_end_command", the 2nd is the value of that parameter, the |
| 171 | + third an already translated error message. */ |
| 172 | + (errmsg("%s \"%s\": %s", commandName, |
| 173 | + command, wait_result_to_str(rc)))); |
| 174 | + } |
| 175 | +} |
0 commit comments