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

Commit 71c6a8e

Browse files
committed
Add recovery_target='immediate' option.
This allows ending recovery as a consistent state has been reached. Without this, there was no easy way to e.g restore an online backup, without replaying any extra WAL after the backup ended. MauMau and me.
1 parent 820f08c commit 71c6a8e

File tree

5 files changed

+86
-15
lines changed

5 files changed

+86
-15
lines changed

doc/src/sgml/backup.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ restore_command = 'cp /mnt/server/archivedir/%f %p'
11241124
<para>
11251125
If you want to recover to some previous point in time (say, right before
11261126
the junior DBA dropped your main transaction table), just specify the
1127-
required stopping point in <filename>recovery.conf</>. You can specify
1127+
required <link linkend="recovery-target-settings">stopping point</link> in <filename>recovery.conf</>. You can specify
11281128
the stop point, known as the <quote>recovery target</>, either by
11291129
date/time, named restore point or by completion of a specific transaction
11301130
ID. As of this writing only the date/time and named restore point options

doc/src/sgml/recovery-config.sgml

+32-12
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,33 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
199199
<sect1 id="recovery-target-settings">
200200

201201
<title>Recovery Target Settings</title>
202+
<para>
203+
By default, recovery will recover to the end of the WAL log. The
204+
following parameters can be used to specify an earlier stopping point.
205+
At most one of <varname>recovery_target</>,
206+
<varname>recovery_target_name</>, <varname>recovery_target_time</>, or
207+
<varname>recovery_target_xid</> can be specified.
208+
</para>
202209
<variablelist>
203210

211+
<varlistentry id="recovery-target" xreflabel="recovery_target_name">
212+
<term><varname>recovery_target</varname><literal> = 'immediate'</literal></term>
213+
<indexterm>
214+
<primary><varname>recovery_target</> recovery parameter</primary>
215+
</indexterm>
216+
<listitem>
217+
<para>
218+
This parameter specifies that recovery should end as soon as a
219+
consistency is reached, ie. as early as possible. When restoring from an
220+
online backup, this means the point where taking the backup ended.
221+
</para>
222+
<para>
223+
Technically, this is a string parameter, but <literal>'immediate'</>
224+
is currently the only allowed value.
225+
</para>
226+
</listitem>
227+
</varlistentry>
228+
204229
<varlistentry id="recovery-target-name" xreflabel="recovery_target_name">
205230
<term><varname>recovery_target_name</varname>
206231
(<type>string</type>)
@@ -212,10 +237,6 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
212237
<para>
213238
This parameter specifies the named restore point, created with
214239
<function>pg_create_restore_point()</> to which recovery will proceed.
215-
At most one of <varname>recovery_target_name</>,
216-
<xref linkend="recovery-target-time"> or
217-
<xref linkend="recovery-target-xid"> can be specified. The default is to
218-
recover to the end of the WAL log.
219240
</para>
220241
</listitem>
221242
</varlistentry>
@@ -231,10 +252,6 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
231252
<para>
232253
This parameter specifies the time stamp up to which recovery
233254
will proceed.
234-
At most one of <varname>recovery_target_time</>,
235-
<xref linkend="recovery-target-name"> or
236-
<xref linkend="recovery-target-xid"> can be specified.
237-
The default is to recover to the end of the WAL log.
238255
The precise stopping point is also influenced by
239256
<xref linkend="recovery-target-inclusive">.
240257
</para>
@@ -254,15 +271,18 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
254271
start, transactions can complete in a different numeric order.
255272
The transactions that will be recovered are those that committed
256273
before (and optionally including) the specified one.
257-
At most one of <varname>recovery_target_xid</>,
258-
<xref linkend="recovery-target-name"> or
259-
<xref linkend="recovery-target-time"> can be specified.
260-
The default is to recover to the end of the WAL log.
261274
The precise stopping point is also influenced by
262275
<xref linkend="recovery-target-inclusive">.
263276
</para>
264277
</listitem>
265278
</varlistentry>
279+
</variablelist>
280+
<para>
281+
The following options further specify the recovery target, and affect
282+
what happens when the target is reached:
283+
</para>
284+
285+
<variablelist>
266286

267287
<varlistentry id="recovery-target-inclusive"
268288
xreflabel="recovery_target_inclusive">

