2015-03-23 18:47:52 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_rewind.c
|
2015-12-01 16:56:44 +01:00
|
|
|
* Synchronizes a PostgreSQL data directory to a new timeline
|
2015-03-23 18:47:52 +01:00
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2015-03-23 18:47:52 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "access/timeline.h"
|
|
|
|
#include "access/xlog_internal.h"
|
|
|
|
#include "catalog/catversion.h"
|
|
|
|
#include "catalog/pg_control.h"
|
2019-03-12 02:03:33 +01:00
|
|
|
#include "common/controldata_utils.h"
|
2018-04-07 23:45:39 +02:00
|
|
|
#include "common/file_perm.h"
|
2018-07-10 01:39:27 +02:00
|
|
|
#include "common/file_utils.h"
|
2015-04-07 22:04:25 +02:00
|
|
|
#include "common/restricted_token.h"
|
2019-09-30 17:57:35 +02:00
|
|
|
#include "fe_utils/recovery_gen.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "fetch.h"
|
|
|
|
#include "file_ops.h"
|
|
|
|
#include "filemap.h"
|
2015-03-23 18:47:52 +01:00
|
|
|
#include "getopt_long.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "pg_rewind.h"
|
2015-03-23 18:47:52 +01:00
|
|
|
#include "storage/bufpage.h"
|
|
|
|
|
|
|
|
static void usage(const char *progname);
|
|
|
|
|
|
|
|
static void createBackupLabel(XLogRecPtr startpoint, TimeLineID starttli,
|
2019-05-22 19:04:48 +02:00
|
|
|
XLogRecPtr checkpointloc);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
static void digestControlFile(ControlFileData *ControlFile, char *source,
|
2019-05-22 19:04:48 +02:00
|
|
|
size_t size);
|
2019-02-26 08:08:24 +01:00
|
|
|
static void syncTargetDirectory(void);
|
2015-03-23 18:47:52 +01:00
|
|
|
static void sanityChecks(void);
|
2015-12-01 16:56:44 +01:00
|
|
|
static void findCommonAncestorTimeline(XLogRecPtr *recptr, int *tliIndex);
|
2019-09-27 21:40:01 +02:00
|
|
|
static void ensureCleanShutdown(const char *argv0);
|
2019-09-30 17:57:35 +02:00
|
|
|
static void disconnect_atexit(void);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
static ControlFileData ControlFile_target;
|
|
|
|
static ControlFileData ControlFile_source;
|
|
|
|
|
|
|
|
const char *progname;
|
Make WAL segment size configurable at initdb time.
For performance reasons a larger segment size than the default 16MB
can be useful. A larger segment size has two main benefits: Firstly,
in setups using archiving, it makes it easier to write scripts that
can keep up with higher amounts of WAL, secondly, the WAL has to be
written and synced to disk less frequently.
But at the same time large segment size are disadvantageous for
smaller databases. So far the segment size had to be configured at
compile time, often making it unrealistic to choose one fitting to a
particularly load. Therefore change it to a initdb time setting.
This includes a breaking changes to the xlogreader.h API, which now
requires the current segment size to be configured. For that and
similar reasons a number of binaries had to be taught how to recognize
the current segment size.
Author: Beena Emerson, editorialized by Andres Freund
Reviewed-By: Andres Freund, David Steele, Kuntal Ghosh, Michael
Paquier, Peter Eisentraut, Robert Hass, Tushar Ahuja
Discussion: https://postgr.es/m/CAOG9ApEAcQ--1ieKbhFzXSQPw_YLmepaa4hNdnY5+ZULpt81Mw@mail.gmail.com
2017-09-20 07:03:48 +02:00
|
|
|
int WalSegSz;
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/* Configuration options */
|
|
|
|
char *datadir_target = NULL;
|
|
|
|
char *datadir_source = NULL;
|
|
|
|
char *connstr_source = NULL;
|
|
|
|
|
2019-05-22 18:55:34 +02:00
|
|
|
static bool debug = false;
|
2015-03-23 18:47:52 +01:00
|
|
|
bool showprogress = false;
|
|
|
|
bool dry_run = false;
|
2018-07-10 01:51:10 +02:00
|
|
|
bool do_sync = true;
|
2015-03-23 18:47:52 +01:00
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
/* Target history */
|
|
|
|
TimeLineHistoryEntry *targetHistory;
|
2016-06-10 00:02:36 +02:00
|
|
|
int targetNentries;
|
2015-12-01 16:56:44 +01:00
|
|
|
|
2019-05-14 19:11:23 +02:00
|
|
|
/* Progress counters */
|
|
|
|
uint64 fetch_size;
|
|
|
|
uint64 fetch_done;
|
|
|
|
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
static void
|
|
|
|
usage(const char *progname)
|
|
|
|
{
|
2015-06-23 02:40:01 +02:00
|
|
|
printf(_("%s resynchronizes a PostgreSQL cluster with another copy of the cluster.\n\n"), progname);
|
2015-03-23 18:47:52 +01:00
|
|
|
printf(_("Usage:\n %s [OPTION]...\n\n"), progname);
|
|
|
|
printf(_("Options:\n"));
|
2015-06-23 02:40:01 +02:00
|
|
|
printf(_(" -D, --target-pgdata=DIRECTORY existing data directory to modify\n"));
|
2015-09-16 06:37:39 +02:00
|
|
|
printf(_(" --source-pgdata=DIRECTORY source data directory to synchronize with\n"));
|
|
|
|
printf(_(" --source-server=CONNSTR source server to synchronize with\n"));
|
2019-09-30 17:57:35 +02:00
|
|
|
printf(_(" -R, --write-recovery-conf write configuration for replication\n"
|
|
|
|
" (requires --source-server)\n"));
|
2015-06-23 02:40:01 +02:00
|
|
|
printf(_(" -n, --dry-run stop before modifying anything\n"));
|
2019-04-29 16:05:07 +02:00
|
|
|
printf(_(" -N, --no-sync do not wait for changes to be written\n"
|
|
|
|
" safely to disk\n"));
|
2015-06-23 02:40:01 +02:00
|
|
|
printf(_(" -P, --progress write progress messages\n"));
|
2019-09-27 21:40:01 +02:00
|
|
|
printf(_(" --no-ensure-shutdown do not automatically fix unclean shutdown\n"));
|
2015-06-23 02:40:01 +02:00
|
|
|
printf(_(" --debug write a lot of debug messages\n"));
|
|
|
|
printf(_(" -V, --version output version information, then exit\n"));
|
|
|
|
printf(_(" -?, --help show this help, then exit\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);
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", no_argument, NULL, '?'},
|
|
|
|
{"target-pgdata", required_argument, NULL, 'D'},
|
2019-09-30 17:57:35 +02:00
|
|
|
{"write-recovery-conf", no_argument, NULL, 'R'},
|
2015-03-23 18:47:52 +01:00
|
|
|
{"source-pgdata", required_argument, NULL, 1},
|
|
|
|
{"source-server", required_argument, NULL, 2},
|
2019-10-04 09:18:29 +02:00
|
|
|
{"no-ensure-shutdown", no_argument, NULL, 4},
|
2015-03-23 18:47:52 +01:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"dry-run", no_argument, NULL, 'n'},
|
2018-07-10 01:51:10 +02:00
|
|
|
{"no-sync", no_argument, NULL, 'N'},
|
2015-03-23 18:47:52 +01:00
|
|
|
{"progress", no_argument, NULL, 'P'},
|
|
|
|
{"debug", no_argument, NULL, 3},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
int option_index;
|
|
|
|
int c;
|
|
|
|
XLogRecPtr divergerec;
|
2015-12-01 16:56:44 +01:00
|
|
|
int lastcommontliIndex;
|
2015-03-23 18:47:52 +01:00
|
|
|
XLogRecPtr chkptrec;
|
|
|
|
TimeLineID chkpttli;
|
|
|
|
XLogRecPtr chkptredo;
|
|
|
|
size_t size;
|
|
|
|
char *buffer;
|
2019-09-27 21:40:01 +02:00
|
|
|
bool no_ensure_shutdown = false;
|
2015-03-23 18:47:52 +01:00
|
|
|
bool rewind_needed;
|
|
|
|
XLogRecPtr endrec;
|
|
|
|
TimeLineID endtli;
|
|
|
|
ControlFileData ControlFile_new;
|
2019-09-30 17:57:35 +02:00
|
|
|
bool writerecoveryconf = false;
|
2015-03-23 18:47:52 +01: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_logging_init(argv[0]);
|
2015-04-07 22:04:25 +02:00
|
|
|
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_rewind"));
|
2015-03-23 18:47:52 +01:00
|
|
|
progname = get_progname(argv[0]);
|
|
|
|
|
|
|
|
/* Process command-line arguments */
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
|
|
|
usage(progname);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
|
|
|
puts("pg_rewind (PostgreSQL) " PG_VERSION);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 17:57:35 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "D:nNPR", long_options, &option_index)) != -1)
|
2015-03-23 18:47:52 +01:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '?':
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
showprogress = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
dry_run = true;
|
|
|
|
break;
|
|
|
|
|
2018-07-10 01:51:10 +02:00
|
|
|
case 'N':
|
|
|
|
do_sync = false;
|
|
|
|
break;
|
|
|
|
|
2019-09-30 17:57:35 +02:00
|
|
|
case 'R':
|
|
|
|
writerecoveryconf = true;
|
|
|
|
break;
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
case 3:
|
|
|
|
debug = true;
|
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_set_level(PG_LOG_DEBUG);
|
2015-03-23 18:47:52 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D': /* -D or --target-pgdata */
|
|
|
|
datadir_target = pg_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* --source-pgdata */
|
|
|
|
datadir_source = pg_strdup(optarg);
|
|
|
|
break;
|
2019-10-04 09:18:29 +02:00
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
case 2: /* --source-server */
|
|
|
|
connstr_source = pg_strdup(optarg);
|
|
|
|
break;
|
2019-10-04 09:18:29 +02:00
|
|
|
|
2019-09-27 21:40:01 +02:00
|
|
|
case 4:
|
|
|
|
no_ensure_shutdown = true;
|
|
|
|
break;
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (datadir_source == NULL && connstr_source == NULL)
|
|
|
|
{
|
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("no source specified (--source-pgdata or --source-server)");
|
2015-04-07 22:28:28 +02:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
2015-03-23 18:47:52 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-10-07 13:35:17 +02:00
|
|
|
if (datadir_source != NULL && connstr_source != NULL)
|
|
|
|
{
|
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("only one of --source-pgdata or --source-server can be specified");
|
2016-10-07 13:35:17 +02:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
if (datadir_target == NULL)
|
|
|
|
{
|
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("no target data directory specified (--target-pgdata)");
|
2015-03-23 18:47:52 +01:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-09-30 17:57:35 +02:00
|
|
|
if (writerecoveryconf && connstr_source == NULL)
|
|
|
|
{
|
|
|
|
pg_log_error("no source server information (--source--server) specified for --write-recovery-conf");
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-06-23 02:40:01 +02:00
|
|
|
if (optind < argc)
|
2015-03-23 18:47:52 +01: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("too many command-line arguments (first is \"%s\")",
|
|
|
|
argv[optind]);
|
2015-03-23 18:47:52 +01:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:04:25 +02:00
|
|
|
/*
|
|
|
|
* Don't allow pg_rewind to be run as root, to avoid overwriting the
|
|
|
|
* ownership of files in the data directory. We need only check for root
|
|
|
|
* -- any other user won't have sufficient permissions to modify files in
|
|
|
|
* the data directory.
|
|
|
|
*/
|
|
|
|
#ifndef WIN32
|
|
|
|
if (geteuid() == 0)
|
2015-06-23 02:40:01 +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("cannot be executed by \"root\"");
|
2015-06-23 02:40:01 +02:00
|
|
|
fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
|
|
|
|
progname);
|
2018-04-09 21:33:33 +02:00
|
|
|
exit(1);
|
2015-06-23 02:40:01 +02:00
|
|
|
}
|
2015-04-07 22:04:25 +02:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
get_restricted_token();
|
2015-04-07 22:04:25 +02:00
|
|
|
|
2018-05-23 16:59:55 +02:00
|
|
|
/* Set mask based on PGDATA permissions */
|
|
|
|
if (!GetDataDirectoryCreatePerm(datadir_target))
|
|
|
|
{
|
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("could not read permissions of directory \"%s\": %m",
|
|
|
|
datadir_target);
|
2018-05-23 16:59:55 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
umask(pg_mode_mask);
|
|
|
|
|
2019-09-30 17:57:35 +02:00
|
|
|
atexit(disconnect_atexit);
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
/* Connect to remote server */
|
|
|
|
if (connstr_source)
|
|
|
|
libpqConnect(connstr_source);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we have all the options and we're ready to start. Read in all the
|
|
|
|
* information we need from both clusters.
|
|
|
|
*/
|
|
|
|
buffer = slurpFile(datadir_target, "global/pg_control", &size);
|
|
|
|
digestControlFile(&ControlFile_target, buffer, size);
|
|
|
|
pg_free(buffer);
|
|
|
|
|
2019-09-27 21:40:01 +02:00
|
|
|
/*
|
2019-10-07 02:07:22 +02:00
|
|
|
* If the target instance was not cleanly shut down, start and stop the
|
|
|
|
* target cluster once in single-user mode to enforce recovery to finish,
|
|
|
|
* ensuring that the cluster can be used by pg_rewind. Note that if
|
|
|
|
* no_ensure_shutdown is specified, pg_rewind ignores this step, and users
|
|
|
|
* need to make sure by themselves that the target cluster is in a clean
|
|
|
|
* state.
|
2019-09-27 21:40:01 +02:00
|
|
|
*/
|
|
|
|
if (!no_ensure_shutdown &&
|
|
|
|
ControlFile_target.state != DB_SHUTDOWNED &&
|
|
|
|
ControlFile_target.state != DB_SHUTDOWNED_IN_RECOVERY)
|
|
|
|
{
|
|
|
|
ensureCleanShutdown(argv[0]);
|
|
|
|
|
|
|
|
buffer = slurpFile(datadir_target, "global/pg_control", &size);
|
|
|
|
digestControlFile(&ControlFile_target, buffer, size);
|
|
|
|
pg_free(buffer);
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
buffer = fetchFile("global/pg_control", &size);
|
|
|
|
digestControlFile(&ControlFile_source, buffer, size);
|
|
|
|
pg_free(buffer);
|
|
|
|
|
|
|
|
sanityChecks();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If both clusters are already on the same timeline, there's nothing to
|
|
|
|
* do.
|
|
|
|
*/
|
|
|
|
if (ControlFile_target.checkPointCopy.ThisTimeLineID == ControlFile_source.checkPointCopy.ThisTimeLineID)
|
|
|
|
{
|
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_info("source and target cluster are on the same timeline");
|
2015-12-04 04:21:16 +01:00
|
|
|
rewind_needed = false;
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-04 04:21:16 +01:00
|
|
|
findCommonAncestorTimeline(&divergerec, &lastcommontliIndex);
|
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_info("servers diverged at WAL location %X/%X on timeline %u",
|
2019-05-22 18:55:34 +02:00
|
|
|
(uint32) (divergerec >> 32), (uint32) divergerec,
|
|
|
|
targetHistory[lastcommontliIndex].tli);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/*
|
2016-06-10 00:02:36 +02:00
|
|
|
* Check for the possibility that the target is in fact a direct
|
|
|
|
* ancestor of the source. In that case, there is no divergent history
|
|
|
|
* in the target that needs rewinding.
|
2015-03-23 18:47:52 +01:00
|
|
|
*/
|
2015-12-04 04:21:16 +01:00
|
|
|
if (ControlFile_target.checkPoint >= divergerec)
|
|
|
|
{
|
2015-03-23 18:47:52 +01:00
|
|
|
rewind_needed = true;
|
2015-12-04 04:21:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
XLogRecPtr chkptendrec;
|
|
|
|
|
|
|
|
/* Read the checkpoint record on the target to see where it ends. */
|
|
|
|
chkptendrec = readOneRecord(datadir_target,
|
|
|
|
ControlFile_target.checkPoint,
|
|
|
|
targetNentries - 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the histories diverged exactly at the end of the shutdown
|
2016-06-10 00:02:36 +02:00
|
|
|
* checkpoint record on the target, there are no WAL records in
|
|
|
|
* the target that don't belong in the source's history, and no
|
|
|
|
* rewind is needed.
|
2015-12-04 04:21:16 +01:00
|
|
|
*/
|
|
|
|
if (chkptendrec == divergerec)
|
|
|
|
rewind_needed = false;
|
|
|
|
else
|
|
|
|
rewind_needed = true;
|
|
|
|
}
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rewind_needed)
|
|
|
|
{
|
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_info("no rewind required");
|
2019-10-04 09:18:29 +02:00
|
|
|
if (writerecoveryconf && !dry_run)
|
2019-09-30 17:57:35 +02:00
|
|
|
WriteRecoveryConfig(conn, datadir_target,
|
|
|
|
GenerateRecoveryConfig(conn, NULL));
|
2015-03-23 18:47:52 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
findLastCheckpoint(datadir_target, divergerec,
|
|
|
|
lastcommontliIndex,
|
2015-03-23 18:47:52 +01:00
|
|
|
&chkptrec, &chkpttli, &chkptredo);
|
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_info("rewinding from last common checkpoint at %X/%X on timeline %u",
|
2019-05-22 18:55:34 +02:00
|
|
|
(uint32) (chkptrec >> 32), (uint32) chkptrec,
|
|
|
|
chkpttli);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/*
|
2015-04-15 21:52:00 +02:00
|
|
|
* Build the filemap, by comparing the source and target data directories.
|
2015-03-23 18:47:52 +01:00
|
|
|
*/
|
2015-03-30 02:02:14 +02:00
|
|
|
filemap_create();
|
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
|
|
|
if (showprogress)
|
|
|
|
pg_log_info("reading source file list");
|
2015-04-15 21:52:00 +02:00
|
|
|
fetchSourceFileList();
|
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
|
|
|
if (showprogress)
|
|
|
|
pg_log_info("reading target file list");
|
2015-04-15 21:52:00 +02:00
|
|
|
traverse_datadir(datadir_target, &process_target_file);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the target WAL from last checkpoint before the point of fork, to
|
|
|
|
* extract all the pages that were modified on the target cluster after
|
|
|
|
* the fork. We can stop reading after reaching the final shutdown record.
|
|
|
|
* XXX: If we supported rewinding a server that was not shut down cleanly,
|
|
|
|
* we would need to replay until the end of WAL here.
|
|
|
|
*/
|
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
|
|
|
if (showprogress)
|
|
|
|
pg_log_info("reading WAL in target");
|
2015-12-01 16:56:44 +01:00
|
|
|
extractPageMap(datadir_target, chkptrec, lastcommontliIndex,
|
2015-03-23 18:47:52 +01:00
|
|
|
ControlFile_target.checkPoint);
|
|
|
|
filemap_finalize();
|
|
|
|
|
|
|
|
if (showprogress)
|
|
|
|
calculate_totals();
|
|
|
|
|
|
|
|
/* this is too verbose even for verbose mode */
|
|
|
|
if (debug)
|
|
|
|
print_filemap();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we're ready to start copying things over.
|
|
|
|
*/
|
|
|
|
if (showprogress)
|
|
|
|
{
|
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_info("need to copy %lu MB (total source directory size is %lu MB)",
|
2019-05-22 18:55:34 +02:00
|
|
|
(unsigned long) (filemap->fetch_size / (1024 * 1024)),
|
|
|
|
(unsigned long) (filemap->total_size / (1024 * 1024)));
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
fetch_size = filemap->fetch_size;
|
|
|
|
fetch_done = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the point of no return. Once we start copying things, we have
|
|
|
|
* modified the target directory and there is no turning back!
|
|
|
|
*/
|
|
|
|
|
|
|
|
executeFileMap();
|
|
|
|
|
|
|
|
progress_report(true);
|
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
|
|
|
printf("\n");
|
2015-03-23 18:47:52 +01: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
|
|
|
if (showprogress)
|
|
|
|
pg_log_info("creating backup label and updating control file");
|
2015-03-23 18:47:52 +01:00
|
|
|
createBackupLabel(chkptredo, chkpttli, chkptrec);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update control file of target. Make it ready to perform archive
|
|
|
|
* recovery when restarting.
|
|
|
|
*
|
|
|
|
* minRecoveryPoint is set to the current WAL insert location in the
|
|
|
|
* source server. Like in an online backup, it's important that we recover
|
|
|
|
* all the WAL that was generated while we copied the files over.
|
|
|
|
*/
|
|
|
|
memcpy(&ControlFile_new, &ControlFile_source, sizeof(ControlFileData));
|
|
|
|
|
|
|
|
if (connstr_source)
|
|
|
|
{
|
|
|
|
endrec = libpqGetCurrentXlogInsertLocation();
|
|
|
|
endtli = ControlFile_source.checkPointCopy.ThisTimeLineID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
endrec = ControlFile_source.checkPoint;
|
|
|
|
endtli = ControlFile_source.checkPointCopy.ThisTimeLineID;
|
|
|
|
}
|
|
|
|
ControlFile_new.minRecoveryPoint = endrec;
|
|
|
|
ControlFile_new.minRecoveryPointTLI = endtli;
|
|
|
|
ControlFile_new.state = DB_IN_ARCHIVE_RECOVERY;
|
2019-10-04 02:14:51 +02:00
|
|
|
if (!dry_run)
|
|
|
|
update_controlfile(datadir_target, &ControlFile_new, do_sync);
|
2015-03-23 18:47:52 +01: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
|
|
|
if (showprogress)
|
|
|
|
pg_log_info("syncing target data directory");
|
2019-02-26 08:08:24 +01:00
|
|
|
syncTargetDirectory();
|
2016-03-27 23:46:25 +02:00
|
|
|
|
2019-10-04 09:18:29 +02:00
|
|
|
if (writerecoveryconf && !dry_run)
|
2019-09-30 17:57:35 +02:00
|
|
|
WriteRecoveryConfig(conn, datadir_target,
|
|
|
|
GenerateRecoveryConfig(conn, NULL));
|
|
|
|
|
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_info("Done!");
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sanityChecks(void)
|
|
|
|
{
|
|
|
|
/* TODO Check that there's no backup_label in either cluster */
|
|
|
|
|
2019-08-13 06:53:41 +02:00
|
|
|
/* Check system_identifier match */
|
2015-03-23 18:47:52 +01:00
|
|
|
if (ControlFile_target.system_identifier != ControlFile_source.system_identifier)
|
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_fatal("source and target clusters are from different systems");
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/* check version */
|
|
|
|
if (ControlFile_target.pg_control_version != PG_CONTROL_VERSION ||
|
|
|
|
ControlFile_source.pg_control_version != PG_CONTROL_VERSION ||
|
|
|
|
ControlFile_target.catalog_version_no != CATALOG_VERSION_NO ||
|
|
|
|
ControlFile_source.catalog_version_no != CATALOG_VERSION_NO)
|
|
|
|
{
|
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_fatal("clusters are not compatible with this version of pg_rewind");
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Target cluster need to use checksums or hint bit wal-logging, this to
|
|
|
|
* prevent from data corruption that could occur because of hint bits.
|
|
|
|
*/
|
|
|
|
if (ControlFile_target.data_checksum_version != PG_DATA_CHECKSUM_VERSION &&
|
|
|
|
!ControlFile_target.wal_log_hints)
|
|
|
|
{
|
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_fatal("target server needs to use either data checksums or \"wal_log_hints = on\"");
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Target cluster better not be running. This doesn't guard against
|
|
|
|
* someone starting the cluster concurrently. Also, this is probably more
|
2015-12-01 16:56:44 +01:00
|
|
|
* strict than necessary; it's OK if the target node was not shut down
|
|
|
|
* cleanly, as long as it isn't running at the moment.
|
2015-03-23 18:47:52 +01:00
|
|
|
*/
|
2015-12-01 16:56:44 +01:00
|
|
|
if (ControlFile_target.state != DB_SHUTDOWNED &&
|
|
|
|
ControlFile_target.state != DB_SHUTDOWNED_IN_RECOVERY)
|
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_fatal("target server must be shut down cleanly");
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When the source is a data directory, also require that the source
|
|
|
|
* server is shut down. There isn't any very strong reason for this
|
|
|
|
* limitation, but better safe than sorry.
|
|
|
|
*/
|
2015-12-01 16:56:44 +01:00
|
|
|
if (datadir_source &&
|
|
|
|
ControlFile_source.state != DB_SHUTDOWNED &&
|
|
|
|
ControlFile_source.state != DB_SHUTDOWNED_IN_RECOVERY)
|
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_fatal("source data directory must be shut down cleanly");
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
2019-05-14 19:11:23 +02:00
|
|
|
/*
|
|
|
|
* Print a progress report based on the fetch_size and fetch_done variables.
|
|
|
|
*
|
|
|
|
* Progress report is written at maximum once per second, unless the
|
|
|
|
* force parameter is set to true.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
progress_report(bool force)
|
|
|
|
{
|
|
|
|
static pg_time_t last_progress_report = 0;
|
|
|
|
int percent;
|
|
|
|
char fetch_done_str[32];
|
|
|
|
char fetch_size_str[32];
|
|
|
|
pg_time_t now;
|
|
|
|
|
|
|
|
if (!showprogress)
|
|
|
|
return;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
if (now == last_progress_report && !force)
|
|
|
|
return; /* Max once per second */
|
|
|
|
|
|
|
|
last_progress_report = now;
|
|
|
|
percent = fetch_size ? (int) ((fetch_done) * 100 / fetch_size) : 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid overflowing past 100% or the full size. This may make the total
|
|
|
|
* size number change as we approach the end of the backup (the estimate
|
|
|
|
* will always be wrong if WAL is included), but that's better than having
|
|
|
|
* the done column be bigger than the total.
|
|
|
|
*/
|
|
|
|
if (percent > 100)
|
|
|
|
percent = 100;
|
|
|
|
if (fetch_done > fetch_size)
|
|
|
|
fetch_size = fetch_done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate step to keep platform-dependent format code out of
|
|
|
|
* translatable strings. And we only test for INT64_FORMAT availability
|
|
|
|
* in snprintf, not fprintf.
|
|
|
|
*/
|
|
|
|
snprintf(fetch_done_str, sizeof(fetch_done_str), INT64_FORMAT,
|
|
|
|
fetch_done / 1024);
|
|
|
|
snprintf(fetch_size_str, sizeof(fetch_size_str), INT64_FORMAT,
|
|
|
|
fetch_size / 1024);
|
|
|
|
|
|
|
|
fprintf(stderr, _("%*s/%s kB (%d%%) copied"),
|
2019-05-22 18:55:34 +02:00
|
|
|
(int) strlen(fetch_size_str), fetch_done_str, fetch_size_str,
|
|
|
|
percent);
|
2019-05-14 19:11:23 +02:00
|
|
|
if (isatty(fileno(stderr)))
|
|
|
|
fprintf(stderr, "\r");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
/*
|
2017-05-12 19:51:27 +02:00
|
|
|
* Find minimum from two WAL locations assuming InvalidXLogRecPtr means
|
2015-12-01 16:56:44 +01:00
|
|
|
* infinity as src/include/access/timeline.h states. This routine should
|
2017-05-12 19:51:27 +02:00
|
|
|
* be used only when comparing WAL locations related to history files.
|
2015-03-23 18:47:52 +01:00
|
|
|
*/
|
2015-12-01 16:56:44 +01:00
|
|
|
static XLogRecPtr
|
|
|
|
MinXLogRecPtr(XLogRecPtr a, XLogRecPtr b)
|
2015-03-23 18:47:52 +01:00
|
|
|
{
|
2015-12-01 16:56:44 +01:00
|
|
|
if (XLogRecPtrIsInvalid(a))
|
|
|
|
return b;
|
|
|
|
else if (XLogRecPtrIsInvalid(b))
|
|
|
|
return a;
|
|
|
|
else
|
|
|
|
return Min(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve timeline history for given control file which should behold
|
|
|
|
* either source or target.
|
|
|
|
*/
|
|
|
|
static TimeLineHistoryEntry *
|
|
|
|
getTimelineHistory(ControlFileData *controlFile, int *nentries)
|
|
|
|
{
|
2016-06-10 00:02:36 +02:00
|
|
|
TimeLineHistoryEntry *history;
|
|
|
|
TimeLineID tli;
|
2015-03-23 18:47:52 +01:00
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
tli = controlFile->checkPointCopy.ThisTimeLineID;
|
2015-03-23 18:47:52 +01:00
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
/*
|
2016-06-10 00:02:36 +02:00
|
|
|
* Timeline 1 does not have a history file, so there is no need to check
|
|
|
|
* and fake an entry with infinite start and end positions.
|
2015-12-01 16:56:44 +01:00
|
|
|
*/
|
|
|
|
if (tli == 1)
|
2015-03-23 18:47:52 +01:00
|
|
|
{
|
2015-12-01 16:56:44 +01:00
|
|
|
history = (TimeLineHistoryEntry *) pg_malloc(sizeof(TimeLineHistoryEntry));
|
|
|
|
history->tli = tli;
|
|
|
|
history->begin = history->end = InvalidXLogRecPtr;
|
|
|
|
*nentries = 1;
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char *histfile;
|
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
TLHistoryFilePath(path, tli);
|
|
|
|
|
|
|
|
/* Get history file from appropriate source */
|
|
|
|
if (controlFile == &ControlFile_source)
|
|
|
|
histfile = fetchFile(path, NULL);
|
|
|
|
else if (controlFile == &ControlFile_target)
|
|
|
|
histfile = slurpFile(datadir_target, path, NULL);
|
|
|
|
else
|
2019-07-03 00:18:43 +02:00
|
|
|
pg_fatal("invalid control file");
|
2015-03-23 18:47:52 +01:00
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
history = rewind_parseTimeLineHistory(histfile, tli, nentries);
|
2015-03-23 18:47:52 +01:00
|
|
|
pg_free(histfile);
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
if (debug)
|
2015-03-23 18:47:52 +01:00
|
|
|
{
|
2016-06-10 00:02:36 +02:00
|
|
|
int i;
|
2015-12-01 16:56:44 +01:00
|
|
|
|
|
|
|
if (controlFile == &ControlFile_source)
|
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("Source timeline history:");
|
2015-12-01 16:56:44 +01:00
|
|
|
else if (controlFile == &ControlFile_target)
|
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("Target timeline history:");
|
2015-12-01 16:56:44 +01:00
|
|
|
else
|
|
|
|
Assert(false);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
/*
|
|
|
|
* Print the target timeline history.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < targetNentries; i++)
|
2015-03-23 18:47:52 +01:00
|
|
|
{
|
2015-12-01 16:56:44 +01:00
|
|
|
TimeLineHistoryEntry *entry;
|
2015-03-23 18:47:52 +01:00
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
entry = &history[i];
|
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("%d: %X/%X - %X/%X", entry->tli,
|
2019-05-22 18:55:34 +02:00
|
|
|
(uint32) (entry->begin >> 32), (uint32) (entry->begin),
|
|
|
|
(uint32) (entry->end >> 32), (uint32) (entry->end));
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:56:44 +01:00
|
|
|
return history;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the TLI of the last common timeline in the timeline history of the
|
|
|
|
* two clusters. targetHistory is filled with target timeline history and
|
|
|
|
* targetNentries is number of items in targetHistory. *tliIndex is set to the
|
|
|
|
* index of last common timeline in targetHistory array, and *recptr is set to
|
|
|
|
* the position where the timeline history diverged (ie. the first WAL record
|
|
|
|
* that's not the same in both clusters).
|
|
|
|
*
|
|
|
|
* Control files of both clusters must be read into ControlFile_target/source
|
|
|
|
* before calling this routine.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
findCommonAncestorTimeline(XLogRecPtr *recptr, int *tliIndex)
|
|
|
|
{
|
|
|
|
TimeLineHistoryEntry *sourceHistory;
|
|
|
|
int sourceNentries;
|
2016-06-10 00:02:36 +02:00
|
|
|
int i,
|
|
|
|
n;
|
2015-12-01 16:56:44 +01:00
|
|
|
|
|
|
|
/* Retrieve timelines for both source and target */
|
|
|
|
sourceHistory = getTimelineHistory(&ControlFile_source, &sourceNentries);
|
|
|
|
targetHistory = getTimelineHistory(&ControlFile_target, &targetNentries);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trace the history forward, until we hit the timeline diverge. It may
|
|
|
|
* still be possible that the source and target nodes used the same
|
|
|
|
* timeline number in their history but with different start position
|
|
|
|
* depending on the history files that each node has fetched in previous
|
|
|
|
* recovery processes. Hence check the start position of the new timeline
|
|
|
|
* as well and move down by one extra timeline entry if they do not match.
|
|
|
|
*/
|
|
|
|
n = Min(sourceNentries, targetNentries);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if (sourceHistory[i].tli != targetHistory[i].tli ||
|
|
|
|
sourceHistory[i].begin != targetHistory[i].begin)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
*recptr = MinXLogRecPtr(sourceHistory[i].end, targetHistory[i].end);
|
|
|
|
*tliIndex = i;
|
|
|
|
|
|
|
|
pg_free(sourceHistory);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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_fatal("could not find common ancestor of the source and target cluster's timelines");
|
2015-12-01 16:56:44 +01:00
|
|
|
}
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a backup_label file that forces recovery to begin at the last common
|
|
|
|
* checkpoint.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
createBackupLabel(XLogRecPtr startpoint, TimeLineID starttli, XLogRecPtr checkpointloc)
|
|
|
|
{
|
|
|
|
XLogSegNo startsegno;
|
|
|
|
time_t stamp_time;
|
|
|
|
char strfbuf[128];
|
|
|
|
char xlogfilename[MAXFNAMELEN];
|
|
|
|
struct tm *tmp;
|
|
|
|
char buf[1000];
|
|
|
|
int len;
|
|
|
|
|
Make WAL segment size configurable at initdb time.
For performance reasons a larger segment size than the default 16MB
can be useful. A larger segment size has two main benefits: Firstly,
in setups using archiving, it makes it easier to write scripts that
can keep up with higher amounts of WAL, secondly, the WAL has to be
written and synced to disk less frequently.
But at the same time large segment size are disadvantageous for
smaller databases. So far the segment size had to be configured at
compile time, often making it unrealistic to choose one fitting to a
particularly load. Therefore change it to a initdb time setting.
This includes a breaking changes to the xlogreader.h API, which now
requires the current segment size to be configured. For that and
similar reasons a number of binaries had to be taught how to recognize
the current segment size.
Author: Beena Emerson, editorialized by Andres Freund
Reviewed-By: Andres Freund, David Steele, Kuntal Ghosh, Michael
Paquier, Peter Eisentraut, Robert Hass, Tushar Ahuja
Discussion: https://postgr.es/m/CAOG9ApEAcQ--1ieKbhFzXSQPw_YLmepaa4hNdnY5+ZULpt81Mw@mail.gmail.com
2017-09-20 07:03:48 +02:00
|
|
|
XLByteToSeg(startpoint, startsegno, WalSegSz);
|
|
|
|
XLogFileName(xlogfilename, starttli, startsegno, WalSegSz);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct backup label file
|
|
|
|
*/
|
|
|
|
stamp_time = time(NULL);
|
|
|
|
tmp = localtime(&stamp_time);
|
|
|
|
strftime(strfbuf, sizeof(strfbuf), "%Y-%m-%d %H:%M:%S %Z", tmp);
|
|
|
|
|
|
|
|
len = snprintf(buf, sizeof(buf),
|
|
|
|
"START WAL LOCATION: %X/%X (file %s)\n"
|
|
|
|
"CHECKPOINT LOCATION: %X/%X\n"
|
|
|
|
"BACKUP METHOD: pg_rewind\n"
|
|
|
|
"BACKUP FROM: standby\n"
|
|
|
|
"START TIME: %s\n",
|
2015-05-24 03:35:49 +02:00
|
|
|
/* omit LABEL: line */
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
(uint32) (startpoint >> 32), (uint32) startpoint, xlogfilename,
|
2015-03-23 18:47:52 +01:00
|
|
|
(uint32) (checkpointloc >> 32), (uint32) checkpointloc,
|
|
|
|
strfbuf);
|
|
|
|
if (len >= sizeof(buf))
|
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_fatal("backup label buffer too small"); /* shouldn't happen */
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/* TODO: move old file out of the way, if any. */
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
open_target_file("backup_label", true); /* BACKUP_LABEL_FILE */
|
2015-03-23 18:47:52 +01:00
|
|
|
write_target_range(buf, 0, len);
|
2016-03-27 22:48:31 +02:00
|
|
|
close_target_file();
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check CRC of control file
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
checkControlFile(ControlFileData *ControlFile)
|
|
|
|
{
|
2015-04-14 16:03:42 +02:00
|
|
|
pg_crc32c crc;
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
/* Calculate CRC */
|
|
|
|
INIT_CRC32C(crc);
|
|
|
|
COMP_CRC32C(crc, (char *) ControlFile, offsetof(ControlFileData, crc));
|
|
|
|
FIN_CRC32C(crc);
|
|
|
|
|
|
|
|
/* And simply compare it */
|
|
|
|
if (!EQ_CRC32C(crc, ControlFile->crc))
|
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_fatal("unexpected control file CRC");
|
2015-03-23 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify control file contents in the buffer src, and copy it to *ControlFile.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
digestControlFile(ControlFileData *ControlFile, char *src, size_t size)
|
|
|
|
{
|
2017-07-19 22:16:57 +02:00
|
|
|
if (size != PG_CONTROL_FILE_SIZE)
|
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_fatal("unexpected control file size %d, expected %d",
|
2017-07-19 22:16:57 +02:00
|
|
|
(int) size, PG_CONTROL_FILE_SIZE);
|
2015-03-23 18:47:52 +01:00
|
|
|
|
|
|
|
memcpy(ControlFile, src, sizeof(ControlFileData));
|
|
|
|
|
Make WAL segment size configurable at initdb time.
For performance reasons a larger segment size than the default 16MB
can be useful. A larger segment size has two main benefits: Firstly,
in setups using archiving, it makes it easier to write scripts that
can keep up with higher amounts of WAL, secondly, the WAL has to be
written and synced to disk less frequently.
But at the same time large segment size are disadvantageous for
smaller databases. So far the segment size had to be configured at
compile time, often making it unrealistic to choose one fitting to a
particularly load. Therefore change it to a initdb time setting.
This includes a breaking changes to the xlogreader.h API, which now
requires the current segment size to be configured. For that and
similar reasons a number of binaries had to be taught how to recognize
the current segment size.
Author: Beena Emerson, editorialized by Andres Freund
Reviewed-By: Andres Freund, David Steele, Kuntal Ghosh, Michael
Paquier, Peter Eisentraut, Robert Hass, Tushar Ahuja
Discussion: https://postgr.es/m/CAOG9ApEAcQ--1ieKbhFzXSQPw_YLmepaa4hNdnY5+ZULpt81Mw@mail.gmail.com
2017-09-20 07:03:48 +02:00
|
|
|
/* set and validate WalSegSz */
|
|
|
|
WalSegSz = ControlFile->xlog_seg_size;
|
|
|
|
|
|
|
|
if (!IsValidWalSegSize(WalSegSz))
|
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_fatal(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the control file specifies %d byte",
|
|
|
|
"WAL segment size must be a power of two between 1 MB and 1 GB, but the control file specifies %d bytes",
|
2018-05-18 05:05:27 +02:00
|
|
|
WalSegSz),
|
Make WAL segment size configurable at initdb time.
For performance reasons a larger segment size than the default 16MB
can be useful. A larger segment size has two main benefits: Firstly,
in setups using archiving, it makes it easier to write scripts that
can keep up with higher amounts of WAL, secondly, the WAL has to be
written and synced to disk less frequently.
But at the same time large segment size are disadvantageous for
smaller databases. So far the segment size had to be configured at
compile time, often making it unrealistic to choose one fitting to a
particularly load. Therefore change it to a initdb time setting.
This includes a breaking changes to the xlogreader.h API, which now
requires the current segment size to be configured. For that and
similar reasons a number of binaries had to be taught how to recognize
the current segment size.
Author: Beena Emerson, editorialized by Andres Freund
Reviewed-By: Andres Freund, David Steele, Kuntal Ghosh, Michael
Paquier, Peter Eisentraut, Robert Hass, Tushar Ahuja
Discussion: https://postgr.es/m/CAOG9ApEAcQ--1ieKbhFzXSQPw_YLmepaa4hNdnY5+ZULpt81Mw@mail.gmail.com
2017-09-20 07:03:48 +02:00
|
|
|
WalSegSz);
|
|
|
|
|
2015-03-23 18:47:52 +01:00
|
|
|
/* Additional checks on control file */
|
|
|
|
checkControlFile(ControlFile);
|
|
|
|
}
|
|
|
|
|
2016-03-27 23:46:25 +02:00
|
|
|
/*
|
|
|
|
* Sync target data directory to ensure that modifications are safely on disk.
|
|
|
|
*
|
|
|
|
* We do this once, for the whole data directory, for performance reasons. At
|
|
|
|
* the end of pg_rewind's run, the kernel is likely to already have flushed
|
2018-07-10 01:39:27 +02:00
|
|
|
* most dirty buffers to disk. Additionally fsync_pgdata uses a two-pass
|
|
|
|
* approach (only initiating writeback in the first pass), which often reduces
|
|
|
|
* the overall amount of IO noticeably.
|
2016-03-27 23:46:25 +02:00
|
|
|
*/
|
|
|
|
static void
|
2019-02-26 08:08:24 +01:00
|
|
|
syncTargetDirectory(void)
|
2016-03-27 23:46:25 +02:00
|
|
|
{
|
2018-07-10 01:51:10 +02:00
|
|
|
if (!do_sync || dry_run)
|
2016-03-27 23:46:25 +02:00
|
|
|
return;
|
|
|
|
|
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
|
|
|
fsync_pgdata(datadir_target, PG_VERSION_NUM);
|
2016-03-27 23:46:25 +02:00
|
|
|
}
|
2019-09-27 21:40:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure clean shutdown of target instance by launching single-user mode
|
|
|
|
* postgres to do crash recovery.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ensureCleanShutdown(const char *argv0)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
#define MAXCMDLEN (2 * MAXPGPATH)
|
|
|
|
char exec_path[MAXPGPATH];
|
|
|
|
char cmd[MAXCMDLEN];
|
|
|
|
|
|
|
|
/* locate postgres binary */
|
|
|
|
if ((ret = find_other_exec(argv0, "postgres",
|
|
|
|
PG_BACKEND_VERSIONSTR,
|
|
|
|
exec_path)) < 0)
|
|
|
|
{
|
|
|
|
char full_path[MAXPGPATH];
|
|
|
|
|
|
|
|
if (find_my_exec(argv0, full_path) < 0)
|
|
|
|
strlcpy(full_path, progname, sizeof(full_path));
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
pg_fatal("The program \"%s\" is needed by %s but was\n"
|
|
|
|
"not found in the same directory as \"%s\".\n"
|
|
|
|
"Check your installation.",
|
|
|
|
"postgres", progname, full_path);
|
|
|
|
else
|
|
|
|
pg_fatal("The program \"%s\" was found by \"%s\" but was\n"
|
|
|
|
"not the same version as %s.\n"
|
|
|
|
"Check your installation.",
|
|
|
|
"postgres", full_path, progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_log_info("executing \"%s\" for target server to complete crash recovery",
|
|
|
|
exec_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip processing if requested, but only after ensuring presence of
|
|
|
|
* postgres.
|
|
|
|
*/
|
|
|
|
if (dry_run)
|
|
|
|
return;
|
|
|
|
|
2019-10-07 02:07:22 +02:00
|
|
|
/*
|
|
|
|
* Finally run postgres in single-user mode. There is no need to use
|
|
|
|
* fsync here. This makes the recovery faster, and the target data folder
|
|
|
|
* is synced at the end anyway.
|
|
|
|
*/
|
|
|
|
snprintf(cmd, MAXCMDLEN, "\"%s\" --single -F -D \"%s\" template1 < \"%s\"",
|
2019-09-27 21:40:01 +02:00
|
|
|
exec_path, datadir_target, DEVNULL);
|
|
|
|
|
|
|
|
if (system(cmd) != 0)
|
|
|
|
{
|
|
|
|
pg_log_error("postgres single-user mode of target instance failed");
|
|
|
|
pg_fatal("Command was: %s", cmd);
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 17:57:35 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
disconnect_atexit(void)
|
|
|
|
{
|
|
|
|
if (conn != NULL)
|
|
|
|
PQfinish(conn);
|
|
|
|
}
|