I recently wanted several times to have slave server prepared at certain
point in time to reduce the time it takes for it to replay remaining
WALs (say I have pg_basebackup -x on busy db for example).
Currently the way to do it is to have pause_at_recovery_target true
(default) and wait until pg accepts connection and the shut it down. The
issue is that this is ugly, and also there is a chance that somebody
else connects and does bad things (tm) before my process does.
So I wrote simple patch that adds option to shut down the cluster once
recovery_target is reached. The server will still be able to continue
WAL replay if needed later or can be configured to start standalone.
Petr Jelinek http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
diff --git a/doc/src/sgml/recovery-config.sgml b/doc/src/sgml/recovery-config.sgml
index 0f1ff34..278bbaa 100644
@@ -309,6 +309,36 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows
+ <varlistentry id="shutdown-at-recovery-target"
+ <term><varname>shutdown_at_recovery_target</varname> (<type>boolean</type>)
+ <primary><varname>shutdown_at_recovery_target</> recovery parameter</primary>
+ Specifies whether postgres should shutdown the once the recovery target
+ is reached. The default is false.
+ This is intended to have instance ready at exact replay point desired.
+ The instance will still be able to replay more WAL records.
+ Note that because reconvery.conf will not be removed if this option is
+ set to true, the above means that unless you change your configuration,
+ any subsequent start will end with immediate shutdown.
+ This setting has no effect if <xref linkend="guc-hot-standby"> is not
+ enabled, or if no recovery target is set.
+ Setting this to true will set <link linkend="pause-at-recovery-target">
+ <varname>pause_at_recovery_target</></link> to false.
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 34f2fc0..bbe3987 100644
@@ -222,6 +222,7 @@ static char *archiveCleanupCommand = NULL;
static RecoveryTargetType recoveryTarget = RECOVERY_TARGET_UNSET;
static bool recoveryTargetInclusive = true;
static bool recoveryPauseAtTarget = true;
+static bool recoveryShutdownAtTarget = false;
static TransactionId recoveryTargetXid;
static TimestampTz recoveryTargetTime;
static char *recoveryTargetName;
@@ -5159,6 +5160,18 @@ readRecoveryCommandFile(void)
(errmsg_internal("pause_at_recovery_target = '%s'",
+ else if (strcmp(item->name, "shutdown_at_recovery_target") == 0)
+ if (!parse_bool(item->value, &recoveryShutdownAtTarget))
+ errmsg("parameter \"%s\" requires a Boolean value", "shutdown_at_recovery_target")));
+ (errmsg_internal("shutdown_at_recovery_target = '%s'",
+ if (recoveryShutdownAtTarget)
+ recoveryPauseAtTarget = false;
else if (strcmp(item->name, "recovery_target_timeline") == 0)
rtliGiven = true;
@@ -6907,6 +6920,24 @@ StartupXLOG(void)
(errmsg("last completed transaction was at log time %s",
+ * Shutdown here when requested. We need to exit this early because
+ * we want to be able to continue the WAL replay when started
+ * the next time.
+ if (recoveryShutdownAtTarget && reachedStopPoint)
+ ereport(LOG, (errmsg("shutting down")));
+ * Note that we exit with status 2 instead of 0 here to force
+ * postmaster shutdown the whole instance. This produces one
+ * unnecessary log line, but we don't have better way to
+ * shutdown postmaster from within single backend.
InRedo = false;
Sent via pgsql-hackers mailing list (email@example.com)
To make changes to your subscription: