]> granicus.if.org Git - postgresql/commitdiff
Change the order that pg_xlog and WAL archive are polled for WAL segments.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 14 Feb 2014 13:15:09 +0000 (15:15 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 14 Feb 2014 13:15:09 +0000 (15:15 +0200)
If there is a WAL segment with same ID but different TLI present in both
the WAL archive and pg_xlog, prefer the one with higher TLI. Before this
patch, the archive was polled first, for all expected TLIs, and only if no
file was found was pg_xlog scanned. This was a change in behavior from 9.3,
which first scanned archive and pg_xlog for the highest TLI, then archive
and pg_xlog for the next highest TLI and so forth. This patch reverts the
behavior back to what it was in 9.2.

The reason for this is that if for example you try to do archive recovery
to timeline 2, which branched off timeline 1, but the WAL for timeline 2 is
not archived yet, we would replay past the timeline switch point on
timeline 1 using the archived files, before even looking timeline 2's files
in pg_xlog

Report and patch by Kyotaro Horiguchi. Backpatch to 9.3 where the behavior
was changed.

src/backend/access/transam/xlog.c

index 508970a751dfdfcdf4324355dd4e9dec3d88a035..85a0ce90180eae2105535bdbed597b932550b001 100644 (file)
@@ -11006,17 +11006,15 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
        /*-------
         * Standby mode is implemented by a state machine:
         *
-        * 1. Read from archive (XLOG_FROM_ARCHIVE)
-        * 2. Read from pg_xlog (XLOG_FROM_PG_XLOG)
-        * 3. Check trigger file
-        * 4. Read from primary server via walreceiver (XLOG_FROM_STREAM)
-        * 5. Rescan timelines
-        * 6. Sleep 5 seconds, and loop back to 1.
+        * 1. Read from either archive or pg_xlog (XLOG_FROM_ARCHIVE), or just
+        *    pg_xlog (XLOG_FROM_XLOG)
+        * 2. Check trigger file
+        * 3. Read from primary server via walreceiver (XLOG_FROM_STREAM)
+        * 4. Rescan timelines
+        * 5. Sleep 5 seconds, and loop back to 1.
         *
         * Failure to read from the current source advances the state machine to
-        * the next state. In addition, successfully reading a file from pg_xlog
-        * moves the state machine from state 2 back to state 1 (we always prefer
-        * files in the archive over files in pg_xlog).
+        * the next state.
         *
         * 'currentSource' indicates the current state. There are no currentSource
         * values for "check trigger", "rescan timelines", and "sleep" states,
@@ -11044,9 +11042,6 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
                        switch (currentSource)
                        {
                                case XLOG_FROM_ARCHIVE:
-                                       currentSource = XLOG_FROM_PG_XLOG;
-                                       break;
-
                                case XLOG_FROM_PG_XLOG:
 
                                        /*
@@ -11212,7 +11207,9 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
                                 * Try to restore the file from archive, or read an existing
                                 * file from pg_xlog.
                                 */
-                               readFile = XLogFileReadAnyTLI(readSegNo, DEBUG2, currentSource);
+                               readFile = XLogFileReadAnyTLI(readSegNo, DEBUG2,
+                                               currentSource == XLOG_FROM_ARCHIVE ? XLOG_FROM_ANY :
+                                                                                currentSource);
                                if (readFile >= 0)
                                        return true;    /* success! */