static void XLogWrite(XLogwrtRqst WriteRqst, bool flexible);
 static bool InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
                       bool find_free, XLogSegNo max_segno,
-                      bool use_lock, int elevel);
+                      bool use_lock);
 static int XLogFileRead(XLogSegNo segno, int emode, TimeLineID tli,
             int source, bool notexistOk);
 static int XLogFileReadAnyTLI(XLogSegNo segno, int emode, int source);
    max_segno = logsegno + CheckPointSegments;
    if (!InstallXLogFileSegment(&installed_segno, tmppath,
                                *use_existent, max_segno,
-                               use_lock, LOG))
+                               use_lock))
    {
        /*
         * No need for any more future segments, or InstallXLogFileSegment()
 }
 
 /*
- * Copy a WAL segment file in pg_xlog directory.
+ * Create a new XLOG file segment by copying a pre-existing one.
  *
- * srcfname        source filename
- * upto            how much of the source file to copy? (the rest is filled with
- *             zeros)
- * segno       identify segment to install.
+ * destsegno: identify segment to be created.
  *
- * The file is first copied with a temporary filename, and then installed as
- * a newly-created segment.
+ * srcTLI, srclog, srcseg: identify segment to be copied (could be from
+ *     a different timeline)
+ *
+ * upto: how much of the source file to copy (the rest is filled with
+ *     zeros)
+ *
+ * Currently this is only used during recovery, and so there are no locking
+ * considerations.  But we should be just as tense as XLogFileInit to avoid
+ * emplacing a bogus file.
  */
 static void
-XLogFileCopy(char *srcfname, int upto, XLogSegNo segno)
+XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
+            int upto)
 {
-   char        srcpath[MAXPGPATH];
+   char        path[MAXPGPATH];
    char        tmppath[MAXPGPATH];
    char        buffer[XLOG_BLCKSZ];
    int         srcfd;
    /*
     * Open the source file
     */
-   snprintf(srcpath, MAXPGPATH, XLOGDIR "/%s", srcfname);
-   srcfd = OpenTransientFile(srcpath, O_RDONLY | PG_BINARY, 0);
+   XLogFilePath(path, srcTLI, srcsegno);
+   srcfd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0);
    if (srcfd < 0)
        ereport(ERROR,
                (errcode_for_file_access(),
-                errmsg("could not open file \"%s\": %m", srcpath)));
+                errmsg("could not open file \"%s\": %m", path)));
 
    /*
     * Copy into a temp file name.
                    ereport(ERROR,
                            (errcode_for_file_access(),
                             errmsg("could not read file \"%s\": %m",
-                                   srcpath)));
+                                   path)));
                else
                    ereport(ERROR,
                            (errmsg("not enough data in file \"%s\"",
-                                   srcpath)));
+                                   path)));
            }
        }
        errno = 0;
 
    CloseTransientFile(srcfd);
 
-   /* install the new file */
-   (void) InstallXLogFileSegment(&segno, tmppath, false,
-                                 0, false, ERROR);
+   /*
+    * Now move the segment into place with its final name.
+    */
+   if (!InstallXLogFileSegment(&destsegno, tmppath, false, 0, false))
+       elog(ERROR, "InstallXLogFileSegment should not have failed");
 }
 
 /*
  * place.  This should be TRUE except during bootstrap log creation.  The
  * caller must *not* hold the lock at call.
  *
- * elevel: log level used by this routine.
- *
  * Returns TRUE if the file was installed successfully.  FALSE indicates that
  * max_segno limit was exceeded, or an error occurred while renaming the
  * file into place.
 static bool
 InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
                       bool find_free, XLogSegNo max_segno,
-                      bool use_lock, int elevel)
+                      bool use_lock)
 {
    char        path[MAXPGPATH];
    struct stat stat_buf;
    {
        if (use_lock)
            LWLockRelease(ControlFileLock);
-       ereport(elevel,
+       ereport(LOG,
                (errcode_for_file_access(),
                 errmsg("could not link file \"%s\" to \"%s\" (initialization of log file): %m",
                        tmppath, path)));
    {
        if (use_lock)
            LWLockRelease(ControlFileLock);
-       ereport(elevel,
+       ereport(LOG,
                (errcode_for_file_access(),
                 errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file): %m",
                        tmppath, path)));
    if (endlogSegNo <= recycleSegNo &&
        lstat(path, &statbuf) == 0 && S_ISREG(statbuf.st_mode) &&
        InstallXLogFileSegment(&endlogSegNo, path,
-                              true, recycleSegNo, true, LOG))
+                              true, recycleSegNo, true))
    {
        ereport(DEBUG2,
                (errmsg("recycled transaction log file \"%s\"",
     */
    if (endLogSegNo == startLogSegNo)
    {
-       XLogFileName(xlogfname, endTLI, endLogSegNo);
-
        /*
         * Make a copy of the file on the new timeline.
         *
         * considerations. But we should be just as tense as XLogFileInit to
         * avoid emplacing a bogus file.
         */
-       XLogFileCopy(xlogfname, endOfLog % XLOG_SEG_SIZE, endLogSegNo);
+       XLogFileCopy(endLogSegNo, endTLI, endLogSegNo,
+                    endOfLog % XLOG_SEG_SIZE);
    }
    else
    {