4 * To be used as archive_cleanup_command to clean an archive when using
7 * src/bin/pg_archivecleanup/pg_archivecleanup.c
9 #include "postgres_fe.h"
18 #include "access/xlog_internal.h"
19 #include "common/logging.h"
20 #include "pg_getopt.h"
24 /* Options and defaults */
25 bool dryrun = false; /* are we performing a dry-run operation? */
26 char *additional_ext = NULL; /* Extension to remove from filenames */
28 char *archiveLocation; /* where to find the archive? */
29 char *restartWALFileName; /* the file from which we can restart restore */
30 char exclusiveCleanupFileName[MAXFNAMELEN]; /* the oldest file we want
31 * to remain in archive */
34 /* =====================================================================
36 * Customizable section
38 * =====================================================================
40 * Currently, this section assumes that the Archive is a locally
41 * accessible directory. If you want to make other assumptions,
42 * such as using a vendor-specific archive and access API, these
43 * routines are the ones you'll need to change. You're
44 * encouraged to submit any changes to pgsql-hackers@lists.postgresql.org
45 * or personally to the current maintainer. Those changes may be
46 * folded in to later versions of this program.
50 * Initialize allows customized commands into the archive cleanup program.
52 * You may wish to add code to check for tape libraries, etc..
58 * This code assumes that archiveLocation is a directory, so we use stat
59 * to test if it's accessible.
63 if (stat(archiveLocation, &stat_buf) != 0 ||
64 !S_ISDIR(stat_buf.st_mode))
66 pg_log_error("archive location \"%s\" does not exist",
73 TrimExtension(char *filename, char *extension)
78 if (extension == NULL)
81 elen = strlen(extension);
82 flen = strlen(filename);
84 if (flen > elen && strcmp(filename + flen - elen, extension) == 0)
85 filename[flen - elen] = '\0';
89 CleanupPriorWALFiles(void)
94 char walfile[MAXPGPATH];
96 if ((xldir = opendir(archiveLocation)) != NULL)
98 while (errno = 0, (xlde = readdir(xldir)) != NULL)
101 * Truncation is essentially harmless, because we skip names of
102 * length other than XLOG_FNAME_LEN. (In principle, one could use
103 * a 1000-character additional_ext and get trouble.)
105 strlcpy(walfile, xlde->d_name, MAXPGPATH);
106 TrimExtension(walfile, additional_ext);
109 * We ignore the timeline part of the XLOG segment identifiers in
110 * deciding whether a segment is still needed. This ensures that
111 * we won't prematurely remove a segment from a parent timeline.
112 * We could probably be a little more proactive about removing
113 * segments of non-parent timelines, but that would be a whole lot
116 * We use the alphanumeric sorting property of the filenames to
117 * decide which ones are earlier than the exclusiveCleanupFileName
118 * file. Note that this means files are not removed in the order
119 * they were originally written, in case this worries you.
121 if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&
122 strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
124 char WALFilePath[MAXPGPATH * 2]; /* the file path
125 * including archive */
128 * Use the original file name again now, including any
129 * extension that might have been chopped off before testing
132 snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s",
133 archiveLocation, xlde->d_name);
138 * Prints the name of the file to be removed and skips the
139 * actual removal. The regular printout is so that the
140 * user can pipe the output into some other program.
142 printf("%s\n", WALFilePath);
143 pg_log_debug("file \"%s\" would be removed", WALFilePath);
147 pg_log_debug("removing file \"%s\"", WALFilePath);
149 rc = unlink(WALFilePath);
152 pg_log_error("could not remove file \"%s\": %m",
160 pg_log_error("could not read archive location \"%s\": %m",
163 pg_log_error("could not close archive location \"%s\": %m",
167 pg_log_error("could not open archive location \"%s\": %m",
172 * SetWALFileNameForCleanup()
174 * Set the earliest WAL filename that we want to keep on the archive
175 * and decide whether we need cleanup
178 SetWALFileNameForCleanup(void)
180 bool fnameOK = false;
182 TrimExtension(restartWALFileName, additional_ext);
185 * If restartWALFileName is a WAL file name then just use it directly. If
186 * restartWALFileName is a .partial or .backup filename, make sure we use
187 * the prefix of the filename, otherwise we will remove wrong files since
188 * 000000010000000000000010.partial and
189 * 000000010000000000000010.00000020.backup are after
190 * 000000010000000000000010.
192 if (IsXLogFileName(restartWALFileName))
194 strcpy(exclusiveCleanupFileName, restartWALFileName);
197 else if (IsPartialXLogFileName(restartWALFileName))
204 args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
211 * Use just the prefix of the filename, ignore everything after
214 XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
217 else if (IsBackupHistoryFileName(restartWALFileName))
225 args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
231 * Use just the prefix of the filename, ignore everything after
234 XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
240 pg_log_error("invalid file name argument");
241 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
246 /* =====================================================================
247 * End of Customizable section
248 * =====================================================================
254 printf(_("%s removes older WAL files from PostgreSQL archives.\n\n"), progname);
255 printf(_("Usage:\n"));
256 printf(_(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
257 printf(_("\nOptions:\n"));
258 printf(_(" -d generate debug output (verbose mode)\n"));
259 printf(_(" -n dry run, show the names of the files that would be removed\n"));
260 printf(_(" -V, --version output version information, then exit\n"));
261 printf(_(" -x EXT clean up files if they have this extension\n"));
262 printf(_(" -?, --help show this help, then exit\n"));
264 "For use as archive_cleanup_command in postgresql.conf:\n"
265 " archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
267 " archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n"));
269 "Or for use as a standalone archive cleaner:\n"
271 " pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n"));
272 printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
275 /*------------ MAIN ----------------------------------------*/
277 main(int argc, char **argv)
281 pg_logging_init(argv[0]);
282 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
283 progname = get_progname(argv[0]);
287 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
292 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
294 puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
299 while ((c = getopt(argc, argv, "x:dn")) != -1)
303 case 'd': /* Debug mode */
304 pg_logging_set_level(PG_LOG_DEBUG);
306 case 'n': /* Dry-Run mode */
310 additional_ext = pg_strdup(optarg); /* Extension to remove
311 * from xlogfile names */
314 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
321 * We will go to the archiveLocation to check restartWALFileName.
322 * restartWALFileName may not exist anymore, which would not be an error,
323 * so we separate the archiveLocation and restartWALFileName so we can
324 * check separately whether archiveLocation exists, if not that is an
329 archiveLocation = argv[optind];
334 pg_log_error("must specify archive location");
335 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
341 restartWALFileName = argv[optind];
346 pg_log_error("must specify oldest kept WAL file");
347 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
353 pg_log_error("too many command-line arguments");
354 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
359 * Check archive exists and other initialization if required.
364 * Check filename is a valid name, then process to find cut-off
366 SetWALFileNameForCleanup();
368 pg_log_debug("keeping WAL file \"%s/%s\" and later",
369 archiveLocation, exclusiveCleanupFileName);
372 * Remove WAL files older than cut-off
374 CleanupPriorWALFiles();