Commit 246f136e authored by Peter Eisentraut's avatar Peter Eisentraut

Improve handling of parameter differences in physical replication

When certain parameters are changed on a physical replication primary,
this is communicated to standbys using the XLOG_PARAMETER_CHANGE WAL
record.  The standby then checks whether its own settings are at least
as big as the ones on the primary.  If not, the standby shuts down
with a fatal error.

The correspondence of settings between primary and standby is required
because those settings influence certain shared memory sizings that
are required for processing WAL records that the primary might send.
For example, if the primary sends a prepared transaction, the standby
must have had max_prepared_transaction set appropriately or it won't
be able to process those WAL records.

However, fatally shutting down the standby immediately upon receipt of
the parameter change record might be a bit of an overreaction.  The
resources related to those settings are not required immediately at
that point, and might never be required if the activity on the primary
does not exhaust all those resources.  If we just let the standby roll
on with recovery, it will eventually produce an appropriate error when
those resources are used.

So this patch relaxes this a bit.  Upon receipt of
XLOG_PARAMETER_CHANGE, we still check the settings but only issue a
warning and set a global flag if there is a problem.  Then when we
actually hit the resource issue and the flag was set, we issue another
warning message with relevant information.  At that point we pause
recovery, so a hot standby remains usable.  We also repeat the last
warning message once a minute so it is harder to miss or ignore.
Reviewed-by: default avatarSergei Kornilov <sk@zsrv.org>
Reviewed-by: default avatarMasahiko Sawada <masahiko.sawada@2ndquadrant.com>
Reviewed-by: default avatarKyotaro Horiguchi <horikyota.ntt@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/4ad69a4c-cc9b-0dfe-0352-8b1b0cd36c7b@2ndquadrant.com
parent a01e1b8b
...@@ -2148,18 +2148,14 @@ LOG: database system is ready to accept read only connections ...@@ -2148,18 +2148,14 @@ LOG: database system is ready to accept read only connections
</para> </para>
<para> <para>
The setting of some parameters on the standby will need reconfiguration The settings of some parameters determine the size of shared memory for
if they have been changed on the primary. For these parameters, tracking transaction IDs, locks, and prepared transactions. These shared
the value on the standby must memory structures should be no smaller on a standby than on the primary.
be equal to or greater than the value on the primary. Otherwise, it could happen that the standby runs out of shared memory
Therefore, if you want to increase these values, you should do so on all during recovery. For example, if the primary uses a prepared transaction
standby servers first, before applying the changes to the primary server. but the standby did not allocate any shared memory for tracking prepared
Conversely, if you want to decrease these values, you should do so on the transactions, then recovery will abort and cannot continue until the
primary server first, before applying the changes to all standby servers. standby's configuration is changed. The parameters affected are:
If these parameters
are not set high enough then the standby will refuse to start.
Higher values can then be supplied and the server
restarted to begin recovery again. These parameters are:
<itemizedlist> <itemizedlist>
<listitem> <listitem>
...@@ -2188,6 +2184,34 @@ LOG: database system is ready to accept read only connections ...@@ -2188,6 +2184,34 @@ LOG: database system is ready to accept read only connections
</para> </para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
The easiest way to ensure this does not become a problem is to have these
parameters set on the standbys to values equal to or greater than on the
primary. Therefore, if you want to increase these values, you should do
so on all standby servers first, before applying the changes to the
primary server. Conversely, if you want to decrease these values, you
should do so on the primary server first, before applying the changes to
all standby servers. The WAL tracks changes to these parameters on the
primary, and if a standby processes WAL that indicates that the current
value on the primary is higher than its own value, it will log a warning, for example:
<screen>
WARNING: insufficient setting for parameter max_connections
DETAIL: max_connections = 80 is a lower setting than on the master server (where its value was 100).
HINT: Change parameters and restart the server, or there may be resource exhaustion errors sooner or later.
</screen>
Recovery will continue but could abort at any time thereafter. (It could
also never end up failing if the activity on the primary does not actually
require the full extent of the allocated shared memory resources.) If
recovery reaches a point where it cannot continue due to lack of shared
memory, recovery will pause and another warning will be logged, for example:
<screen>
WARNING: recovery paused because of insufficient parameter settings
DETAIL: See earlier in the log about which settings are insufficient.
HINT: Recovery cannot continue unless the configuration is changed and the server restarted.
</screen>
This warning will repeated once a minute. At that point, the settings on
the standby need to be updated and the instance restarted before recovery
can continue.
</para> </para>
<para> <para>
......
...@@ -2360,11 +2360,14 @@ PrepareRedoAdd(char *buf, XLogRecPtr start_lsn, ...@@ -2360,11 +2360,14 @@ PrepareRedoAdd(char *buf, XLogRecPtr start_lsn,
/* Get a free gxact from the freelist */ /* Get a free gxact from the freelist */
if (TwoPhaseState->freeGXacts == NULL) if (TwoPhaseState->freeGXacts == NULL)
{
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("maximum number of prepared transactions reached"), errmsg("maximum number of prepared transactions reached"),
errhint("Increase max_prepared_transactions (currently %d).", errhint("Increase max_prepared_transactions (currently %d).",
max_prepared_xacts))); max_prepared_xacts)));
}
gxact = TwoPhaseState->freeGXacts; gxact = TwoPhaseState->freeGXacts;
TwoPhaseState->freeGXacts = gxact->next; TwoPhaseState->freeGXacts = gxact->next;
......
...@@ -264,6 +264,8 @@ bool InArchiveRecovery = false; ...@@ -264,6 +264,8 @@ bool InArchiveRecovery = false;
static bool standby_signal_file_found = false; static bool standby_signal_file_found = false;
static bool recovery_signal_file_found = false; static bool recovery_signal_file_found = false;
static bool need_restart_for_parameter_values = false;
/* Was the last xlog file restored from archive, or local? */ /* Was the last xlog file restored from archive, or local? */
static bool restoredFromArchive = false; static bool restoredFromArchive = false;
...@@ -5998,6 +6000,54 @@ SetRecoveryPause(bool recoveryPause) ...@@ -5998,6 +6000,54 @@ SetRecoveryPause(bool recoveryPause)
SpinLockRelease(&XLogCtl->info_lck); SpinLockRelease(&XLogCtl->info_lck);
} }
/*
* If in hot standby, pause recovery because of a parameter conflict.
*
* Similar to recoveryPausesHere() but with a different messaging. The user
* is expected to make the parameter change and restart the server. If they
* just unpause recovery, they will then run into whatever error is after this
* function call for the non-hot-standby case.
*
* We intentionally do not give advice about specific parameters or values
* here because it might be misleading. For example, if we run out of lock
* space, then in the single-server case we would recommend raising
* max_locks_per_transaction, but in recovery it could equally be the case
* that max_connections is out of sync with the primary. If we get here, we
* have already logged any parameter discrepancies in
* RecoveryRequiresIntParameter(), so users can go back to that and get
* concrete and accurate information.
*/
void
StandbyParamErrorPauseRecovery(void)
{
TimestampTz last_warning = 0;
if (!AmStartupProcess() || !need_restart_for_parameter_values)
return;
SetRecoveryPause(true);
do
{
TimestampTz now = GetCurrentTimestamp();
if (TimestampDifferenceExceeds(last_warning, now, 60000))
{
ereport(WARNING,
(errmsg("recovery paused because of insufficient parameter settings"),
errdetail("See earlier in the log about which settings are insufficient."),
errhint("Recovery cannot continue unless the configuration is changed and the server restarted.")));
last_warning = now;
}
pgstat_report_wait_start(WAIT_EVENT_RECOVERY_PAUSE);
pg_usleep(1000000L); /* 1000 ms */
pgstat_report_wait_end();
HandleStartupProcInterrupts();
}
while (RecoveryIsPaused());
}
/* /*
* When recovery_min_apply_delay is set, we wait long enough to make sure * When recovery_min_apply_delay is set, we wait long enough to make sure
* certain record types are applied at least that interval behind the master. * certain record types are applied at least that interval behind the master.
...@@ -6177,16 +6227,20 @@ GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream) ...@@ -6177,16 +6227,20 @@ GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)
* Note that text field supplied is a parameter name and does not require * Note that text field supplied is a parameter name and does not require
* translation * translation
*/ */
#define RecoveryRequiresIntParameter(param_name, currValue, minValue) \ static void
do { \ RecoveryRequiresIntParameter(const char *param_name, int currValue, int minValue)
if ((currValue) < (minValue)) \ {
ereport(ERROR, \ if (currValue < minValue)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \ {
errmsg("hot standby is not possible because %s = %d is a lower setting than on the master server (its value was %d)", \ ereport(WARNING,
param_name, \ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
currValue, \ errmsg("insufficient setting for parameter %s", param_name),
minValue))); \ errdetail("%s = %d is a lower setting than on the master server (where its value was %d).",
} while(0) param_name, currValue, minValue),
errhint("Change parameters and restart the server, or there may be resource exhaustion errors sooner or later.")));
need_restart_for_parameter_values = true;
}
}
/* /*
* Check to see if required parameters are set high enough on this server * Check to see if required parameters are set high enough on this server
......
...@@ -3654,7 +3654,14 @@ KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid, ...@@ -3654,7 +3654,14 @@ KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
* If it still won't fit then we're out of memory * If it still won't fit then we're out of memory
*/ */
if (head + nxids > pArray->maxKnownAssignedXids) if (head + nxids > pArray->maxKnownAssignedXids)
elog(ERROR, "too many KnownAssignedXids"); {
StandbyParamErrorPauseRecovery();
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"),
errdetail("There are no more KnownAssignedXids slots."),
errhint("You might need to increase max_connections.")));
}
} }
/* Now we can insert the xids into the space starting at head */ /* Now we can insert the xids into the space starting at head */
......
...@@ -965,10 +965,13 @@ LockAcquireExtended(const LOCKTAG *locktag, ...@@ -965,10 +965,13 @@ LockAcquireExtended(const LOCKTAG *locktag,
if (locallockp) if (locallockp)
*locallockp = NULL; *locallockp = NULL;
if (reportMemoryError) if (reportMemoryError)
{
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"), errmsg("out of shared memory"),
errhint("You might need to increase max_locks_per_transaction."))); errhint("You might need to increase max_locks_per_transaction.")));
}
else else
return LOCKACQUIRE_NOT_AVAIL; return LOCKACQUIRE_NOT_AVAIL;
} }
...@@ -1003,10 +1006,13 @@ LockAcquireExtended(const LOCKTAG *locktag, ...@@ -1003,10 +1006,13 @@ LockAcquireExtended(const LOCKTAG *locktag,
if (locallockp) if (locallockp)
*locallockp = NULL; *locallockp = NULL;
if (reportMemoryError) if (reportMemoryError)
{
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"), errmsg("out of shared memory"),
errhint("You might need to increase max_locks_per_transaction."))); errhint("You might need to increase max_locks_per_transaction.")));
}
else else
return LOCKACQUIRE_NOT_AVAIL; return LOCKACQUIRE_NOT_AVAIL;
} }
...@@ -2828,6 +2834,7 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock) ...@@ -2828,6 +2834,7 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock)
{ {
LWLockRelease(partitionLock); LWLockRelease(partitionLock);
LWLockRelease(&MyProc->backendLock); LWLockRelease(&MyProc->backendLock);
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"), errmsg("out of shared memory"),
...@@ -4158,6 +4165,7 @@ lock_twophase_recover(TransactionId xid, uint16 info, ...@@ -4158,6 +4165,7 @@ lock_twophase_recover(TransactionId xid, uint16 info,
if (!lock) if (!lock)
{ {
LWLockRelease(partitionLock); LWLockRelease(partitionLock);
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"), errmsg("out of shared memory"),
...@@ -4223,6 +4231,7 @@ lock_twophase_recover(TransactionId xid, uint16 info, ...@@ -4223,6 +4231,7 @@ lock_twophase_recover(TransactionId xid, uint16 info,
elog(PANIC, "lock table corrupted"); elog(PANIC, "lock table corrupted");
} }
LWLockRelease(partitionLock); LWLockRelease(partitionLock);
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"), errmsg("out of shared memory"),
...@@ -4515,6 +4524,7 @@ VirtualXactLock(VirtualTransactionId vxid, bool wait) ...@@ -4515,6 +4524,7 @@ VirtualXactLock(VirtualTransactionId vxid, bool wait)
{ {
LWLockRelease(partitionLock); LWLockRelease(partitionLock);
LWLockRelease(&proc->backendLock); LWLockRelease(&proc->backendLock);
StandbyParamErrorPauseRecovery();
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY), (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of shared memory"), errmsg("out of shared memory"),
......
...@@ -287,6 +287,7 @@ extern XLogRecPtr GetXLogInsertRecPtr(void); ...@@ -287,6 +287,7 @@ extern XLogRecPtr GetXLogInsertRecPtr(void);
extern XLogRecPtr GetXLogWriteRecPtr(void); extern XLogRecPtr GetXLogWriteRecPtr(void);
extern bool RecoveryIsPaused(void); extern bool RecoveryIsPaused(void);
extern void SetRecoveryPause(bool recoveryPause); extern void SetRecoveryPause(bool recoveryPause);
extern void StandbyParamErrorPauseRecovery(void);
extern TimestampTz GetLatestXTime(void); extern TimestampTz GetLatestXTime(void);
extern TimestampTz GetCurrentChunkReplayStartTime(void); extern TimestampTz GetCurrentChunkReplayStartTime(void);
......
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