src/backend/access/transam/recovery.conf.sample

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
#recovery_target_inclusive = true
8282
#
8383
#
84+
# Alternatively, you can request stopping as soon as a consistent state
85+
# is reached, by uncommenting this option.
86+
#
87+
#recovery_target = 'immediate'
88+
#
89+
#
8490
# If you want to recover into a timeline other than the "main line" shown in
8591
# pg_control, specify the timeline number here, or write 'latest' to get
8692
# the latest branch for which there's a history file.

src/backend/access/transam/xlog.c

+45-1
Original file line numberDiff line numberDiff line change
@@ -5434,6 +5434,19 @@ readRecoveryCommandFile(void)
54345434
(errmsg_internal("recovery_target_name = '%s'",
54355435
recoveryTargetName)));
54365436
}
5437+
else if (strcmp(item->name, "recovery_target") == 0)
5438+
{
5439+
if (strcmp(item->value, "immediate") == 0)
5440+
recoveryTarget = RECOVERY_TARGET_IMMEDIATE;
5441+
else
5442+
ereport(ERROR,
5443+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5444+
errmsg("invalid recovery_target parameter"),
5445+
errhint("The only allowed value is 'immediate'")));
5446+
ereport(DEBUG2,
5447+
(errmsg_internal("recovery_target = '%s'",
5448+
item->value)));
5449+
}
54375450
else if (strcmp(item->name, "recovery_target_inclusive") == 0)
54385451
{
54395452
/*
@@ -5676,7 +5689,20 @@ recoveryStopsBefore(XLogRecord *record)
56765689
bool isCommit;
56775690
TimestampTz recordXtime = 0;
56785691

5679-
/* We only consider stopping before COMMIT or ABORT records. */
5692+
/* Check if we should stop as soon as reaching consistency */
5693+
if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE && reachedConsistency)
5694+
{
5695+
ereport(LOG,
5696+
(errmsg("recovery stopping after reaching consistency")));
5697+
5698+
recoveryStopAfter = false;
5699+
recoveryStopXid = InvalidTransactionId;
5700+
recoveryStopTime = 0;
5701+
recoveryStopName[0] = '\0';
5702+
return true;
5703+
}
5704+
5705+
/* Otherwise we only consider stopping before COMMIT or ABORT records. */
56805706
if (record->xl_rmid != RM_XACT_ID)
56815707
return false;
56825708
record_info = record->xl_info & ~XLR_INFO_MASK;
@@ -5825,6 +5851,19 @@ recoveryStopsAfter(XLogRecord *record)
58255851
}
58265852
}
58275853

5854+
/* Check if we should stop as soon as reaching consistency */
5855+
if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE && reachedConsistency)
5856+
{
5857+
ereport(LOG,
5858+
(errmsg("recovery stopping after reaching consistency")));
5859+
5860+
recoveryStopAfter = true;
5861+
recoveryStopXid = InvalidTransactionId;
5862+
recoveryStopTime = 0;
5863+
recoveryStopName[0] = '\0';
5864+
return true;
5865+
}
5866+
58285867
return false;
58295868
}
58305869

@@ -6246,6 +6285,9 @@ StartupXLOG(void)
62466285
ereport(LOG,
62476286
(errmsg("starting point-in-time recovery to \"%s\"",
62486287
recoveryTargetName)));
6288+
else if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE)
6289+
ereport(LOG,
6290+
(errmsg("starting point-in-time recovery to earliest consistent point")));
62496291
else
62506292
ereport(LOG,
62516293
(errmsg("starting archive recovery")));
@@ -7125,6 +7167,8 @@ StartupXLOG(void)
71257167
snprintf(reason, sizeof(reason),
71267168
"at restore point \"%s\"",
71277169
recoveryStopName);
7170+
else if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE)
7171+
snprintf(reason, sizeof(reason), "reached consistency");
71287172
else
71297173
snprintf(reason, sizeof(reason), "no recovery target specified");
71307174

src/include/access/xlog.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ typedef enum
173173
RECOVERY_TARGET_UNSET,
174174
RECOVERY_TARGET_XID,
175175
RECOVERY_TARGET_TIME,
176-
RECOVERY_TARGET_NAME
176+
RECOVERY_TARGET_NAME,
177+
RECOVERY_TARGET_IMMEDIATE
177178
} RecoveryTargetType;
178179

179180
extern XLogRecPtr XactLastRecEnd;

0 commit comments

Comments
 (0)