Skip to content

Commit

Permalink
Add '--recovery-target-action' option for restore(ossc-db#182) (ossc-…
Browse files Browse the repository at this point in the history
…db#190)

Previously, users must execute 'pg_xlog_replay_resume()' or configure
'recovery_target_action' GUC parameter manually if the restored data
need archive recovery and users want to promote the server.

Since this limitation annoys users, this patch adds a new option
'--recovery-target-action'. If a user specifies this option when to
restore, pg_rman configures 'recovery_target_action' GUC parameter
automatically. So, the task which the user have to do is only to
start the server. After archive recovery is done, the server will
promote.
  • Loading branch information
mikecaat committed Jun 30, 2021
1 parent 56bb634 commit 17f7109
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 39 deletions.
14 changes: 14 additions & 0 deletions docs/index-ja.html
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,12 @@ <h2>リストア・オプション</h2>
<ul>
<li>指定したリカバリ対象の直後で停止する (true) か、リカバリ対象の直前で停止する (false) かを指定します。デフォルトは true です。</li>
</ul>

<li><strong><code>--recovery-target-action {{ pause | promote | shutdown }}</code></strong>

<ul>
<li>リカバリ対象に到達した場合に、サーバがする動作を指定します。 デフォルトはpauseで、リカバリを休止することを意味します。 promoteは、リカバリの過程が終われば、サーバは接続の受け付けを始めることを意味します。 最後に、shutdownは、リカバリ対象に到達した後にサーバを停止します。1.3.12よりも後のバージョンで利用可能です。</li>
</ul>
</li>
</ul>

Expand Down Expand Up @@ -990,6 +996,14 @@ <h1 id="waytopassoptions">パラメータの指定方法</h1>
</tr>
<tr>
<td></td>
<td>&ndash;recovery-target-action</td>
<td>RECOVERY_TARGET_ACTION</td>
<td>指定可</td>
<td>リカバリ対象に到達した場合にサーバがする動作</td>
<td>1.3.12よりも後のバージョンで利用可能</td>
</tr>
<tr>
<td></td>
<td>&ndash;hard-copy</td>
<td>HARD_COPY</td>
<td>指定可</td>
Expand Down
15 changes: 15 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,13 @@ <h2>Restore options</h2>
<li>Specifies whether we stop just after the specified recovery target (true), or just before the recovery target (false). Default is true.
</ul>
</li>
<li><strong><code>--recovery-target-action {{ pause | promote | shutdown }}</code></strong>

<ul>
<li>Specifies what action the server should take once the recovery target is reached. The default is pause, which means recovery will be paused. promote means the recovery process will finish and the server will start to accept connections. Finally shutdown will stop the server after reaching the recovery target. This option is provided version higher than 1.3.12.</li>
</ul>
</li>
</ul>
</li>
</ul>

Expand Down Expand Up @@ -961,6 +968,14 @@ <h1 id="waytopassoptions">Way to pass options</h1>
</tr>
<tr>
<td></td>
<td>&ndash;recovery-target-action</td>
<td>RECOVERY_TARGET_ACTION</td>
<td>Yes</td>
<td>action the server should take once the recovery target is reached</td>
<td>This option is provided versions higher than 1.3.12</td>
</tr>
<tr>
<td></td>
<td>&ndash;hard-copy</td>
<td>HARD_COPY</td>
<td>Yes</td>
Expand Down
1 change: 1 addition & 0 deletions expected/option.out
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Restore options:
--recovery-target-xid transaction ID up to which recovery will proceed
--recovery-target-inclusive whether we stop just after the recovery target
--recovery-target-timeline recovering into a particular timeline
--recovery-target-action action the server should take once the recovery target is reached
--hard-copy copying archivelog not symbolic link

Catalog options:
Expand Down
36 changes: 33 additions & 3 deletions expected/restore.out
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,54 @@ OK: recovery-target-xid options works well.
OK: recovery-target-inclusive=false works well.

###### RESTORE COMMAND TEST-0009 ######
###### recovery with target action pause ######
recovery-target-action=pause
0
0
0
0
0
OK: not promoted. recovery-target-action works well.

###### RESTORE COMMAND TEST-0010 ######
###### recovery with target action promote ######
recovery-target-action=promote
0
0
0
0
0
OK: promoted. recovery-target-action works well.

###### RESTORE COMMAND TEST-0011 ######
###### recovery with target action shutdown ######
recovery-target-action=shutdown
0
0
0
0
0
OK: server is stopped. recovery-target-action works well.

###### RESTORE COMMAND TEST-0012 ######
###### recovery with hard-copy option ######
0
0
OK: hard-copy option works well.

###### RESTORE COMMAND TEST-0010 ######
###### RESTORE COMMAND TEST-0013 ######
###### recovery from incremental backup after database creation ######
0
0
0

###### RESTORE COMMAND TEST-0011 ######
###### RESTORE COMMAND TEST-0014 ######
###### vacuum shrinks a page between full and incremental backups ######
0
0
0

###### RESTORE COMMAND TEST-0012 ######
###### RESTORE COMMAND TEST-0015 ######
###### vacuum shrinks a page between full and incremental backups(compressed) ######
0
0
Expand Down
36 changes: 33 additions & 3 deletions expected/restore_checksum.out
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,54 @@ OK: recovery-target-xid options works well.
OK: recovery-target-inclusive=false works well.

###### RESTORE COMMAND TEST-0009 ######
###### recovery with target action pause ######
recovery-target-action=pause
0
0
0
0
0
OK: not promoted. recovery-target-action works well.

###### RESTORE COMMAND TEST-0010 ######
###### recovery with target action promote ######
recovery-target-action=promote
0
0
0
0
0
OK: promoted. recovery-target-action works well.

###### RESTORE COMMAND TEST-0011 ######
###### recovery with target action shutdown ######
recovery-target-action=shutdown
0
0
0
0
0
OK: server is stopped. recovery-target-action works well.

###### RESTORE COMMAND TEST-0012 ######
###### recovery with hard-copy option ######
0
0
OK: hard-copy option works well.

###### RESTORE COMMAND TEST-0010 ######
###### RESTORE COMMAND TEST-0013 ######
###### recovery from incremental backup after database creation ######
0
0
0

###### RESTORE COMMAND TEST-0011 ######
###### RESTORE COMMAND TEST-0014 ######
###### vacuum shrinks a page between full and incremental backups ######
0
0
0

###### RESTORE COMMAND TEST-0012 ######
###### RESTORE COMMAND TEST-0015 ######
###### vacuum shrinks a page between full and incremental backups(compressed) ######
0
0
Expand Down
7 changes: 5 additions & 2 deletions pg_rman.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static char *target_time;
static char *target_xid;
static char *target_inclusive;
static char *target_tli_string;
static char *target_action;
static bool is_hard_copy = false;

/* delete configuration */
Expand Down Expand Up @@ -92,7 +93,8 @@ static pgut_option options[] =
{ 's', 8, "recovery-target-xid" , &target_xid , SOURCE_ENV },
{ 's', 9, "recovery-target-inclusive" , &target_inclusive , SOURCE_ENV },
{ 's', 10, "recovery-target-timeline" , &target_tli_string, SOURCE_ENV },
{ 'b', 11, "hard-copy" , &is_hard_copy , SOURCE_ENV },
{ 's', 11, "recovery-target-action" , &target_action , SOURCE_ENV },
{ 'b', 12, "hard-copy" , &is_hard_copy , SOURCE_ENV },
/* catalog options */
{ 'b', 'a', "show-all" , &show_all },
{ 0 }
Expand Down Expand Up @@ -221,7 +223,7 @@ main(int argc, char *argv[])
}
else if (pg_strcasecmp(cmd, "restore") == 0)
return do_restore(target_time, target_xid,
target_inclusive, target_tli_string, is_hard_copy);
target_inclusive, target_tli_string, target_action, is_hard_copy);
else if (pg_strcasecmp(cmd, "show") == 0)
return do_show(&range, show_detail, show_all);
else if (pg_strcasecmp(cmd, "validate") == 0)
Expand Down Expand Up @@ -285,6 +287,7 @@ pgut_help(bool details)
printf(_(" --recovery-target-xid transaction ID up to which recovery will proceed\n"));
printf(_(" --recovery-target-inclusive whether we stop just after the recovery target\n"));
printf(_(" --recovery-target-timeline recovering into a particular timeline\n"));
printf(_(" --recovery-target-action action the server should take once the recovery target is reached\n"));
printf(_(" --hard-copy copying archivelog not symbolic link\n"));
printf(_("\nCatalog options:\n"));
printf(_(" -a, --show-all show deleted backup too\n"));
Expand Down
2 changes: 2 additions & 0 deletions pg_rman.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ typedef struct pgRecoveryTarget
bool xid_specified;
unsigned int recovery_target_xid;
bool recovery_target_inclusive;
const char *recovery_target_action;
} pgRecoveryTarget;

typedef enum CompressionMode
Expand Down Expand Up @@ -264,6 +265,7 @@ extern int do_restore(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_tli_string,
const char *target_action,
bool is_hard_copy);

/* in init.c */
Expand Down
31 changes: 27 additions & 4 deletions restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ static void restore_archive_logs(pgBackup *backup, bool is_hard_copy);
static void add_recovery_related_options(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action,
TimeLineID target_tli,
bool target_tli_latest);
static void create_recovery_signal(void);
static void remove_standby_signal(void);

static pgRecoveryTarget *checkIfCreateRecoveryConf(const char *target_time,
const char *target_xid,
const char *target_inclusive);
const char *target_inclusive,
const char *target_action);
static parray * readTimeLineHistory(TimeLineID targetTLI);
static bool satisfy_timeline(const parray *timelines, const pgBackup *backup);
static bool satisfy_recovery_target(const pgBackup *backup, const pgRecoveryTarget *rt);
Expand All @@ -52,6 +54,7 @@ do_restore(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_tli_string,
const char *target_action,
bool is_hard_copy)
{
int i;
Expand Down Expand Up @@ -115,7 +118,7 @@ do_restore(const char *target_time,
errmsg("PostgreSQL server is running"),
errhint("Please stop PostgreSQL server before executing restore.")));

rt = checkIfCreateRecoveryConf(target_time, target_xid, target_inclusive);
rt = checkIfCreateRecoveryConf(target_time, target_xid, target_inclusive, target_action);
if(rt == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
Expand Down Expand Up @@ -375,7 +378,7 @@ do_restore(const char *target_time,

/* Add recovery related options to postgresql.conf file */
add_recovery_related_options(target_time, target_xid, target_inclusive,
target_tli, target_tli_latest);
target_action, target_tli, target_tli_latest);
/* Create recovery.signal file */
create_recovery_signal();

Expand Down Expand Up @@ -744,6 +747,7 @@ static void
add_recovery_related_options(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action,
TimeLineID target_tli,
bool target_tli_latest)
{
Expand Down Expand Up @@ -780,6 +784,8 @@ add_recovery_related_options(const char *target_time,
fprintf(fp, "recovery_target_timeline = 'latest'\n");
else
fprintf(fp, "recovery_target_timeline = '%u'\n", target_tli);
if (target_action)
fprintf(fp, "recovery_target_action = '%s'\n", target_action);

fclose(fp);
}
Expand Down Expand Up @@ -1252,7 +1258,8 @@ search_next_wal(const char *path, uint32 *needId, uint32 *needSeg, parray *timel
static pgRecoveryTarget *
checkIfCreateRecoveryConf(const char *target_time,
const char *target_xid,
const char *target_inclusive)
const char *target_inclusive,
const char *target_action)
{
time_t dummy_time;
unsigned int dummy_xid;
Expand All @@ -1266,6 +1273,7 @@ checkIfCreateRecoveryConf(const char *target_time,
rt->recovery_target_time = 0;
rt->recovery_target_xid = 0;
rt->recovery_target_inclusive = false;
rt->recovery_target_action = NULL;

if(target_time)
{
Expand Down Expand Up @@ -1304,6 +1312,21 @@ checkIfCreateRecoveryConf(const char *target_time,
"add recovery related options to postgresql.conf(after PG12) with %s", target_inclusive)));
}

if(target_action)
{
if (pg_strcasecmp("pause", target_action) == 0 ||
pg_strcasecmp("promote", target_action) == 0 ||
pg_strcasecmp("shutdown", target_action) == 0 )
{
/* Although this value doesn't be used, set to match "recovery_target_inclusive". */
rt->recovery_target_action = target_action;
} else
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("could not create recovery.conf or"
"add recovery related options to postgresql.conf(after PG12) with %s", target_action)));
}

return rt;

}
Expand Down
Loading

0 comments on commit 17f7109

Please sign in to comment.