2 * contrib/pg_archivecleanup/pg_archivecleanup.c
6 * Production-ready example of an archive_cleanup_command
7 * used to clean an archive when using standby_mode = on in 9.0
8 * or for standalone use for any version of PostgreSQL 8.0+.
10 * Original author: Simon Riggs simon@2ndquadrant.com
11 * Current maintainer: Simon Riggs
13 #include "postgres_fe.h"
22 #include "pg_getopt.h"
26 /* Options and defaults */
27 bool debug = false; /* are we debugging? */
28 bool dryrun = false; /* are we performing a dry-run operation? */
29 char *additional_ext = NULL; /* Extension to remove from filenames */
31 char *archiveLocation; /* where to find the archive? */
32 char *restartWALFileName; /* the file from which we can restart restore */
33 char WALFilePath[MAXPGPATH]; /* the file path including archive */
34 char exclusiveCleanupFileName[MAXPGPATH]; /* the oldest file we
39 /* =====================================================================
41 * Customizable section
43 * =====================================================================
45 * Currently, this section assumes that the Archive is a locally
46 * accessible directory. If you want to make other assumptions,
47 * such as using a vendor-specific archive and access API, these
48 * routines are the ones you'll need to change. You're
49 * enouraged to submit any changes to pgsql-hackers@postgresql.org
50 * or personally to the current maintainer. Those changes may be
51 * folded in to later versions of this program.
54 #define XLOG_DATA_FNAME_LEN 24
55 /* Reworked from access/xlog_internal.h */
56 #define XLogFileName(fname, tli, log, seg) \
57 snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
58 #define XLOG_BACKUP_FNAME_LEN 40
61 * Initialize allows customized commands into the archive cleanup program.
63 * You may wish to add code to check for tape libraries, etc..
69 * This code assumes that archiveLocation is a directory, so we use stat
70 * to test if it's accessible.
74 if (stat(archiveLocation, &stat_buf) != 0 ||
75 !S_ISDIR(stat_buf.st_mode))
77 fprintf(stderr, "%s: archive location \"%s\" does not exist\n",
78 progname, archiveLocation);
84 TrimExtension(char *filename, char *extension)
89 if (extension == NULL)
92 elen = strlen(extension);
93 flen = strlen(filename);
95 if (flen > elen && strcmp(filename + flen - elen, extension) == 0)
96 filename[flen - elen] = '\0';
100 CleanupPriorWALFiles(void)
105 char walfile[MAXPGPATH];
107 if ((xldir = opendir(archiveLocation)) != NULL)
109 while ((xlde = readdir(xldir)) != NULL)
111 strncpy(walfile, xlde->d_name, MAXPGPATH);
112 TrimExtension(walfile, additional_ext);
115 * We ignore the timeline part of the XLOG segment identifiers in
116 * deciding whether a segment is still needed. This ensures that
117 * we won't prematurely remove a segment from a parent timeline.
118 * We could probably be a little more proactive about removing
119 * segments of non-parent timelines, but that would be a whole lot
122 * We use the alphanumeric sorting property of the filenames to
123 * decide which ones are earlier than the exclusiveCleanupFileName
124 * file. Note that this means files are not removed in the order
125 * they were originally written, in case this worries you.
127 if (strlen(walfile) == XLOG_DATA_FNAME_LEN &&
128 strspn(walfile, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN &&
129 strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
132 * Use the original file name again now, including any
133 * extension that might have been chopped off before testing
136 snprintf(WALFilePath, MAXPGPATH, "%s/%s",
137 archiveLocation, xlde->d_name);
142 * Prints the name of the file to be removed and skips the
143 * actual removal. The regular printout is so that the
144 * user can pipe the output into some other program.
146 printf("%s\n", WALFilePath);
149 "%s: file \"%s\" would be removed\n",
150 progname, WALFilePath);
155 fprintf(stderr, "%s: removing file \"%s\"\n",
156 progname, WALFilePath);
158 rc = unlink(WALFilePath);
161 fprintf(stderr, "%s: ERROR: could not remove file \"%s\": %s\n",
162 progname, WALFilePath, strerror(errno));
170 fprintf(stderr, "%s: could not open archive location \"%s\": %s\n",
171 progname, archiveLocation, strerror(errno));
175 * SetWALFileNameForCleanup()
177 * Set the earliest WAL filename that we want to keep on the archive
178 * and decide whether we need_cleanup
181 SetWALFileNameForCleanup(void)
183 bool fnameOK = false;
185 TrimExtension(restartWALFileName, additional_ext);
188 * If restartWALFileName is a WAL file name then just use it directly. If
189 * restartWALFileName is a .backup filename, make sure we use the prefix
190 * of the filename, otherwise we will remove wrong files since
191 * 000000010000000000000010.00000020.backup is after
192 * 000000010000000000000010.
194 if (strlen(restartWALFileName) == XLOG_DATA_FNAME_LEN &&
195 strspn(restartWALFileName, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN)
197 strcpy(exclusiveCleanupFileName, restartWALFileName);
200 else if (strlen(restartWALFileName) == XLOG_BACKUP_FNAME_LEN)
208 args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
214 * Use just the prefix of the filename, ignore everything after
217 XLogFileName(exclusiveCleanupFileName, tli, log, seg);
223 fprintf(stderr, "%s: invalid filename input\n", progname);
224 fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
229 /* =====================================================================
230 * End of Customizable section
231 * =====================================================================
237 printf("%s removes older WAL files from PostgreSQL archives.\n\n", progname);
239 printf(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n", progname);
240 printf("\nOptions:\n");
241 printf(" -d generate debug output (verbose mode)\n");
242 printf(" -n dry run, show the names of the files that would be removed\n");
243 printf(" -V, --version output version information, then exit\n");
244 printf(" -x EXT clean up files if they have this extension\n");
245 printf(" -?, --help show this help, then exit\n");
247 "For use as archive_cleanup_command in recovery.conf when standby_mode = on:\n"
248 " archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
250 " archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n");
252 "Or for use as a standalone archive cleaner:\n"
254 " pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n");
255 printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
258 /*------------ MAIN ----------------------------------------*/
260 main(int argc, char **argv)
264 progname = get_progname(argv[0]);
268 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
273 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
275 puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
280 while ((c = getopt(argc, argv, "x:dn")) != -1)
284 case 'd': /* Debug mode */
287 case 'n': /* Dry-Run mode */
291 additional_ext = strdup(optarg); /* Extension to remove
292 * from xlogfile names */
295 fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
302 * We will go to the archiveLocation to check restartWALFileName.
303 * restartWALFileName may not exist anymore, which would not be an error,
304 * so we separate the archiveLocation and restartWALFileName so we can
305 * check separately whether archiveLocation exists, if not that is an
310 archiveLocation = argv[optind];
315 fprintf(stderr, "%s: must specify archive location\n", progname);
316 fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
322 restartWALFileName = argv[optind];
327 fprintf(stderr, "%s: must specify restartfilename\n", progname);
328 fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
334 fprintf(stderr, "%s: too many parameters\n", progname);
335 fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
340 * Check archive exists and other initialization if required.
345 * Check filename is a valid name, then process to find cut-off
347 SetWALFileNameForCleanup();
351 snprintf(WALFilePath, MAXPGPATH, "%s/%s",
352 archiveLocation, exclusiveCleanupFileName);
353 fprintf(stderr, "%s: keep WAL file \"%s\" and later\n",
354 progname, WALFilePath);
358 * Remove WAL files older than cut-off
360 CleanupPriorWALFiles();