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.

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);
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  */
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  pg_fatal("could not remove file \"%s\": %m",
152  WALFilePath);
153  }
154  }
155 
156  if (errno)
157  pg_fatal("could not read archive location \"%s\": %m",
159  if (closedir(xldir))
160  pg_fatal("could not close archive location \"%s\": %m",
162  }
163  else
164  pg_fatal("could not open archive location \"%s\": %m",
166 }
int closedir(DIR *)
Definition: dirent.c:127
struct dirent * readdir(DIR *)
Definition: dirent.c:78
DIR * opendir(const char *)
Definition: dirent.c:33
#define pg_log_debug(...)
Definition: logging.h:133
char * archiveLocation
char * additional_ext
char exclusiveCleanupFileName[MAXFNAMELEN]
static void TrimExtension(char *filename, char *extension)
bool dryrun
#define pg_fatal(...)
#define MAXPGPATH
#define snprintf
Definition: port.h:238
#define printf(...)
Definition: port.h:244
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static Walfile * walfile
Definition: receivelog.c:29
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
static bool IsXLogFileName(const char *fname)
static bool IsPartialXLogFileName(const char *fname)

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

Referenced by main().

◆ Initialize()

static void Initialize ( void  )
static

Definition at line 55 of file pg_archivecleanup.c.

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 }
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
#define stat
Definition: win32_port.h:292
#define S_ISDIR(m)
Definition: win32_port.h:333

References archiveLocation, exit(), pg_log_error, S_ISDIR, stat::st_mode, and stat.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 275 of file pg_archivecleanup.c.

276 {
277  int c;
278 
279  pg_logging_init(argv[0]);
280  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
281  progname = get_progname(argv[0]);
282 
283  if (argc > 1)
284  {
285  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
286  {
287  usage();
288  exit(0);
289  }
290  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
291  {
292  puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
293  exit(0);
294  }
295  }
296 
297  while ((c = getopt(argc, argv, "dnx:")) != -1)
298  {
299  switch (c)
300  {
301  case 'd': /* Debug mode */
303  break;
304  case 'n': /* Dry-Run mode */
305  dryrun = true;
306  break;
307  case 'x':
308  additional_ext = pg_strdup(optarg); /* Extension to remove
309  * from xlogfile names */
310  break;
311  default:
312  /* getopt already emitted a complaint */
313  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
314  exit(2);
315  }
316  }
317 
318  /*
319  * We will go to the archiveLocation to check restartWALFileName.
320  * restartWALFileName may not exist anymore, which would not be an error,
321  * so we separate the archiveLocation and restartWALFileName so we can
322  * check separately whether archiveLocation exists, if not that is an
323  * error
324  */
325  if (optind < argc)
326  {
327  archiveLocation = argv[optind];
328  optind++;
329  }
330  else
331  {
332  pg_log_error("must specify archive location");
333  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
334  exit(2);
335  }
336 
337  if (optind < argc)
338  {
339  restartWALFileName = argv[optind];
340  optind++;
341  }
342  else
343  {
344  pg_log_error("must specify oldest kept WAL file");
345  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
346  exit(2);
347  }
348 
349  if (optind < argc)
350  {
351  pg_log_error("too many command-line arguments");
352  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
353  exit(2);
354  }
355 
356  /*
357  * Check archive exists and other initialization if required.
358  */
359  Initialize();
360 
361  /*
362  * Check filename is a valid name, then process to find cut-off
363  */
365 
366  pg_log_debug("keeping WAL file \"%s/%s\" and later",
368 
369  /*
370  * Remove WAL files older than cut-off
371  */
373 
374  exit(0);
375 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1222
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:436
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_logging_increase_verbosity(void)
Definition: logging.c:182
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error_hint(...)
Definition: logging.h:112
char * restartWALFileName
static void SetWALFileNameForCleanup(void)
static void CleanupPriorWALFiles(void)
static void Initialize(void)
const char * progname
static void usage(void)
PGDLLIMPORT int optind
Definition: getopt.c:50
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:71
PGDLLIMPORT char * optarg
Definition: getopt.c:52
const char * get_progname(const char *argv0)
Definition: path.c:574
char * c

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

◆ SetWALFileNameForCleanup()

static void SetWALFileNameForCleanup ( void  )
static

Definition at line 175 of file pg_archivecleanup.c.

176 {
177  bool fnameOK = false;
178 
180 
181  /*
182  * If restartWALFileName is a WAL file name then just use it directly. If
183  * restartWALFileName is a .partial or .backup filename, make sure we use
184  * the prefix of the filename, otherwise we will remove wrong files since
185  * 000000010000000000000010.partial and
186  * 000000010000000000000010.00000020.backup are after
187  * 000000010000000000000010.
188  */
190  {
192  fnameOK = true;
193  }
195  {
196  int args;
197  uint32 tli = 1,
198  log = 0,
199  seg = 0;
200 
201  args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
202  &tli, &log, &seg);
203  if (args == 3)
204  {
205  fnameOK = true;
206 
207  /*
208  * Use just the prefix of the filename, ignore everything after
209  * first period
210  */
212  }
213  }
215  {
216  int args;
217  uint32 tli = 1,
218  log = 0,
219  seg = 0,
220  offset = 0;
221 
222  args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
223  if (args == 4)
224  {
225  fnameOK = true;
226 
227  /*
228  * Use just the prefix of the filename, ignore everything after
229  * first period
230  */
232  }
233  }
234 
235  if (!fnameOK)
236  {
237  pg_log_error("invalid file name argument");
238  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
239  exit(2);
240  }
241 }
unsigned int uint32
Definition: c.h:490
static bool IsBackupHistoryFileName(const char *fname)
static void XLogFileNameById(char *fname, TimeLineID tli, uint32 log, uint32 seg)

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

Referenced by main().

◆ TrimExtension()

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

Definition at line 73 of file pg_archivecleanup.c.

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:119

References filename.

Referenced by CleanupPriorWALFiles(), and SetWALFileNameForCleanup().

◆ usage()

static void usage ( void  )
static

Definition at line 249 of file pg_archivecleanup.c.

250 {
251  printf(_("%s removes older WAL files from PostgreSQL archives.\n\n"), progname);
252  printf(_("Usage:\n"));
253  printf(_(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
254  printf(_("\nOptions:\n"));
255  printf(_(" -d generate debug output (verbose mode)\n"));
256  printf(_(" -n dry run, show the names of the files that would be removed\n"));
257  printf(_(" -V, --version output version information, then exit\n"));
258  printf(_(" -x EXT clean up files if they have this extension\n"));
259  printf(_(" -?, --help show this help, then exit\n"));
260  printf(_("\n"
261  "For use as archive_cleanup_command in postgresql.conf:\n"
262  " archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
263  "e.g.\n"
264  " archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n"));
265  printf(_("\n"
266  "Or for use as a standalone archive cleaner:\n"
267  "e.g.\n"
268  " pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n"));
269  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
270  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
271 }
#define _(x)
Definition: elog.c:91

References _, printf, and progname.

Referenced by main().

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().