When fetching WAL for a basebackup, report errors with a sensible TLI.
authorRobert Haas <[email protected]>
Fri, 29 Oct 2021 18:00:32 +0000 (14:00 -0400)
committerRobert Haas <[email protected]>
Fri, 29 Oct 2021 18:00:32 +0000 (14:00 -0400)
The previous code used ThisTimeLineID, which need not even be
initialized here, although it usually was in practice, because
pg_basebackup issues IDENTIFY_SYSTEM before calling BASE_BACKUP,
and that initializes ThisTimeLineID as a side effect. That's not
really good enough, though, not only because we shoudn't be counting
on side effects like that, but also because the TLI could change
meanwhile. Fortunately, we have convenient access to more meaningful
TLI values, so use those instead.

Because of the way this logic is coded, the consequences of using
a possibly-incorrect TLI here are no worse than a slightly confusing
error message, I don't want to take any risk here, so no back-patch
at least for now.

Patch by me, reviewed by Kyotaro Horiguchi and Michael Paquier

Discussion: http://postgr.es/m/CA+TgmoZRNWGWYDX9RgTXMG6_nwSdB=PB-PPRUbvMUTGfmL2sHQ@mail.gmail.com

src/backend/replication/basebackup.c

index b31c36d918c0fa3e9686d9a5a7c6a90b5afb6b4f..b7359f43903450ad5f957514c0cfa9af1489f4d7 100644 (file)
@@ -502,9 +502,9 @@ perform_base_backup(basebackup_options *opt)
                 * including them.
                 */
                XLByteToSeg(startptr, startsegno, wal_segment_size);
-               XLogFileName(firstoff, ThisTimeLineID, startsegno, wal_segment_size);
+               XLogFileName(firstoff, starttli, startsegno, wal_segment_size);
                XLByteToPrevSeg(endptr, endsegno, wal_segment_size);
-               XLogFileName(lastoff, ThisTimeLineID, endsegno, wal_segment_size);
+               XLogFileName(lastoff, endtli, endsegno, wal_segment_size);
 
                dir = AllocateDir("pg_wal");
                while ((de = ReadDir(dir, "pg_wal")) != NULL)
@@ -528,7 +528,7 @@ perform_base_backup(basebackup_options *opt)
                 * Before we go any further, check that none of the WAL segments we
                 * need were removed.
                 */
-               CheckXLogRemoved(startsegno, ThisTimeLineID);
+               CheckXLogRemoved(startsegno, starttli);
 
                /*
                 * Sort the WAL filenames.  We want to send the files in order from
@@ -555,7 +555,7 @@ perform_base_backup(basebackup_options *opt)
                {
                        char            startfname[MAXFNAMELEN];
 
-                       XLogFileName(startfname, ThisTimeLineID, startsegno,
+                       XLogFileName(startfname, starttli, startsegno,
                                                 wal_segment_size);
                        ereport(ERROR,
                                        (errmsg("could not find WAL file \"%s\"", startfname)));
@@ -571,8 +571,7 @@ perform_base_backup(basebackup_options *opt)
                        {
                                char            nextfname[MAXFNAMELEN];
 
-                               XLogFileName(nextfname, ThisTimeLineID, nextsegno,
-                                                        wal_segment_size);
+                               XLogFileName(nextfname, tli, nextsegno, wal_segment_size);
                                ereport(ERROR,
                                                (errmsg("could not find WAL file \"%s\"", nextfname)));
                        }
@@ -581,7 +580,7 @@ perform_base_backup(basebackup_options *opt)
                {
                        char            endfname[MAXFNAMELEN];
 
-                       XLogFileName(endfname, ThisTimeLineID, endsegno, wal_segment_size);
+                       XLogFileName(endfname, endtli, endsegno, wal_segment_size);
                        ereport(ERROR,
                                        (errmsg("could not find WAL file \"%s\"", endfname)));
                }