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

Commit f2cbffc

Browse files
committed
Only allow one recovery target setting
The previous recovery.conf regime accepted multiple recovery_target* settings and used the last one. This does not translate well to the general GUC system. Specifically, under EXEC_BACKEND, the settings are written out not in any particular order, so the order in which they were originally set is not available to new processes. Rather than redesign the GUC system, it was decided to abandon the old behavior and only allow one recovery target setting. A second setting will cause an error. However, it is allowed to set the same parameter multiple times or unset a parameter and set a different one. Discussion: https://www.postgresql.org/message-id/flat/27802171543235530%40iva2-6ec8f0a6115e.qloud-c.yandex.net#701a59c837ad0bf8c244344aaf3ef5a4
1 parent eae9143 commit f2cbffc

File tree

3 files changed

+62
-32
lines changed

3 files changed

+62
-32
lines changed

doc/src/sgml/config.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3221,7 +3221,7 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
32213221
<varname>recovery_target_lsn</varname>, <varname>recovery_target_name</varname>,
32223222
<varname>recovery_target_time</varname>, or <varname>recovery_target_xid</varname>
32233223
can be used; if more than one of these is specified in the configuration
3224-
file, the last entry will be used.
3224+
file, an error will be raised.
32253225
These parameters can only be set at server start.
32263226
</para>
32273227

src/backend/utils/misc/guc.c

+42-4
Original file line numberDiff line numberDiff line change
@@ -11051,6 +11051,28 @@ assign_recovery_target_timeline(const char *newval, void *extra)
1105111051
recoveryTargetTLIRequested = 0;
1105211052
}
1105311053

