2010-06-14 18:19:24 +02:00
|
|
|
/*
|
|
|
|
* pg_archivecleanup.c
|
|
|
|
*
|
2018-11-19 08:57:03 +01:00
|
|
|
* To be used as archive_cleanup_command to clean an archive when using
|
|
|
|
* standby mode.
|
2010-06-14 18:19:24 +02:00
|
|
|
*
|
2018-11-19 08:57:03 +01:00
|
|
|
* src/bin/pg_archivecleanup/pg_archivecleanup.c
|
2010-06-14 18:19:24 +02:00
|
|
|
*/
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2015-07-02 03:35:38 +02:00
|
|
|
#include "access/xlog_internal.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "common/logging.h"
|
|
|
|
#include "pg_getopt.h"
|
2015-07-02 03:35:38 +02:00
|
|
|
|
2010-06-14 18:19:24 +02:00
|
|
|
const char *progname;
|
|
|
|
|
|
|
|
/* Options and defaults */
|
2012-02-01 17:56:59 +01:00
|
|
|
bool dryrun = false; /* are we performing a dry-run operation? */
|
2012-04-05 20:18:42 +02:00
|
|
|
char *additional_ext = NULL; /* Extension to remove from filenames */
|
2010-06-14 18:19:24 +02:00
|
|
|
|
|
|
|
char *archiveLocation; /* where to find the archive? */
|
|
|
|
char *restartWALFileName; /* the file from which we can restart restore */
|
2015-07-02 03:35:38 +02:00
|
|
|
char exclusiveCleanupFileName[MAXFNAMELEN]; /* the oldest file we want
|
2010-06-14 18:19:24 +02:00
|
|
|
* to remain in archive */
|
|
|
|
|
|
|
|
|
|
|
|
/* =====================================================================
|
|
|
|
*
|
|
|
|
* Customizable section
|
|
|
|
*
|
|
|
|
* =====================================================================
|
|
|
|
*
|
|
|
|
* Currently, this section assumes that the Archive is a locally
|
|
|
|
* accessible directory. If you want to make other assumptions,
|
|
|
|
* such as using a vendor-specific archive and access API, these
|
|
|
|
* routines are the ones you'll need to change. You're
|
2019-01-19 19:06:35 +01:00
|
|
|
* encouraged to submit any changes to pgsql-hackers@lists.postgresql.org
|
2010-06-14 18:19:24 +02:00
|
|
|
* or personally to the current maintainer. Those changes may be
|
|
|
|
* folded in to later versions of this program.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize allows customized commands into the archive cleanup program.
|
|
|
|
*
|
|
|
|
* You may wish to add code to check for tape libraries, etc..
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
Initialize(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This code assumes that archiveLocation is a directory, so we use stat
|
|
|
|
* to test if it's accessible.
|
|
|
|
*/
|
2010-06-17 19:31:27 +02:00
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
if (stat(archiveLocation, &stat_buf) != 0 ||
|
|
|
|
!S_ISDIR(stat_buf.st_mode))
|
2010-06-14 18:19:24 +02:00
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("archive location \"%s\" does not exist",
|
|
|
|
archiveLocation);
|
2010-06-14 18:19:24 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-05 20:18:42 +02:00
|
|
|
static void
|
|
|
|
TrimExtension(char *filename, char *extension)
|
|
|
|
{
|
|
|
|
int flen;
|
|
|
|
int elen;
|
|
|
|
|
|
|
|
if (extension == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
elen = strlen(extension);
|
|
|
|
flen = strlen(filename);
|
|
|
|
|
|
|
|
if (flen > elen && strcmp(filename + flen - elen, extension) == 0)
|
|
|
|
filename[flen - elen] = '\0';
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:19:24 +02:00
|
|
|
static void
|
|
|
|
CleanupPriorWALFiles(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
DIR *xldir;
|
|
|
|
struct dirent *xlde;
|
2012-04-05 20:18:42 +02:00
|
|
|
char walfile[MAXPGPATH];
|
2010-06-14 18:19:24 +02:00
|
|
|
|
|
|
|
if ((xldir = opendir(archiveLocation)) != NULL)
|
|
|
|
{
|
2014-03-21 18:45:11 +01:00
|
|
|
while (errno = 0, (xlde = readdir(xldir)) != NULL)
|
2010-06-14 18:19:24 +02:00
|
|
|
{
|
2014-08-19 04:59:31 +02:00
|
|
|
/*
|
|
|
|
* Truncation is essentially harmless, because we skip names of
|
2015-07-02 03:35:38 +02:00
|
|
|
* length other than XLOG_FNAME_LEN. (In principle, one could use
|
2014-08-19 04:59:31 +02:00
|
|
|
* a 1000-character additional_ext and get trouble.)
|
|
|
|
*/
|
|
|
|
strlcpy(walfile, xlde->d_name, MAXPGPATH);
|
2012-04-05 20:18:42 +02:00
|
|
|
TrimExtension(walfile, additional_ext);
|
|
|
|
|
2010-06-14 18:19:24 +02:00
|
|
|
/*
|
|
|
|
* We ignore the timeline part of the XLOG segment identifiers in
|
|
|
|
* deciding whether a segment is still needed. This ensures that
|
|
|
|
* we won't prematurely remove a segment from a parent timeline.
|
|
|
|
* We could probably be a little more proactive about removing
|
|
|
|
* segments of non-parent timelines, but that would be a whole lot
|
|
|
|
* more complicated.
|
|
|
|
*
|
|
|
|
* We use the alphanumeric sorting property of the filenames to
|
|
|
|
* decide which ones are earlier than the exclusiveCleanupFileName
|
|
|
|
* file. Note that this means files are not removed in the order
|
|
|
|
* they were originally written, in case this worries you.
|
|
|
|
*/
|
2015-07-03 04:53:58 +02:00
|
|
|
if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&
|
2012-04-05 20:18:42 +02:00
|
|
|
strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
|
2010-06-14 18:19:24 +02:00
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
char WALFilePath[MAXPGPATH * 2]; /* the file path
|
|
|
|
* including archive */
|
|
|
|
|
2012-04-05 20:18:42 +02:00
|
|
|
/*
|
|
|
|
* Use the original file name again now, including any
|
|
|
|
* extension that might have been chopped off before testing
|
|
|
|
* the sequence.
|
|
|
|
*/
|
2017-04-11 20:13:31 +02:00
|
|
|
snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s",
|
2010-08-23 04:56:24 +02:00
|
|
|
archiveLocation, xlde->d_name);
|
2012-02-01 17:56:59 +01:00
|
|
|
|
|
|
|
if (dryrun)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Prints the name of the file to be removed and skips the
|
|
|
|
* actual removal. The regular printout is so that the
|
|
|
|
* user can pipe the output into some other program.
|
|
|
|
*/
|
|
|
|
printf("%s\n", WALFilePath);
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_debug("file \"%s\" would be removed", WALFilePath);
|
2012-02-01 17:56:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_debug("removing file \"%s\"", WALFilePath);
|
2010-06-14 18:19:24 +02:00
|
|
|
|
|
|
|
rc = unlink(WALFilePath);
|
|
|
|
if (rc != 0)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not remove file \"%s\": %m",
|
|
|
|
WALFilePath);
|
2010-06-14 18:19:24 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 18:45:11 +01:00
|
|
|
|
|
|
|
if (errno)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not read archive location \"%s\": %m",
|
|
|
|
archiveLocation);
|
2014-03-21 18:45:11 +01:00
|
|
|
if (closedir(xldir))
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not close archive location \"%s\": %m",
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
archiveLocation);
|
2021-07-29 04:42:58 +02:00
|
|
|
}
|
2022-04-08 20:55:14 +02:00
|
|
|
else
|
|
|
|
pg_fatal("could not open archive location \"%s\": %m",
|
|
|
|
archiveLocation);
|
2010-06-14 18:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SetWALFileNameForCleanup()
|
|
|
|
*
|
|
|
|
* Set the earliest WAL filename that we want to keep on the archive
|
2015-07-03 04:53:58 +02:00
|
|
|
* and decide whether we need cleanup
|
2010-06-14 18:19:24 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
SetWALFileNameForCleanup(void)
|
|
|
|
{
|
|
|
|
bool fnameOK = false;
|
|
|
|
|
2012-04-05 20:18:42 +02:00
|
|
|
TrimExtension(restartWALFileName, additional_ext);
|
|
|
|
|
2010-06-14 18:19:24 +02:00
|
|
|
/*
|
|
|
|
* If restartWALFileName is a WAL file name then just use it directly. If
|
2015-07-03 04:53:58 +02:00
|
|
|
* restartWALFileName is a .partial or .backup filename, make sure we use
|
|
|
|
* the prefix of the filename, otherwise we will remove wrong files since
|
|
|
|
* 000000010000000000000010.partial and
|
|
|
|
* 000000010000000000000010.00000020.backup are after
|
2010-06-14 18:19:24 +02:00
|
|
|
* 000000010000000000000010.
|
|
|
|
*/
|
2015-07-02 03:35:38 +02:00
|
|
|
if (IsXLogFileName(restartWALFileName))
|
2010-06-14 18:19:24 +02:00
|
|
|
{
|
|
|
|
strcpy(exclusiveCleanupFileName, restartWALFileName);
|
|
|
|
fnameOK = true;
|
|
|
|
}
|
2015-07-03 04:53:58 +02:00
|
|
|
else if (IsPartialXLogFileName(restartWALFileName))
|
|
|
|
{
|
|
|
|
int args;
|
|
|
|
uint32 tli = 1,
|
|
|
|
log = 0,
|
|
|
|
seg = 0;
|
|
|
|
|
|
|
|
args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
|
|
|
|
&tli, &log, &seg);
|
|
|
|
if (args == 3)
|
|
|
|
{
|
|
|
|
fnameOK = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use just the prefix of the filename, ignore everything after
|
|
|
|
* first period
|
|
|
|
*/
|
|
|
|
XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 03:35:38 +02:00
|
|
|
else if (IsBackupHistoryFileName(restartWALFileName))
|
2010-06-14 18:19:24 +02:00
|
|
|
{
|
|
|
|
int args;
|
|
|
|
uint32 tli = 1,
|
|
|
|
log = 0,
|
|
|
|
seg = 0,
|
|
|
|
offset = 0;
|
2010-07-06 21:19:02 +02:00
|
|
|
|
2010-06-14 18:19:24 +02:00
|
|
|
args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
|
|
|
|
if (args == 4)
|
|
|
|
{
|
|
|
|
fnameOK = true;
|
2010-07-06 21:19:02 +02:00
|
|
|
|
2010-06-14 18:19:24 +02:00
|
|
|
/*
|
|
|
|
* Use just the prefix of the filename, ignore everything after
|
|
|
|
* first period
|
|
|
|
*/
|
2015-07-02 03:35:38 +02:00
|
|
|
XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
|
2010-06-14 18:19:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fnameOK)
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("invalid file name argument");
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2010-06-14 18:19:24 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* =====================================================================
|
|
|
|
* End of Customizable section
|
|
|
|
* =====================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_("%s removes older WAL files from PostgreSQL archives.\n\n"), progname);
|
|
|
|
printf(_("Usage:\n"));
|
|
|
|
printf(_(" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
|
|
|
|
printf(_("\nOptions:\n"));
|
|
|
|
printf(_(" -d generate debug output (verbose mode)\n"));
|
|
|
|
printf(_(" -n dry run, show the names of the files that would be removed\n"));
|
|
|
|
printf(_(" -V, --version output version information, then exit\n"));
|
|
|
|
printf(_(" -x EXT clean up files if they have this extension\n"));
|
|
|
|
printf(_(" -?, --help show this help, then exit\n"));
|
|
|
|
printf(_("\n"
|
2018-11-25 16:31:16 +01:00
|
|
|
"For use as archive_cleanup_command in postgresql.conf:\n"
|
2016-10-14 18:00:00 +02:00
|
|
|
" archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
|
|
|
|
"e.g.\n"
|
|
|
|
" archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n"));
|
|
|
|
printf(_("\n"
|
|
|
|
"Or for use as a standalone archive cleaner:\n"
|
|
|
|
"e.g.\n"
|
|
|
|
" pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n"));
|
2020-02-28 08:54:49 +01:00
|
|
|
printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
2020-02-28 08:54:49 +01:00
|
|
|
printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
|
2010-06-14 18:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------ MAIN ----------------------------------------*/
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_logging_init(argv[0]);
|
2016-10-14 18:00:00 +02:00
|
|
|
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
|
2010-06-14 18:19:24 +02:00
|
|
|
progname = get_progname(argv[0]);
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
|
|
|
puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 14:33:41 +01:00
|
|
|
while ((c = getopt(argc, argv, "dnx:")) != -1)
|
2010-06-14 18:19:24 +02:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'd': /* Debug mode */
|
2020-09-17 18:52:18 +02:00
|
|
|
pg_logging_increase_verbosity();
|
2010-06-14 18:19:24 +02:00
|
|
|
break;
|
2012-02-01 17:56:59 +01:00
|
|
|
case 'n': /* Dry-Run mode */
|
|
|
|
dryrun = true;
|
|
|
|
break;
|
2012-04-05 20:18:42 +02:00
|
|
|
case 'x':
|
2016-08-31 00:22:43 +02:00
|
|
|
additional_ext = pg_strdup(optarg); /* Extension to remove
|
2012-04-05 20:18:42 +02:00
|
|
|
* from xlogfile names */
|
|
|
|
break;
|
2010-06-14 18:19:24 +02:00
|
|
|
default:
|
2022-04-08 20:55:14 +02:00
|
|
|
/* getopt already emitted a complaint */
|
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2010-06-14 18:19:24 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We will go to the archiveLocation to check restartWALFileName.
|
|
|
|
* restartWALFileName may not exist anymore, which would not be an error,
|
|
|
|
* so we separate the archiveLocation and restartWALFileName so we can
|
|
|
|
* check separately whether archiveLocation exists, if not that is an
|
|
|
|
* error
|
|
|
|
*/
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
archiveLocation = argv[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("must specify archive location");
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2010-06-14 18:19:24 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
restartWALFileName = argv[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("must specify oldest kept WAL file");
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2010-06-14 18:19:24 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("too many command-line arguments");
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2010-06-14 18:19:24 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check archive exists and other initialization if required.
|
|
|
|
*/
|
|
|
|
Initialize();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check filename is a valid name, then process to find cut-off
|
|
|
|
*/
|
|
|
|
SetWALFileNameForCleanup();
|
|
|
|
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_debug("keeping WAL file \"%s/%s\" and later",
|
2010-08-23 04:56:24 +02:00
|
|
|
archiveLocation, exclusiveCleanupFileName);
|
2010-06-14 18:19:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove WAL files older than cut-off
|
|
|
|
*/
|
|
|
|
CleanupPriorWALFiles();
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|