Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f4d907c

Browse files
committed
Remove old *.backup files when we do pg_stop_backup(). This
prevents a large number of *.backup files from existing in pg_xlog/
1 parent 713507b commit f4d907c

File tree

1 file changed

+59
-1
lines changed
  • src/backend/access/transam

1 file changed

+59
-1
lines changed

src/backend/access/transam/xlog.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.199 2005/06/09 22:36:27 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.200 2005/06/15 01:36:08 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -450,6 +450,7 @@ static bool RestoreArchivedFile(char *path, const char *xlogfname,
450450
static int PreallocXlogFiles(XLogRecPtr endptr);
451451
static void MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
452452
int *nsegsremoved, int *nsegsrecycled);
453+
static void RemoveOldBackupHistory(void);
453454
static XLogRecord *ReadRecord(XLogRecPtr *RecPtr, int emode);
454455
static bool ValidXLOGHeader(XLogPageHeader hdr, int emode);
455456
static XLogRecord *ReadCheckpointRecord(XLogRecPtr RecPtr, int whichChkpt);
@@ -2355,6 +2356,61 @@ MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
23552356
FreeDir(xldir);
23562357
}
23572358

2359+
/*
2360+
* Remove previous backup history files
2361+
*/
2362+
static void
2363+
RemoveOldBackupHistory(void)
2364+
{
2365+
DIR *xldir;
2366+
struct dirent *xlde;
2367+
char path[MAXPGPATH];
2368+
2369+
xldir = AllocateDir(XLogDir);
2370+
if (xldir == NULL)
2371+
ereport(ERROR,
2372+
(errcode_for_file_access(),
2373+
errmsg("could not open transaction log directory \"%s\": %m",
2374+
XLogDir)));
2375+
2376+
errno = 0;
2377+
while ((xlde = readdir(xldir)) != NULL)
2378+
{
2379+
if (strlen(xlde->d_name) > 24 &&
2380+
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
2381+
strcmp(xlde->d_name + strlen(xlde->d_name) - strlen(".backup"),
2382+
".backup") == 0)
2383+
{
2384+
/* Remove any *.backup files that have been archived. */
2385+
if (!XLogArchivingActive() || XLogArchiveIsDone(xlde->d_name))
2386+
{
2387+
ereport(DEBUG2,
2388+
(errmsg("removing transaction log backup history file \"%s\"",
2389+
xlde->d_name)));
2390+
snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
2391+
unlink(path);
2392+
XLogArchiveCleanup(xlde->d_name);
2393+
}
2394+
}
2395+
errno = 0;
2396+
}
2397+
#ifdef WIN32
2398+
2399+
/*
2400+
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
2401+
* not in released version
2402+
*/
2403+
if (GetLastError() == ERROR_NO_MORE_FILES)
2404+
errno = 0;
2405+
#endif
2406+
if (errno)
2407+
ereport(ERROR,
2408+
(errcode_for_file_access(),
2409+
errmsg("could not read transaction log directory \"%s\": %m",
2410+
XLogDir)));
2411+
FreeDir(xldir);
2412+
}
2413+
23582414
/*
23592415
* Restore the backup blocks present in an XLOG record, if any.
23602416
*
@@ -5738,6 +5794,8 @@ pg_stop_backup(PG_FUNCTION_ARGS)
57385794
errmsg("could not remove file \"%s\": %m",
57395795
labelfilepath)));
57405796

5797+
RemoveOldBackupHistory();
5798+
57415799
/*
57425800
* Notify archiver that history file may be archived immediately
57435801
*/

0 commit comments

Comments
 (0)