PostgreSQL Source Code  git master
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 "access/xlog_internal.h"
#include "common/logging.h"
#include "pg_getopt.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 dryrun = false
 
char * additional_ext = NULL
 
char * archiveLocation
 
char * restartWALFileName
 
char exclusiveCleanupFileName [MAXFNAMELEN]
 

Function Documentation

◆ CleanupPriorWALFiles()

static void CleanupPriorWALFiles ( void  )
static

Definition at line 89 of file pg_archivecleanup.c.

References additional_ext, archiveLocation, closedir(), dirent::d_name, dryrun, exclusiveCleanupFileName, IsPartialXLogFileName, IsXLogFileName, MAXPGPATH, opendir(), pg_log_debug, pg_log_error, printf, readdir(), snprintf, strlcpy(), TrimExtension(), and walfile.

Referenced by main().

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 }
#define pg_log_error(...)
Definition: logging.h:80
int closedir(DIR *)
Definition: dirent.c:123
#define printf(...)
Definition: port.h:223
char * archiveLocation
Definition: dirent.h:9
static void TrimExtension(char *filename, char *extension)
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
bool dryrun
char exclusiveCleanupFileName[MAXFNAMELEN]
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
struct dirent * readdir(DIR *)
Definition: dirent.c:78
#define IsPartialXLogFileName(fname)
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:217
char * additional_ext

◆ Initialize()

static void Initialize ( void  )
static

Definition at line 55 of file pg_archivecleanup.c.

References archiveLocation, pg_log_error, S_ISDIR, stat::st_mode, and stat.

Referenced by main().

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 }
#define pg_log_error(...)
Definition: logging.h:80
char * archiveLocation
#define S_ISDIR(m)
Definition: win32_port.h:324
#define stat
Definition: win32_port.h:283

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 287 of file pg_archivecleanup.c.

References _, additional_ext, archiveLocation, CleanupPriorWALFiles(), dryrun, exclusiveCleanupFileName, fprintf, get_progname(), getopt(), Initialize(), optarg, optind, pg_log_debug, pg_log_error, pg_logging_increase_verbosity(), pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, progname, restartWALFileName, set_pglocale_pgservice(), SetWALFileNameForCleanup(), and usage().

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
char * archiveLocation
#define fprintf
Definition: port.h:221
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:71
static void usage(void)
int optind
Definition: getopt.c:50
#define pg_log_debug(...)
Definition: logging.h:92
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:1215
char exclusiveCleanupFileName[MAXFNAMELEN]
void pg_logging_increase_verbosity(void)
Definition: logging.c:174
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
char * optarg
Definition: getopt.c:52
static void Initialize(void)
#define _(x)
Definition: elog.c:89
char * additional_ext

◆ SetWALFileNameForCleanup()

static void SetWALFileNameForCleanup ( void  )
static

Definition at line 187 of file pg_archivecleanup.c.

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

Referenced by main().

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 }
#define pg_log_error(...)
Definition: logging.h:80
#define XLogFileNameById(fname, tli, log, seg)
#define fprintf
Definition: port.h:221
static void TrimExtension(char *filename, char *extension)
#define IsXLogFileName(fname)
char * restartWALFileName
unsigned int uint32
Definition: c.h:441
#define IsBackupHistoryFileName(fname)
const char * progname
char exclusiveCleanupFileName[MAXFNAMELEN]
#define IsPartialXLogFileName(fname)
#define _(x)
Definition: elog.c:89
char * additional_ext

◆ TrimExtension()

static void TrimExtension ( char *  filename,
char *  extension 
)
static

Definition at line 73 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), and SetWALFileNameForCleanup().

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 }
static char * filename
Definition: pg_dumpall.c:92

◆ usage()

static void usage ( void  )
static

Definition at line 261 of file pg_archivecleanup.c.

References _, printf, and progname.

Referenced by main().

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 }
#define printf(...)
Definition: port.h:223
const char * progname
#define _(x)
Definition: elog.c:89

Variable Documentation

◆ additional_ext

char* additional_ext = NULL

Definition at line 26 of file pg_archivecleanup.c.

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

◆ archiveLocation

char* archiveLocation

Definition at line 28 of file pg_archivecleanup.c.

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

◆ dryrun

bool dryrun = false

Definition at line 25 of file pg_archivecleanup.c.

Referenced by CleanupPriorWALFiles(), and main().

◆ exclusiveCleanupFileName

char exclusiveCleanupFileName[MAXFNAMELEN]

Definition at line 30 of file pg_archivecleanup.c.

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

◆ progname

const char* progname

Definition at line 22 of file pg_archivecleanup.c.

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

◆ restartWALFileName

char* restartWALFileName

Definition at line 29 of file pg_archivecleanup.c.

Referenced by main(), and SetWALFileNameForCleanup().