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"
29
extern int getopt(int argc, char *const argv[], const char *optstring);
37
/* Options and defaults */
38
bool debug = false; /* are we debugging? */
40
char *archiveLocation; /* where to find the archive? */
41
char *restartWALFileName; /* the file from which we can restart restore */
42
char WALFilePath[MAXPGPATH]; /* the file path including archive */
43
char exclusiveCleanupFileName[MAXPGPATH]; /* the oldest file we
48
/* =====================================================================
50
* Customizable section
52
* =====================================================================
54
* Currently, this section assumes that the Archive is a locally
55
* accessible directory. If you want to make other assumptions,
56
* such as using a vendor-specific archive and access API, these
57
* routines are the ones you'll need to change. You're
58
* enouraged to submit any changes to pgsql-hackers@postgresql.org
59
* or personally to the current maintainer. Those changes may be
60
* folded in to later versions of this program.
63
#define XLOG_DATA_FNAME_LEN 24
64
/* Reworked from access/xlog_internal.h */
65
#define XLogFileName(fname, tli, log, seg) \
66
snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
67
#define XLOG_BACKUP_FNAME_LEN 40
70
* Initialize allows customized commands into the archive cleanup program.
72
* You may wish to add code to check for tape libraries, etc..
78
* This code assumes that archiveLocation is a directory, so we use stat
79
* to test if it's accessible.
83
if (stat(archiveLocation, &stat_buf) != 0 ||
84
!S_ISDIR(stat_buf.st_mode))
86
fprintf(stderr, "%s: archiveLocation \"%s\" does not exist\n",
87
progname, archiveLocation);
93
CleanupPriorWALFiles(void)
99
if ((xldir = opendir(archiveLocation)) != NULL)
101
while ((xlde = readdir(xldir)) != NULL)
104
* We ignore the timeline part of the XLOG segment identifiers in
105
* deciding whether a segment is still needed. This ensures that
106
* we won't prematurely remove a segment from a parent timeline.
107
* We could probably be a little more proactive about removing
108
* segments of non-parent timelines, but that would be a whole lot
111
* We use the alphanumeric sorting property of the filenames to
112
* decide which ones are earlier than the exclusiveCleanupFileName
113
* file. Note that this means files are not removed in the order
114
* they were originally written, in case this worries you.
116
if (strlen(xlde->d_name) == XLOG_DATA_FNAME_LEN &&
117
strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN &&
118
strcmp(xlde->d_name + 8, exclusiveCleanupFileName + 8) < 0)
120
snprintf(WALFilePath, MAXPGPATH, "%s/%s",
121
archiveLocation, xlde->d_name);
123
fprintf(stderr, "%s: removing file \"%s\"\n",
124
progname, WALFilePath);
126
rc = unlink(WALFilePath);
129
fprintf(stderr, "%s: ERROR: could not remove file \"%s\": %s\n",
130
progname, WALFilePath, strerror(errno));
138
fprintf(stderr, "%s: could not open archiveLocation \"%s\": %s\n",
139
progname, archiveLocation, strerror(errno));
143
* SetWALFileNameForCleanup()
145
* Set the earliest WAL filename that we want to keep on the archive
146
* and decide whether we need_cleanup
149
SetWALFileNameForCleanup(void)
151
bool fnameOK = false;
154
* If restartWALFileName is a WAL file name then just use it directly. If
155
* restartWALFileName is a .backup filename, make sure we use the prefix
156
* of the filename, otherwise we will remove wrong files since
157
* 000000010000000000000010.00000020.backup is after
158
* 000000010000000000000010.
160
if (strlen(restartWALFileName) == XLOG_DATA_FNAME_LEN &&
161
strspn(restartWALFileName, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN)
163
strcpy(exclusiveCleanupFileName, restartWALFileName);
166
else if (strlen(restartWALFileName) == XLOG_BACKUP_FNAME_LEN)
174
args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
180
* Use just the prefix of the filename, ignore everything after
183
XLogFileName(exclusiveCleanupFileName, tli, log, seg);
189
fprintf(stderr, "%s: invalid filename input\n", progname);
190
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
195
/* =====================================================================
196
* End of Customizable section
197
* =====================================================================
203
printf("%s removes older WAL files from PostgreSQL archives.\n\n", progname);
205
printf(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n", progname);
207
"for use as an archive_cleanup_command in the recovery.conf when standby_mode = on:\n"
208
" archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
210
" archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n");
212
"or for use as a standalone archive cleaner:\n"
214
" pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n");
215
printf("\nOptions:\n");
216
printf(" -d generates debug output (verbose mode)\n");
217
printf(" --help show this help, then exit\n");
218
printf(" --version output version information, then exit\n");
219
printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
222
/*------------ MAIN ----------------------------------------*/
224
main(int argc, char **argv)
228
progname = get_progname(argv[0]);
232
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
237
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
239
puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
244
while ((c = getopt(argc, argv, "d")) != -1)
248
case 'd': /* Debug mode */
252
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
259
* We will go to the archiveLocation to check restartWALFileName.
260
* restartWALFileName may not exist anymore, which would not be an error,
261
* so we separate the archiveLocation and restartWALFileName so we can
262
* check separately whether archiveLocation exists, if not that is an
267
archiveLocation = argv[optind];
272
fprintf(stderr, "%s: must specify archive location\n", progname);
273
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
279
restartWALFileName = argv[optind];
284
fprintf(stderr, "%s: must specify restartfilename\n", progname);
285
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
291
fprintf(stderr, "%s: too many parameters\n", progname);
292
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
297
* Check archive exists and other initialization if required.
302
* Check filename is a valid name, then process to find cut-off
304
SetWALFileNameForCleanup();
308
snprintf(WALFilePath, MAXPGPATH, "%s/%s",
309
archiveLocation, exclusiveCleanupFileName);
310
fprintf(stderr, "%s: keep WAL file \"%s\" and later\n",
311
progname, WALFilePath);
315
* Remove WAL files older than cut-off
317
CleanupPriorWALFiles();