11054+
/*
11055+
* Recovery target settings: Only one of the several recovery_target* settings
11056+
* may be set. Setting a second one results in an error. The global variable
11057+
* recoveryTarget tracks which kind of recovery target was chosen. Other
11058+
* variables store the actual target value (for example a string or a xid).
11059+
* The assign functions of the parameters check whether a competing parameter
11060+
* was already set. But we want to allow setting the same parameter multiple
11061+
* times. We also want to allow unsetting a parameter and setting a different
11062+
* one, so we unset recoveryTarget when the parameter is set to an empty
11063+
* string.
11064+
*/
11065+
11066+
static void
11067+
pg_attribute_noreturn()
11068+
error_multiple_recovery_targets(void)
11069+
{
11070+
ereport(ERROR,
11071+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11072+
errmsg("multiple recovery targets specified"),
11073+
errdetail("At most one of recovery_target, recovery_target_lsn, recovery_target_name, recovery_target_time, recovery_target_xid may be set.")));
11074+
}
11075+
1105411076
static bool
1105511077
check_recovery_target(char **newval, void **extra, GucSource source)
1105611078
{
@@ -11065,13 +11087,13 @@ check_recovery_target(char **newval, void **extra, GucSource source)
1106511087
static void
1106611088
assign_recovery_target(const char *newval, void *extra)
1106711089
{
11090+
if (recoveryTarget != RECOVERY_TARGET_UNSET &&
11091+
recoveryTarget != RECOVERY_TARGET_IMMEDIATE)
11092+
error_multiple_recovery_targets();
11093+
1106811094
if (newval && strcmp(newval, "") != 0)
1106911095
recoveryTarget = RECOVERY_TARGET_IMMEDIATE;
1107011096
else
11071-
/*
11072-
* Reset recoveryTarget to RECOVERY_TARGET_UNSET to proper handle user
11073-
* setting multiple recovery_target with blank value on last.
11074-
*/
1107511097
recoveryTarget = RECOVERY_TARGET_UNSET;
1107611098
}
1107711099

@@ -11098,6 +11120,10 @@ check_recovery_target_xid(char **newval, void **extra, GucSource source)
1109811120
static void
1109911121
assign_recovery_target_xid(const char *newval, void *extra)
1110011122
{
11123+
if (recoveryTarget != RECOVERY_TARGET_UNSET &&
11124+
recoveryTarget != RECOVERY_TARGET_XID)
11125+
error_multiple_recovery_targets();
11126+
1110111127
if (newval && strcmp(newval, "") != 0)
1110211128
{
1110311129
recoveryTarget = RECOVERY_TARGET_XID;
@@ -11161,6 +11187,10 @@ check_recovery_target_time(char **newval, void **extra, GucSource source)
1116111187
static void
1116211188
assign_recovery_target_time(const char *newval, void *extra)
1116311189
{
11190+
if (recoveryTarget != RECOVERY_TARGET_UNSET &&
11191+
recoveryTarget != RECOVERY_TARGET_TIME)
11192+
error_multiple_recovery_targets();
11193+
1116411194
if (newval && strcmp(newval, "") != 0)
1116511195
{
1116611196
recoveryTarget = RECOVERY_TARGET_TIME;
@@ -11186,6 +11216,10 @@ check_recovery_target_name(char **newval, void **extra, GucSource source)
1118611216
static void
1118711217
assign_recovery_target_name(const char *newval, void *extra)
1118811218
{
11219+
if (recoveryTarget != RECOVERY_TARGET_UNSET &&
11220+
recoveryTarget != RECOVERY_TARGET_NAME)
11221+
error_multiple_recovery_targets();
11222+
1118911223
if (newval && strcmp(newval, "") != 0)
1119011224
{
1119111225
recoveryTarget = RECOVERY_TARGET_NAME;
@@ -11240,6 +11274,10 @@ check_recovery_target_lsn(char **newval, void **extra, GucSource source)
1124011274
static void
1124111275
assign_recovery_target_lsn(const char *newval, void *extra)
1124211276
{
11277+
if (recoveryTarget != RECOVERY_TARGET_UNSET &&
11278+
recoveryTarget != RECOVERY_TARGET_LSN)
11279+
error_multiple_recovery_targets();
11280+
1124311281
if (newval && strcmp(newval, "") != 0)
1124411282
{
1124511283
recoveryTarget = RECOVERY_TARGET_LSN;

src/test/recovery/t/003_recovery_targets.pl

+19-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use warnings;
44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 9;
6+
use Test::More tests => 8;
77

88
# Create and test a standby from given backup, with a certain recovery target.
99
# Choose $until_lsn later than the transaction commit that causes the row
@@ -116,30 +116,22 @@ sub test_recovery_standby
116116
"5000", $lsn5);
117117
118118
# Multiple targets
119-
# Last entry has priority (note that an array respects the order of items
120-
# not hashes).
121-
@recovery_params = (
122-
"recovery_target_name = '$recovery_name'",
123-
"recovery_target_xid = '$recovery_txid'",
124-
"recovery_target_time = '$recovery_time'");
125-
test_recovery_standby('name + XID + time',
126-
'standby_6', $node_master, \@recovery_params, "3000", $lsn3);
127-
@recovery_params = (
128-
"recovery_target_time = '$recovery_time'",
129-
"recovery_target_name = '$recovery_name'",
130-
"recovery_target_xid = '$recovery_txid'");
131-
test_recovery_standby('time + name + XID',
132-
'standby_7', $node_master, \@recovery_params, "2000", $lsn2);
133-
@recovery_params = (
134-
"recovery_target_xid = '$recovery_txid'",
135-
"recovery_target_time = '$recovery_time'",
136-
"recovery_target_name = '$recovery_name'");
137-
test_recovery_standby('XID + time + name',
138-
'standby_8', $node_master, \@recovery_params, "4000", $lsn4);
119+
#
120+
# Multiple conflicting settings are not allowed, but setting the same
121+
# parameter multiple times or unsetting a parameter and setting a
122+
# different one is allowed.
123+
139124
@recovery_params = (
140-
"recovery_target_xid = '$recovery_txid'",
141-
"recovery_target_time = '$recovery_time'",
142-
"recovery_target_name = '$recovery_name'",
143-
"recovery_target_lsn = '$recovery_lsn'",);
144-
test_recovery_standby('XID + time + name + LSN',
145-
'standby_9', $node_master, \@recovery_params, "5000", $lsn5);
125+
"recovery_target_name = '$recovery_name'",
126+
"recovery_target_name = ''",
127+
"recovery_target_time = '$recovery_time'");
128+
test_recovery_standby('multiple overriding settings',
129+
'standby_6', $node_master, \@recovery_params, "3000", $lsn3);
130+
131+
my $node_standby = get_new_node('standby_7');
132+
$node_standby->init_from_backup($node_master, 'my_backup', has_restoring => 1);
133+
$node_standby->append_conf('postgresql.conf', "recovery_target_name = '$recovery_name'
134+
recovery_target_time = '$recovery_time'");
135+
command_fails_like(['postgres', '-D', $node_standby->data_dir],
136+
qr/multiple recovery targets specified/,
137+
'multiple conflicting settings');

0 commit comments

Comments
 (0)