PostgreSQL Source Code  git master
pg_archivecleanup.c
Go to the documentation of this file.
1 /*
2  * pg_archivecleanup.c
3  *
4  * To be used as archive_cleanup_command to clean an archive when using
5  * standby mode.
6  *
7  * src/bin/pg_archivecleanup/pg_archivecleanup.c
8  */
9 #include "postgres_fe.h"
10 
11 #include <ctype.h>
12 #include <dirent.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <sys/time.h>
17 
18 #include "access/xlog_internal.h"
19 #include "common/logging.h"
20 #include "pg_getopt.h"
21 
22 const char *progname;
23 
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 */
27 
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 */
32 
33 
34 /* =====================================================================
35  *
36  * Customizable section
37  *
38  * =====================================================================
39  *
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.
47  */
48 
49 /*
50  * Initialize allows customized commands into the archive cleanup program.
51  *
52  * You may wish to add code to check for tape libraries, etc..
53  */
54 static void
56 {
57  /*
58  * This code assumes that archiveLocation is a directory, so we use stat
59  * to test if it's accessible.
60  */
61  struct stat stat_buf;
62 
63  if (stat(archiveLocation, &stat_buf) != 0 ||
64  !S_ISDIR(stat_buf.st_mode))
65  {
66  pg_log_error("archive location \"%s\" does not exist",
68  exit(2);
69  }
70 }
71 
72 static void
73 TrimExtension(char *filename, char *extension)
74 {
75  int flen;
76  int elen;
77 
78  if (extension == NULL)
79  return;
80 
81  elen = strlen(extension);
82  flen = strlen(filename);
83 
84  if (flen > elen && strcmp(filename + flen - elen, extension) == 0)
85  filename[flen - elen] = '\0';
86 }
87 
88 static void
90 {
91  int rc;
92  DIR *xldir;
93  struct dirent *xlde;
94  char walfile[MAXPGPATH];
95 
96  if ((xldir = opendir(archiveLocation)) != NULL)
97  {
98  while (errno = 0, (xlde = readdir(xldir)) != NULL)
99  {
100  /*
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.)
104  */
105  strlcpy(walfile, xlde->d_name, MAXPGPATH);
106  TrimExtension(walfile, additional_ext);
107 
108  /*
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
114  * more complicated.
115  *
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.
120  */
121  if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&
122  strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
123  {
124  char WALFilePath[MAXPGPATH * 2]; /* the file path
125  * including archive */
126 
127  /*
128  * Use the original file name again now, including any
129  * extension that might have been chopped off before testing
130  * the sequence.
131  */
132  snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s",
133  archiveLocation, xlde->d_name);
134 
135  if (dryrun)
136  {
137  /*
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.
141  */
142  printf("%s\n", WALFilePath);
143  pg_log_debug("file \"%s\" would be removed", WALFilePath);
144  continue;
145  }
146 
147  pg_log_debug("removing file \"%s\"", WALFilePath);
148 
149  rc = unlink(WALFilePath);
150  if (rc != 0)
151  {
152  pg_log_error("could not remove file \"%s\": %m",
153  WALFilePath);
154  exit(1);
155  }
156  }
157  }
158 
159  if (errno)
160  {
161  pg_log_error("could not read archive location \"%s\": %m",
163  exit(1);
164  }
165  if (closedir(xldir))
166  {
167  pg_log_error("could not close archive location \"%s\": %m",
169  exit(1);
170  }
171  }
172  else
173  {
174  pg_log_error("could not open archive location \"%s\": %m",
176  exit(1);
177  }
178 }
179 
180 /*
181  * SetWALFileNameForCleanup()
182  *
183  * Set the earliest WAL filename that we want to keep on the archive
184  * and decide whether we need cleanup
185  */
186 static void
188 {
189  bool fnameOK = false;
190 
192 
193  /*
194  * If restartWALFileName is a WAL file name then just use it directly. If
195  * restartWALFileName is a .partial or .backup filename, make sure we use
196  * the prefix of the filename, otherwise we will remove wrong files since
197  * 000000010000000000000010.partial and
198  * 000000010000000000000010.00000020.backup are after
199  * 000000010000000000000010.
200  */
202  {
204  fnameOK = true;
205  }
207  {
208  int args;
209  uint32 tli = 1,
210  log = 0,
211  seg = 0;
212 
213  args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
214  &tli, &log, &seg);
215  if (args == 3)
216  {
217  fnameOK = true;
218 
219  /*
220  * Use just the prefix of the filename, ignore everything after
221  * first period
222  */
224  }
225  }
227  {
228  int args;
229  uint32 tli = 1,
230  log = 0,
231  seg = 0,
232  offset = 0;
233 
234  args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
235  if (args == 4)
236  {
237  fnameOK = true;
238 
239  /*
240  * Use just the prefix of the filename, ignore everything after
241  * first period
242  */
244  }
245  }
246 
247  if (!fnameOK)
248  {
249  pg_log_error("invalid file name argument");
250  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
251  exit(2);
252  }
253 }
254 
255 /* =====================================================================
256  * End of Customizable section
257  * =====================================================================
258  */
259 
260 static void
261 usage(void)
262 {
263  printf(_("%s removes older WAL files from PostgreSQL archives.\n\n"), progname);
264  printf(_("Usage:\n"));
265  printf(_(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
266  printf(_("\nOptions:\n"));
267  printf(_(" -d generate debug output (verbose mode)\n"));
268  printf(_(" -n dry run, show the names of the files that would be removed\n"));
269  printf(_(" -V, --version output version information, then exit\n"));
270  printf(_(" -x EXT clean up files if they have this extension\n"));
271  printf(_(" -?, --help show this help, then exit\n"));
272  printf(_("\n"
273  "For use as archive_cleanup_command in postgresql.conf:\n"
274  " archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
275  "e.g.\n"
276  " archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n"));
277  printf(_("\n"
278  "Or for use as a standalone archive cleaner:\n"
279  "e.g.\n"
280  " pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n"));
281  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
282  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
283 }
284 
285 /*------------ MAIN ----------------------------------------*/
286 int
287 main(int argc, char **argv)
288 {
289  int c;
290 
291  pg_logging_init(argv[0]);
292  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
293  progname = get_progname(argv[0]);
294 
295  if (argc > 1)
296  {
297  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
298  {
299  usage();
300  exit(0);
301  }
302  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
303  {
304  puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
305  exit(0);
306  }
307  }
308 
309  while ((c = getopt(argc, argv, "x:dn")) != -1)
310  {
311  switch (c)
312  {
313  case 'd': /* Debug mode */
315  break;
316  case 'n': /* Dry-Run mode */
317  dryrun = true;
318  break;
319  case 'x':
320  additional_ext = pg_strdup(optarg); /* Extension to remove
321  * from xlogfile names */
322  break;
323  default:
324  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
325  exit(2);
326  break;
327  }
328  }
329 
330  /*
331  * We will go to the archiveLocation to check restartWALFileName.
332  * restartWALFileName may not exist anymore, which would not be an error,
333  * so we separate the archiveLocation and restartWALFileName so we can
334  * check separately whether archiveLocation exists, if not that is an
335  * error
336  */
337  if (optind < argc)
338  {
339  archiveLocation = argv[optind];
340  optind++;
341  }
342  else
343  {
344  pg_log_error("must specify archive location");
345  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
346  exit(2);
347  }
348 
349  if (optind < argc)
350  {
351  restartWALFileName = argv[optind];
352  optind++;
353  }
354  else
355  {
356  pg_log_error("must specify oldest kept WAL file");
357  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
358  exit(2);
359  }
360 
361  if (optind < argc)
362  {
363  pg_log_error("too many command-line arguments");
364  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
365  exit(2);
366  }
367 
368  /*
369  * Check archive exists and other initialization if required.
370  */
371  Initialize();
372 
373  /*
374  * Check filename is a valid name, then process to find cut-off
375  */
377 
378  pg_log_debug("keeping WAL file \"%s/%s\" and later",
380 
381  /*
382  * Remove WAL files older than cut-off
383  */
385 
386  exit(0);
387 }
static void CleanupPriorWALFiles(void)
static void SetWALFileNameForCleanup(void)
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:80
void pg_logging_init(const char *argv0)
Definition: logging.c:81
int closedir(DIR *)
Definition: dirent.c:123
#define XLogFileNameById(fname, tli, log, seg)
#define printf(...)
Definition: port.h:222
char * archiveLocation
#define fprintf
Definition: port.h:220
Definition: dirent.h:9
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:71
static void usage(void)
static void TrimExtension(char *filename, char *extension)
int optind
Definition: getopt.c:50
Definition: dirent.c:25
#define IsXLogFileName(fname)
#define pg_log_debug(...)
Definition: logging.h:92
#define MAXPGPATH
static Walfile * walfile
Definition: receivelog.c:31
DIR * opendir(const char *)
Definition: dirent.c:33
char * restartWALFileName
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:441
#define IsBackupHistoryFileName(fname)
const char * progname
#define MAXFNAMELEN
bool dryrun
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1215
unsigned short st_mode
Definition: win32_port.h:260
char exclusiveCleanupFileName[MAXFNAMELEN]
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void pg_logging_increase_verbosity(void)
Definition: logging.c:174
struct dirent * readdir(DIR *)
Definition: dirent.c:78
int main(int argc, char **argv)
#define S_ISDIR(m)
Definition: win32_port.h:316
static char * filename
Definition: pg_dumpall.c:92
#define IsPartialXLogFileName(fname)
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
char * optarg
Definition: getopt.c:52
static void Initialize(void)
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:216
#define _(x)
Definition: elog.c:89
char * additional_ext
#define stat
Definition: win32_port.h:275