Commit 71c6a8e3 authored by Heikki Linnakangas's avatar Heikki Linnakangas

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.
parent 820f08ca
...@@ -1124,7 +1124,7 @@ restore_command = 'cp /mnt/server/archivedir/%f %p' ...@@ -1124,7 +1124,7 @@ restore_command = 'cp /mnt/server/archivedir/%f %p'
<para> <para>
If you want to recover to some previous point in time (say, right before If you want to recover to some previous point in time (say, right before
the junior DBA dropped your main transaction table), just specify the the junior DBA dropped your main transaction table), just specify the
required stopping point in <filename>recovery.conf</>. You can specify required <link linkend="recovery-target-settings">stopping point</link> in <filename>recovery.conf</>. You can specify
the stop point, known as the <quote>recovery target</>, either by the stop point, known as the <quote>recovery target</>, either by
date/time, named restore point or by completion of a specific transaction date/time, named restore point or by completion of a specific transaction
ID. As of this writing only the date/time and named restore point options ID. As of this writing only the date/time and named restore point options
......
...@@ -199,8 +199,33 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows ...@@ -199,8 +199,33 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
<sect1 id="recovery-target-settings"> <sect1 id="recovery-target-settings">
<title>Recovery Target Settings</title> <title>Recovery Target Settings</title>
<para>
By default, recovery will recover to the end of the WAL log. The
following parameters can be used to specify an earlier stopping point.
At most one of <varname>recovery_target</>,
<varname>recovery_target_name</>, <varname>recovery_target_time</>, or
<varname>recovery_target_xid</> can be specified.
</para>
<variablelist> <variablelist>
<varlistentry id="recovery-target" xreflabel="recovery_target_name">
<term><varname>recovery_target</varname><literal> = 'immediate'</literal></term>
<indexterm>
<primary><varname>recovery_target</> recovery parameter</primary>
</indexterm>
<listitem>
<para>
This parameter specifies that recovery should end as soon as a
consistency is reached, ie. as early as possible. When restoring from an
online backup, this means the point where taking the backup ended.
</para>
<para>
Technically, this is a string parameter, but <literal>'immediate'</>
is currently the only allowed value.
</para>
</listitem>
</varlistentry>
<varlistentry id="recovery-target-name" xreflabel="recovery_target_name"> <varlistentry id="recovery-target-name" xreflabel="recovery_target_name">
<term><varname>recovery_target_name</varname> <term><varname>recovery_target_name</varname>
(<type>string</type>) (<type>string</type>)
...@@ -212,10 +237,6 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows ...@@ -212,10 +237,6 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
<para> <para>
This parameter specifies the named restore point, created with This parameter specifies the named restore point, created with
<function>pg_create_restore_point()</> to which recovery will proceed. <function>pg_create_restore_point()</> to which recovery will proceed.
At most one of <varname>recovery_target_name</>,
<xref linkend="recovery-target-time"> or
<xref linkend="recovery-target-xid"> can be specified. The default is to
recover to the end of the WAL log.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -231,10 +252,6 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows ...@@ -231,10 +252,6 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
<para> <para>
This parameter specifies the time stamp up to which recovery This parameter specifies the time stamp up to which recovery
will proceed. will proceed.
At most one of <varname>recovery_target_time</>,
<xref linkend="recovery-target-name"> or
<xref linkend="recovery-target-xid"> can be specified.
The default is to recover to the end of the WAL log.
The precise stopping point is also influenced by The precise stopping point is also influenced by
<xref linkend="recovery-target-inclusive">. <xref linkend="recovery-target-inclusive">.
</para> </para>
...@@ -254,15 +271,18 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows ...@@ -254,15 +271,18 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
start, transactions can complete in a different numeric order. start, transactions can complete in a different numeric order.
The transactions that will be recovered are those that committed The transactions that will be recovered are those that committed
before (and optionally including) the specified one. before (and optionally including) the specified one.
At most one of <varname>recovery_target_xid</>,
<xref linkend="recovery-target-name"> or
<xref linkend="recovery-target-time"> can be specified.
The default is to recover to the end of the WAL log.
The precise stopping point is also influenced by The precise stopping point is also influenced by
<xref linkend="recovery-target-inclusive">. <xref linkend="recovery-target-inclusive">.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist>
<para>
The following options further specify the recovery target, and affect
what happens when the target is reached:
</para>
<variablelist>
<varlistentry id="recovery-target-inclusive" <varlistentry id="recovery-target-inclusive"
xreflabel="recovery_target_inclusive"> xreflabel="recovery_target_inclusive">
......
...@@ -81,6 +81,12 @@ ...@@ -81,6 +81,12 @@
#recovery_target_inclusive = true #recovery_target_inclusive = true
# #
# #
# Alternatively, you can request stopping as soon as a consistent state
# is reached, by uncommenting this option.
#
#recovery_target = 'immediate'
#
#
# If you want to recover into a timeline other than the "main line" shown in # If you want to recover into a timeline other than the "main line" shown in
# pg_control, specify the timeline number here, or write 'latest' to get # pg_control, specify the timeline number here, or write 'latest' to get
# the latest branch for which there's a history file. # the latest branch for which there's a history file.
......
...@@ -5434,6 +5434,19 @@ readRecoveryCommandFile(void) ...@@ -5434,6 +5434,19 @@ readRecoveryCommandFile(void)
(errmsg_internal("recovery_target_name = '%s'", (errmsg_internal("recovery_target_name = '%s'",
recoveryTargetName))); recoveryTargetName)));
} }
else if (strcmp(item->name, "recovery_target") == 0)
{
if (strcmp(item->value, "immediate") == 0)
recoveryTarget = RECOVERY_TARGET_IMMEDIATE;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid recovery_target parameter"),
errhint("The only allowed value is 'immediate'")));
ereport(DEBUG2,
(errmsg_internal("recovery_target = '%s'",
item->value)));
}
else if (strcmp(item->name, "recovery_target_inclusive") == 0) else if (strcmp(item->name, "recovery_target_inclusive") == 0)
{ {
/* /*
...@@ -5676,7 +5689,20 @@ recoveryStopsBefore(XLogRecord *record) ...@@ -5676,7 +5689,20 @@ recoveryStopsBefore(XLogRecord *record)
bool isCommit; bool isCommit;
TimestampTz recordXtime = 0; TimestampTz recordXtime = 0;
/* We only consider stopping before COMMIT or ABORT records. */ /* Check if we should stop as soon as reaching consistency */
if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE && reachedConsistency)
{
ereport(LOG,
(errmsg("recovery stopping after reaching consistency")));
recoveryStopAfter = false;
recoveryStopXid = InvalidTransactionId;
recoveryStopTime = 0;
recoveryStopName[0] = '\0';
return true;
}
/* Otherwise we only consider stopping before COMMIT or ABORT records. */
if (record->xl_rmid != RM_XACT_ID) if (record->xl_rmid != RM_XACT_ID)
return false; return false;
record_info = record->xl_info & ~XLR_INFO_MASK; record_info = record->xl_info & ~XLR_INFO_MASK;
...@@ -5825,6 +5851,19 @@ recoveryStopsAfter(XLogRecord *record) ...@@ -5825,6 +5851,19 @@ recoveryStopsAfter(XLogRecord *record)
} }
} }
/* Check if we should stop as soon as reaching consistency */
if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE && reachedConsistency)
{
ereport(LOG,
(errmsg("recovery stopping after reaching consistency")));
recoveryStopAfter = true;
recoveryStopXid = InvalidTransactionId;
recoveryStopTime = 0;
recoveryStopName[0] = '\0';
return true;
}
return false; return false;
} }
...@@ -6246,6 +6285,9 @@ StartupXLOG(void) ...@@ -6246,6 +6285,9 @@ StartupXLOG(void)
ereport(LOG, ereport(LOG,
(errmsg("starting point-in-time recovery to \"%s\"", (errmsg("starting point-in-time recovery to \"%s\"",
recoveryTargetName))); recoveryTargetName)));
else if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE)
ereport(LOG,
(errmsg("starting point-in-time recovery to earliest consistent point")));
else else
ereport(LOG, ereport(LOG,
(errmsg("starting archive recovery"))); (errmsg("starting archive recovery")));
...@@ -7125,6 +7167,8 @@ StartupXLOG(void) ...@@ -7125,6 +7167,8 @@ StartupXLOG(void)
snprintf(reason, sizeof(reason), snprintf(reason, sizeof(reason),
"at restore point \"%s\"", "at restore point \"%s\"",
recoveryStopName); recoveryStopName);
else if (recoveryTarget == RECOVERY_TARGET_IMMEDIATE)
snprintf(reason, sizeof(reason), "reached consistency");
else else
snprintf(reason, sizeof(reason), "no recovery target specified"); snprintf(reason, sizeof(reason), "no recovery target specified");
......
...@@ -173,7 +173,8 @@ typedef enum ...@@ -173,7 +173,8 @@ typedef enum
RECOVERY_TARGET_UNSET, RECOVERY_TARGET_UNSET,
RECOVERY_TARGET_XID, RECOVERY_TARGET_XID,
RECOVERY_TARGET_TIME, RECOVERY_TARGET_TIME,
RECOVERY_TARGET_NAME RECOVERY_TARGET_NAME,
RECOVERY_TARGET_IMMEDIATE
} RecoveryTargetType; } RecoveryTargetType;
extern XLogRecPtr XactLastRecEnd; extern XLogRecPtr XactLastRecEnd;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment