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(), walfile, and WALFilePath.

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  break;
155  }
156  }
157  }
158 
159  if (errno)
160  pg_log_error("could not read archive location \"%s\": %m",
162  if (closedir(xldir))
163  pg_log_error("could not close archive location \"%s\": %m",
165  }
166  else
167  pg_log_error("could not open archive location \"%s\": %m",
169 }
#define pg_log_error(...)
Definition: logging.h:79
int closedir(DIR *)
Definition: dirent.c:113
#define printf(...)
Definition: port.h:198
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:91
#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
char WALFilePath[MAXPGPATH *2]
Definition: pg_standby.c:60
struct dirent * readdir(DIR *)
Definition: dirent.c:77
#define IsPartialXLogFileName(fname)
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:192
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, 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:79
char * archiveLocation
struct stat stat_buf
Definition: pg_standby.c:100
#define stat(a, b)
Definition: win32_port.h:255
#define S_ISDIR(m)
Definition: win32_port.h:296

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 277 of file pg_archivecleanup.c.

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

278 {
279  int c;
280 
281  pg_logging_init(argv[0]);
282  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
283  progname = get_progname(argv[0]);
284 
285  if (argc > 1)
286  {
287  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
288  {
289  usage();
290  exit(0);
291  }
292  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
293  {
294  puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
295  exit(0);
296  }
297  }
298 
299  while ((c = getopt(argc, argv, "x:dn")) != -1)
300  {
301  switch (c)
302  {
303  case 'd': /* Debug mode */
305  break;
306  case 'n': /* Dry-Run mode */
307  dryrun = true;
308  break;
309  case 'x':
310  additional_ext = pg_strdup(optarg); /* Extension to remove
311  * from xlogfile names */
312  break;
313  default:
314  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
315  exit(2);
316  break;
317  }
318  }
319 
320  /*
321  * We will go to the archiveLocation to check restartWALFileName.
322  * restartWALFileName may not exist anymore, which would not be an error,
323  * so we separate the archiveLocation and restartWALFileName so we can
324  * check separately whether archiveLocation exists, if not that is an
325  * error
326  */
327  if (optind < argc)
328  {
329  archiveLocation = argv[optind];
330  optind++;
331  }
332  else
333  {
334  pg_log_error("must specify archive location");
335  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
336  exit(2);
337  }
338 
339  if (optind < argc)
340  {
341  restartWALFileName = argv[optind];
342  optind++;
343  }
344  else
345  {
346  pg_log_error("must specify oldest kept WAL file");
347  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
348  exit(2);
349  }
350 
351  if (optind < argc)
352  {
353  pg_log_error("too many command-line arguments");
354  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
355  exit(2);
356  }
357 
358  /*
359  * Check archive exists and other initialization if required.
360  */
361  Initialize();
362 
363  /*
364  * Check filename is a valid name, then process to find cut-off
365  */
367 
368  pg_log_debug("keeping WAL file \"%s/%s\" and later",
370 
371  /*
372  * Remove WAL files older than cut-off
373  */
375 
376  exit(0);
377 }
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:79
void pg_logging_init(const char *argv0)
Definition: logging.c:39
char * archiveLocation
#define fprintf
Definition: port.h:196
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:91
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:1166
char exclusiveCleanupFileName[MAXFNAMELEN]
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
char * optarg
Definition: getopt.c:52
void pg_logging_set_level(enum pg_log_level new_level)
Definition: logging.c:108
static void Initialize(void)
#define _(x)
Definition: elog.c:87
char * additional_ext

◆ SetWALFileNameForCleanup()

static void SetWALFileNameForCleanup ( void  )
static

Definition at line 178 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().

179 {
180  bool fnameOK = false;
181 
183 
184  /*
185  * If restartWALFileName is a WAL file name then just use it directly. If
186  * restartWALFileName is a .partial or .backup filename, make sure we use
187  * the prefix of the filename, otherwise we will remove wrong files since
188  * 000000010000000000000010.partial and
189  * 000000010000000000000010.00000020.backup are after
190  * 000000010000000000000010.
191  */
193  {
195  fnameOK = true;
196  }
198  {
199  int args;
200  uint32 tli = 1,
201  log = 0,
202  seg = 0;
203 
204  args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
205  &tli, &log, &seg);
206  if (args == 3)
207  {
208  fnameOK = true;
209 
210  /*
211  * Use just the prefix of the filename, ignore everything after
212  * first period
213  */
215  }
216  }
218  {
219  int args;
220  uint32 tli = 1,
221  log = 0,
222  seg = 0,
223  offset = 0;
224 
225  args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
226  if (args == 4)
227  {
228  fnameOK = true;
229 
230  /*
231  * Use just the prefix of the filename, ignore everything after
232  * first period
233  */
235  }
236  }
237 
238  if (!fnameOK)
239  {
240  pg_log_error("invalid file name argument");
241  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
242  exit(2);
243  }
244 }
#define pg_log_error(...)
Definition: logging.h:79
#define XLogFileNameById(fname, tli, log, seg)
#define fprintf
Definition: port.h:196
static void TrimExtension(char *filename, char *extension)
#define IsXLogFileName(fname)
char * restartWALFileName
unsigned int uint32
Definition: c.h:359
#define IsBackupHistoryFileName(fname)
const char * progname
char exclusiveCleanupFileName[MAXFNAMELEN]
#define IsPartialXLogFileName(fname)
#define _(x)
Definition: elog.c:87
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:90

◆ usage()

static void usage ( void  )
static

Definition at line 252 of file pg_archivecleanup.c.

References _, printf, and progname.

Referenced by main().

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

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