2011-01-23 12:21:23 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_basebackup.c - receive a base backup using streaming replication protocol
|
|
|
|
*
|
|
|
|
* Author: Magnus Hagander <magnus@hagander.net>
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
2011-01-23 12:21:23 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/bin/pg_basebackup/pg_basebackup.c
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2012-12-13 13:59:13 +01:00
|
|
|
#include "postgres_fe.h"
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
2011-10-26 20:13:33 +02:00
|
|
|
#include <sys/wait.h>
|
2014-02-12 20:04:13 +01:00
|
|
|
#include <signal.h>
|
2013-01-05 16:54:06 +01:00
|
|
|
#include <time.h>
|
2016-09-27 06:05:21 +02:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2011-01-23 12:21:23 +01:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
#include "access/xlog_internal.h"
|
2018-04-07 23:45:39 +02:00
|
|
|
#include "common/file_perm.h"
|
2016-09-29 18:00:00 +02:00
|
|
|
#include "common/file_utils.h"
|
2015-01-03 20:51:52 +01:00
|
|
|
#include "common/string.h"
|
2016-08-22 04:05:57 +02:00
|
|
|
#include "fe_utils/string_utils.h"
|
2011-01-23 12:21:23 +01:00
|
|
|
#include "getopt_long.h"
|
2014-02-27 22:55:57 +01:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "pqexpbuffer.h"
|
|
|
|
#include "pgtar.h"
|
|
|
|
#include "pgtime.h"
|
2011-10-26 20:13:33 +02:00
|
|
|
#include "receivelog.h"
|
2014-02-27 22:55:57 +01:00
|
|
|
#include "replication/basebackup.h"
|
2011-10-26 20:13:33 +02:00
|
|
|
#include "streamutil.h"
|
|
|
|
|
2018-04-03 13:47:16 +02:00
|
|
|
#define ERRCODE_DATA_CORRUPTED "XX001"
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2014-02-22 19:38:06 +01:00
|
|
|
typedef struct TablespaceListCell
|
|
|
|
{
|
|
|
|
struct TablespaceListCell *next;
|
2014-05-06 18:12:18 +02:00
|
|
|
char old_dir[MAXPGPATH];
|
|
|
|
char new_dir[MAXPGPATH];
|
2014-02-22 19:38:06 +01:00
|
|
|
} TablespaceListCell;
|
|
|
|
|
|
|
|
typedef struct TablespaceList
|
|
|
|
{
|
|
|
|
TablespaceListCell *head;
|
|
|
|
TablespaceListCell *tail;
|
|
|
|
} TablespaceList;
|
|
|
|
|
2016-10-20 17:24:37 +02:00
|
|
|
/*
|
|
|
|
* pg_xlog has been renamed to pg_wal in version 10. This version number
|
|
|
|
* should be compared with PQserverVersion().
|
|
|
|
*/
|
|
|
|
#define MINIMUM_VERSION_FOR_PG_WAL 100000
|
|
|
|
|
2017-01-16 13:56:43 +01:00
|
|
|
/*
|
|
|
|
* Temporary replication slots are supported from version 10.
|
|
|
|
*/
|
|
|
|
#define MINIMUM_VERSION_FOR_TEMP_SLOTS 100000
|
|
|
|
|
2017-01-09 16:03:47 +01:00
|
|
|
/*
|
|
|
|
* Different ways to include WAL
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
NO_WAL,
|
|
|
|
FETCH_WAL,
|
|
|
|
STREAM_WAL
|
|
|
|
} IncludeWal;
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/* Global options */
|
2013-12-16 10:27:30 +01:00
|
|
|
static char *basedir = NULL;
|
2014-02-22 19:38:06 +01:00
|
|
|
static TablespaceList tablespace_dirs = {NULL, NULL};
|
2017-08-31 04:28:36 +02:00
|
|
|
static char *xlog_dir = NULL;
|
2014-05-06 18:12:18 +02:00
|
|
|
static char format = 'p'; /* p(lain)/t(ar) */
|
2013-12-16 10:27:30 +01:00
|
|
|
static char *label = "pg_basebackup base backup";
|
2016-09-12 18:00:00 +02:00
|
|
|
static bool noclean = false;
|
2018-04-03 13:47:16 +02:00
|
|
|
static bool checksum_failure = false;
|
2014-05-06 18:12:18 +02:00
|
|
|
static bool showprogress = false;
|
|
|
|
static int verbose = 0;
|
2013-12-16 10:27:30 +01:00
|
|
|
static int compresslevel = 0;
|
2017-01-09 16:03:47 +01:00
|
|
|
static IncludeWal includewal = STREAM_WAL;
|
2014-05-06 18:12:18 +02:00
|
|
|
static bool fastcheckpoint = false;
|
|
|
|
static bool writerecoveryconf = false;
|
2016-09-29 18:00:00 +02:00
|
|
|
static bool do_sync = true;
|
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
|
|
|
static int standby_message_timeout = 10 * 1000; /* 10 sec = default */
|
2014-02-09 12:47:09 +01:00
|
|
|
static pg_time_t last_progress_report = 0;
|
2014-02-27 22:55:57 +01:00
|
|
|
static int32 maxrate = 0; /* no limit by default */
|
2017-01-16 13:56:43 +01:00
|
|
|
static char *replication_slot = NULL;
|
|
|
|
static bool temp_replication_slot = true;
|
2017-09-26 22:07:52 +02:00
|
|
|
static bool create_slot = false;
|
|
|
|
static bool no_slot = false;
|
2018-04-03 13:47:16 +02:00
|
|
|
static bool verify_checksums = true;
|
2014-02-27 22:55:57 +01:00
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
static bool success = false;
|
|
|
|
static bool made_new_pgdata = false;
|
|
|
|
static bool found_existing_pgdata = false;
|
|
|
|
static bool made_new_xlogdir = false;
|
|
|
|
static bool found_existing_xlogdir = false;
|
|
|
|
static bool made_tablespace_dirs = false;
|
|
|
|
static bool found_tablespace_dirs = false;
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/* Progress counters */
|
|
|
|
static uint64 totalsize;
|
|
|
|
static uint64 totaldone;
|
|
|
|
static int tablespacecount;
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
/* Pipe to communicate with background wal receiver process */
|
|
|
|
#ifndef WIN32
|
|
|
|
static int bgpipe[2] = {-1, -1};
|
|
|
|
#endif
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
/* Handle to child process */
|
|
|
|
static pid_t bgchild = -1;
|
2016-09-12 18:00:00 +02:00
|
|
|
static bool in_log_streamer = false;
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
/* End position for xlog streaming, empty string if unknown yet */
|
|
|
|
static XLogRecPtr xlogendptr;
|
2012-06-10 21:20:04 +02:00
|
|
|
|
2011-12-11 00:15:15 +01:00
|
|
|
#ifndef WIN32
|
2011-10-26 20:13:33 +02:00
|
|
|
static int has_xlogendptr = 0;
|
2011-12-11 00:15:15 +01:00
|
|
|
#else
|
|
|
|
static volatile LONG has_xlogendptr = 0;
|
|
|
|
#endif
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2018-11-25 16:31:16 +01:00
|
|
|
/* Contents of configuration file to be generated */
|
2013-01-05 16:54:06 +01:00
|
|
|
static PQExpBuffer recoveryconfcontents = NULL;
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/* Function headers */
|
|
|
|
static void usage(void);
|
2016-09-12 18:00:00 +02:00
|
|
|
static void verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found);
|
2014-02-09 12:47:09 +01:00
|
|
|
static void progress_report(int tablespacenum, const char *filename, bool force);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
static void ReceiveTarFile(PGconn *conn, PGresult *res, int rownum);
|
|
|
|
static void ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum);
|
2013-01-05 16:54:06 +01:00
|
|
|
static void GenerateRecoveryConf(PGconn *conn);
|
|
|
|
static void WriteRecoveryConf(void);
|
2011-03-27 22:31:41 +02:00
|
|
|
static void BaseBackup(void);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2012-07-31 16:11:11 +02:00
|
|
|
static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
|
|
|
|
bool segment_finished);
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2014-02-22 19:38:06 +01:00
|
|
|
static const char *get_tablespace_mapping(const char *dir);
|
|
|
|
static void tablespace_list_append(const char *arg);
|
|
|
|
|
2014-02-09 13:10:14 +01:00
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
static void
|
|
|
|
cleanup_directories_atexit(void)
|
|
|
|
{
|
|
|
|
if (success || in_log_streamer)
|
|
|
|
return;
|
|
|
|
|
2018-04-03 13:47:16 +02:00
|
|
|
if (!noclean && !checksum_failure)
|
2016-09-12 18:00:00 +02:00
|
|
|
{
|
|
|
|
if (made_new_pgdata)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: removing data directory \"%s\"\n"),
|
|
|
|
progname, basedir);
|
|
|
|
if (!rmtree(basedir, true))
|
|
|
|
fprintf(stderr, _("%s: failed to remove data directory\n"),
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
else if (found_existing_pgdata)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: removing contents of data directory \"%s\"\n"),
|
|
|
|
progname, basedir);
|
|
|
|
if (!rmtree(basedir, false))
|
|
|
|
fprintf(stderr, _("%s: failed to remove contents of data directory\n"),
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (made_new_xlogdir)
|
|
|
|
{
|
2017-05-12 17:49:56 +02:00
|
|
|
fprintf(stderr, _("%s: removing WAL directory \"%s\"\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname, xlog_dir);
|
|
|
|
if (!rmtree(xlog_dir, true))
|
2017-05-12 17:49:56 +02:00
|
|
|
fprintf(stderr, _("%s: failed to remove WAL directory\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname);
|
|
|
|
}
|
|
|
|
else if (found_existing_xlogdir)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2017-05-12 17:49:56 +02:00
|
|
|
_("%s: removing contents of WAL directory \"%s\"\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname, xlog_dir);
|
|
|
|
if (!rmtree(xlog_dir, false))
|
2017-05-12 17:49:56 +02:00
|
|
|
fprintf(stderr, _("%s: failed to remove contents of WAL directory\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-03 13:47:16 +02:00
|
|
|
if ((made_new_pgdata || found_existing_pgdata) && !checksum_failure)
|
2016-09-12 18:00:00 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: data directory \"%s\" not removed at user's request\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname, basedir);
|
|
|
|
|
|
|
|
if (made_new_xlogdir || found_existing_xlogdir)
|
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: WAL directory \"%s\" not removed at user's request\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname, xlog_dir);
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:47:16 +02:00
|
|
|
if ((made_tablespace_dirs || found_tablespace_dirs) && !checksum_failure)
|
2016-09-12 18:00:00 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: changes to tablespace directories will not be undone\n"),
|
2016-09-12 18:00:00 +02:00
|
|
|
progname);
|
|
|
|
}
|
|
|
|
|
2014-05-06 18:12:18 +02:00
|
|
|
static void
|
2018-12-29 13:21:47 +01:00
|
|
|
disconnect_atexit(void)
|
2014-02-09 13:10:14 +01:00
|
|
|
{
|
|
|
|
if (conn != NULL)
|
|
|
|
PQfinish(conn);
|
2018-12-29 13:21:47 +01:00
|
|
|
}
|
2014-02-09 13:10:14 +01:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2018-12-29 13:21:47 +01:00
|
|
|
/*
|
|
|
|
* On windows, our background thread dies along with the process. But on
|
|
|
|
* Unix, if we have started a subprocess, we want to kill it off so it
|
|
|
|
* doesn't remain running trying to stream data.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
kill_bgchild_atexit(void)
|
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
if (bgchild > 0)
|
2014-02-09 13:10:14 +01:00
|
|
|
kill(bgchild, SIGTERM);
|
|
|
|
}
|
2018-12-29 13:21:47 +01:00
|
|
|
#endif
|
2014-02-09 13:10:14 +01:00
|
|
|
|
2014-02-22 19:38:06 +01:00
|
|
|
/*
|
|
|
|
* Split argument into old_dir and new_dir and append to tablespace mapping
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tablespace_list_append(const char *arg)
|
|
|
|
{
|
|
|
|
TablespaceListCell *cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell));
|
2014-05-06 18:12:18 +02:00
|
|
|
char *dst;
|
|
|
|
char *dst_ptr;
|
|
|
|
const char *arg_ptr;
|
2014-02-22 19:38:06 +01:00
|
|
|
|
|
|
|
dst_ptr = dst = cell->old_dir;
|
|
|
|
for (arg_ptr = arg; *arg_ptr; arg_ptr++)
|
|
|
|
{
|
|
|
|
if (dst_ptr - dst >= MAXPGPATH)
|
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
fprintf(stderr, _("%s: directory name too long\n"), progname);
|
2014-02-22 19:38:06 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
|
2014-05-06 18:12:18 +02:00
|
|
|
; /* skip backslash escaping = */
|
2014-02-22 19:38:06 +01:00
|
|
|
else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
|
|
|
|
{
|
|
|
|
if (*cell->new_dir)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: multiple \"=\" signs in tablespace mapping\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dst = dst_ptr = cell->new_dir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*dst_ptr++ = *arg_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*cell->old_dir || !*cell->new_dir)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"\n"),
|
|
|
|
progname, arg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-05-06 18:12:18 +02:00
|
|
|
/*
|
|
|
|
* This check isn't absolutely necessary. But all tablespaces are created
|
2014-02-22 19:38:06 +01:00
|
|
|
* with absolute directories, so specifying a non-absolute path here would
|
|
|
|
* just never match, possibly confusing users. It's also good to be
|
2014-05-06 18:12:18 +02:00
|
|
|
* consistent with the new_dir check.
|
|
|
|
*/
|
2014-02-22 19:38:06 +01:00
|
|
|
if (!is_absolute_path(cell->old_dir))
|
|
|
|
{
|
2014-05-15 13:49:11 +02:00
|
|
|
fprintf(stderr, _("%s: old directory is not an absolute path in tablespace mapping: %s\n"),
|
2014-02-22 19:38:06 +01:00
|
|
|
progname, cell->old_dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_absolute_path(cell->new_dir))
|
|
|
|
{
|
2014-05-15 13:49:11 +02:00
|
|
|
fprintf(stderr, _("%s: new directory is not an absolute path in tablespace mapping: %s\n"),
|
2014-02-22 19:38:06 +01:00
|
|
|
progname, cell->new_dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-11-01 15:20:05 +01:00
|
|
|
/*
|
|
|
|
* Comparisons done with these values should involve similarly
|
|
|
|
* canonicalized path values. This is particularly sensitive on Windows
|
|
|
|
* where path values may not necessarily use Unix slashes.
|
|
|
|
*/
|
2015-04-29 02:12:10 +02:00
|
|
|
canonicalize_path(cell->old_dir);
|
|
|
|
canonicalize_path(cell->new_dir);
|
|
|
|
|
2014-02-22 19:38:06 +01:00
|
|
|
if (tablespace_dirs.tail)
|
|
|
|
tablespace_dirs.tail->next = cell;
|
|
|
|
else
|
|
|
|
tablespace_dirs.head = cell;
|
|
|
|
tablespace_dirs.tail = cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
static const char *
|
2012-03-02 21:30:01 +01:00
|
|
|
get_gz_error(gzFile gzf)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
int errnum;
|
|
|
|
const char *errmsg;
|
|
|
|
|
|
|
|
errmsg = gzerror(gzf, &errnum);
|
|
|
|
if (errnum == Z_ERRNO)
|
|
|
|
return strerror(errno);
|
|
|
|
else
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
printf(_("%s takes a base backup of a running PostgreSQL server.\n\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname);
|
|
|
|
printf(_("Usage:\n"));
|
|
|
|
printf(_(" %s [OPTION]...\n"), progname);
|
|
|
|
printf(_("\nOptions controlling the output:\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -D, --pgdata=DIRECTORY receive base backup into directory\n"));
|
|
|
|
printf(_(" -F, --format=p|t output format (plain (default), tar)\n"));
|
2017-05-17 19:04:03 +02:00
|
|
|
printf(_(" -r, --max-rate=RATE maximum transfer rate to transfer data directory\n"
|
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
|
|
|
" (in kB/s, or use suffix \"k\" or \"M\")\n"));
|
2017-05-17 19:04:03 +02:00
|
|
|
printf(_(" -R, --write-recovery-conf\n"
|
2018-11-25 16:31:16 +01:00
|
|
|
" write configuration for replication\n"));
|
2017-05-17 19:04:03 +02:00
|
|
|
printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
|
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
|
|
|
" relocate tablespace in OLDDIR to NEWDIR\n"));
|
2017-09-26 17:58:22 +02:00
|
|
|
printf(_(" --waldir=WALDIR location for the write-ahead log directory\n"));
|
2017-05-17 19:04:03 +02:00
|
|
|
printf(_(" -X, --wal-method=none|fetch|stream\n"
|
|
|
|
" include required WAL files with specified method\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -z, --gzip compress tar output\n"));
|
|
|
|
printf(_(" -Z, --compress=0-9 compress tar output with given compression level\n"));
|
2011-01-23 12:21:23 +01:00
|
|
|
printf(_("\nGeneral options:\n"));
|
2017-05-17 19:04:03 +02:00
|
|
|
printf(_(" -c, --checkpoint=fast|spread\n"
|
|
|
|
" set fast or spread checkpointing\n"));
|
2017-09-26 22:07:52 +02:00
|
|
|
printf(_(" -C, --create-slot create replication slot\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -l, --label=LABEL set backup label\n"));
|
2016-10-19 18:00:00 +02:00
|
|
|
printf(_(" -n, --no-clean do not clean up after errors\n"));
|
|
|
|
printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -P, --progress show progress information\n"));
|
2017-09-26 17:58:22 +02:00
|
|
|
printf(_(" -S, --slot=SLOTNAME replication slot to use\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -v, --verbose output verbose messages\n"));
|
|
|
|
printf(_(" -V, --version output version information, then exit\n"));
|
2018-05-21 16:01:49 +02:00
|
|
|
printf(_(" --no-slot prevent creation of temporary replication slot\n"));
|
|
|
|
printf(_(" --no-verify-checksums\n"
|
|
|
|
" do not verify checksums\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -?, --help show this help, then exit\n"));
|
2011-01-23 12:21:23 +01:00
|
|
|
printf(_("\nConnection options:\n"));
|
2013-02-25 13:48:27 +01:00
|
|
|
printf(_(" -d, --dbname=CONNSTR connection string\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
|
|
|
|
printf(_(" -p, --port=PORT database server port number\n"));
|
2017-05-17 19:04:03 +02:00
|
|
|
printf(_(" -s, --status-interval=INTERVAL\n"
|
|
|
|
" time between status packets sent to server (in seconds)\n"));
|
2012-07-31 16:11:11 +02:00
|
|
|
printf(_(" -U, --username=NAME connect as specified database user\n"));
|
|
|
|
printf(_(" -w, --no-password never prompt for password\n"));
|
|
|
|
printf(_(" -W, --password force password prompt (should happen automatically)\n"));
|
2019-01-19 19:06:35 +01:00
|
|
|
printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
/*
|
2012-05-25 11:36:22 +02:00
|
|
|
* Called in the background process every time data is received.
|
2011-10-26 20:13:33 +02:00
|
|
|
* On Unix, we check to see if there is any data on our pipe
|
|
|
|
* (which would mean we have a stop position), and if it is, check if
|
|
|
|
* it is time to stop.
|
|
|
|
* On Windows, we are in a single process, so we can just check if it's
|
|
|
|
* time to stop.
|
|
|
|
*/
|
|
|
|
static bool
|
2012-07-31 16:11:11 +02:00
|
|
|
reached_end_position(XLogRecPtr segendpos, uint32 timeline,
|
|
|
|
bool segment_finished)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
|
|
|
if (!has_xlogendptr)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't have the end pointer yet - check our pipe to see if it has
|
|
|
|
* been sent yet.
|
|
|
|
*/
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(bgpipe[0], &fds);
|
|
|
|
|
|
|
|
MemSet(&tv, 0, sizeof(tv));
|
|
|
|
|
|
|
|
r = select(bgpipe[0] + 1, &fds, NULL, NULL, &tv);
|
|
|
|
if (r == 1)
|
|
|
|
{
|
|
|
|
char xlogend[64];
|
2012-06-24 17:51:37 +02:00
|
|
|
uint32 hi,
|
|
|
|
lo;
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
MemSet(xlogend, 0, sizeof(xlogend));
|
2014-05-06 18:12:18 +02:00
|
|
|
r = read(bgpipe[0], xlogend, sizeof(xlogend) - 1);
|
2011-10-26 20:13:33 +02:00
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not read from ready pipe: %s\n"),
|
|
|
|
progname, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-06-24 17:51:37 +02:00
|
|
|
if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: could not parse write-ahead log location \"%s\"\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, xlogend);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-24 17:51:37 +02:00
|
|
|
xlogendptr = ((uint64) hi) << 32 | lo;
|
2011-10-26 20:13:33 +02:00
|
|
|
has_xlogendptr = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fall through to check if we've reached the point further
|
|
|
|
* already.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No data received on the pipe means we don't know the end
|
|
|
|
* position yet - so just say it's not time to stop yet.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On win32, has_xlogendptr is set by the main thread, so if it's not
|
|
|
|
* set here, we just go back and wait until it shows up.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we have an end pointer, so compare it to the current
|
|
|
|
* position to figure out if it's time to stop.
|
|
|
|
*/
|
2012-06-24 17:51:37 +02:00
|
|
|
if (segendpos >= xlogendptr)
|
2011-10-26 20:13:33 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Have end pointer, but haven't reached it yet - so tell the caller to
|
|
|
|
* keep streaming.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
PGconn *bgconn;
|
|
|
|
XLogRecPtr startptr;
|
2016-10-23 15:16:31 +02:00
|
|
|
char xlog[MAXPGPATH]; /* directory or tarfile depending on mode */
|
2011-10-26 20:13:33 +02:00
|
|
|
char *sysidentifier;
|
|
|
|
int timeline;
|
2012-06-10 21:20:04 +02:00
|
|
|
} logstreamer_param;
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
static int
|
2012-06-10 21:20:04 +02:00
|
|
|
LogStreamerMain(logstreamer_param *param)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2016-03-11 11:08:01 +01:00
|
|
|
StreamCtl stream;
|
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
in_log_streamer = true;
|
|
|
|
|
2016-03-13 13:11:06 +01:00
|
|
|
MemSet(&stream, 0, sizeof(stream));
|
2016-03-11 11:08:01 +01:00
|
|
|
stream.startpos = param->startptr;
|
|
|
|
stream.timeline = param->timeline;
|
|
|
|
stream.sysidentifier = param->sysidentifier;
|
|
|
|
stream.stream_stop = reached_end_position;
|
2017-04-28 00:27:02 +02:00
|
|
|
#ifndef WIN32
|
|
|
|
stream.stop_socket = bgpipe[0];
|
|
|
|
#else
|
|
|
|
stream.stop_socket = PGINVALID_SOCKET;
|
|
|
|
#endif
|
2016-03-11 11:08:01 +01:00
|
|
|
stream.standby_message_timeout = standby_message_timeout;
|
|
|
|
stream.synchronous = false;
|
2016-09-29 18:00:00 +02:00
|
|
|
stream.do_sync = do_sync;
|
2016-03-11 11:08:01 +01:00
|
|
|
stream.mark_done = true;
|
|
|
|
stream.partial_suffix = NULL;
|
2017-01-16 13:56:43 +01:00
|
|
|
stream.replication_slot = replication_slot;
|
2016-03-11 11:08:01 +01:00
|
|
|
|
2016-10-23 15:16:31 +02:00
|
|
|
if (format == 'p')
|
2017-01-17 12:10:26 +01:00
|
|
|
stream.walmethod = CreateWalDirectoryMethod(param->xlog, 0, do_sync);
|
2016-10-23 15:16:31 +02:00
|
|
|
else
|
|
|
|
stream.walmethod = CreateWalTarMethod(param->xlog, compresslevel, do_sync);
|
|
|
|
|
2016-03-11 11:08:01 +01:00
|
|
|
if (!ReceiveXlogStream(param->bgconn, &stream))
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Any errors will already have been reported in the function process,
|
|
|
|
* but we need to tell the parent that we didn't shutdown in a nice
|
|
|
|
* way.
|
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
|
2016-10-23 15:16:31 +02:00
|
|
|
if (!stream.walmethod->finish())
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not finish writing WAL files: %s\n"),
|
|
|
|
progname, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
PQfinish(param->bgconn);
|
2016-10-25 18:57:56 +02:00
|
|
|
|
|
|
|
if (format == 'p')
|
|
|
|
FreeWalDirectoryMethod();
|
|
|
|
else
|
|
|
|
FreeWalTarMethod();
|
|
|
|
pg_free(stream.walmethod);
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initiate background process for receiving xlog during the backup.
|
|
|
|
* The background stream will use its own database connection so we can
|
|
|
|
* stream the logfile in parallel with the backups.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier)
|
|
|
|
{
|
|
|
|
logstreamer_param *param;
|
2012-06-24 17:51:37 +02:00
|
|
|
uint32 hi,
|
|
|
|
lo;
|
2015-01-03 20:51:52 +01:00
|
|
|
char statusdir[MAXPGPATH];
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2012-10-02 21:35:10 +02:00
|
|
|
param = pg_malloc0(sizeof(logstreamer_param));
|
2011-10-26 20:13:33 +02:00
|
|
|
param->timeline = timeline;
|
|
|
|
param->sysidentifier = sysidentifier;
|
|
|
|
|
|
|
|
/* Convert the starting position */
|
2012-06-24 17:51:37 +02:00
|
|
|
if (sscanf(startpos, "%X/%X", &hi, &lo) != 2)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
2017-05-12 17:49:56 +02:00
|
|
|
_("%s: could not parse write-ahead log location \"%s\"\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, startpos);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2012-06-24 17:51:37 +02:00
|
|
|
param->startptr = ((uint64) hi) << 32 | lo;
|
2011-10-26 20:13:33 +02:00
|
|
|
/* Round off to even segment position */
|
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
|
|
|
param->startptr -= XLogSegmentOffset(param->startptr, WalSegSz);
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
/* Create our background pipe */
|
2012-03-29 05:24:07 +02:00
|
|
|
if (pipe(bgpipe) < 0)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not create pipe for background process: %s\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get a second connection */
|
|
|
|
param->bgconn = GetConnection();
|
2012-05-27 11:05:24 +02:00
|
|
|
if (!param->bgconn)
|
|
|
|
/* Error message already written in GetConnection() */
|
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2016-10-20 17:24:37 +02:00
|
|
|
/* In post-10 cluster, pg_xlog has been renamed to pg_wal */
|
2016-10-23 15:16:31 +02:00
|
|
|
snprintf(param->xlog, sizeof(param->xlog), "%s/%s",
|
2016-10-20 17:24:37 +02:00
|
|
|
basedir,
|
|
|
|
PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
|
2017-05-17 22:31:56 +02:00
|
|
|
"pg_xlog" : "pg_wal");
|
2015-01-03 20:51:52 +01:00
|
|
|
|
2017-01-16 13:56:43 +01:00
|
|
|
/* Temporary replication slots are only supported in 10 and newer */
|
|
|
|
if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS)
|
2017-09-26 22:07:52 +02:00
|
|
|
temp_replication_slot = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create replication slot if requested
|
|
|
|
*/
|
|
|
|
if (temp_replication_slot && !replication_slot)
|
|
|
|
replication_slot = psprintf("pg_basebackup_%d", (int) PQbackendPID(param->bgconn));
|
|
|
|
if (temp_replication_slot || create_slot)
|
|
|
|
{
|
|
|
|
if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL,
|
|
|
|
temp_replication_slot, true, true, false))
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2017-09-26 22:07:52 +02:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
if (temp_replication_slot)
|
|
|
|
fprintf(stderr, _("%s: created temporary replication slot \"%s\"\n"),
|
|
|
|
progname, replication_slot);
|
|
|
|
else
|
|
|
|
fprintf(stderr, _("%s: created replication slot \"%s\"\n"),
|
|
|
|
progname, replication_slot);
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 20:51:52 +01:00
|
|
|
|
2016-10-23 15:16:31 +02:00
|
|
|
if (format == 'p')
|
2015-01-03 20:51:52 +01:00
|
|
|
{
|
2016-10-23 15:16:31 +02:00
|
|
|
/*
|
|
|
|
* Create pg_wal/archive_status or pg_xlog/archive_status (and thus
|
2017-05-17 22:31:56 +02:00
|
|
|
* pg_wal or pg_xlog) depending on the target server so we can write
|
|
|
|
* to basedir/pg_wal or basedir/pg_xlog as the directory entry in the
|
|
|
|
* tar file may arrive later.
|
2016-10-23 15:16:31 +02:00
|
|
|
*/
|
|
|
|
snprintf(statusdir, sizeof(statusdir), "%s/%s/archive_status",
|
|
|
|
basedir,
|
|
|
|
PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
|
|
|
|
"pg_xlog" : "pg_wal");
|
|
|
|
|
2018-04-07 23:45:39 +02:00
|
|
|
if (pg_mkdir_p(statusdir, pg_dir_create_mode) != 0 && errno != EEXIST)
|
2016-10-23 15:16:31 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not create directory \"%s\": %s\n"),
|
|
|
|
progname, statusdir, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2016-10-23 15:16:31 +02:00
|
|
|
}
|
2015-01-03 20:51:52 +01:00
|
|
|
}
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start a child process and tell it to start streaming. On Unix, this is
|
|
|
|
* a fork(). On Windows, we create a thread.
|
|
|
|
*/
|
|
|
|
#ifndef WIN32
|
|
|
|
bgchild = fork();
|
|
|
|
if (bgchild == 0)
|
|
|
|
{
|
|
|
|
/* in child process */
|
|
|
|
exit(LogStreamerMain(param));
|
|
|
|
}
|
|
|
|
else if (bgchild < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create background process: %s\n"),
|
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Else we are in the parent process and all is well.
|
|
|
|
*/
|
2018-12-29 13:21:47 +01:00
|
|
|
atexit(kill_bgchild_atexit);
|
2011-10-26 20:13:33 +02:00
|
|
|
#else /* WIN32 */
|
|
|
|
bgchild = _beginthreadex(NULL, 0, (void *) LogStreamerMain, param, 0, NULL);
|
|
|
|
if (bgchild == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create background thread: %s\n"),
|
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/*
|
|
|
|
* Verify that the given directory exists and is empty. If it does not
|
|
|
|
* exist, it is created. If it exists but is not empty, an error will
|
2017-05-15 11:08:02 +02:00
|
|
|
* be given and the process ended.
|
2011-01-23 12:21:23 +01:00
|
|
|
*/
|
|
|
|
static void
|
2016-09-12 18:00:00 +02:00
|
|
|
verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
switch (pg_check_dir(dirname))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does not exist, so create
|
|
|
|
*/
|
2018-04-07 23:45:39 +02:00
|
|
|
if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not create directory \"%s\": %s\n"),
|
|
|
|
progname, dirname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
2016-09-12 18:00:00 +02:00
|
|
|
if (created)
|
|
|
|
*created = true;
|
2011-01-23 12:21:23 +01:00
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exists, empty
|
|
|
|
*/
|
2016-09-12 18:00:00 +02:00
|
|
|
if (found)
|
|
|
|
*found = true;
|
2011-01-23 12:21:23 +01:00
|
|
|
return;
|
|
|
|
case 2:
|
2013-02-17 00:52:50 +01:00
|
|
|
case 3:
|
|
|
|
case 4:
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Exists, not empty
|
|
|
|
*/
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: directory \"%s\" exists but is not empty\n"),
|
|
|
|
progname, dirname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
case -1:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Access problem
|
|
|
|
*/
|
|
|
|
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
|
|
|
|
progname, dirname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a progress report based on the global variables. If verbose output
|
|
|
|
* is enabled, also print the current file name.
|
2014-02-09 12:47:09 +01:00
|
|
|
*
|
|
|
|
* Progress report is written at maximum once per second, unless the
|
|
|
|
* force parameter is set to true.
|
2011-01-23 12:21:23 +01:00
|
|
|
*/
|
|
|
|
static void
|
2014-02-09 12:47:09 +01:00
|
|
|
progress_report(int tablespacenum, const char *filename, bool force)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2014-02-09 12:47:09 +01:00
|
|
|
int percent;
|
2011-08-16 10:24:08 +02:00
|
|
|
char totaldone_str[32];
|
|
|
|
char totalsize_str[32];
|
2014-02-09 12:47:09 +01:00
|
|
|
pg_time_t now;
|
|
|
|
|
|
|
|
if (!showprogress)
|
|
|
|
return;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
if (now == last_progress_report && !force)
|
2014-05-06 18:12:18 +02:00
|
|
|
return; /* Max once per second */
|
2014-02-09 12:47:09 +01:00
|
|
|
|
|
|
|
last_progress_report = now;
|
|
|
|
percent = totalsize ? (int) ((totaldone / 1024) * 100 / totalsize) : 0;
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2011-08-16 16:56:47 +02:00
|
|
|
/*
|
2011-10-26 23:23:33 +02:00
|
|
|
* 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.
|
2011-08-16 16:56:47 +02:00
|
|
|
*/
|
2011-01-30 21:30:09 +01:00
|
|
|
if (percent > 100)
|
|
|
|
percent = 100;
|
2011-08-16 16:56:47 +02:00
|
|
|
if (totaldone / 1024 > totalsize)
|
|
|
|
totalsize = totaldone / 1024;
|
2011-01-30 21:30:09 +01:00
|
|
|
|
2011-08-16 10:24:08 +02:00
|
|
|
/*
|
2011-10-26 23:23:33 +02:00
|
|
|
* Separate step to keep platform-dependent format code out of
|
|
|
|
* translatable strings. And we only test for INT64_FORMAT availability
|
|
|
|
* in snprintf, not fprintf.
|
2011-08-16 10:24:08 +02:00
|
|
|
*/
|
2012-07-31 16:11:11 +02:00
|
|
|
snprintf(totaldone_str, sizeof(totaldone_str), INT64_FORMAT,
|
|
|
|
totaldone / 1024);
|
2011-08-16 10:24:08 +02:00
|
|
|
snprintf(totalsize_str, sizeof(totalsize_str), INT64_FORMAT, totalsize);
|
|
|
|
|
2013-01-17 14:38:49 +01:00
|
|
|
#define VERBOSE_FILENAME_LENGTH 35
|
2011-01-23 12:21:23 +01:00
|
|
|
if (verbose)
|
2011-03-19 16:38:50 +01:00
|
|
|
{
|
2011-08-16 10:19:50 +02:00
|
|
|
if (!filename)
|
2011-03-19 16:38:50 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No filename given, so clear the status line (used for last
|
|
|
|
* call)
|
|
|
|
*/
|
|
|
|
fprintf(stderr,
|
2013-01-17 14:43:33 +01:00
|
|
|
ngettext("%*s/%s kB (100%%), %d/%d tablespace %*s",
|
|
|
|
"%*s/%s kB (100%%), %d/%d tablespaces %*s",
|
2011-08-16 10:24:08 +02:00
|
|
|
tablespacecount),
|
2013-01-17 16:10:16 +01:00
|
|
|
(int) strlen(totalsize_str),
|
2012-07-31 16:11:11 +02:00
|
|
|
totaldone_str, totalsize_str,
|
2013-01-17 14:38:49 +01:00
|
|
|
tablespacenum, tablespacecount,
|
|
|
|
VERBOSE_FILENAME_LENGTH + 5, "");
|
2011-03-19 16:38:50 +01:00
|
|
|
else
|
2013-01-17 14:38:49 +01:00
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
bool truncate = (strlen(filename) > VERBOSE_FILENAME_LENGTH);
|
2013-01-17 14:38:49 +01:00
|
|
|
|
2011-03-19 16:38:50 +01:00
|
|
|
fprintf(stderr,
|
2013-01-17 14:43:33 +01:00
|
|
|
ngettext("%*s/%s kB (%d%%), %d/%d tablespace (%s%-*.*s)",
|
|
|
|
"%*s/%s kB (%d%%), %d/%d tablespaces (%s%-*.*s)",
|
2011-08-16 10:24:08 +02:00
|
|
|
tablespacecount),
|
2013-01-17 16:10:16 +01:00
|
|
|
(int) strlen(totalsize_str),
|
2012-07-31 16:11:11 +02:00
|
|
|
totaldone_str, totalsize_str, percent,
|
2013-01-17 14:38:49 +01:00
|
|
|
tablespacenum, tablespacecount,
|
2013-05-29 22:58:43 +02:00
|
|
|
/* Prefix with "..." if we do leading truncation */
|
2013-01-17 14:38:49 +01:00
|
|
|
truncate ? "..." : "",
|
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
|
|
|
truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
|
|
|
|
truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
|
2013-05-29 22:58:43 +02:00
|
|
|
/* Truncate filename at beginning if it's too long */
|
2013-01-17 14:38:49 +01:00
|
|
|
truncate ? filename + strlen(filename) - VERBOSE_FILENAME_LENGTH + 3 : filename);
|
|
|
|
}
|
2011-03-19 16:38:50 +01:00
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
else
|
2011-08-16 10:24:08 +02:00
|
|
|
fprintf(stderr,
|
2013-01-17 14:43:33 +01:00
|
|
|
ngettext("%*s/%s kB (%d%%), %d/%d tablespace",
|
|
|
|
"%*s/%s kB (%d%%), %d/%d tablespaces",
|
2011-08-16 10:24:08 +02:00
|
|
|
tablespacecount),
|
2013-01-17 16:10:16 +01:00
|
|
|
(int) strlen(totalsize_str),
|
2012-07-31 16:11:11 +02:00
|
|
|
totaldone_str, totalsize_str, percent,
|
|
|
|
tablespacenum, tablespacecount);
|
2011-08-17 09:52:35 +02:00
|
|
|
|
2017-12-01 15:21:34 +01:00
|
|
|
if (isatty(fileno(stderr)))
|
|
|
|
fprintf(stderr, "\r");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:55:57 +01:00
|
|
|
static int32
|
|
|
|
parse_max_rate(char *src)
|
|
|
|
{
|
|
|
|
double result;
|
|
|
|
char *after_num;
|
2014-05-06 18:12:18 +02:00
|
|
|
char *suffix = NULL;
|
2014-02-27 22:55:57 +01:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
result = strtod(src, &after_num);
|
|
|
|
if (src == after_num)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: transfer rate \"%s\" is not a valid value\n"),
|
|
|
|
progname, src);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: invalid transfer rate \"%s\": %s\n"),
|
|
|
|
progname, src, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result <= 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Reject obviously wrong values here.
|
|
|
|
*/
|
|
|
|
fprintf(stderr, _("%s: transfer rate must be greater than zero\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Evaluate suffix, after skipping over possible whitespace. Lack of
|
|
|
|
* suffix means kilobytes.
|
2014-02-27 22:55:57 +01:00
|
|
|
*/
|
|
|
|
while (*after_num != '\0' && isspace((unsigned char) *after_num))
|
|
|
|
after_num++;
|
|
|
|
|
|
|
|
if (*after_num != '\0')
|
|
|
|
{
|
|
|
|
suffix = after_num;
|
|
|
|
if (*after_num == 'k')
|
|
|
|
{
|
|
|
|
/* kilobyte is the expected unit. */
|
|
|
|
after_num++;
|
|
|
|
}
|
|
|
|
else if (*after_num == 'M')
|
|
|
|
{
|
|
|
|
after_num++;
|
|
|
|
result *= 1024.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The rest can only consist of white space. */
|
|
|
|
while (*after_num != '\0' && isspace((unsigned char) *after_num))
|
|
|
|
after_num++;
|
|
|
|
|
|
|
|
if (*after_num != '\0')
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2014-05-15 13:49:11 +02:00
|
|
|
_("%s: invalid --max-rate unit: \"%s\"\n"),
|
2014-02-27 22:55:57 +01:00
|
|
|
progname, suffix);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Valid integer? */
|
|
|
|
if ((uint64) result != (uint64) ((uint32) result))
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2014-05-06 18:12:18 +02:00
|
|
|
_("%s: transfer rate \"%s\" exceeds integer range\n"),
|
|
|
|
progname, src);
|
2014-02-27 22:55:57 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The range is checked on the server side too, but avoid the server
|
|
|
|
* connection if a nonsensical value was passed.
|
|
|
|
*/
|
|
|
|
if (result < MAX_RATE_LOWER || result > MAX_RATE_UPPER)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: transfer rate \"%s\" is out of range\n"),
|
|
|
|
progname, src);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int32) result;
|
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
/*
|
|
|
|
* Write a piece of tar data
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
writeTarData(
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
gzFile ztarfile,
|
|
|
|
#endif
|
|
|
|
FILE *tarfile, char *buf, int r, char *current_file)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (ztarfile != NULL)
|
|
|
|
{
|
|
|
|
if (gzwrite(ztarfile, buf, r) != r)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not write to compressed file \"%s\": %s\n"),
|
|
|
|
progname, current_file, get_gz_error(ztarfile));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (fwrite(buf, r, 1, tarfile) != 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"),
|
|
|
|
progname, current_file, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
#define WRITE_TAR_DATA(buf, sz) writeTarData(ztarfile, tarfile, buf, sz, filename)
|
|
|
|
#else
|
|
|
|
#define WRITE_TAR_DATA(buf, sz) writeTarData(tarfile, buf, sz, filename)
|
|
|
|
#endif
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/*
|
|
|
|
* Receive a tar format file from the connection to the server, and write
|
|
|
|
* the data from this file directly into a tar file. If compression is
|
|
|
|
* enabled, the data will be compressed while written to the file.
|
|
|
|
*
|
|
|
|
* The file will be named base.tar[.gz] if it's for the main data directory
|
|
|
|
* or <tablespaceoid>.tar[.gz] if it's for another tablespace.
|
|
|
|
*
|
|
|
|
* No attempt to inspect or validate the contents of the file is done.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
|
|
|
|
{
|
2011-08-16 10:19:50 +02:00
|
|
|
char filename[MAXPGPATH];
|
2011-01-23 12:21:23 +01:00
|
|
|
char *copybuf = NULL;
|
|
|
|
FILE *tarfile = NULL;
|
2013-01-05 16:54:06 +01:00
|
|
|
char tarhdr[512];
|
|
|
|
bool basetablespace = PQgetisnull(res, rownum, 0);
|
|
|
|
bool in_tarhdr = true;
|
|
|
|
bool skip_file = false;
|
2018-11-25 16:31:16 +01:00
|
|
|
bool is_postgresql_auto_conf = false;
|
|
|
|
bool found_postgresql_auto_conf = false;
|
|
|
|
int file_padding_len = 0;
|
2013-01-05 16:54:06 +01:00
|
|
|
size_t tarhdrsz = 0;
|
Adopt the GNU convention for handling tar-archive members exceeding 8GB.
The POSIX standard for tar headers requires archive member sizes to be
printed in octal with at most 11 digits, limiting the representable file
size to 8GB. However, GNU tar and apparently most other modern tars
support a convention in which oversized values can be stored in base-256,
allowing any practical file to be a tar member. Adopt this convention
to remove two limitations:
* pg_dump with -Ft output format failed if the contents of any one table
exceeded 8GB.
* pg_basebackup failed if the data directory contained any file exceeding
8GB. (This would be a fatal problem for installations configured with a
table segment size of 8GB or more, and it has also been seen to fail when
large core dump files exist in the data directory.)
File sizes under 8GB are still printed in octal, so that no compatibility
issues are created except in cases that would have failed entirely before.
In addition, this patch fixes several bugs in the same area:
* In 9.3 and later, we'd defined tarCreateHeader's file-size argument as
size_t, which meant that on 32-bit machines it would write a corrupt tar
header for file sizes between 4GB and 8GB, even though no error was raised.
This broke both "pg_dump -Ft" and pg_basebackup for such cases.
* pg_restore from a tar archive would fail on tables of size between 4GB
and 8GB, on machines where either "size_t" or "unsigned long" is 32 bits.
This happened even with an archive file not affected by the previous bug.
* pg_basebackup would fail if there were files of size between 4GB and 8GB,
even on 64-bit machines.
* In 9.3 and later, "pg_basebackup -Ft" failed entirely, for any file size,
on 64-bit big-endian machines.
In view of these potential data-loss bugs, back-patch to all supported
branches, even though removal of the documented 8GB limit might otherwise
be considered a new feature rather than a bug fix.
2015-11-22 02:21:31 +01:00
|
|
|
pgoff_t filesz = 0;
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
2012-03-02 21:30:01 +01:00
|
|
|
gzFile ztarfile = NULL;
|
2011-01-23 12:21:23 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
if (basetablespace)
|
2012-07-31 16:11:11 +02:00
|
|
|
{
|
2011-01-23 12:21:23 +01:00
|
|
|
/*
|
|
|
|
* Base tablespaces
|
|
|
|
*/
|
|
|
|
if (strcmp(basedir, "-") == 0)
|
2011-05-29 00:17:37 +02:00
|
|
|
{
|
2017-07-14 15:02:53 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
_setmode(fileno(stdout), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2011-05-29 00:17:37 +02:00
|
|
|
#ifdef HAVE_LIBZ
|
2011-06-15 19:15:05 +02:00
|
|
|
if (compresslevel != 0)
|
2011-05-29 00:17:37 +02:00
|
|
|
{
|
|
|
|
ztarfile = gzdopen(dup(fileno(stdout)), "wb");
|
2012-07-31 16:11:11 +02:00
|
|
|
if (gzsetparams(ztarfile, compresslevel,
|
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK)
|
2011-05-29 00:17:37 +02:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not set compression level %d: %s\n"),
|
2011-05-29 00:17:37 +02:00
|
|
|
progname, compresslevel, get_gz_error(ztarfile));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-05-29 00:17:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
tarfile = stdout;
|
2014-02-09 11:54:33 +01:00
|
|
|
strcpy(filename, "-");
|
2011-05-29 00:17:37 +02:00
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
2011-06-15 19:15:05 +02:00
|
|
|
if (compresslevel != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2011-08-16 10:19:50 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%s/base.tar.gz", basedir);
|
|
|
|
ztarfile = gzopen(filename, "wb");
|
2012-07-31 16:11:11 +02:00
|
|
|
if (gzsetparams(ztarfile, compresslevel,
|
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not set compression level %d: %s\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, compresslevel, get_gz_error(ztarfile));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2011-08-16 10:19:50 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%s/base.tar", basedir);
|
|
|
|
tarfile = fopen(filename, "wb");
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
2012-07-31 16:11:11 +02:00
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Specific tablespace
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_LIBZ
|
2011-06-15 19:15:05 +02:00
|
|
|
if (compresslevel != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%s/%s.tar.gz", basedir,
|
|
|
|
PQgetvalue(res, rownum, 0));
|
2011-08-16 10:19:50 +02:00
|
|
|
ztarfile = gzopen(filename, "wb");
|
2012-07-31 16:11:11 +02:00
|
|
|
if (gzsetparams(ztarfile, compresslevel,
|
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not set compression level %d: %s\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, compresslevel, get_gz_error(ztarfile));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%s/%s.tar", basedir,
|
|
|
|
PQgetvalue(res, rownum, 0));
|
2011-08-16 10:19:50 +02:00
|
|
|
tarfile = fopen(filename, "wb");
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
2011-06-15 19:15:05 +02:00
|
|
|
if (compresslevel != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
if (!ztarfile)
|
|
|
|
{
|
|
|
|
/* Compression is in use */
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not create compressed file \"%s\": %s\n"),
|
2011-08-16 10:19:50 +02:00
|
|
|
progname, filename, get_gz_error(ztarfile));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Either no zlib support, or zlib support but compresslevel = 0 */
|
|
|
|
if (!tarfile)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create file \"%s\": %s\n"),
|
2011-08-16 10:19:50 +02:00
|
|
|
progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the COPY data stream
|
|
|
|
*/
|
|
|
|
res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(res) != PGRES_COPY_OUT)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: could not get COPY data stream: %s"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (copybuf != NULL)
|
|
|
|
{
|
|
|
|
PQfreemem(copybuf);
|
|
|
|
copybuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = PQgetCopyData(conn, ©buf, 0);
|
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
/*
|
2013-01-05 16:54:06 +01:00
|
|
|
* End of chunk. If requested, and this is the base tablespace,
|
2018-11-25 16:31:16 +01:00
|
|
|
* write configuration file into the tarfile. When done, close the
|
|
|
|
* file (but not stdout).
|
2011-01-23 12:21:23 +01:00
|
|
|
*
|
|
|
|
* Also, write two completely empty blocks at the end of the tar
|
|
|
|
* file, as required by some tar programs.
|
|
|
|
*/
|
|
|
|
char zerobuf[1024];
|
|
|
|
|
|
|
|
MemSet(zerobuf, 0, sizeof(zerobuf));
|
2013-01-05 16:54:06 +01:00
|
|
|
|
|
|
|
if (basetablespace && writerecoveryconf)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2013-01-05 16:54:06 +01:00
|
|
|
char header[512];
|
|
|
|
|
2018-11-25 16:31:16 +01:00
|
|
|
if (!found_postgresql_auto_conf)
|
|
|
|
{
|
|
|
|
int padding;
|
|
|
|
|
|
|
|
tarCreateHeader(header, "postgresql.auto.conf", NULL,
|
|
|
|
recoveryconfcontents->len,
|
|
|
|
pg_file_create_mode, 04000, 02000,
|
|
|
|
time(NULL));
|
|
|
|
|
|
|
|
padding = ((recoveryconfcontents->len + 511) & ~511) - recoveryconfcontents->len;
|
|
|
|
|
|
|
|
WRITE_TAR_DATA(header, sizeof(header));
|
|
|
|
WRITE_TAR_DATA(recoveryconfcontents->data, recoveryconfcontents->len);
|
|
|
|
if (padding)
|
|
|
|
WRITE_TAR_DATA(zerobuf, padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
tarCreateHeader(header, "standby.signal", NULL,
|
|
|
|
0, /* zero-length file */
|
2018-04-07 23:45:39 +02:00
|
|
|
pg_file_create_mode, 04000, 02000,
|
2013-01-05 16:54:06 +01:00
|
|
|
time(NULL));
|
|
|
|
|
|
|
|
WRITE_TAR_DATA(header, sizeof(header));
|
2018-11-25 16:31:16 +01:00
|
|
|
WRITE_TAR_DATA(zerobuf, 511);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
/* 2 * 512 bytes empty data at end of file */
|
|
|
|
WRITE_TAR_DATA(zerobuf, sizeof(zerobuf));
|
|
|
|
|
2011-05-29 00:17:37 +02:00
|
|
|
#ifdef HAVE_LIBZ
|
2012-03-28 18:19:22 +02:00
|
|
|
if (ztarfile != NULL)
|
|
|
|
{
|
|
|
|
if (gzclose(ztarfile) != 0)
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: could not close compressed file \"%s\": %s\n"),
|
2012-03-28 18:19:22 +02:00
|
|
|
progname, filename, get_gz_error(ztarfile));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2012-03-28 18:19:22 +02:00
|
|
|
}
|
2011-05-29 00:17:37 +02:00
|
|
|
}
|
|
|
|
else
|
2011-01-23 12:21:23 +01:00
|
|
|
#endif
|
2012-03-28 18:19:22 +02:00
|
|
|
{
|
|
|
|
if (strcmp(basedir, "-") != 0)
|
|
|
|
{
|
|
|
|
if (fclose(tarfile) != 0)
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not close file \"%s\": %s\n"),
|
2012-03-28 18:19:22 +02:00
|
|
|
progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2012-03-28 18:19:22 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (r == -2)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: could not read COPY data: %s"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
if (!writerecoveryconf || !basetablespace)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2013-01-05 16:54:06 +01:00
|
|
|
/*
|
2018-11-25 16:31:16 +01:00
|
|
|
* When not writing config file, or when not working on the base
|
|
|
|
* tablespace, we never have to look for an existing configuration
|
2013-01-05 16:54:06 +01:00
|
|
|
* file in the stream.
|
|
|
|
*/
|
|
|
|
WRITE_TAR_DATA(copybuf, r);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-05 16:54:06 +01:00
|
|
|
/*
|
2018-11-25 16:31:16 +01:00
|
|
|
* Look for a config file in the existing tar stream. If it's
|
2013-01-05 16:54:06 +01:00
|
|
|
* there, we must skip it so we can later overwrite it with our
|
|
|
|
* own version of the file.
|
|
|
|
*
|
|
|
|
* To do this, we have to process the individual files inside the
|
|
|
|
* TAR stream. The stream consists of a header and zero or more
|
|
|
|
* chunks, all 512 bytes long. The stream from the server is
|
|
|
|
* broken up into smaller pieces, so we have to track the size of
|
|
|
|
* the files to find the next header structure.
|
|
|
|
*/
|
|
|
|
int rr = r;
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
while (rr > 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2013-01-05 16:54:06 +01:00
|
|
|
if (in_tarhdr)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We're currently reading a header structure inside the
|
|
|
|
* TAR stream, i.e. the file metadata.
|
|
|
|
*/
|
|
|
|
if (tarhdrsz < 512)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Copy the header structure into tarhdr in case the
|
|
|
|
* header is not aligned to 512 bytes or it's not
|
|
|
|
* returned in whole by the last PQgetCopyData call.
|
|
|
|
*/
|
|
|
|
int hdrleft;
|
|
|
|
int bytes2copy;
|
|
|
|
|
|
|
|
hdrleft = 512 - tarhdrsz;
|
|
|
|
bytes2copy = (rr > hdrleft ? hdrleft : rr);
|
|
|
|
|
|
|
|
memcpy(&tarhdr[tarhdrsz], copybuf + pos, bytes2copy);
|
|
|
|
|
|
|
|
rr -= bytes2copy;
|
|
|
|
pos += bytes2copy;
|
|
|
|
tarhdrsz += bytes2copy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have the complete header structure in tarhdr,
|
2018-11-25 16:31:16 +01:00
|
|
|
* look at the file metadata: we may want append
|
|
|
|
* recovery info into postgresql.auto.conf and skip
|
|
|
|
* standby.signal file. In both cases we must
|
|
|
|
* calculate tar padding
|
2013-01-05 16:54:06 +01:00
|
|
|
*/
|
2018-11-25 16:31:16 +01:00
|
|
|
skip_file = (strcmp(&tarhdr[0], "standby.signal") == 0);
|
|
|
|
is_postgresql_auto_conf = (strcmp(&tarhdr[0], "postgresql.auto.conf") == 0);
|
2013-01-05 16:54:06 +01:00
|
|
|
|
Adopt the GNU convention for handling tar-archive members exceeding 8GB.
The POSIX standard for tar headers requires archive member sizes to be
printed in octal with at most 11 digits, limiting the representable file
size to 8GB. However, GNU tar and apparently most other modern tars
support a convention in which oversized values can be stored in base-256,
allowing any practical file to be a tar member. Adopt this convention
to remove two limitations:
* pg_dump with -Ft output format failed if the contents of any one table
exceeded 8GB.
* pg_basebackup failed if the data directory contained any file exceeding
8GB. (This would be a fatal problem for installations configured with a
table segment size of 8GB or more, and it has also been seen to fail when
large core dump files exist in the data directory.)
File sizes under 8GB are still printed in octal, so that no compatibility
issues are created except in cases that would have failed entirely before.
In addition, this patch fixes several bugs in the same area:
* In 9.3 and later, we'd defined tarCreateHeader's file-size argument as
size_t, which meant that on 32-bit machines it would write a corrupt tar
header for file sizes between 4GB and 8GB, even though no error was raised.
This broke both "pg_dump -Ft" and pg_basebackup for such cases.
* pg_restore from a tar archive would fail on tables of size between 4GB
and 8GB, on machines where either "size_t" or "unsigned long" is 32 bits.
This happened even with an archive file not affected by the previous bug.
* pg_basebackup would fail if there were files of size between 4GB and 8GB,
even on 64-bit machines.
* In 9.3 and later, "pg_basebackup -Ft" failed entirely, for any file size,
on 64-bit big-endian machines.
In view of these potential data-loss bugs, back-patch to all supported
branches, even though removal of the documented 8GB limit might otherwise
be considered a new feature rather than a bug fix.
2015-11-22 02:21:31 +01:00
|
|
|
filesz = read_tar_number(&tarhdr[124], 12);
|
2018-11-25 16:31:16 +01:00
|
|
|
file_padding_len = ((filesz + 511) & ~511) - filesz;
|
|
|
|
|
|
|
|
if (is_postgresql_auto_conf && writerecoveryconf)
|
|
|
|
{
|
|
|
|
/* replace tar header */
|
|
|
|
char header[512];
|
2013-01-05 16:54:06 +01:00
|
|
|
|
2018-11-25 16:31:16 +01:00
|
|
|
tarCreateHeader(header, "postgresql.auto.conf", NULL,
|
|
|
|
filesz + recoveryconfcontents->len,
|
|
|
|
pg_file_create_mode, 04000, 02000,
|
|
|
|
time(NULL));
|
|
|
|
|
|
|
|
WRITE_TAR_DATA(header, sizeof(header));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* copy stream with padding */
|
|
|
|
filesz += file_padding_len;
|
|
|
|
|
|
|
|
if (!skip_file)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we're not skipping the file, write the
|
|
|
|
* tar header unmodified.
|
|
|
|
*/
|
|
|
|
WRITE_TAR_DATA(tarhdr, 512);
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 16:54:06 +01:00
|
|
|
|
|
|
|
/* Next part is the file, not the header */
|
|
|
|
in_tarhdr = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We're processing a file's contents.
|
|
|
|
*/
|
|
|
|
if (filesz > 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We still have data to read (and possibly write).
|
|
|
|
*/
|
|
|
|
int bytes2write;
|
|
|
|
|
|
|
|
bytes2write = (filesz > rr ? rr : filesz);
|
|
|
|
|
|
|
|
if (!skip_file)
|
|
|
|
WRITE_TAR_DATA(copybuf + pos, bytes2write);
|
|
|
|
|
|
|
|
rr -= bytes2write;
|
|
|
|
pos += bytes2write;
|
|
|
|
filesz -= bytes2write;
|
|
|
|
}
|
2018-11-25 16:31:16 +01:00
|
|
|
else if (is_postgresql_auto_conf && writerecoveryconf)
|
|
|
|
{
|
|
|
|
/* append recovery config to postgresql.auto.conf */
|
|
|
|
int padding;
|
|
|
|
int tailsize;
|
|
|
|
|
|
|
|
tailsize = (512 - file_padding_len) + recoveryconfcontents->len;
|
|
|
|
padding = ((tailsize + 511) & ~511) - tailsize;
|
|
|
|
|
|
|
|
WRITE_TAR_DATA(recoveryconfcontents->data, recoveryconfcontents->len);
|
|
|
|
|
|
|
|
if (padding)
|
|
|
|
{
|
|
|
|
char zerobuf[512];
|
|
|
|
|
|
|
|
MemSet(zerobuf, 0, sizeof(zerobuf));
|
|
|
|
WRITE_TAR_DATA(zerobuf, padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip original file padding */
|
|
|
|
is_postgresql_auto_conf = false;
|
|
|
|
skip_file = true;
|
|
|
|
filesz += file_padding_len;
|
|
|
|
|
|
|
|
found_postgresql_auto_conf = true;
|
|
|
|
}
|
2013-01-05 16:54:06 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No more data in the current file, the next piece of
|
|
|
|
* data (if any) will be a new file header structure.
|
|
|
|
*/
|
|
|
|
in_tarhdr = true;
|
|
|
|
skip_file = false;
|
2018-11-25 16:31:16 +01:00
|
|
|
is_postgresql_auto_conf = false;
|
2013-01-05 16:54:06 +01:00
|
|
|
tarhdrsz = 0;
|
|
|
|
filesz = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
totaldone += r;
|
2014-02-09 12:47:09 +01:00
|
|
|
progress_report(rownum, filename, false);
|
2011-01-23 12:21:23 +01:00
|
|
|
} /* while (1) */
|
2014-02-09 12:47:09 +01:00
|
|
|
progress_report(rownum, filename, true);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
if (copybuf != NULL)
|
|
|
|
PQfreemem(copybuf);
|
2016-09-29 18:00:00 +02:00
|
|
|
|
|
|
|
/* sync the resulting tar file, errors are not considered fatal */
|
2016-09-29 18:00:00 +02:00
|
|
|
if (do_sync && strcmp(basedir, "-") != 0)
|
2016-09-29 18:00:00 +02:00
|
|
|
(void) fsync_fname(filename, false, progname);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
2014-02-22 19:38:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve tablespace path, either relocated or original depending on whether
|
|
|
|
* -T was passed or not.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
get_tablespace_mapping(const char *dir)
|
|
|
|
{
|
|
|
|
TablespaceListCell *cell;
|
2017-11-01 15:20:05 +01:00
|
|
|
char canon_dir[MAXPGPATH];
|
|
|
|
|
|
|
|
/* Canonicalize path for comparison consistency */
|
|
|
|
strlcpy(canon_dir, dir, sizeof(canon_dir));
|
|
|
|
canonicalize_path(canon_dir);
|
2014-02-22 19:38:06 +01:00
|
|
|
|
|
|
|
for (cell = tablespace_dirs.head; cell; cell = cell->next)
|
2017-11-01 15:20:05 +01:00
|
|
|
if (strcmp(canon_dir, cell->old_dir) == 0)
|
2014-02-22 19:38:06 +01:00
|
|
|
return cell->new_dir;
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/*
|
|
|
|
* Receive a tar format stream from the connection to the server, and unpack
|
|
|
|
* the contents of it into a directory. Only files, directories and
|
|
|
|
* symlinks are supported, no other kinds of special files.
|
|
|
|
*
|
|
|
|
* If the data is for the main data directory, it will be restored in the
|
|
|
|
* specified directory. If it's for another tablespace, it will be restored
|
2014-02-22 19:38:06 +01:00
|
|
|
* in the original or mapped directory.
|
2011-01-23 12:21:23 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
|
|
|
|
{
|
|
|
|
char current_path[MAXPGPATH];
|
2011-08-16 10:19:50 +02:00
|
|
|
char filename[MAXPGPATH];
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
const char *mapped_tblspc_path;
|
Adopt the GNU convention for handling tar-archive members exceeding 8GB.
The POSIX standard for tar headers requires archive member sizes to be
printed in octal with at most 11 digits, limiting the representable file
size to 8GB. However, GNU tar and apparently most other modern tars
support a convention in which oversized values can be stored in base-256,
allowing any practical file to be a tar member. Adopt this convention
to remove two limitations:
* pg_dump with -Ft output format failed if the contents of any one table
exceeded 8GB.
* pg_basebackup failed if the data directory contained any file exceeding
8GB. (This would be a fatal problem for installations configured with a
table segment size of 8GB or more, and it has also been seen to fail when
large core dump files exist in the data directory.)
File sizes under 8GB are still printed in octal, so that no compatibility
issues are created except in cases that would have failed entirely before.
In addition, this patch fixes several bugs in the same area:
* In 9.3 and later, we'd defined tarCreateHeader's file-size argument as
size_t, which meant that on 32-bit machines it would write a corrupt tar
header for file sizes between 4GB and 8GB, even though no error was raised.
This broke both "pg_dump -Ft" and pg_basebackup for such cases.
* pg_restore from a tar archive would fail on tables of size between 4GB
and 8GB, on machines where either "size_t" or "unsigned long" is 32 bits.
This happened even with an archive file not affected by the previous bug.
* pg_basebackup would fail if there were files of size between 4GB and 8GB,
even on 64-bit machines.
* In 9.3 and later, "pg_basebackup -Ft" failed entirely, for any file size,
on 64-bit big-endian machines.
In view of these potential data-loss bugs, back-patch to all supported
branches, even though removal of the documented 8GB limit might otherwise
be considered a new feature rather than a bug fix.
2015-11-22 02:21:31 +01:00
|
|
|
pgoff_t current_len_left = 0;
|
2011-01-23 19:06:38 +01:00
|
|
|
int current_padding = 0;
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
bool basetablespace;
|
2011-01-23 12:21:23 +01:00
|
|
|
char *copybuf = NULL;
|
|
|
|
FILE *file = NULL;
|
|
|
|
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
basetablespace = PQgetisnull(res, rownum, 0);
|
2013-01-05 16:54:06 +01:00
|
|
|
if (basetablespace)
|
2014-02-17 17:20:21 +01:00
|
|
|
strlcpy(current_path, basedir, sizeof(current_path));
|
2011-01-23 12:21:23 +01:00
|
|
|
else
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
strlcpy(current_path,
|
|
|
|
get_tablespace_mapping(PQgetvalue(res, rownum, 1)),
|
|
|
|
sizeof(current_path));
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the COPY data
|
|
|
|
*/
|
|
|
|
res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(res) != PGRES_COPY_OUT)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: could not get COPY data stream: %s"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (copybuf != NULL)
|
|
|
|
{
|
|
|
|
PQfreemem(copybuf);
|
|
|
|
copybuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = PQgetCopyData(conn, ©buf, 0);
|
|
|
|
|
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* End of chunk
|
|
|
|
*/
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (r == -2)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: could not read COPY data: %s"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
int filemode;
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No current file, so this must be the header for a new file
|
|
|
|
*/
|
|
|
|
if (r != 512)
|
|
|
|
{
|
2011-07-26 21:54:29 +02:00
|
|
|
fprintf(stderr, _("%s: invalid tar block header size: %d\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, r);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
totaldone += 512;
|
|
|
|
|
Adopt the GNU convention for handling tar-archive members exceeding 8GB.
The POSIX standard for tar headers requires archive member sizes to be
printed in octal with at most 11 digits, limiting the representable file
size to 8GB. However, GNU tar and apparently most other modern tars
support a convention in which oversized values can be stored in base-256,
allowing any practical file to be a tar member. Adopt this convention
to remove two limitations:
* pg_dump with -Ft output format failed if the contents of any one table
exceeded 8GB.
* pg_basebackup failed if the data directory contained any file exceeding
8GB. (This would be a fatal problem for installations configured with a
table segment size of 8GB or more, and it has also been seen to fail when
large core dump files exist in the data directory.)
File sizes under 8GB are still printed in octal, so that no compatibility
issues are created except in cases that would have failed entirely before.
In addition, this patch fixes several bugs in the same area:
* In 9.3 and later, we'd defined tarCreateHeader's file-size argument as
size_t, which meant that on 32-bit machines it would write a corrupt tar
header for file sizes between 4GB and 8GB, even though no error was raised.
This broke both "pg_dump -Ft" and pg_basebackup for such cases.
* pg_restore from a tar archive would fail on tables of size between 4GB
and 8GB, on machines where either "size_t" or "unsigned long" is 32 bits.
This happened even with an archive file not affected by the previous bug.
* pg_basebackup would fail if there were files of size between 4GB and 8GB,
even on 64-bit machines.
* In 9.3 and later, "pg_basebackup -Ft" failed entirely, for any file size,
on 64-bit big-endian machines.
In view of these potential data-loss bugs, back-patch to all supported
branches, even though removal of the documented 8GB limit might otherwise
be considered a new feature rather than a bug fix.
2015-11-22 02:21:31 +01:00
|
|
|
current_len_left = read_tar_number(©buf[124], 12);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/* Set permissions on the file */
|
Adopt the GNU convention for handling tar-archive members exceeding 8GB.
The POSIX standard for tar headers requires archive member sizes to be
printed in octal with at most 11 digits, limiting the representable file
size to 8GB. However, GNU tar and apparently most other modern tars
support a convention in which oversized values can be stored in base-256,
allowing any practical file to be a tar member. Adopt this convention
to remove two limitations:
* pg_dump with -Ft output format failed if the contents of any one table
exceeded 8GB.
* pg_basebackup failed if the data directory contained any file exceeding
8GB. (This would be a fatal problem for installations configured with a
table segment size of 8GB or more, and it has also been seen to fail when
large core dump files exist in the data directory.)
File sizes under 8GB are still printed in octal, so that no compatibility
issues are created except in cases that would have failed entirely before.
In addition, this patch fixes several bugs in the same area:
* In 9.3 and later, we'd defined tarCreateHeader's file-size argument as
size_t, which meant that on 32-bit machines it would write a corrupt tar
header for file sizes between 4GB and 8GB, even though no error was raised.
This broke both "pg_dump -Ft" and pg_basebackup for such cases.
* pg_restore from a tar archive would fail on tables of size between 4GB
and 8GB, on machines where either "size_t" or "unsigned long" is 32 bits.
This happened even with an archive file not affected by the previous bug.
* pg_basebackup would fail if there were files of size between 4GB and 8GB,
even on 64-bit machines.
* In 9.3 and later, "pg_basebackup -Ft" failed entirely, for any file size,
on 64-bit big-endian machines.
In view of these potential data-loss bugs, back-patch to all supported
branches, even though removal of the documented 8GB limit might otherwise
be considered a new feature rather than a bug fix.
2015-11-22 02:21:31 +01:00
|
|
|
filemode = read_tar_number(©buf[100], 8);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All files are padded up to 512 bytes
|
|
|
|
*/
|
|
|
|
current_padding =
|
|
|
|
((current_len_left + 511) & ~511) - current_len_left;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First part of header is zero terminated filename
|
|
|
|
*/
|
2012-07-31 16:11:11 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%s/%s", current_path,
|
|
|
|
copybuf);
|
2011-08-16 10:19:50 +02:00
|
|
|
if (filename[strlen(filename) - 1] == '/')
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Ends in a slash means directory or symlink to directory
|
|
|
|
*/
|
|
|
|
if (copybuf[156] == '5')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Directory
|
|
|
|
*/
|
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
|
|
|
filename[strlen(filename) - 1] = '\0'; /* Remove trailing slash */
|
2018-04-07 23:45:39 +02:00
|
|
|
if (mkdir(filename, pg_dir_create_mode) != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2011-10-26 20:13:33 +02:00
|
|
|
/*
|
2016-10-20 17:24:37 +02:00
|
|
|
* When streaming WAL, pg_wal (or pg_xlog for pre-9.6
|
2017-05-17 22:31:56 +02:00
|
|
|
* clusters) will have been created by the wal
|
|
|
|
* receiver process. Also, when the WAL directory
|
|
|
|
* location was specified, pg_wal (or pg_xlog) has
|
|
|
|
* already been created as a symbolic link before
|
|
|
|
* starting the actual backup. So just ignore creation
|
|
|
|
* failures on related directories.
|
2011-10-26 20:13:33 +02:00
|
|
|
*/
|
2016-10-20 17:24:37 +02:00
|
|
|
if (!((pg_str_endswith(filename, "/pg_wal") ||
|
2017-05-17 22:31:56 +02:00
|
|
|
pg_str_endswith(filename, "/pg_xlog") ||
|
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
|
|
|
pg_str_endswith(filename, "/archive_status")) &&
|
2015-01-03 20:51:52 +01:00
|
|
|
errno == EEXIST))
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: could not create directory \"%s\": %s\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
#ifndef WIN32
|
2011-08-16 10:19:50 +02:00
|
|
|
if (chmod(filename, (mode_t) filemode))
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not set permissions on directory \"%s\": %s\n"),
|
2011-08-16 10:19:50 +02:00
|
|
|
progname, filename, strerror(errno));
|
2011-01-23 12:21:23 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (copybuf[156] == '2')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Symbolic link
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
*
|
2014-08-22 12:45:38 +02:00
|
|
|
* It's most likely a link in pg_tblspc directory, to the
|
|
|
|
* location of a tablespace. Apply any tablespace mapping
|
2015-05-24 03:35:49 +02:00
|
|
|
* given on the command line (--tablespace-mapping). (We
|
|
|
|
* blindly apply the mapping without checking that the
|
|
|
|
* link really is inside pg_tblspc. We don't expect there
|
|
|
|
* to be other symlinks in a data directory, but if there
|
|
|
|
* are, you can call it an undocumented feature that you
|
|
|
|
* can map them too.)
|
2011-01-23 12:21:23 +01:00
|
|
|
*/
|
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
|
|
|
filename[strlen(filename) - 1] = '\0'; /* Remove trailing slash */
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
|
|
|
|
mapped_tblspc_path = get_tablespace_mapping(©buf[157]);
|
|
|
|
if (symlink(mapped_tblspc_path, filename) != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2011-05-24 20:25:28 +02:00
|
|
|
_("%s: could not create symbolic link from \"%s\" to \"%s\": %s\n"),
|
Change the way pg_basebackup's tablespace mapping is implemented.
Previously, we would first create the symlinks the way they are in the
original system, and at the end replace them with the mapped symlinks.
That never really made much sense, so now we create the symlink pointing
to the correct location to begin with, so that there's no need to fix
them at the end.
The old coding didn't work correctly on Windows, because Windows junction
points look more like directories than files, and ought to be removed with
rmdir rather than unlink. Also, it incorrectly used "%d" rather than "%u"
to print an Oid, but that's gone now.
Report and patch by Amit Kapila, with minor changes by me. Reviewed by
MauMau. Backpatch to 9.4, where the --tablespace feature was added.
2014-08-22 09:16:26 +02:00
|
|
|
progname, filename, mapped_tblspc_path,
|
|
|
|
strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: unrecognized link indicator \"%c\"\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, copybuf[156]);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
continue; /* directory or link handled */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* regular file
|
|
|
|
*/
|
2011-08-16 10:19:50 +02:00
|
|
|
file = fopen(filename, "wb");
|
2011-01-23 12:21:23 +01:00
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create file \"%s\": %s\n"),
|
2011-08-16 10:19:50 +02:00
|
|
|
progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef WIN32
|
2011-08-16 10:19:50 +02:00
|
|
|
if (chmod(filename, (mode_t) filemode))
|
2011-01-23 12:21:23 +01:00
|
|
|
fprintf(stderr, _("%s: could not set permissions on file \"%s\": %s\n"),
|
2011-08-16 10:19:50 +02:00
|
|
|
progname, filename, strerror(errno));
|
2011-01-23 12:21:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (current_len_left == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Done with this file, next one will be a new tar header
|
|
|
|
*/
|
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} /* new file */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Continuing blocks in existing file
|
|
|
|
*/
|
|
|
|
if (current_len_left == 0 && r == current_padding)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Received the padding block for this file, ignore it and
|
|
|
|
* close the file, then move on to the next tar header.
|
|
|
|
*/
|
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
totaldone += r;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite(copybuf, r, 1, file) != 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"),
|
2011-08-16 10:19:50 +02:00
|
|
|
progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
totaldone += r;
|
2014-02-09 12:47:09 +01:00
|
|
|
progress_report(rownum, filename, false);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
current_len_left -= r;
|
|
|
|
if (current_len_left == 0 && current_padding == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Received the last block, and there is no padding to be
|
|
|
|
* expected. Close the file and move on to the next tar
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} /* continuing data in existing file */
|
|
|
|
} /* loop over all data blocks */
|
2014-02-09 12:47:09 +01:00
|
|
|
progress_report(rownum, filename, true);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
if (file != NULL)
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: COPY stream ended before last file was finished\n"),
|
|
|
|
progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (copybuf != NULL)
|
|
|
|
PQfreemem(copybuf);
|
2013-01-05 16:54:06 +01:00
|
|
|
|
2013-01-09 16:57:32 +01:00
|
|
|
if (basetablespace && writerecoveryconf)
|
2013-01-05 16:54:06 +01:00
|
|
|
WriteRecoveryConf();
|
2016-09-29 18:00:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No data is synced here, everything is done for all tablespaces at the
|
|
|
|
* end.
|
|
|
|
*/
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:34:27 +02:00
|
|
|
/*
|
|
|
|
* Escape a string so that it can be used as a value in a key-value pair
|
|
|
|
* a configuration file.
|
2013-01-05 16:54:06 +01:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
escape_quotes(const char *src)
|
|
|
|
{
|
|
|
|
char *result = escape_single_quotes_ascii(src);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-25 16:31:16 +01:00
|
|
|
* Create a configuration file in memory using a PQExpBuffer
|
2013-01-05 16:54:06 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GenerateRecoveryConf(PGconn *conn)
|
|
|
|
{
|
|
|
|
PQconninfoOption *connOptions;
|
|
|
|
PQconninfoOption *option;
|
2013-05-20 18:34:27 +02:00
|
|
|
PQExpBufferData conninfo_buf;
|
|
|
|
char *escaped;
|
2013-01-05 16:54:06 +01:00
|
|
|
|
|
|
|
recoveryconfcontents = createPQExpBuffer();
|
|
|
|
if (!recoveryconfcontents)
|
|
|
|
{
|
2013-03-10 21:53:04 +01:00
|
|
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
connOptions = PQconninfo(conn);
|
|
|
|
if (connOptions == NULL)
|
|
|
|
{
|
2013-03-10 21:53:04 +01:00
|
|
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:34:27 +02:00
|
|
|
initPQExpBuffer(&conninfo_buf);
|
2013-01-05 16:54:06 +01:00
|
|
|
for (option = connOptions; option && option->keyword; option++)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Do not emit this setting if: - the setting is "replication",
|
|
|
|
* "dbname" or "fallback_application_name", since these would be
|
|
|
|
* overridden by the libpqwalreceiver module anyway. - not set or
|
|
|
|
* empty.
|
|
|
|
*/
|
|
|
|
if (strcmp(option->keyword, "replication") == 0 ||
|
|
|
|
strcmp(option->keyword, "dbname") == 0 ||
|
|
|
|
strcmp(option->keyword, "fallback_application_name") == 0 ||
|
|
|
|
(option->val == NULL) ||
|
|
|
|
(option->val != NULL && option->val[0] == '\0'))
|
|
|
|
continue;
|
|
|
|
|
2013-05-20 18:34:27 +02:00
|
|
|
/* Separate key-value pairs with spaces */
|
|
|
|
if (conninfo_buf.len != 0)
|
2015-07-02 11:32:48 +02:00
|
|
|
appendPQExpBufferChar(&conninfo_buf, ' ');
|
2013-05-20 18:34:27 +02:00
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
/*
|
2013-05-20 18:34:27 +02:00
|
|
|
* Write "keyword=value" pieces, the value string is escaped and/or
|
|
|
|
* quoted if necessary.
|
2013-01-05 16:54:06 +01:00
|
|
|
*/
|
2016-08-22 04:05:57 +02:00
|
|
|
appendPQExpBuffer(&conninfo_buf, "%s=", option->keyword);
|
|
|
|
appendConnStrVal(&conninfo_buf, option->val);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:34:27 +02:00
|
|
|
/*
|
|
|
|
* Escape the connection string, so that it can be put in the config file.
|
|
|
|
* Note that this is different from the escaping of individual connection
|
|
|
|
* options above!
|
|
|
|
*/
|
|
|
|
escaped = escape_quotes(conninfo_buf.data);
|
|
|
|
appendPQExpBuffer(recoveryconfcontents, "primary_conninfo = '%s'\n", escaped);
|
|
|
|
free(escaped);
|
|
|
|
|
2015-07-22 03:06:45 +02:00
|
|
|
if (replication_slot)
|
|
|
|
{
|
|
|
|
escaped = escape_quotes(replication_slot);
|
|
|
|
appendPQExpBuffer(recoveryconfcontents, "primary_slot_name = '%s'\n", replication_slot);
|
|
|
|
free(escaped);
|
|
|
|
}
|
|
|
|
|
2013-05-20 18:34:27 +02:00
|
|
|
if (PQExpBufferBroken(recoveryconfcontents) ||
|
|
|
|
PQExpBufferDataBroken(conninfo_buf))
|
2013-01-05 16:54:06 +01:00
|
|
|
{
|
2013-03-10 21:53:04 +01:00
|
|
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:34:27 +02:00
|
|
|
termPQExpBuffer(&conninfo_buf);
|
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
PQconninfoFree(connOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2018-11-25 16:31:16 +01:00
|
|
|
* Write the configuration file into the directory specified in basedir,
|
2013-01-05 16:54:06 +01:00
|
|
|
* with the contents already collected in memory.
|
2018-11-25 16:31:16 +01:00
|
|
|
* Then write the signal file into the basedir also.
|
2013-01-05 16:54:06 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
WriteRecoveryConf(void)
|
|
|
|
{
|
|
|
|
char filename[MAXPGPATH];
|
|
|
|
FILE *cf;
|
|
|
|
|
2018-11-25 16:31:16 +01:00
|
|
|
snprintf(filename, MAXPGPATH, "%s/%s", basedir, "postgresql.auto.conf");
|
2013-01-05 16:54:06 +01:00
|
|
|
|
2018-11-25 16:31:16 +01:00
|
|
|
cf = fopen(filename, "a");
|
2013-01-05 16:54:06 +01:00
|
|
|
if (cf == NULL)
|
|
|
|
{
|
2018-11-25 16:31:16 +01:00
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"), progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite(recoveryconfcontents->data, recoveryconfcontents->len, 1, cf) != 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not write to file \"%s\": %s\n"),
|
|
|
|
progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-01-05 16:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(cf);
|
2018-11-25 16:31:16 +01:00
|
|
|
|
|
|
|
snprintf(filename, MAXPGPATH, "%s/%s", basedir, "standby.signal");
|
|
|
|
cf = fopen(filename, "w");
|
|
|
|
if (cf == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create file \"%s\": %s\n"), progname, filename, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2018-11-25 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(cf);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-03-27 22:31:41 +02:00
|
|
|
BaseBackup(void)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
PGresult *res;
|
2011-10-26 20:13:33 +02:00
|
|
|
char *sysidentifier;
|
2014-10-01 17:22:21 +02:00
|
|
|
TimeLineID latesttli;
|
|
|
|
TimeLineID starttli;
|
2014-02-27 22:55:57 +01:00
|
|
|
char *basebkp;
|
2011-01-23 12:21:23 +01:00
|
|
|
char escaped_label[MAXPGPATH];
|
2014-02-27 22:55:57 +01:00
|
|
|
char *maxrate_clause = NULL;
|
2011-01-23 12:21:23 +01:00
|
|
|
int i;
|
2011-02-03 13:46:23 +01:00
|
|
|
char xlogstart[64];
|
|
|
|
char xlogend[64];
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
int minServerMajor,
|
|
|
|
maxServerMajor;
|
2016-10-20 17:24:37 +02:00
|
|
|
int serverVersion,
|
|
|
|
serverMajor;
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2016-10-20 17:24:37 +02:00
|
|
|
Assert(conn != NULL);
|
2011-01-23 12:21:23 +01:00
|
|
|
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
/*
|
2013-05-29 22:58:43 +02:00
|
|
|
* Check server version. BASE_BACKUP command was introduced in 9.1, so we
|
|
|
|
* can't work with servers older than 9.1.
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
*/
|
|
|
|
minServerMajor = 901;
|
|
|
|
maxServerMajor = PG_VERSION_NUM / 100;
|
2016-10-20 17:24:37 +02:00
|
|
|
serverVersion = PQserverVersion(conn);
|
|
|
|
serverMajor = serverVersion / 100;
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
if (serverMajor < minServerMajor || serverMajor > maxServerMajor)
|
|
|
|
{
|
|
|
|
const char *serverver = PQparameterStatus(conn, "server_version");
|
2013-05-29 22:58:43 +02:00
|
|
|
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
fprintf(stderr, _("%s: incompatible server version %s\n"),
|
|
|
|
progname, serverver ? serverver : "'unknown'");
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If WAL streaming was requested, also check that the server is new
|
|
|
|
* enough for that.
|
|
|
|
*/
|
2017-01-09 16:03:47 +01:00
|
|
|
if (includewal == STREAM_WAL && !CheckServerVersionForStreaming(conn))
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
{
|
2017-01-04 10:40:38 +01:00
|
|
|
/*
|
|
|
|
* Error message already written in CheckServerVersionForStreaming(),
|
|
|
|
* but add a hint about using -X none.
|
|
|
|
*/
|
|
|
|
fprintf(stderr, _("HINT: use -X none or -X fetch to disable log streaming\n"));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
}
|
|
|
|
|
2013-01-05 16:54:06 +01:00
|
|
|
/*
|
2018-11-25 16:31:16 +01:00
|
|
|
* Build contents of configuration file if requested
|
2013-01-05 16:54:06 +01:00
|
|
|
*/
|
|
|
|
if (writerecoveryconf)
|
|
|
|
GenerateRecoveryConf(conn);
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
/*
|
|
|
|
* Run IDENTIFY_SYSTEM so we can get the timeline
|
|
|
|
*/
|
2014-10-01 17:22:21 +02:00
|
|
|
if (!RunIdentifySystem(conn, &sysidentifier, &latesttli, NULL, NULL))
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2011-02-03 13:46:23 +01:00
|
|
|
/*
|
|
|
|
* Start the actual backup
|
|
|
|
*/
|
2011-01-23 12:21:23 +01:00
|
|
|
PQescapeStringConn(conn, escaped_label, label, sizeof(escaped_label), &i);
|
2014-02-27 22:55:57 +01:00
|
|
|
|
|
|
|
if (maxrate > 0)
|
|
|
|
maxrate_clause = psprintf("MAX_RATE %u", maxrate);
|
|
|
|
|
2017-02-26 21:27:51 +01:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: initiating base backup, waiting for checkpoint to complete\n"),
|
2017-02-26 21:27:51 +01:00
|
|
|
progname);
|
|
|
|
|
|
|
|
if (showprogress && !verbose)
|
2017-12-01 15:21:34 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "waiting for checkpoint");
|
|
|
|
if (isatty(fileno(stderr)))
|
|
|
|
fprintf(stderr, "\r");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2017-02-26 21:27:51 +01:00
|
|
|
|
2014-02-27 22:55:57 +01:00
|
|
|
basebkp =
|
2018-04-03 13:47:16 +02:00
|
|
|
psprintf("BASE_BACKUP LABEL '%s' %s %s %s %s %s %s %s",
|
2014-02-27 22:55:57 +01:00
|
|
|
escaped_label,
|
|
|
|
showprogress ? "PROGRESS" : "",
|
2017-01-09 16:03:47 +01:00
|
|
|
includewal == FETCH_WAL ? "WAL" : "",
|
2014-02-27 22:55:57 +01:00
|
|
|
fastcheckpoint ? "FAST" : "",
|
2017-01-09 16:03:47 +01:00
|
|
|
includewal == NO_WAL ? "" : "NOWAIT",
|
2015-05-12 15:29:10 +02:00
|
|
|
maxrate_clause ? maxrate_clause : "",
|
2018-04-03 13:47:16 +02:00
|
|
|
format == 't' ? "TABLESPACE_MAP" : "",
|
|
|
|
verify_checksums ? "" : "NOVERIFY_CHECKSUMS");
|
2014-02-27 22:55:57 +01:00
|
|
|
|
|
|
|
if (PQsendQuery(conn, basebkp) == 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
|
|
|
|
progname, "BASE_BACKUP", PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-05-12 19:51:27 +02:00
|
|
|
* Get the starting WAL location
|
2011-01-23 12:21:23 +01:00
|
|
|
*/
|
|
|
|
res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: could not initiate base backup: %s"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
if (PQntuples(res) != 1)
|
2011-02-03 13:46:23 +01:00
|
|
|
{
|
Make pg_receivexlog and pg_basebackup -X stream work across timeline switches.
This mirrors the changes done earlier to the server in standby mode. When
receivelog reaches the end of a timeline, as reported by the server, it
fetches the timeline history file of the next timeline, and restarts
streaming from the new timeline by issuing a new START_STREAMING command.
When pg_receivexlog crosses a timeline, it leaves the .partial suffix on the
last segment on the old timeline. This helps you to tell apart a partial
segment left in the directory because of a timeline switch, and a completed
segment. If you just follow a single server, it won't make a difference, but
it can be significant in more complicated scenarios where new WAL is still
generated on the old timeline.
This includes two small changes to the streaming replication protocol:
First, when you reach the end of timeline while streaming, the server now
sends the TLI of the next timeline in the server's history to the client.
pg_receivexlog uses that as the next timeline, so that it doesn't need to
parse the timeline history file like a standby server does. Second, when
BASE_BACKUP command sends the begin and end WAL positions, it now also sends
the timeline IDs corresponding the positions.
2013-01-17 19:23:00 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields\n"),
|
|
|
|
progname, PQntuples(res), PQnfields(res), 1, 2);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-02-03 13:46:23 +01:00
|
|
|
}
|
Make pg_receivexlog and pg_basebackup -X stream work across timeline switches.
This mirrors the changes done earlier to the server in standby mode. When
receivelog reaches the end of a timeline, as reported by the server, it
fetches the timeline history file of the next timeline, and restarts
streaming from the new timeline by issuing a new START_STREAMING command.
When pg_receivexlog crosses a timeline, it leaves the .partial suffix on the
last segment on the old timeline. This helps you to tell apart a partial
segment left in the directory because of a timeline switch, and a completed
segment. If you just follow a single server, it won't make a difference, but
it can be significant in more complicated scenarios where new WAL is still
generated on the old timeline.
This includes two small changes to the streaming replication protocol:
First, when you reach the end of timeline while streaming, the server now
sends the TLI of the next timeline in the server's history to the client.
pg_receivexlog uses that as the next timeline, so that it doesn't need to
parse the timeline history file like a standby server does. Second, when
BASE_BACKUP command sends the begin and end WAL positions, it now also sends
the timeline IDs corresponding the positions.
2013-01-17 19:23:00 +01:00
|
|
|
|
2014-02-17 17:20:21 +01:00
|
|
|
strlcpy(xlogstart, PQgetvalue(res, 0, 0), sizeof(xlogstart));
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2017-02-26 21:27:51 +01:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, _("%s: checkpoint completed\n"), progname);
|
|
|
|
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
/*
|
|
|
|
* 9.3 and later sends the TLI of the starting point. With older servers,
|
2013-05-29 22:58:43 +02:00
|
|
|
* assume it's the same as the latest timeline reported by
|
|
|
|
* IDENTIFY_SYSTEM.
|
Make pg_basebackup work with pre-9.3 servers, and add server version check.
A new 'starttli' field was added to the response of BASE_BACKUP command.
Make pg_basebackup tolerate the case that it's missing, so that it still
works with older servers.
Add an explicit check for the server version, so that you get a nicer error
message if you try to use it with a pre-9.1 server.
The streaming protocol message format changed in 9.3, so -X stream still won't
work with pre-9.3 servers. I added a version check to ReceiveXLogStream()
earlier, but write that slightly differently, so that in 9.4, it will still
work with a 9.3 server. (In 9.4, the error message needs to be adjusted to
"9.3 or above", though). Also, if the version check fails, don't retry.
2013-03-22 12:02:59 +01:00
|
|
|
*/
|
|
|
|
if (PQnfields(res) >= 2)
|
|
|
|
starttli = atoi(PQgetvalue(res, 0, 1));
|
|
|
|
else
|
|
|
|
starttli = latesttli;
|
2011-02-03 13:46:23 +01:00
|
|
|
PQclear(res);
|
|
|
|
MemSet(xlogend, 0, sizeof(xlogend));
|
|
|
|
|
2017-01-09 16:03:47 +01:00
|
|
|
if (verbose && includewal != NO_WAL)
|
2017-05-12 17:49:56 +02:00
|
|
|
fprintf(stderr, _("%s: write-ahead log start point: %s on timeline %u\n"),
|
2017-02-26 21:29:34 +01:00
|
|
|
progname, xlogstart, starttli);
|
Make pg_receivexlog and pg_basebackup -X stream work across timeline switches.
This mirrors the changes done earlier to the server in standby mode. When
receivelog reaches the end of a timeline, as reported by the server, it
fetches the timeline history file of the next timeline, and restarts
streaming from the new timeline by issuing a new START_STREAMING command.
When pg_receivexlog crosses a timeline, it leaves the .partial suffix on the
last segment on the old timeline. This helps you to tell apart a partial
segment left in the directory because of a timeline switch, and a completed
segment. If you just follow a single server, it won't make a difference, but
it can be significant in more complicated scenarios where new WAL is still
generated on the old timeline.
This includes two small changes to the streaming replication protocol:
First, when you reach the end of timeline while streaming, the server now
sends the TLI of the next timeline in the server's history to the client.
pg_receivexlog uses that as the next timeline, so that it doesn't need to
parse the timeline history file like a standby server does. Second, when
BASE_BACKUP command sends the begin and end WAL positions, it now also sends
the timeline IDs corresponding the positions.
2013-01-17 19:23:00 +01:00
|
|
|
|
2011-02-03 13:46:23 +01:00
|
|
|
/*
|
|
|
|
* Get the header
|
|
|
|
*/
|
|
|
|
res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: could not get backup header: %s"),
|
2011-02-03 13:46:23 +01:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-02-03 13:46:23 +01:00
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
if (PQntuples(res) < 1)
|
|
|
|
{
|
2011-05-04 19:56:52 +02:00
|
|
|
fprintf(stderr, _("%s: no data returned from server\n"), progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sum up the total size, for progress reporting
|
|
|
|
*/
|
|
|
|
totalsize = totaldone = 0;
|
|
|
|
tablespacecount = PQntuples(res);
|
|
|
|
for (i = 0; i < PQntuples(res); i++)
|
|
|
|
{
|
2014-02-09 12:47:09 +01:00
|
|
|
totalsize += atol(PQgetvalue(res, i, 2));
|
2011-01-23 12:21:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify tablespace directories are empty. Don't bother with the
|
|
|
|
* first once since it can be relocated, and it will be checked before
|
|
|
|
* we do anything anyway.
|
|
|
|
*/
|
2011-01-30 21:30:09 +01:00
|
|
|
if (format == 'p' && !PQgetisnull(res, i, 1))
|
2014-02-22 19:38:06 +01:00
|
|
|
{
|
2019-01-29 01:16:24 +01:00
|
|
|
char *path = unconstify(char *, get_tablespace_mapping(PQgetvalue(res, i, 1)));
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
verify_dir_is_empty_or_create(path, &made_tablespace_dirs, &found_tablespace_dirs);
|
2014-02-22 19:38:06 +01:00
|
|
|
}
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When writing to stdout, require a single tablespace
|
|
|
|
*/
|
|
|
|
if (format == 't' && strcmp(basedir, "-") == 0 && PQntuples(res) > 1)
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: can only write single tablespace to stdout, database has %d\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, PQntuples(res));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
/*
|
|
|
|
* If we're streaming WAL, start the streaming session before we start
|
|
|
|
* receiving the actual data chunks.
|
|
|
|
*/
|
2017-01-09 16:03:47 +01:00
|
|
|
if (includewal == STREAM_WAL)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, _("%s: starting background WAL receiver\n"),
|
|
|
|
progname);
|
Make pg_receivexlog and pg_basebackup -X stream work across timeline switches.
This mirrors the changes done earlier to the server in standby mode. When
receivelog reaches the end of a timeline, as reported by the server, it
fetches the timeline history file of the next timeline, and restarts
streaming from the new timeline by issuing a new START_STREAMING command.
When pg_receivexlog crosses a timeline, it leaves the .partial suffix on the
last segment on the old timeline. This helps you to tell apart a partial
segment left in the directory because of a timeline switch, and a completed
segment. If you just follow a single server, it won't make a difference, but
it can be significant in more complicated scenarios where new WAL is still
generated on the old timeline.
This includes two small changes to the streaming replication protocol:
First, when you reach the end of timeline while streaming, the server now
sends the TLI of the next timeline in the server's history to the client.
pg_receivexlog uses that as the next timeline, so that it doesn't need to
parse the timeline history file like a standby server does. Second, when
BASE_BACKUP command sends the begin and end WAL positions, it now also sends
the timeline IDs corresponding the positions.
2013-01-17 19:23:00 +01:00
|
|
|
StartLogStreamer(xlogstart, starttli, sysidentifier);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/*
|
|
|
|
* Start receiving chunks
|
|
|
|
*/
|
|
|
|
for (i = 0; i < PQntuples(res); i++)
|
|
|
|
{
|
|
|
|
if (format == 't')
|
|
|
|
ReceiveTarFile(conn, res, i);
|
|
|
|
else
|
|
|
|
ReceiveAndUnpackTarFile(conn, res, i);
|
|
|
|
} /* Loop over all tablespaces */
|
|
|
|
|
|
|
|
if (showprogress)
|
|
|
|
{
|
2014-02-09 12:47:09 +01:00
|
|
|
progress_report(PQntuples(res), NULL, true);
|
2017-12-01 15:21:34 +01:00
|
|
|
if (isatty(fileno(stderr)))
|
|
|
|
fprintf(stderr, "\n"); /* Need to move to next line */
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
2014-02-22 19:38:06 +01:00
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
PQclear(res);
|
|
|
|
|
2011-02-03 13:46:23 +01:00
|
|
|
/*
|
|
|
|
* Get the stop position
|
|
|
|
*/
|
|
|
|
res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: could not get write-ahead log end position from server: %s"),
|
2012-03-28 14:43:35 +02:00
|
|
|
progname, PQerrorMessage(conn));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-02-03 13:46:23 +01:00
|
|
|
}
|
|
|
|
if (PQntuples(res) != 1)
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: no write-ahead log end position returned from server\n"),
|
2011-02-03 13:46:23 +01:00
|
|
|
progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-02-03 13:46:23 +01:00
|
|
|
}
|
2014-02-17 17:20:21 +01:00
|
|
|
strlcpy(xlogend, PQgetvalue(res, 0, 0), sizeof(xlogend));
|
2017-01-09 16:03:47 +01:00
|
|
|
if (verbose && includewal != NO_WAL)
|
2017-05-12 17:49:56 +02:00
|
|
|
fprintf(stderr, _("%s: write-ahead log end point: %s\n"), progname, xlogend);
|
2011-02-03 13:46:23 +01:00
|
|
|
PQclear(res);
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2018-04-03 13:47:16 +02:00
|
|
|
const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
|
|
|
|
|
|
|
|
if (sqlstate &&
|
|
|
|
strcmp(sqlstate, ERRCODE_DATA_CORRUPTED) == 0)
|
|
|
|
{
|
2018-10-08 08:57:24 +02:00
|
|
|
fprintf(stderr, _("%s: checksum error occurred\n"),
|
2018-04-03 13:47:16 +02:00
|
|
|
progname);
|
|
|
|
checksum_failure = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: final receive failed: %s"),
|
|
|
|
progname, PQerrorMessage(conn));
|
|
|
|
}
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
if (bgchild > 0)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
2011-12-11 00:15:15 +01:00
|
|
|
int status;
|
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3)
for looking up descriptive strings for signal numbers. We hadn't gotten
the word though, and were still using the crufty old sys_siglist array,
which is in no standard even though most Unixen provide it.
Aside from not being formally standards-compliant, this was just plain
ugly because it involved #ifdef's at every place using the code.
To eliminate the #ifdef's, create a portability function pg_strsignal,
which wraps strsignal(3) if available and otherwise falls back to
sys_siglist[] if available. The set of Unixen with neither API is
probably empty these days, but on any platform with neither, you'll
just get "unrecognized signal". All extant callers print the numeric
signal number too, so no need to work harder than that.
Along the way, upgrade pg_basebackup's child-error-exit reporting
to match the rest of the system.
Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
2018-12-17 01:38:57 +01:00
|
|
|
pid_t r;
|
2011-12-11 00:15:15 +01:00
|
|
|
#else
|
2012-06-10 21:20:04 +02:00
|
|
|
DWORD status;
|
2016-06-10 00:02:36 +02:00
|
|
|
|
2016-04-29 13:59:47 +02:00
|
|
|
/*
|
|
|
|
* get a pointer sized version of bgchild to avoid warnings about
|
|
|
|
* casting to a different size on WIN64.
|
|
|
|
*/
|
|
|
|
intptr_t bgchild_handle = bgchild;
|
2012-06-24 21:14:31 +02:00
|
|
|
uint32 hi,
|
|
|
|
lo;
|
2011-10-26 20:13:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (verbose)
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
2012-12-19 13:01:11 +01:00
|
|
|
_("%s: waiting for background process to finish streaming ...\n"), progname);
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2012-03-29 05:24:07 +02:00
|
|
|
if (write(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend))
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not send command to background pipe: %s\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just wait for the background process to exit */
|
|
|
|
r = waitpid(bgchild, &status, 0);
|
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3)
for looking up descriptive strings for signal numbers. We hadn't gotten
the word though, and were still using the crufty old sys_siglist array,
which is in no standard even though most Unixen provide it.
Aside from not being formally standards-compliant, this was just plain
ugly because it involved #ifdef's at every place using the code.
To eliminate the #ifdef's, create a portability function pg_strsignal,
which wraps strsignal(3) if available and otherwise falls back to
sys_siglist[] if available. The set of Unixen with neither API is
probably empty these days, but on any platform with neither, you'll
just get "unrecognized signal". All extant callers print the numeric
signal number too, so no need to work harder than that.
Along the way, upgrade pg_basebackup's child-error-exit reporting
to match the rest of the system.
Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
2018-12-17 01:38:57 +01:00
|
|
|
if (r == (pid_t) -1)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not wait for child process: %s\n"),
|
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
if (r != bgchild)
|
|
|
|
{
|
2012-03-30 19:40:46 +02:00
|
|
|
fprintf(stderr, _("%s: child %d died, expected %d\n"),
|
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3)
for looking up descriptive strings for signal numbers. We hadn't gotten
the word though, and were still using the crufty old sys_siglist array,
which is in no standard even though most Unixen provide it.
Aside from not being formally standards-compliant, this was just plain
ugly because it involved #ifdef's at every place using the code.
To eliminate the #ifdef's, create a portability function pg_strsignal,
which wraps strsignal(3) if available and otherwise falls back to
sys_siglist[] if available. The set of Unixen with neither API is
probably empty these days, but on any platform with neither, you'll
just get "unrecognized signal". All extant callers print the numeric
signal number too, so no need to work harder than that.
Along the way, upgrade pg_basebackup's child-error-exit reporting
to match the rest of the system.
Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
2018-12-17 01:38:57 +01:00
|
|
|
progname, (int) r, (int) bgchild);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3)
for looking up descriptive strings for signal numbers. We hadn't gotten
the word though, and were still using the crufty old sys_siglist array,
which is in no standard even though most Unixen provide it.
Aside from not being formally standards-compliant, this was just plain
ugly because it involved #ifdef's at every place using the code.
To eliminate the #ifdef's, create a portability function pg_strsignal,
which wraps strsignal(3) if available and otherwise falls back to
sys_siglist[] if available. The set of Unixen with neither API is
probably empty these days, but on any platform with neither, you'll
just get "unrecognized signal". All extant callers print the numeric
signal number too, so no need to work harder than that.
Along the way, upgrade pg_basebackup's child-error-exit reporting
to match the rest of the system.
Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
2018-12-17 01:38:57 +01:00
|
|
|
if (status != 0)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
Modernize our code for looking up descriptive strings for Unix signals.
At least as far back as the 2008 spec, POSIX has defined strsignal(3)
for looking up descriptive strings for signal numbers. We hadn't gotten
the word though, and were still using the crufty old sys_siglist array,
which is in no standard even though most Unixen provide it.
Aside from not being formally standards-compliant, this was just plain
ugly because it involved #ifdef's at every place using the code.
To eliminate the #ifdef's, create a portability function pg_strsignal,
which wraps strsignal(3) if available and otherwise falls back to
sys_siglist[] if available. The set of Unixen with neither API is
probably empty these days, but on any platform with neither, you'll
just get "unrecognized signal". All extant callers print the numeric
signal number too, so no need to work harder than that.
Along the way, upgrade pg_basebackup's child-error-exit reporting
to match the rest of the system.
Discussion: https://postgr.es/m/25758.1544983503@sss.pgh.pa.us
2018-12-17 01:38:57 +01:00
|
|
|
fprintf(stderr, "%s: %s\n",
|
|
|
|
progname, wait_result_to_str(status));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
/* Exited normally, we're happy! */
|
|
|
|
#else /* WIN32 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On Windows, since we are in the same process, we can just store the
|
|
|
|
* value directly in the variable, and then set the flag that says
|
|
|
|
* it's there.
|
|
|
|
*/
|
2012-06-24 21:14:31 +02:00
|
|
|
if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: could not parse write-ahead log location \"%s\"\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, xlogend);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2012-06-24 21:14:31 +02:00
|
|
|
xlogendptr = ((uint64) hi) << 32 | lo;
|
2011-10-26 20:13:33 +02:00
|
|
|
InterlockedIncrement(&has_xlogendptr);
|
|
|
|
|
|
|
|
/* First wait for the thread to exit */
|
2016-04-29 13:59:47 +02:00
|
|
|
if (WaitForSingleObjectEx((HANDLE) bgchild_handle, INFINITE, FALSE) !=
|
2012-07-31 16:11:11 +02:00
|
|
|
WAIT_OBJECT_0)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
fprintf(stderr, _("%s: could not wait for child thread: %s\n"),
|
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2016-04-29 13:59:47 +02:00
|
|
|
if (GetExitCodeThread((HANDLE) bgchild_handle, &status) == 0)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
fprintf(stderr, _("%s: could not get child thread exit status: %s\n"),
|
|
|
|
progname, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: child thread exited with error %u\n"),
|
2011-12-11 00:15:15 +01:00
|
|
|
progname, (unsigned int) status);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
/* Exited normally, we're happy */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-25 16:31:16 +01:00
|
|
|
/* Free the configuration file contents */
|
2013-01-05 16:54:06 +01:00
|
|
|
destroyPQExpBuffer(recoveryconfcontents);
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
/*
|
|
|
|
* End of copy data. Final result is already checked inside the loop.
|
|
|
|
*/
|
2012-01-20 13:57:02 +01:00
|
|
|
PQclear(res);
|
2011-01-23 12:21:23 +01:00
|
|
|
PQfinish(conn);
|
2018-12-29 13:21:47 +01:00
|
|
|
conn = NULL;
|
2011-01-23 12:21:23 +01:00
|
|
|
|
2016-09-29 18:00:00 +02:00
|
|
|
/*
|
2017-05-17 22:31:56 +02:00
|
|
|
* Make data persistent on disk once backup is completed. For tar format
|
|
|
|
* once syncing the parent directory is fine, each tar file created per
|
|
|
|
* tablespace has been already synced. In plain format, all the data of
|
|
|
|
* the base directory is synced, taking into account all the tablespaces.
|
|
|
|
* Errors are not considered fatal.
|
2016-09-29 18:00:00 +02:00
|
|
|
*/
|
2016-09-29 18:00:00 +02:00
|
|
|
if (do_sync)
|
2016-09-29 18:00:00 +02:00
|
|
|
{
|
2018-07-29 00:53:11 +02:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: syncing data to disk ...\n"), progname);
|
2016-09-29 18:00:00 +02:00
|
|
|
if (format == 't')
|
|
|
|
{
|
|
|
|
if (strcmp(basedir, "-") != 0)
|
|
|
|
(void) fsync_fname(basedir, true, progname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-20 17:24:37 +02:00
|
|
|
(void) fsync_pgdata(basedir, progname, serverVersion);
|
2016-09-29 18:00:00 +02:00
|
|
|
}
|
2016-09-29 18:00:00 +02:00
|
|
|
}
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
if (verbose)
|
2017-02-26 21:29:34 +01:00
|
|
|
fprintf(stderr, _("%s: base backup completed\n"), progname);
|
2011-01-23 12:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", no_argument, NULL, '?'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"pgdata", required_argument, NULL, 'D'},
|
|
|
|
{"format", required_argument, NULL, 'F'},
|
|
|
|
{"checkpoint", required_argument, NULL, 'c'},
|
2017-09-26 22:07:52 +02:00
|
|
|
{"create-slot", no_argument, NULL, 'C'},
|
2014-02-27 22:55:57 +01:00
|
|
|
{"max-rate", required_argument, NULL, 'r'},
|
2013-01-05 16:54:06 +01:00
|
|
|
{"write-recovery-conf", no_argument, NULL, 'R'},
|
2015-07-22 03:06:45 +02:00
|
|
|
{"slot", required_argument, NULL, 'S'},
|
2014-02-22 19:38:06 +01:00
|
|
|
{"tablespace-mapping", required_argument, NULL, 'T'},
|
2017-02-09 22:42:51 +01:00
|
|
|
{"wal-method", required_argument, NULL, 'X'},
|
2011-05-30 00:02:02 +02:00
|
|
|
{"gzip", no_argument, NULL, 'z'},
|
2011-01-23 12:21:23 +01:00
|
|
|
{"compress", required_argument, NULL, 'Z'},
|
|
|
|
{"label", required_argument, NULL, 'l'},
|
2016-10-19 18:00:00 +02:00
|
|
|
{"no-clean", no_argument, NULL, 'n'},
|
|
|
|
{"no-sync", no_argument, NULL, 'N'},
|
2013-02-25 13:48:27 +01:00
|
|
|
{"dbname", required_argument, NULL, 'd'},
|
2011-01-23 12:21:23 +01:00
|
|
|
{"host", required_argument, NULL, 'h'},
|
|
|
|
{"port", required_argument, NULL, 'p'},
|
|
|
|
{"username", required_argument, NULL, 'U'},
|
|
|
|
{"no-password", no_argument, NULL, 'w'},
|
|
|
|
{"password", no_argument, NULL, 'W'},
|
2012-07-31 16:11:11 +02:00
|
|
|
{"status-interval", required_argument, NULL, 's'},
|
2011-01-23 12:21:23 +01:00
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{"progress", no_argument, NULL, 'P'},
|
2017-02-09 22:42:51 +01:00
|
|
|
{"waldir", required_argument, NULL, 1},
|
2017-01-16 13:56:43 +01:00
|
|
|
{"no-slot", no_argument, NULL, 2},
|
2018-05-21 16:01:49 +02:00
|
|
|
{"no-verify-checksums", no_argument, NULL, 3},
|
2011-01-23 12:21:23 +01:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
int c;
|
|
|
|
|
|
|
|
int option_index;
|
|
|
|
|
|
|
|
progname = get_progname(argv[0]);
|
|
|
|
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "-V") == 0
|
|
|
|
|| strcmp(argv[1], "--version") == 0)
|
|
|
|
{
|
|
|
|
puts("pg_basebackup (PostgreSQL) " PG_VERSION);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
atexit(cleanup_directories_atexit);
|
|
|
|
|
2018-04-03 13:47:16 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP",
|
2011-01-23 12:21:23 +01:00
|
|
|
long_options, &option_index)) != -1)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2017-09-26 22:07:52 +02:00
|
|
|
case 'C':
|
|
|
|
create_slot = true;
|
|
|
|
break;
|
2011-01-23 12:21:23 +01:00
|
|
|
case 'D':
|
2012-10-02 21:35:10 +02:00
|
|
|
basedir = pg_strdup(optarg);
|
2011-01-23 12:21:23 +01:00
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
|
|
|
|
format = 'p';
|
|
|
|
else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
|
|
|
|
format = 't';
|
|
|
|
else
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: invalid output format \"%s\", must be \"plain\" or \"tar\"\n"),
|
2011-01-23 12:21:23 +01:00
|
|
|
progname, optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2014-02-27 22:55:57 +01:00
|
|
|
case 'r':
|
|
|
|
maxrate = parse_max_rate(optarg);
|
|
|
|
break;
|
2013-01-05 16:54:06 +01:00
|
|
|
case 'R':
|
|
|
|
writerecoveryconf = true;
|
|
|
|
break;
|
2015-07-22 03:06:45 +02:00
|
|
|
case 'S':
|
2017-01-16 13:56:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When specifying replication slot name, use a permanent
|
|
|
|
* slot.
|
|
|
|
*/
|
2015-07-22 03:06:45 +02:00
|
|
|
replication_slot = pg_strdup(optarg);
|
2017-01-16 13:56:43 +01:00
|
|
|
temp_replication_slot = false;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
no_slot = true;
|
2015-07-22 03:06:45 +02:00
|
|
|
break;
|
2014-02-22 19:38:06 +01:00
|
|
|
case 'T':
|
|
|
|
tablespace_list_append(optarg);
|
|
|
|
break;
|
2012-06-10 13:43:51 +02:00
|
|
|
case 'X':
|
2017-01-04 10:40:38 +01:00
|
|
|
if (strcmp(optarg, "n") == 0 ||
|
|
|
|
strcmp(optarg, "none") == 0)
|
2012-06-10 13:43:51 +02:00
|
|
|
{
|
2017-01-09 16:03:47 +01:00
|
|
|
includewal = NO_WAL;
|
2012-06-10 13:43:51 +02:00
|
|
|
}
|
2017-01-04 10:40:38 +01:00
|
|
|
else if (strcmp(optarg, "f") == 0 ||
|
2017-05-17 22:31:56 +02:00
|
|
|
strcmp(optarg, "fetch") == 0)
|
2017-01-04 10:40:38 +01:00
|
|
|
{
|
2017-01-09 16:03:47 +01:00
|
|
|
includewal = FETCH_WAL;
|
2017-01-04 10:40:38 +01:00
|
|
|
}
|
2011-10-26 20:13:33 +02:00
|
|
|
else if (strcmp(optarg, "s") == 0 ||
|
|
|
|
strcmp(optarg, "stream") == 0)
|
2017-01-04 10:40:38 +01:00
|
|
|
{
|
2017-01-09 16:03:47 +01:00
|
|
|
includewal = STREAM_WAL;
|
2017-01-04 10:40:38 +01:00
|
|
|
}
|
2011-10-26 20:13:33 +02:00
|
|
|
else
|
|
|
|
{
|
2012-07-31 16:11:11 +02:00
|
|
|
fprintf(stderr,
|
2017-06-14 20:43:54 +02:00
|
|
|
_("%s: invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"\n"),
|
2011-10-26 20:13:33 +02:00
|
|
|
progname, optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-01-30 21:30:09 +01:00
|
|
|
break;
|
2013-11-27 06:00:16 +01:00
|
|
|
case 1:
|
|
|
|
xlog_dir = pg_strdup(optarg);
|
|
|
|
break;
|
2011-01-23 12:21:23 +01:00
|
|
|
case 'l':
|
2012-10-02 21:35:10 +02:00
|
|
|
label = pg_strdup(optarg);
|
2011-01-23 12:21:23 +01:00
|
|
|
break;
|
2016-09-12 18:00:00 +02:00
|
|
|
case 'n':
|
|
|
|
noclean = true;
|
|
|
|
break;
|
2016-09-29 18:00:00 +02:00
|
|
|
case 'N':
|
|
|
|
do_sync = false;
|
|
|
|
break;
|
2011-05-30 00:02:02 +02:00
|
|
|
case 'z':
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
compresslevel = Z_DEFAULT_COMPRESSION;
|
|
|
|
#else
|
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
|
|
|
compresslevel = 1; /* will be rejected below */
|
2011-05-30 00:02:02 +02:00
|
|
|
#endif
|
|
|
|
break;
|
2011-01-23 12:21:23 +01:00
|
|
|
case 'Z':
|
|
|
|
compresslevel = atoi(optarg);
|
2016-08-01 10:36:14 +02:00
|
|
|
if (compresslevel < 0 || compresslevel > 9)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: invalid compression level \"%s\"\n"),
|
|
|
|
progname, optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
2011-01-23 17:35:02 +01:00
|
|
|
if (pg_strcasecmp(optarg, "fast") == 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
fastcheckpoint = true;
|
2011-01-23 17:35:02 +01:00
|
|
|
else if (pg_strcasecmp(optarg, "spread") == 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
fastcheckpoint = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"\n"),
|
|
|
|
progname, optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2013-02-25 13:48:27 +01:00
|
|
|
case 'd':
|
|
|
|
connection_string = pg_strdup(optarg);
|
|
|
|
break;
|
2011-01-23 12:21:23 +01:00
|
|
|
case 'h':
|
2012-10-02 21:35:10 +02:00
|
|
|
dbhost = pg_strdup(optarg);
|
2011-01-23 12:21:23 +01:00
|
|
|
break;
|
|
|
|
case 'p':
|
2012-10-02 21:35:10 +02:00
|
|
|
dbport = pg_strdup(optarg);
|
2011-01-23 12:21:23 +01:00
|
|
|
break;
|
|
|
|
case 'U':
|
2012-10-02 21:35:10 +02:00
|
|
|
dbuser = pg_strdup(optarg);
|
2011-01-23 12:21:23 +01:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
dbgetpassword = -1;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
dbgetpassword = 1;
|
|
|
|
break;
|
2011-10-26 20:13:33 +02:00
|
|
|
case 's':
|
2012-06-10 12:12:36 +02:00
|
|
|
standby_message_timeout = atoi(optarg) * 1000;
|
2011-10-26 20:13:33 +02:00
|
|
|
if (standby_message_timeout < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: invalid status interval \"%s\"\n"),
|
|
|
|
progname, optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2011-01-23 12:21:23 +01:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
showprogress = true;
|
|
|
|
break;
|
2018-05-21 16:01:49 +02:00
|
|
|
case 3:
|
2018-04-03 13:47:16 +02:00
|
|
|
verify_checksums = false;
|
|
|
|
break;
|
2011-01-23 12:21:23 +01:00
|
|
|
default:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getopt_long already emitted a complaint
|
|
|
|
*/
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Any non-option arguments?
|
|
|
|
*/
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: too many command-line arguments (first is \"%s\")\n"),
|
|
|
|
progname, argv[optind]);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Required arguments
|
|
|
|
*/
|
|
|
|
if (basedir == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: no target directory specified\n"), progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mutually exclusive arguments
|
|
|
|
*/
|
2011-06-15 19:15:05 +02:00
|
|
|
if (format == 'p' && compresslevel != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: only tar mode backups can be compressed\n"),
|
|
|
|
progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-01-09 16:03:47 +01:00
|
|
|
if (format == 't' && includewal == STREAM_WAL && strcmp(basedir, "-") == 0)
|
2016-12-21 12:27:37 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: cannot stream write-ahead logs in tar mode to stdout\n"),
|
2016-12-21 12:27:37 +01:00
|
|
|
progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-01-16 18:20:57 +01:00
|
|
|
if (replication_slot && includewal != STREAM_WAL)
|
2015-07-22 03:06:45 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
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
|
|
|
_("%s: replication slots can only be used with WAL streaming\n"),
|
2015-07-22 03:06:45 +02:00
|
|
|
progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-01-16 13:56:43 +01:00
|
|
|
if (no_slot)
|
|
|
|
{
|
|
|
|
if (replication_slot)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: --no-slot cannot be used with slot name\n"),
|
|
|
|
progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
temp_replication_slot = false;
|
|
|
|
}
|
|
|
|
|
2017-09-26 22:07:52 +02:00
|
|
|
if (create_slot)
|
|
|
|
{
|
|
|
|
if (!replication_slot)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2018-05-18 05:05:27 +02:00
|
|
|
_("%s: %s needs a slot to be specified using --slot\n"),
|
|
|
|
progname, "--create-slot");
|
2017-09-26 22:07:52 +02:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_slot)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: --create-slot and --no-slot are incompatible options\n"),
|
|
|
|
progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:28:36 +02:00
|
|
|
if (xlog_dir)
|
2013-11-27 06:00:16 +01:00
|
|
|
{
|
|
|
|
if (format != 'p')
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2017-05-12 17:49:56 +02:00
|
|
|
_("%s: WAL directory location can only be specified in plain mode\n"),
|
2013-11-27 06:00:16 +01:00
|
|
|
progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up xlog directory name, check it's absolute */
|
|
|
|
canonicalize_path(xlog_dir);
|
|
|
|
if (!is_absolute_path(xlog_dir))
|
|
|
|
{
|
2017-05-12 17:49:56 +02:00
|
|
|
fprintf(stderr, _("%s: WAL directory location must be "
|
2013-11-27 06:00:16 +01:00
|
|
|
"an absolute path\n"), progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
#ifndef HAVE_LIBZ
|
2011-06-15 19:15:05 +02:00
|
|
|
if (compresslevel != 0)
|
2011-01-23 12:21:23 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: this build does not support compression\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-20 17:24:37 +02:00
|
|
|
/* connection in replication mode to server */
|
|
|
|
conn = GetConnection();
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
/* Error message already written in GetConnection() */
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-12-29 13:21:47 +01:00
|
|
|
atexit(disconnect_atexit);
|
2016-10-20 17:24:37 +02:00
|
|
|
|
2018-04-07 23:45:39 +02:00
|
|
|
/*
|
|
|
|
* Set umask so that directories/files are created with the same
|
|
|
|
* permissions as directories/files in the source data directory.
|
|
|
|
*
|
|
|
|
* pg_mode_mask is set to owner-only by default and then updated in
|
|
|
|
* GetConnection() where we get the mode from the server-side with
|
|
|
|
* RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
|
|
|
|
*/
|
|
|
|
umask(pg_mode_mask);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the target directory exists, or create it. For plaintext
|
|
|
|
* backups, always require the directory. For tar backups, require it
|
|
|
|
* unless we are writing to stdout.
|
|
|
|
*/
|
|
|
|
if (format == 'p' || strcmp(basedir, "-") != 0)
|
|
|
|
verify_dir_is_empty_or_create(basedir, &made_new_pgdata, &found_existing_pgdata);
|
|
|
|
|
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
|
|
|
/* determine remote server's xlog segment size */
|
|
|
|
if (!RetrieveWalSegSize(conn))
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
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
|
|
|
|
2017-05-12 17:49:56 +02:00
|
|
|
/* Create pg_wal symlink, if required */
|
2017-08-31 04:28:36 +02:00
|
|
|
if (xlog_dir)
|
2013-11-27 06:00:16 +01:00
|
|
|
{
|
|
|
|
char *linkloc;
|
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
verify_dir_is_empty_or_create(xlog_dir, &made_new_xlogdir, &found_existing_xlogdir);
|
2013-11-27 06:00:16 +01:00
|
|
|
|
2016-10-20 17:24:37 +02:00
|
|
|
/*
|
2017-05-17 22:31:56 +02:00
|
|
|
* Form name of the place where the symlink must go. pg_xlog has been
|
|
|
|
* renamed to pg_wal in post-10 clusters.
|
2016-10-20 17:24:37 +02:00
|
|
|
*/
|
|
|
|
linkloc = psprintf("%s/%s", basedir,
|
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
|
|
|
PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
|
2017-05-17 22:31:56 +02:00
|
|
|
"pg_xlog" : "pg_wal");
|
2013-11-27 06:00:16 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_SYMLINK
|
|
|
|
if (symlink(xlog_dir, linkloc) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
|
|
|
|
progname, linkloc, strerror(errno));
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-11-27 06:00:16 +01:00
|
|
|
}
|
|
|
|
#else
|
2018-12-22 07:21:40 +01:00
|
|
|
fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname);
|
2018-12-29 13:21:47 +01:00
|
|
|
exit(1);
|
2013-11-27 06:00:16 +01:00
|
|
|
#endif
|
|
|
|
free(linkloc);
|
|
|
|
}
|
|
|
|
|
2011-01-23 12:21:23 +01:00
|
|
|
BaseBackup();
|
|
|
|
|
2016-09-12 18:00:00 +02:00
|
|
|
success = true;
|
2011-01-23 12:21:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|