PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_archivecleanup.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include "pg_getopt.h"
#include "access/xlog_internal.h"
Include dependency graph for pg_archivecleanup.c:

Go to the source code of this file.

Functions

static void Initialize (void)
 
static void TrimExtension (char *filename, char *extension)
 
static void CleanupPriorWALFiles (void)
 
static void SetWALFileNameForCleanup (void)
 
static void usage (void)
 
int main (int argc, char **argv)
 

Variables

const char * progname
 
bool debug = false
 
bool dryrun = false
 
char * additional_ext = NULL
 
char * archiveLocation
 
char * restartWALFileName
 
char WALFilePath [MAXPGPATH *2]
 
char exclusiveCleanupFileName [MAXFNAMELEN]
 

Function Documentation

static void CleanupPriorWALFiles ( void  )
static

Definition at line 95 of file pg_archivecleanup.c.

References _, additional_ext, archiveLocation, closedir(), dirent::d_name, debug, dryrun, exclusiveCleanupFileName, IsPartialXLogFileName, IsXLogFileName, MAXPGPATH, NULL, opendir(), progname, readdir(), snprintf(), strerror(), strlcpy(), TrimExtension(), unlink(), walfile, and WALFilePath.

Referenced by main().

96 {
97  int rc;
98  DIR *xldir;
99  struct dirent *xlde;
100  char walfile[MAXPGPATH];
101 
102  if ((xldir = opendir(archiveLocation)) != NULL)
103  {
104  while (errno = 0, (xlde = readdir(xldir)) != NULL)
105  {
106  /*
107  * Truncation is essentially harmless, because we skip names of
108  * length other than XLOG_FNAME_LEN. (In principle, one could use
109  * a 1000-character additional_ext and get trouble.)
110  */
111  strlcpy(walfile, xlde->d_name, MAXPGPATH);
112  TrimExtension(walfile, additional_ext);
113 
114  /*
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
120  * more complicated.
121  *
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.
126  */
127  if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&
128  strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
129  {
130  /*
131  * Use the original file name again now, including any
132  * extension that might have been chopped off before testing
133  * the sequence.
134  */
135  snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s",
136  archiveLocation, xlde->d_name);
137 
138  if (dryrun)
139  {
140  /*
141  * Prints the name of the file to be removed and skips the
142  * actual removal. The regular printout is so that the
143  * user can pipe the output into some other program.
144  */
145  printf("%s\n", WALFilePath);
146  if (debug)
147  fprintf(stderr,
148  _("%s: file \"%s\" would be removed\n"),
150  continue;
151  }
152 
153  if (debug)
154  fprintf(stderr, _("%s: removing file \"%s\"\n"),
156 
157  rc = unlink(WALFilePath);
158  if (rc != 0)
159  {
160  fprintf(stderr, _("%s: ERROR: could not remove file \"%s\": %s\n"),
161  progname, WALFilePath, strerror(errno));
162  break;
163  }
164  }
165  }
166 
167  if (errno)
168  fprintf(stderr, _("%s: could not read archive location \"%s\": %s\n"),
170  if (closedir(xldir))
171  fprintf(stderr, _("%s: could not close archive location \"%s\": %s\n"),
173  }
174  else
175  fprintf(stderr, _("%s: could not open archive location \"%s\": %s\n"),
177 }
int closedir(DIR *)
Definition: dirent.c:111
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * archiveLocation
Definition: dirent.h:9
bool debug
static void TrimExtension(char *filename, char *extension)
char WALFilePath[MAXPGPATH *2]
Definition: dirent.c:25
#define IsXLogFileName(fname)
#define MAXPGPATH
static Walfile * walfile
Definition: receivelog.c:33
DIR * opendir(const char *)
Definition: dirent.c:33
int unlink(const char *filename)
const char * progname
bool dryrun
char exclusiveCleanupFileName[MAXFNAMELEN]
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:229
struct dirent * readdir(DIR *)
Definition: dirent.c:77
#define IsPartialXLogFileName(fname)
const char * strerror(int errnum)
Definition: strerror.c:19
char d_name[MAX_PATH]
Definition: dirent.h:14
#define _(x)
Definition: elog.c:84
char * additional_ext
static void Initialize ( void  )
static

Definition at line 61 of file pg_archivecleanup.c.

References _, archiveLocation, and progname.

Referenced by main().

62 {
63  /*
64  * This code assumes that archiveLocation is a directory, so we use stat
65  * to test if it's accessible.
66  */
67  struct stat stat_buf;
68 
69  if (stat(archiveLocation, &stat_buf) != 0 ||
70  !S_ISDIR(stat_buf.st_mode))
71  {
72  fprintf(stderr, _("%s: archive location \"%s\" does not exist\n"),
74  exit(2);
75  }
76 }
char * archiveLocation
struct stat stat_buf
Definition: pg_standby.c:101
const char * progname
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char **  argv 
)

Definition at line 285 of file pg_archivecleanup.c.

References _, additional_ext, archiveLocation, CleanupPriorWALFiles(), debug, dryrun, exclusiveCleanupFileName, get_progname(), getopt(), Initialize(), MAXPGPATH, optarg, optind, pg_strdup(), PG_TEXTDOMAIN, progname, restartWALFileName, set_pglocale_pgservice(), SetWALFileNameForCleanup(), snprintf(), usage(), and WALFilePath.

286 {
287  int c;
288 
289  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
290  progname = get_progname(argv[0]);
291 
292  if (argc > 1)
293  {
294  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
295  {
296  usage();
297  exit(0);
298  }
299  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
300  {
301  puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
302  exit(0);
303  }
304  }
305 
306  while ((c = getopt(argc, argv, "x:dn")) != -1)
307  {
308  switch (c)
309  {
310  case 'd': /* Debug mode */
311  debug = true;
312  break;
313  case 'n': /* Dry-Run mode */
314  dryrun = true;
315  break;
316  case 'x':
317  additional_ext = pg_strdup(optarg); /* Extension to remove
318  * from xlogfile names */
319  break;
320  default:
321  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
322  exit(2);
323  break;
324  }
325  }
326 
327  /*
328  * We will go to the archiveLocation to check restartWALFileName.
329  * restartWALFileName may not exist anymore, which would not be an error,
330  * so we separate the archiveLocation and restartWALFileName so we can
331  * check separately whether archiveLocation exists, if not that is an
332  * error
333  */
334  if (optind < argc)
335  {
336  archiveLocation = argv[optind];
337  optind++;
338  }
339  else
340  {
341  fprintf(stderr, _("%s: must specify archive location\n"), progname);
342  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
343  exit(2);
344  }
345 
346  if (optind < argc)
347  {
348  restartWALFileName = argv[optind];
349  optind++;
350  }
351  else
352  {
353  fprintf(stderr, _("%s: must specify restartfilename\n"), progname);
354  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
355  exit(2);
356  }
357 
358  if (optind < argc)
359  {
360  fprintf(stderr, _("%s: too many parameters\n"), progname);
361  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
362  exit(2);
363  }
364 
365  /*
366  * Check archive exists and other initialization if required.
367  */
368  Initialize();
369 
370  /*
371  * Check filename is a valid name, then process to find cut-off
372  */
374 
375  if (debug)
376  {
377  snprintf(WALFilePath, MAXPGPATH, "%s/%s",
379  fprintf(stderr, _("%s: keep WAL file \"%s\" and later\n"),
381  }
382 
383  /*
384  * Remove WAL files older than cut-off
385  */
387 
388  exit(0);
389 }
static void CleanupPriorWALFiles(void)
static void SetWALFileNameForCleanup(void)
const char * get_progname(const char *argv0)
Definition: path.c:453
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * archiveLocation
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:72
static void usage(void)
bool debug
char WALFilePath[MAXPGPATH *2]
int optind
Definition: getopt.c:51
#define MAXPGPATH
char * restartWALFileName
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * progname
bool dryrun
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
char exclusiveCleanupFileName[MAXFNAMELEN]
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * optarg
Definition: getopt.c:53
static void Initialize(void)
#define _(x)
Definition: elog.c:84
char * additional_ext
static void SetWALFileNameForCleanup ( void  )
static

Definition at line 186 of file pg_archivecleanup.c.

References _, additional_ext, generate_unaccent_rules::args, exclusiveCleanupFileName, IsBackupHistoryFileName, IsPartialXLogFileName, IsXLogFileName, progname, restartWALFileName, TrimExtension(), and XLogFileNameById.

Referenced by main().

187 {
188  bool fnameOK = false;
189 
191 
192  /*
193  * If restartWALFileName is a WAL file name then just use it directly. If
194  * restartWALFileName is a .partial or .backup filename, make sure we use
195  * the prefix of the filename, otherwise we will remove wrong files since
196  * 000000010000000000000010.partial and
197  * 000000010000000000000010.00000020.backup are after
198  * 000000010000000000000010.
199  */
201  {
203  fnameOK = true;
204  }
206  {
207  int args;
208  uint32 tli = 1,
209  log = 0,
210  seg = 0;
211 
212  args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
213  &tli, &log, &seg);
214  if (args == 3)
215  {
216  fnameOK = true;
217 
218  /*
219  * Use just the prefix of the filename, ignore everything after
220  * first period
221  */
223  }
224  }
226  {
227  int args;
228  uint32 tli = 1,
229  log = 0,
230  seg = 0,
231  offset = 0;
232 
233  args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
234  if (args == 4)
235  {
236  fnameOK = true;
237 
238  /*
239  * Use just the prefix of the filename, ignore everything after
240  * first period
241  */
243  }
244  }
245 
246  if (!fnameOK)
247  {
248  fprintf(stderr, _("%s: invalid filename input\n"), progname);
249  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
250  exit(2);
251  }
252 }
#define XLogFileNameById(fname, tli, log, seg)
static void TrimExtension(char *filename, char *extension)
#define IsXLogFileName(fname)
char * restartWALFileName
unsigned int uint32
Definition: c.h:268
#define IsBackupHistoryFileName(fname)
const char * progname
char exclusiveCleanupFileName[MAXFNAMELEN]
#define IsPartialXLogFileName(fname)
#define _(x)
Definition: elog.c:84
char * additional_ext
static void TrimExtension ( char *  filename,
char *  extension 
)
static

Definition at line 79 of file pg_archivecleanup.c.

References NULL.

Referenced by CleanupPriorWALFiles(), and SetWALFileNameForCleanup().

80 {
81  int flen;
82  int elen;
83 
84  if (extension == NULL)
85  return;
86 
87  elen = strlen(extension);
88  flen = strlen(filename);
89 
90  if (flen > elen && strcmp(filename + flen - elen, extension) == 0)
91  filename[flen - elen] = '\0';
92 }
#define NULL
Definition: c.h:229
static char * filename
Definition: pg_dumpall.c:89
static void usage ( void  )
static

Definition at line 260 of file pg_archivecleanup.c.

References _, and progname.

Referenced by main().

261 {
262  printf(_("%s removes older WAL files from PostgreSQL archives.\n\n"), progname);
263  printf(_("Usage:\n"));
264  printf(_(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
265  printf(_("\nOptions:\n"));
266  printf(_(" -d generate debug output (verbose mode)\n"));
267  printf(_(" -n dry run, show the names of the files that would be removed\n"));
268  printf(_(" -V, --version output version information, then exit\n"));
269  printf(_(" -x EXT clean up files if they have this extension\n"));
270  printf(_(" -?, --help show this help, then exit\n"));
271  printf(_("\n"
272  "For use as archive_cleanup_command in recovery.conf when standby_mode = on:\n"
273  " archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
274  "e.g.\n"
275  " archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n"));
276  printf(_("\n"
277  "Or for use as a standalone archive cleaner:\n"
278  "e.g.\n"
279  " pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n"));
280  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
281 }
const char * progname
#define _(x)
Definition: elog.c:84

Variable Documentation

char* additional_ext = NULL

Definition at line 31 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), main(), and SetWALFileNameForCleanup().

char* archiveLocation

Definition at line 33 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), Initialize(), and main().

bool debug = false

Definition at line 29 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), and main().

bool dryrun = false

Definition at line 30 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), and main().

char exclusiveCleanupFileName[MAXFNAMELEN]

Definition at line 36 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), main(), and SetWALFileNameForCleanup().

const char* progname
char* restartWALFileName

Definition at line 34 of file pg_archivecleanup.c.

Referenced by main(), and SetWALFileNameForCleanup().

char WALFilePath[MAXPGPATH *2]

Definition at line 35 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), and main().