Skip to content

Commit b2e431a

Browse files
committed
Implement dry-run mode for pg_archivecleanup
In dry-run mode, just the name of the file to be removed is printed to stdout; this is so the user can easily plug it into another program through a pipe. If debug mode is also specified, a more verbose message is printed to stderr. Author: Gabriele Bartolini Reviewer: Josh Kupershmidt
1 parent 21238de commit b2e431a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

contrib/pg_archivecleanup/pg_archivecleanup.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const char *progname;
3636

3737
/* Options and defaults */
3838
bool debug = false; /* are we debugging? */
39+
bool dryrun = false; /* are we performing a dry-run operation? */
3940

4041
char *archiveLocation; /* where to find the archive? */
4142
char *restartWALFileName; /* the file from which we can restart restore */
@@ -119,6 +120,22 @@ CleanupPriorWALFiles(void)
119120
{
120121
snprintf(WALFilePath, MAXPGPATH, "%s/%s",
121122
archiveLocation, xlde->d_name);
123+
124+
if (dryrun)
125+
{
126+
/*
127+
* Prints the name of the file to be removed and skips the
128+
* actual removal. The regular printout is so that the
129+
* user can pipe the output into some other program.
130+
*/
131+
printf("%s\n", WALFilePath);
132+
if (debug)
133+
fprintf(stderr,
134+
"%s: file \"%s\" would be removed\n",
135+
progname, WALFilePath);
136+
continue;
137+
}
138+
122139
if (debug)
123140
fprintf(stderr, "%s: removing file \"%s\"\n",
124141
progname, WALFilePath);
@@ -205,6 +222,7 @@ usage(void)
205222
printf(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n", progname);
206223
printf("\nOptions:\n");
207224
printf(" -d generates debug output (verbose mode)\n");
225+
printf(" -n shows the names of the files that would have been removed (dry-run)\n");
208226
printf(" --help show this help, then exit\n");
209227
printf(" --version output version information, then exit\n");
210228
printf("\n"
@@ -241,13 +259,16 @@ main(int argc, char **argv)
241259
}
242260
}
243261

244-
while ((c = getopt(argc, argv, "d")) != -1)
262+
while ((c = getopt(argc, argv, "dn")) != -1)
245263
{
246264
switch (c)
247265
{
248266
case 'd': /* Debug mode */
249267
debug = true;
250268
break;
269+
case 'n': /* Dry-Run mode */
270+
dryrun = true;
271+
break;
251272
default:
252273
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
253274
exit(2);

doc/src/sgml/pgarchivecleanup.sgml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ pg_archivecleanup: removing file "archive/00000001000000370000000E"
9898
</listitem>
9999
</varlistentry>
100100

101+
<varlistentry>
102+
<term><option>-n</option></term>
103+
<listitem>
104+
<para>
105+
Print the names of the files that would have been removed on <filename>stdout</> (performs a dry run).
106+
</para>
107+
</listitem>
108+
</varlistentry>
109+
101110
</variablelist>
102111
</para>
103112

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy