1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-15 01:04:37 +02:00

Add archive-mode option to disable archiving on restore.

When restoring a cluster that will be promoted but is not intended to be the new primary, it is important to disable archiving to avoid polluting the repository with useless WAL. This option makes disabling archiving a bit easier.
This commit is contained in:
David Steele
2020-08-25 15:05:41 -04:00
committed by GitHub
parent 1812725c8e
commit 8c2960fab3
12 changed files with 159 additions and 3 deletions

View File

@ -261,6 +261,7 @@ use constant CFGOPT_STOP_AUTO => 'stop-aut
# Restore options
#-----------------------------------------------------------------------------------------------------------------------------------
use constant CFGOPT_ARCHIVE_MODE => 'archive-mode';
use constant CFGOPT_DB_INCLUDE => 'db-include';
use constant CFGOPT_LINK_ALL => 'link-all';
use constant CFGOPT_LINK_MAP => 'link-map';
@ -2428,6 +2429,22 @@ my %hConfigDefine =
# Restore options
#-------------------------------------------------------------------------------------------------------------------------------
&CFGOPT_ARCHIVE_MODE =>
{
&CFGDEF_SECTION => CFGDEF_SECTION_GLOBAL,
&CFGDEF_TYPE => CFGDEF_TYPE_STRING,
&CFGDEF_DEFAULT => 'preserve',
&CFGDEF_COMMAND =>
{
&CFGCMD_RESTORE => {},
},
&CFGDEF_ALLOW_LIST =>
[
'off',
'preserve',
],
},
&CFGOPT_DB_INCLUDE =>
{
&CFGDEF_SECTION => CFGDEF_SECTION_GLOBAL,

View File

@ -889,6 +889,21 @@
<text>The <setting>restore</setting> section defines settings used for restoring backups.</text>
<config-key-list>
<!-- ======================================================================================================= -->
<config-key id="archive-mode" name="Archive Mode">
<summary>Preserve or disable archiving on restored cluster.</summary>
<text>This option allows archiving to be preserved or disabled on a restored cluster. This is useful when the cluster must be promoted to do some work but is not intended to become the new primary. In this case it is not a good idea to push WAL from the cluster into the repository.
The following modes are supported:
<ul>
<li><id>off</id> - disable archiving by setting <setting>archive_mode=off</setting>.</li>
<li><id>preserve</id> - preserve current <setting>archive_mode</setting> setting.</li>
</ul><b>NOTE</b>: This option is not available on <postgres/> &amp;lt; 12.</text>
<example>off</example>
</config-key>
<!-- CONFIG - RESTORE SECTION - DB-INCLUDE KEY -->
<config-key id="db-include" name="Include Database">
<summary>Restore only specified databases.</summary>

View File

@ -55,6 +55,16 @@
<p>Automatically retrieve temporary <proper>S3</proper> credentials on <proper>AWS</proper> instances.</p>
</release-item>
<release-item>
<release-item-contributor-list>
<release-item-ideator id="stephen.frost"/>
<release-item-contributor id="david.steele"/>
<release-item-reviewer id="stephen.frost"/>
</release-item-contributor-list>
<p>Add <br-option>archive-mode</br-option> option to disable archiving on restore.</p>
</release-item>
</release-feature-list>
<release-improvement-list>

View File

@ -2788,6 +2788,8 @@
<p>Now the standby can be created with the <cmd>restore</cmd> command.</p>
<admonition type="important">If the cluster is intended to be promoted without becoming the new primary (e.g. for reporting or testing), use <br-option>--archive-mode=off</br-option> or set <pg-option>archive_mode=off</pg-option> in <file>postgresql.conf</file> to disable archiving. If archiving is not disabled then the repository may be polluted with WAL that can make restores more difficult.</admonition>
<execute-list host="{[host-pg2]}">
<title>Restore the {[postgres-cluster-demo]} standby cluster</title>

View File

@ -62,6 +62,11 @@ Recovery constants
#define RECOVERY_TYPE_TIME "time"
STRING_STATIC(RECOVERY_TYPE_TIME_STR, RECOVERY_TYPE_TIME);
#define ARCHIVE_MODE "archive_mode"
#define ARCHIVE_MODE_OFF "off"
STRING_STATIC(ARCHIVE_MODE_OFF_STR, ARCHIVE_MODE_OFF);
STRING_STATIC(ARCHIVE_MODE_PRESERVE_STR, "preserve");
/***********************************************************************************************************************************
Validate restore path
***********************************************************************************************************************************/
@ -1329,6 +1334,26 @@ restoreRecoveryOption(unsigned int pgVersion)
strLstSort(recoveryOptionKey, sortOrderAsc);
}
// If archive-mode is not preserve
const String *archiveMode = cfgOptionStr(cfgOptArchiveMode);
if (!strEq(archiveMode, ARCHIVE_MODE_PRESERVE_STR))
{
if (pgVersion < PG_VERSION_12)
{
THROW_FMT(
OptionInvalidError,
"option '" CFGOPT_ARCHIVE_MODE "' is not supported on " PG_NAME " < " PG_VERSION_12_STR "\n"
"HINT: 'archive_mode' should be manually set to 'off' in postgresql.conf.");
}
// The only other valid option is off
ASSERT(strEq(archiveMode, ARCHIVE_MODE_OFF_STR));
// If archive-mode=off then set archive_mode=off
kvPut(result, VARSTRDEF(ARCHIVE_MODE), VARSTR(ARCHIVE_MODE_OFF_STR));
}
// Write restore_command
if (!strLstExists(recoveryOptionKey, RESTORE_COMMAND_STR))
{

View File

@ -287,6 +287,7 @@ STRING_EXTERN(CFGOPT_ARCHIVE_ASYNC_STR, CFGOPT_ARCHI
STRING_EXTERN(CFGOPT_ARCHIVE_CHECK_STR, CFGOPT_ARCHIVE_CHECK);
STRING_EXTERN(CFGOPT_ARCHIVE_COPY_STR, CFGOPT_ARCHIVE_COPY);
STRING_EXTERN(CFGOPT_ARCHIVE_GET_QUEUE_MAX_STR, CFGOPT_ARCHIVE_GET_QUEUE_MAX);
STRING_EXTERN(CFGOPT_ARCHIVE_MODE_STR, CFGOPT_ARCHIVE_MODE);
STRING_EXTERN(CFGOPT_ARCHIVE_PUSH_QUEUE_MAX_STR, CFGOPT_ARCHIVE_PUSH_QUEUE_MAX);
STRING_EXTERN(CFGOPT_ARCHIVE_TIMEOUT_STR, CFGOPT_ARCHIVE_TIMEOUT);
STRING_EXTERN(CFGOPT_BACKUP_STANDBY_STR, CFGOPT_BACKUP_STANDBY);
@ -526,6 +527,14 @@ static ConfigOptionData configOptionData[CFG_OPTION_TOTAL] = CONFIG_OPTION_LIST
CONFIG_OPTION_DEFINE_ID(cfgDefOptArchiveGetQueueMax)
)
//------------------------------------------------------------------------------------------------------------------------------
CONFIG_OPTION
(
CONFIG_OPTION_NAME(CFGOPT_ARCHIVE_MODE)
CONFIG_OPTION_INDEX(0)
CONFIG_OPTION_DEFINE_ID(cfgDefOptArchiveMode)
)
//------------------------------------------------------------------------------------------------------------------------------
CONFIG_OPTION
(

View File

@ -61,6 +61,8 @@ Option constants
STRING_DECLARE(CFGOPT_ARCHIVE_COPY_STR);
#define CFGOPT_ARCHIVE_GET_QUEUE_MAX "archive-get-queue-max"
STRING_DECLARE(CFGOPT_ARCHIVE_GET_QUEUE_MAX_STR);
#define CFGOPT_ARCHIVE_MODE "archive-mode"
STRING_DECLARE(CFGOPT_ARCHIVE_MODE_STR);
#define CFGOPT_ARCHIVE_PUSH_QUEUE_MAX "archive-push-queue-max"
STRING_DECLARE(CFGOPT_ARCHIVE_PUSH_QUEUE_MAX_STR);
#define CFGOPT_ARCHIVE_TIMEOUT "archive-timeout"
@ -464,7 +466,7 @@ Option constants
#define CFGOPT_TYPE "type"
STRING_DECLARE(CFGOPT_TYPE_STR);
#define CFG_OPTION_TOTAL 205
#define CFG_OPTION_TOTAL 206
/***********************************************************************************************************************************
Command enum
@ -502,6 +504,7 @@ typedef enum
cfgOptArchiveCheck,
cfgOptArchiveCopy,
cfgOptArchiveGetQueueMax,
cfgOptArchiveMode,
cfgOptArchivePushQueueMax,
cfgOptArchiveTimeout,
cfgOptBackupStandby,

View File

@ -430,6 +430,51 @@ static ConfigDefineOptionData configDefineOptionData[] = CFGDEFDATA_OPTION_LIST
)
)
// -----------------------------------------------------------------------------------------------------------------------------
CFGDEFDATA_OPTION
(
CFGDEFDATA_OPTION_NAME("archive-mode")
CFGDEFDATA_OPTION_REQUIRED(true)
CFGDEFDATA_OPTION_SECTION(cfgDefSectionGlobal)
CFGDEFDATA_OPTION_TYPE(cfgDefOptTypeString)
CFGDEFDATA_OPTION_INTERNAL(false)
CFGDEFDATA_OPTION_INDEX_TOTAL(1)
CFGDEFDATA_OPTION_SECURE(false)
CFGDEFDATA_OPTION_HELP_SECTION("restore")
CFGDEFDATA_OPTION_HELP_SUMMARY("Preserve or disable archiving on restored cluster.")
CFGDEFDATA_OPTION_HELP_DESCRIPTION
(
"This option allows archiving to be preserved or disabled on a restored cluster. This is useful when the cluster must "
"be promoted to do some work but is not intended to become the new primary. In this case it is not a good idea to "
"push WAL from the cluster into the repository.\n"
"\n"
"The following modes are supported:\n"
"\n"
"* off - disable archiving by setting archive_mode=off.\n"
"* preserve - preserve current archive_mode setting.\n"
"\n"
"NOTE: This option is not available on PostgreSQL < 12."
)
CFGDEFDATA_OPTION_COMMAND_LIST
(
CFGDEFDATA_OPTION_COMMAND(cfgDefCmdRestore)
)
CFGDEFDATA_OPTION_OPTIONAL_LIST
(
CFGDEFDATA_OPTION_OPTIONAL_ALLOW_LIST
(
"off",
"preserve"
)
CFGDEFDATA_OPTION_OPTIONAL_DEFAULT("preserve")
)
)
// -----------------------------------------------------------------------------------------------------------------------------
CFGDEFDATA_OPTION
(

View File

@ -56,6 +56,7 @@ typedef enum
cfgDefOptArchiveCheck,
cfgDefOptArchiveCopy,
cfgDefOptArchiveGetQueueMax,
cfgDefOptArchiveMode,
cfgDefOptArchivePushQueueMax,
cfgDefOptArchiveTimeout,
cfgDefOptBackupStandby,

View File

@ -66,6 +66,18 @@ static const struct option optionList[] =
.val = PARSE_OPTION_FLAG | PARSE_RESET_FLAG | cfgOptArchiveGetQueueMax,
},
// archive-mode option
// -----------------------------------------------------------------------------------------------------------------------------
{
.name = CFGOPT_ARCHIVE_MODE,
.has_arg = required_argument,
.val = PARSE_OPTION_FLAG | cfgOptArchiveMode,
},
{
.name = "reset-" CFGOPT_ARCHIVE_MODE,
.val = PARSE_OPTION_FLAG | PARSE_RESET_FLAG | cfgOptArchiveMode,
},
// archive-push-queue-max option and deprecations
// -----------------------------------------------------------------------------------------------------------------------------
{
@ -2724,6 +2736,7 @@ static const ConfigOption optionResolveOrder[] =
cfgOptStanza,
cfgOptArchiveAsync,
cfgOptArchiveGetQueueMax,
cfgOptArchiveMode,
cfgOptArchivePushQueueMax,
cfgOptArchiveTimeout,
cfgOptBackupStandby,

View File

@ -124,6 +124,8 @@ testRun(void)
"\n"
"Command Options:\n"
"\n"
" --archive-mode preserve or disable archiving on restored\n"
" cluster [default=preserve]\n"
" --db-include restore only specified databases\n"
" [current=db1, db2]\n"
" --force force a restore [default=n]\n"

View File

@ -1473,16 +1473,30 @@ testRun(void)
"check recovery options");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("recovery type = standby with recovery GUCs");
TEST_TITLE("error when archive-mode set on PG < 12");
argList = strLstDup(argBaseList);
strLstAddZ(argList, "--archive-mode=off");
harnessCfgLoad(cfgCmdRestore, argList);
TEST_ERROR(
restoreRecoveryConf(PG_VERSION_94, restoreLabel), OptionInvalidError,
"option 'archive-mode' is not supported on PostgreSQL < 12\n"
"HINT: 'archive_mode' should be manually set to 'off' in postgresql.conf.");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("recovery type = standby with recovery GUCs and archive-mode=off");
argList = strLstDup(argBaseList);
strLstAddZ(argList, "--type=standby");
strLstAddZ(argList, "--archive-mode=off");
harnessCfgLoad(cfgCmdRestore, argList);
TEST_RESULT_STR_Z(
restoreRecoveryConf(PG_VERSION_12, restoreLabel),
RECOVERY_SETTING_HEADER
"restore_command = 'my_restore_command'\n",
"restore_command = 'my_restore_command'\n"
"archive_mode = 'off'\n",
"check recovery options");
}