Re: [HACKERS] Should *.backup files ever be removed from pg_xlog? - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [HACKERS] Should *.backup files ever be removed from pg_xlog?
Date
Msg-id 200506090155.j591txc11470@candle.pha.pa.us
Whole thread Raw
Responses Re: [HACKERS] Should *.backup files ever be removed from pg_xlog?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
Bruce Momjian wrote:
> Tom Lane wrote:
> > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > When you do a pg_start_backup()/pg_stop_backup(), the *.backup files
> > > created in pg_xlog are never deleted.  Is that intended?
> >
> > Yes.  See the documentation.  DBAs can delete 'em if they feel like,
> > but I don't see a strong argument for automatically removing 'em.
> > They aren't actually large ...
>
> I don't see anywhere in the documentation where we say you can get rid
> of them.  I see this:
>
>     For example, if the starting WAL file is 0000000100001234000055CD the
>     backup history file will be named something like
>     0000000100001234000055CD.007C9330.backup. (The second number in the file
>     name stands for an exact position within the WAL file, and can
>     ordinarily be ignored.) Once you have safely archived the file system
>     backup and the WAL segment files used during the backup (as specified in
>     the backup history file), all archived WAL segments with names
>     numerically less are no longer needed to recover the file system backup
>     and may be deleted. However, you should consider keeping several backup
>     sets to be absolutely certain that you are can recover your data. Keep
>     in mind that only completed WAL segment files are archived, so there
>     will be delay between running pg_stop_backup and the archiving of all
>     WAL segment files needed to make the file system backup consistent.
>
> The "all archived WAL segments with names numerically less are no longer
> needed" I assume is talking about files in the archive location, not
> pg_xlog.  Does this need clarifying?

I have fixed the code to properly remove *.backup files from the
/pg_xlog directory once they are archived.  The patch is larger because
of an indenting change --- the 'else if' part is the new part.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/access/transam/xlog.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v
retrieving revision 1.197
diff -c -c -r1.197 xlog.c
*** src/backend/access/transam/xlog.c    6 Jun 2005 20:22:57 -0000    1.197
--- src/backend/access/transam/xlog.c    9 Jun 2005 01:52:53 -0000
***************
*** 2289,2339 ****
           * We use the alphanumeric sorting property of the filenames to
           * decide which ones are earlier than the lastoff segment.
           */
!         if (strlen(xlde->d_name) == 24 &&
!             strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
              strcmp(xlde->d_name + 8, lastoff + 8) <= 0)
          {
!             bool        recycle;
!
!             if (XLogArchivingActive())
!                 recycle = XLogArchiveIsDone(xlde->d_name);
!             else
!                 recycle = true;
!
!             if (recycle)
              {
!                 snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
!
!                 /*
!                  * Before deleting the file, see if it can be recycled as
!                  * a future log segment.
!                  */
!                 if (InstallXLogFileSegment(&endlogId, &endlogSeg, path,
!                                            true, &max_advance,
!                                            true))
                  {
!                     ereport(DEBUG2,
!                           (errmsg("recycled transaction log file \"%s\"",
!                                   xlde->d_name)));
!                     (*nsegsrecycled)++;
!                     /* Needn't recheck that slot on future iterations */
!                     if (max_advance > 0)
                      {
!                         NextLogSeg(endlogId, endlogSeg);
!                         max_advance--;
                      }
                  }
!                 else
                  {
-                     /* No need for any more future segments... */
                      ereport(DEBUG2,
!                           (errmsg("removing transaction log file \"%s\"",
                                    xlde->d_name)));
                      unlink(path);
!                     (*nsegsremoved)++;
                  }
-
-                 XLogArchiveCleanup(xlde->d_name);
              }
          }
          errno = 0;
--- 2289,2354 ----
           * We use the alphanumeric sorting property of the filenames to
           * decide which ones are earlier than the lastoff segment.
           */
!         if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
              strcmp(xlde->d_name + 8, lastoff + 8) <= 0)
          {
!             snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
!
!             if (strlen(xlde->d_name) == 24)
              {
!                 bool        recycle;
!
!                 if (XLogArchivingActive())
!                     recycle = XLogArchiveIsDone(xlde->d_name);
!                 else
!                     recycle = true;
!
!                 if (recycle)
                  {
!                     /*
!                      * Before deleting the file, see if it can be recycled as
!                      * a future log segment.
!                      */
!                     if (InstallXLogFileSegment(&endlogId, &endlogSeg, path,
!                                                true, &max_advance,
!                                                true))
!                     {
!                         ereport(DEBUG2,
!                               (errmsg("recycled transaction log file \"%s\"",
!                                       xlde->d_name)));
!                         (*nsegsrecycled)++;
!                         /* Needn't recheck that slot on future iterations */
!                         if (max_advance > 0)
!                         {
!                             NextLogSeg(endlogId, endlogSeg);
!                             max_advance--;
!                         }
!                     }
!                     else
                      {
!                         /* No need for any more future segments... */
!                         ereport(DEBUG2,
!                               (errmsg("removing transaction log file \"%s\"",
!                                       xlde->d_name)));
!                         unlink(path);
!                         (*nsegsremoved)++;
                      }
+                     XLogArchiveCleanup(xlde->d_name);
                  }
!             }
!             else if (strlen(xlde->d_name) > 24 &&
!                       strcmp(xlde->d_name + strlen(xlde->d_name) - strlen(".backup"),
!                              ".backup") == 0)
!             {
!                 /* Remove any *.backup files that have been archived. */
!                 if (!XLogArchivingActive() || XLogArchiveIsDone(xlde->d_name))
                  {
                      ereport(DEBUG2,
!                           (errmsg("removing transaction log backup status file \"%s\"",
                                    xlde->d_name)));
                      unlink(path);
!                     XLogArchiveCleanup(xlde->d_name);
                  }
              }
          }
          errno = 0;

pgsql-patches by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Are you actively hacking on 2PC?
Next
From: Bruce Momjian
Date:
Subject: Re: ALTER SCHEMA ... SET TABLESPACE