Hi,

Currently pg_archivecleanup doesn't remove backup history files even when they're older than oldestkeptwalfile.

Of course the size of backup history files are smaller than WAL files and they wouldn't consume much disk space, but a lot of backup history files(e.g. daily backup for years) whose backups data have been already removed are unnecessary and I would appreciate if pg_archivecleanup has an option to remove them.

Attached a PoC patch, which added new option -b to remove files including backup history files older than oldestkeptwalfile.

  $ ls archivedir
000000010000000000000001 000000010000000000000003 000000010000000000000006
  000000010000000000000008
000000010000000000000002 000000010000000000000004 000000010000000000000007
  000000010000000000000009
  000000010000000000000002.00000028.backup  000000010000000000000005
000000010000000000000007.00000028.backup 00000001000000000000000A.partial

  $ pg_archivecleanup -b archivedir 000000010000000000000009

  $ ls archivedir
  000000010000000000000009  00000001000000000000000A.partial

Any thoughts?

--
Regards,

--
Atsushi Torikoshi
NTT DATA CORPORATION
From ad87224ec5ba6ee13ccf934bf3e5adefb7e67212 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikos...@oss.nttdata.com>
Date: Mon, 24 Apr 2023 23:28:06 +0900
Subject: [PATCH v1] Allow pg_archivecleanup to remove backup history files

Add new option -b to remove files including backup history files older
than oldestkeptwalfile.

---
 src/bin/pg_archivecleanup/pg_archivecleanup.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_archivecleanup/pg_archivecleanup.c b/src/bin/pg_archivecleanup/pg_archivecleanup.c
index 7726d05149..5bc90fbadd 100644
--- a/src/bin/pg_archivecleanup/pg_archivecleanup.c
+++ b/src/bin/pg_archivecleanup/pg_archivecleanup.c
@@ -23,6 +23,8 @@ const char *progname;
 
 /* Options and defaults */
 bool		dryrun = false;		/* are we performing a dry-run operation? */
+bool		removeBackupHistoryFile = false;	/* remove files including
+												 * backup history files */
 char	   *additional_ext = NULL;	/* Extension to remove from filenames */
 
 char	   *archiveLocation;	/* where to find the archive? */
@@ -118,7 +120,8 @@ CleanupPriorWALFiles(void)
 			 * file. Note that this means files are not removed in the order
 			 * they were originally written, in case this worries you.
 			 */
-			if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&
+			if (((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) ||
+				(removeBackupHistoryFile && IsBackupHistoryFileName(walfile))) &&
 				strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
 			{
 				char		WALFilePath[MAXPGPATH * 2]; /* the file path
@@ -252,6 +255,7 @@ usage(void)
 	printf(_("Usage:\n"));
 	printf(_("  %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
 	printf(_("\nOptions:\n"));
+	printf(_("  -b             remove files including backup history files\n"));
 	printf(_("  -d             generate debug output (verbose mode)\n"));
 	printf(_("  -n             dry run, show the names of the files that would be removed\n"));
 	printf(_("  -V, --version  output version information, then exit\n"));
@@ -294,10 +298,13 @@ main(int argc, char **argv)
 		}
 	}
 
-	while ((c = getopt(argc, argv, "dnx:")) != -1)
+	while ((c = getopt(argc, argv, "bdnx:")) != -1)
 	{
 		switch (c)
 		{
+			case 'b':			/* Remove backup history files too */
+				removeBackupHistoryFile = true;
+				break;
 			case 'd':			/* Debug mode */
 				pg_logging_increase_verbosity();
 				break;

base-commit: c5e4ec293ea527394fc1d0006ab88047b3ce580f
-- 
2.25.1

Reply via email to