diff --git a/doc/src/sgml/recovery-config.sgml b/doc/src/sgml/recovery-config.sgml index c0c543e..641c9c6 100644 --- a/doc/src/sgml/recovery-config.sgml +++ b/doc/src/sgml/recovery-config.sgml @@ -135,6 +135,27 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"' # Windows + + recovery_time_delay (integer) + + recovery_time_delay recovery parameter + + + + Specifies the amount of time (in milliseconds, if no unit is specified) + which recovery of transaction commits should lag the master. This + parameter allows creation of a time-delayed standby. For example, if + you set this parameter to 5min, the standby will + replay each transaction commit only when the system time on the standby + is at least five minutes past the commit time reported by the master. + + + Note that if the master and standby system clocks are not synchronized, + this might lead to unexpected results. + + + + diff --git a/src/backend/access/transam/recovery.conf.sample b/src/backend/access/transam/recovery.conf.sample index 5acfa57..97cc7af 100644 --- a/src/backend/access/transam/recovery.conf.sample +++ b/src/backend/access/transam/recovery.conf.sample @@ -123,6 +123,17 @@ # #trigger_file = '' # +# recovery_time_delay +# +# By default, a standby server keeps restoring XLOG records from the +# primary as soon as possible. If you want to delay the replay of +# commited transactions from the master, specify a recovery time delay. +# For example, if you set this parameter to 5min, the standby will replay +# each transaction commit only whe the system time on the standby is least +# five minutes past the commit time reported by the master. +# +#recovery_time_delay = 0 +# #--------------------------------------------------------------------------- # HOT STANDBY PARAMETERS #--------------------------------------------------------------------------- diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index de19d22..714b1bd 100755 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -218,6 +218,8 @@ static bool recoveryPauseAtTarget = true; static TransactionId recoveryTargetXid; static TimestampTz recoveryTargetTime; static char *recoveryTargetName; +static int recovery_time_delay = 0; +static TimestampTz recoveryDelayUntilTime; /* options taken from recovery.conf for XLOG streaming */ static bool StandbyModeRequested = false; @@ -730,6 +732,7 @@ static void readRecoveryCommandFile(void); static void exitArchiveRecovery(TimeLineID endTLI, XLogSegNo endLogSegNo); static bool recoveryStopsHere(XLogRecord *record, bool *includeThis); static void recoveryPausesHere(void); +static void recoveryDelay(void); static void SetLatestXTime(TimestampTz xtime); static void SetCurrentChunkStartTime(TimestampTz xtime); static void CheckRequiredParameterValues(void); @@ -5474,6 +5477,19 @@ readRecoveryCommandFile(void) (errmsg_internal("trigger_file = '%s'", TriggerFile))); } + else if (strcmp(item->name, "recovery_time_delay") == 0) + { + const char *hintmsg; + + if (!parse_int(item->value, &recovery_time_delay, GUC_UNIT_MS, + &hintmsg)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("parameter \"%s\" requires a temporal value", "recovery_time_delay"), + hintmsg ? errhint("%s", _(hintmsg)) : 0)); + ereport(DEBUG2, + (errmsg("recovery_time_delay = '%s'", item->value))); + } else ereport(FATAL, (errmsg("unrecognized recovery parameter \"%s\"", @@ -5623,7 +5639,8 @@ exitArchiveRecovery(TimeLineID endTLI, XLogSegNo endLogSegNo) * We also track the timestamp of the latest applied COMMIT/ABORT * record in XLogCtl->recoveryLastXTime, for logging purposes. * Also, some information is saved in recoveryStopXid et al for use in - * annotating the new timeline's history file. + * annotating the new timeline's history file; and recoveryDelayUntilTime + * is updated, for time-delayed standbys. */ static bool recoveryStopsHere(XLogRecord *record, bool *includeThis) @@ -5633,6 +5650,9 @@ recoveryStopsHere(XLogRecord *record, bool *includeThis) TimestampTz recordXtime; char recordRPName[MAXFNAMELEN]; + /* Clear any previous recovery delay time */ + recoveryDelayUntilTime = 0; + /* We only consider stopping at COMMIT, ABORT or RESTORE POINT records */ if (record->xl_rmid != RM_XACT_ID && record->xl_rmid != RM_XLOG_ID) return false; @@ -5643,6 +5663,11 @@ recoveryStopsHere(XLogRecord *record, bool *includeThis) recordXactCommitData = (xl_xact_commit_compact *) XLogRecGetData(record); recordXtime = recordXactCommitData->xact_time; + + if (recovery_time_delay > 0) + recoveryDelayUntilTime = + TimestampTzPlusMilliseconds(recordXactCommitData->xact_time, + recovery_time_delay); } else if (record->xl_rmid == RM_XACT_ID && record_info == XLOG_XACT_COMMIT) { @@ -5650,6 +5675,11 @@ recoveryStopsHere(XLogRecord *record, bool *includeThis) recordXactCommitData = (xl_xact_commit *) XLogRecGetData(record); recordXtime = recordXactCommitData->xact_time; + + if (recovery_time_delay > 0) + recoveryDelayUntilTime = + TimestampTzPlusMilliseconds(recordXactCommitData->xact_time, + recovery_time_delay); } else if (record->xl_rmid == RM_XACT_ID && record_info == XLOG_XACT_ABORT) { @@ -5832,6 +5862,48 @@ SetRecoveryPause(bool recoveryPause) } /* + * When recovery_time_delay is set, we wait long enough to make sure we are + * at least that far behind the master. + */ +static void +recoveryDelay(void) +{ + /* main delay loop */ + while (true) + { + long secs; + int microsecs; + + ResetLatch(&XLogCtl->recoveryWakeupLatch); + + /* might change the trigger file's location */ + HandleStartupProcInterrupts(); + + if (CheckForStandbyTrigger()) + break; + + TimestampDifference(GetCurrentTimestamp(), recoveryDelayUntilTime, + &secs, µsecs); + + /* + * Wait for difference between GetCurrentTimestamp() and + * recoveryDelayUntilTime + */ + if (secs > 0 || microsecs > 0) + { + elog(DEBUG2, "recovery delay %ld seconds, %d milliseconds", + secs, microsecs / 1000); + + WaitLatch(&XLogCtl->recoveryWakeupLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, + secs * 1000L + microsecs / 1000); + } + else + break; + } +} + +/* * Save timestamp of latest processed commit/abort record. * * We keep this in XLogCtl, not a simple static variable, so that it can be @@ -6723,6 +6795,18 @@ StartupXLOG(void) break; } + /* + * If we've been asked to lag the master, pause until enough + * time has passed. + */ + if (recovery_time_delay > 0) + { + recoveryDelay(); + + if (CheckForStandbyTrigger()) + break; + } + /* Setup error traceback support for ereport() */ errcallback.callback = rm_redo_error_callback; errcallback.arg = (void *) record;