/*
* Make sure we have enough WAL available before retrieving the current
- * timeline. This is needed to determine am_cascading_walsender accurately
- * which is needed to determine the current timeline.
+ * timeline.
*/
flushptr = WalSndWaitForWal(targetPagePtr + reqLen);
+ /* Fail if not enough (implies we are going to shut down) */
+ if (flushptr < targetPagePtr + reqLen)
+ return -1;
+
/*
* Since logical decoding is also permitted on a standby server, we need
* to check if the server is in recovery to decide how to get the current
- * timeline ID (so that it also cover the promotion or timeline change
- * cases).
+ * timeline ID (so that it also covers the promotion or timeline change
+ * cases). We must determine am_cascading_walsender after waiting for the
+ * required WAL so that it is correct when the walsender wakes up after a
+ * promotion.
*/
am_cascading_walsender = RecoveryInProgress();
sendTimeLineValidUpto = state->currTLIValidUntil;
sendTimeLineNextTLI = state->nextTLI;
- /* fail if not (implies we are going to shut down) */
- if (flushptr < targetPagePtr + reqLen)
- return -1;
-
if (targetPagePtr + XLOG_BLCKSZ <= flushptr)
count = XLOG_BLCKSZ; /* more than one block available */
else