diff options
Diffstat (limited to 'src/bin/pg_basebackup')
-rw-r--r-- | src/bin/pg_basebackup/pg_basebackup.c | 70 | ||||
-rw-r--r-- | src/bin/pg_basebackup/pg_receivexlog.c | 6 | ||||
-rw-r--r-- | src/bin/pg_basebackup/pg_recvlogical.c | 50 | ||||
-rw-r--r-- | src/bin/pg_basebackup/receivelog.c | 10 | ||||
-rw-r--r-- | src/bin/pg_basebackup/streamutil.c | 6 | ||||
-rw-r--r-- | src/bin/pg_basebackup/streamutil.h | 4 | ||||
-rw-r--r-- | src/bin/pg_basebackup/t/010_pg_basebackup.pl | 94 |
7 files changed, 134 insertions, 106 deletions
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 1a468fa1b74..154c52d1909 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -40,8 +40,8 @@ typedef struct TablespaceListCell { struct TablespaceListCell *next; - char old_dir[MAXPGPATH]; - char new_dir[MAXPGPATH]; + char old_dir[MAXPGPATH]; + char new_dir[MAXPGPATH]; } TablespaceListCell; typedef struct TablespaceList @@ -54,15 +54,15 @@ typedef struct TablespaceList static char *basedir = NULL; static TablespaceList tablespace_dirs = {NULL, NULL}; static char *xlog_dir = ""; -static char format = 'p'; /* p(lain)/t(ar) */ +static char format = 'p'; /* p(lain)/t(ar) */ static char *label = "pg_basebackup base backup"; -static bool showprogress = false; -static int verbose = 0; +static bool showprogress = false; +static int verbose = 0; static int compresslevel = 0; -static bool includewal = false; -static bool streamwal = false; -static bool fastcheckpoint = false; -static bool writerecoveryconf = false; +static bool includewal = false; +static bool streamwal = false; +static bool fastcheckpoint = false; +static bool writerecoveryconf = false; static int standby_message_timeout = 10 * 1000; /* 10 sec = default */ static pg_time_t last_progress_report = 0; static int32 maxrate = 0; /* no limit by default */ @@ -113,18 +113,20 @@ static void update_tablespace_symlink(Oid oid, const char *old_dir); static void tablespace_list_append(const char *arg); -static void disconnect_and_exit(int code) +static void +disconnect_and_exit(int code) { if (conn != NULL) PQfinish(conn); #ifndef WIN32 + /* - * 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. + * 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. */ - if (bgchild> 0) + if (bgchild > 0) kill(bgchild, SIGTERM); #endif @@ -140,21 +142,21 @@ static void tablespace_list_append(const char *arg) { TablespaceListCell *cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell)); - char *dst; - char *dst_ptr; - const char *arg_ptr; + char *dst; + char *dst_ptr; + const char *arg_ptr; dst_ptr = dst = cell->old_dir; for (arg_ptr = arg; *arg_ptr; arg_ptr++) { if (dst_ptr - dst >= MAXPGPATH) { - fprintf(stderr, _("%s: directory name too long\n"), progname); + fprintf(stderr, _("%s: directory name too long\n"), progname); exit(1); } if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=') - ; /* skip backslash escaping = */ + ; /* skip backslash escaping = */ else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\')) { if (*cell->new_dir) @@ -177,10 +179,12 @@ tablespace_list_append(const char *arg) exit(1); } - /* This check isn't absolutely necessary. But all tablespaces are created + /* + * This check isn't absolutely necessary. But all tablespaces are created * with absolute directories, so specifying a non-absolute path here would * just never match, possibly confusing users. It's also good to be - * consistent with the new_dir check. */ + * consistent with the new_dir check. + */ if (!is_absolute_path(cell->old_dir)) { fprintf(stderr, _("%s: old directory not absolute in tablespace mapping: %s\n"), @@ -232,7 +236,7 @@ usage(void) printf(_(" -R, --write-recovery-conf\n" " write recovery.conf after backup\n")); printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n" - " relocate tablespace in OLDDIR to NEWDIR\n")); + " relocate tablespace in OLDDIR to NEWDIR\n")); printf(_(" -x, --xlog include required WAL files in backup (fetch mode)\n")); printf(_(" -X, --xlog-method=fetch|stream\n" " include required WAL files with specified method\n")); @@ -296,7 +300,7 @@ reached_end_position(XLogRecPtr segendpos, uint32 timeline, lo; MemSet(xlogend, 0, sizeof(xlogend)); - r = read(bgpipe[0], xlogend, sizeof(xlogend)-1); + r = read(bgpipe[0], xlogend, sizeof(xlogend) - 1); if (r < 0) { fprintf(stderr, _("%s: could not read from ready pipe: %s\n"), @@ -536,7 +540,7 @@ progress_report(int tablespacenum, const char *filename, bool force) now = time(NULL); if (now == last_progress_report && !force) - return; /* Max once per second */ + return; /* Max once per second */ last_progress_report = now; percent = totalsize ? (int) ((totaldone / 1024) * 100 / totalsize) : 0; @@ -614,7 +618,7 @@ parse_max_rate(char *src) { double result; char *after_num; - char *suffix = NULL; + char *suffix = NULL; errno = 0; result = strtod(src, &after_num); @@ -644,8 +648,8 @@ parse_max_rate(char *src) } /* - * Evaluate suffix, after skipping over possible whitespace. - * Lack of suffix means kilobytes. + * Evaluate suffix, after skipping over possible whitespace. Lack of + * suffix means kilobytes. */ while (*after_num != '\0' && isspace((unsigned char) *after_num)) after_num++; @@ -681,8 +685,8 @@ parse_max_rate(char *src) if ((uint64) result != (uint64) ((uint32) result)) { fprintf(stderr, - _("%s: transfer rate \"%s\" exceeds integer range\n"), - progname, src); + _("%s: transfer rate \"%s\" exceeds integer range\n"), + progname, src); exit(1); } @@ -1114,7 +1118,7 @@ update_tablespace_symlink(Oid oid, const char *old_dir) if (strcmp(old_dir, new_dir) != 0) { - char *linkloc = psprintf("%s/pg_tblspc/%d", basedir, oid); + char *linkloc = psprintf("%s/pg_tblspc/%d", basedir, oid); if (unlink(linkloc) != 0 && errno != ENOENT) { @@ -1742,7 +1746,8 @@ BaseBackup(void) */ if (format == 'p' && !PQgetisnull(res, i, 1)) { - char *path = (char *) get_tablespace_mapping(PQgetvalue(res, i, 1)); + char *path = (char *) get_tablespace_mapping(PQgetvalue(res, i, 1)); + verify_dir_is_empty_or_create(path); } } @@ -1791,7 +1796,8 @@ BaseBackup(void) { for (i = 0; i < PQntuples(res); i++) { - Oid tblspc_oid = atooid(PQgetvalue(res, i, 0)); + Oid tblspc_oid = atooid(PQgetvalue(res, i, 0)); + if (tblspc_oid) update_tablespace_symlink(tblspc_oid, PQgetvalue(res, i, 1)); } diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c index 406610a8625..96408389062 100644 --- a/src/bin/pg_basebackup/pg_receivexlog.c +++ b/src/bin/pg_basebackup/pg_receivexlog.c @@ -228,9 +228,9 @@ FindStreamingStart(uint32 *tli) XLogRecPtr high_ptr; /* - * Move the starting pointer to the start of the next segment, if - * the highest one we saw was completed. Otherwise start streaming - * from the beginning of the .partial segment. + * Move the starting pointer to the start of the next segment, if the + * highest one we saw was completed. Otherwise start streaming from + * the beginning of the .partial segment. */ if (!high_ispartial) high_segno++; diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index fe902cf9691..194d10faa5c 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -31,27 +31,27 @@ #define RECONNECT_SLEEP_TIME 5 /* Global Options */ -static char *outfile = NULL; -static int verbose = 0; -static int noloop = 0; -static int standby_message_timeout = 10 * 1000; /* 10 sec = default */ -static int fsync_interval = 10 * 1000; /* 10 sec = default */ +static char *outfile = NULL; +static int verbose = 0; +static int noloop = 0; +static int standby_message_timeout = 10 * 1000; /* 10 sec = default */ +static int fsync_interval = 10 * 1000; /* 10 sec = default */ static XLogRecPtr startpos = InvalidXLogRecPtr; -static bool do_create_slot = false; -static bool do_start_slot = false; -static bool do_drop_slot = false; +static bool do_create_slot = false; +static bool do_start_slot = false; +static bool do_drop_slot = false; /* filled pairwise with option, value. value may be NULL */ -static char **options; -static size_t noptions = 0; +static char **options; +static size_t noptions = 0; static const char *plugin = "test_decoding"; /* Global State */ -static int outfd = -1; +static int outfd = -1; static volatile sig_atomic_t time_to_abort = false; static volatile sig_atomic_t output_reopen = false; -static int64 output_last_fsync = -1; -static bool output_unsynced = false; +static int64 output_last_fsync = -1; +static bool output_unsynced = false; static XLogRecPtr output_written_lsn = InvalidXLogRecPtr; static XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr; @@ -111,8 +111,8 @@ sendFeedback(PGconn *conn, int64 now, bool force, bool replyRequested) /* * we normally don't want to send superfluous feedbacks, but if it's - * because of a timeout we need to, otherwise wal_sender_timeout will - * kill us. + * because of a timeout we need to, otherwise wal_sender_timeout will kill + * us. */ if (!force && last_written_lsn == output_written_lsn && @@ -121,21 +121,21 @@ sendFeedback(PGconn *conn, int64 now, bool force, bool replyRequested) if (verbose) fprintf(stderr, - _("%s: confirming write up to %X/%X, flush to %X/%X (slot %s)\n"), + _("%s: confirming write up to %X/%X, flush to %X/%X (slot %s)\n"), progname, - (uint32) (output_written_lsn >> 32), (uint32) output_written_lsn, + (uint32) (output_written_lsn >> 32), (uint32) output_written_lsn, (uint32) (output_fsync_lsn >> 32), (uint32) output_fsync_lsn, replication_slot); replybuf[len] = 'r'; len += 1; - fe_sendint64(output_written_lsn, &replybuf[len]); /* write */ + fe_sendint64(output_written_lsn, &replybuf[len]); /* write */ len += 8; fe_sendint64(output_fsync_lsn, &replybuf[len]); /* flush */ len += 8; - fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */ + fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */ len += 8; - fe_sendint64(now, &replybuf[len]); /* sendTime */ + fe_sendint64(now, &replybuf[len]); /* sendTime */ len += 8; replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */ len += 1; @@ -227,7 +227,7 @@ StreamLog(void) /* Initiate the replication stream at specified location */ appendPQExpBuffer(query, "START_REPLICATION SLOT \"%s\" LOGICAL %X/%X", - replication_slot, (uint32) (startpos >> 32), (uint32) startpos); + replication_slot, (uint32) (startpos >> 32), (uint32) startpos); /* print options if there are any */ if (noptions) @@ -549,7 +549,7 @@ StreamLog(void) if (outfd != -1 && strcmp(outfile, "-") != 0) { - int64 t = feGetCurrentTimestamp(); + int64 t = feGetCurrentTimestamp(); /* no need to jump to error on failure here, we're finishing anyway */ OutputFsync(t); @@ -693,8 +693,8 @@ main(int argc, char **argv) /* replication options */ case 'o': { - char *data = pg_strdup(optarg); - char *val = strchr(data, '='); + char *data = pg_strdup(optarg); + char *val = strchr(data, '='); if (val != NULL) { @@ -704,7 +704,7 @@ main(int argc, char **argv) } noptions += 1; - options = pg_realloc(options, sizeof(char*) * noptions * 2); + options = pg_realloc(options, sizeof(char *) * noptions * 2); options[(noptions - 1) * 2] = data; options[(noptions - 1) * 2 + 1] = val; diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c index 2bf04484647..33d2911a0e0 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -328,9 +328,9 @@ sendFeedback(PGconn *conn, XLogRecPtr blockpos, int64 now, bool replyRequested) else fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* flush */ len += 8; - fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */ + fe_sendint64(InvalidXLogRecPtr, &replybuf[len]); /* apply */ len += 8; - fe_sendint64(now, &replybuf[len]); /* sendTime */ + fe_sendint64(now, &replybuf[len]); /* sendTime */ len += 8; replybuf[len] = replyRequested ? 1 : 0; /* replyRequested */ len += 1; @@ -437,8 +437,8 @@ ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, * reporting the flush position makes one eligible as a synchronous * replica. People shouldn't include generic names in * synchronous_standby_names, but we've protected them against it so - * far, so let's continue to do so in the situations when possible. - * If they've got a slot, though, we need to report the flush position, + * far, so let's continue to do so in the situations when possible. If + * they've got a slot, though, we need to report the flush position, * so that the master can remove WAL. */ reportFlushPosition = true; @@ -766,7 +766,7 @@ HandleCopyStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, now = feGetCurrentTimestamp(); if (still_sending && standby_message_timeout > 0 && feTimestampDifferenceExceeds(last_status, now, - standby_message_timeout)) + standby_message_timeout)) { /* Time to send feedback! */ if (!sendFeedback(conn, blockpos, now, false)) diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index e440dc4e244..1100260c05a 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -255,7 +255,7 @@ feGetCurrentTimestamp(void) */ void feTimestampDifference(int64 start_time, int64 stop_time, - long *secs, int *microsecs) + long *secs, int *microsecs) { int64 diff = stop_time - start_time; @@ -277,8 +277,8 @@ feTimestampDifference(int64 start_time, int64 stop_time, */ bool feTimestampDifferenceExceeds(int64 start_time, - int64 stop_time, - int msec) + int64 stop_time, + int msec) { int64 diff = stop_time - start_time; diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h index d0f3799d1e3..c36a37bf155 100644 --- a/src/bin/pg_basebackup/streamutil.h +++ b/src/bin/pg_basebackup/streamutil.h @@ -16,9 +16,9 @@ extern PGconn *GetConnection(void); extern int64 feGetCurrentTimestamp(void); extern void feTimestampDifference(int64 start_time, int64 stop_time, - long *secs, int *microsecs); + long *secs, int *microsecs); extern bool feTimestampDifferenceExceeds(int64 start_time, int64 stop_time, - int msec); + int msec); extern void fe_sendint64(int64 i, char *buf); extern int64 fe_recvint64(char *buf); diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 38f46929558..597fb60a528 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -11,8 +11,11 @@ program_options_handling_ok('pg_basebackup'); my $tempdir = tempdir; start_test_server $tempdir; -command_fails(['pg_basebackup'], 'pg_basebackup needs target directory specified'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup"], 'pg_basebackup fails because of hba'); +command_fails(['pg_basebackup'], + 'pg_basebackup needs target directory specified'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup" ], + 'pg_basebackup fails because of hba'); open HBA, ">>$tempdir/pgdata/pg_hba.conf"; print HBA "local replication all trust\n"; @@ -21,7 +24,9 @@ print HBA "host replication all ::1/128 trust\n"; close HBA; system_or_bail 'pg_ctl', '-s', '-D', "$tempdir/pgdata", 'reload'; -command_fails(['pg_basebackup', '-D', "$tempdir/backup"], 'pg_basebackup fails because of WAL configuration'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup" ], + 'pg_basebackup fails because of WAL configuration'); open CONF, ">>$tempdir/pgdata/postgresql.conf"; print CONF "max_wal_senders = 10\n"; @@ -29,62 +34,79 @@ print CONF "wal_level = archive\n"; close CONF; restart_test_server; -command_ok(['pg_basebackup', '-D', "$tempdir/backup"], 'pg_basebackup runs'); +command_ok([ 'pg_basebackup', '-D', "$tempdir/backup" ], + 'pg_basebackup runs'); ok(-f "$tempdir/backup/PG_VERSION", 'backup was created'); -command_ok(['pg_basebackup', '-D', "$tempdir/backup2", '--xlogdir', "$tempdir/xlog2"], 'separate xlog directory'); +command_ok( + [ 'pg_basebackup', '-D', "$tempdir/backup2", '--xlogdir', + "$tempdir/xlog2" ], + 'separate xlog directory'); ok(-f "$tempdir/backup2/PG_VERSION", 'backup was created'); -ok(-d "$tempdir/xlog2/", 'xlog directory was created'); +ok(-d "$tempdir/xlog2/", 'xlog directory was created'); -command_ok(['pg_basebackup', '-D', "$tempdir/tarbackup", '-Ft'], 'tar format'); +command_ok([ 'pg_basebackup', '-D', "$tempdir/tarbackup", '-Ft' ], + 'tar format'); ok(-f "$tempdir/tarbackup/base.tar", 'backup tar was created'); mkdir "$tempdir/tblspc1"; psql 'postgres', "CREATE TABLESPACE tblspc1 LOCATION '$tempdir/tblspc1';"; psql 'postgres', "CREATE TABLE test1 (a int) TABLESPACE tblspc1;"; -command_ok(['pg_basebackup', '-D', "$tempdir/tarbackup2", '-Ft'], 'tar format with tablespaces'); +command_ok([ 'pg_basebackup', '-D', "$tempdir/tarbackup2", '-Ft' ], + 'tar format with tablespaces'); ok(-f "$tempdir/tarbackup2/base.tar", 'backup tar was created'); my @tblspc_tars = glob "$tempdir/tarbackup2/[0-9]*.tar"; is(scalar(@tblspc_tars), 1, 'one tablespace tar was created'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup1", '-Fp'], - 'plain format with tablespaces fails without tablespace mapping'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup1", '-Fp' ], + 'plain format with tablespaces fails without tablespace mapping'); -command_ok(['pg_basebackup', '-D', "$tempdir/backup1", '-Fp', - "-T$tempdir/tblspc1=$tempdir/tbackup/tblspc1"], - 'plain format with tablespaces succeeds with tablespace mapping'); +command_ok( + [ 'pg_basebackup', '-D', + "$tempdir/backup1", '-Fp', + "-T$tempdir/tblspc1=$tempdir/tbackup/tblspc1" ], + 'plain format with tablespaces succeeds with tablespace mapping'); ok(-d "$tempdir/tbackup/tblspc1", 'tablespace was relocated'); opendir(my $dh, "$tempdir/pgdata/pg_tblspc") or die; -ok((grep { -l "$tempdir/backup1/pg_tblspc/$_" and readlink "$tempdir/backup1/pg_tblspc/$_" eq "$tempdir/tbackup/tblspc1" } readdir($dh)), - "tablespace symlink was updated"); +ok( ( grep + { + -l "$tempdir/backup1/pg_tblspc/$_" + and readlink "$tempdir/backup1/pg_tblspc/$_" eq + "$tempdir/tbackup/tblspc1" + } readdir($dh)), + "tablespace symlink was updated"); closedir $dh; mkdir "$tempdir/tbl=spc2"; psql 'postgres', "DROP TABLE test1;"; psql 'postgres', "DROP TABLESPACE tblspc1;"; psql 'postgres', "CREATE TABLESPACE tblspc2 LOCATION '$tempdir/tbl=spc2';"; -command_ok(['pg_basebackup', '-D', "$tempdir/backup3", '-Fp', - "-T$tempdir/tbl\\=spc2=$tempdir/tbackup/tbl\\=spc2"], - 'mapping tablespace with = sign in path'); +command_ok( + [ 'pg_basebackup', '-D', + "$tempdir/backup3", '-Fp', + "-T$tempdir/tbl\\=spc2=$tempdir/tbackup/tbl\\=spc2" ], + 'mapping tablespace with = sign in path'); ok(-d "$tempdir/tbackup/tbl=spc2", 'tablespace with = sign was relocated'); psql 'postgres', "DROP TABLESPACE tblspc2;"; -command_fails(['pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', - "-T=/foo"], - '-T with empty old directory fails'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', - "-T/foo="], - '-T with empty new directory fails'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', - "-T/foo=/bar=/baz"], - '-T with multiple = fails'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', - "-Tfoo=/bar"], - '-T with old directory not absolute fails'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', - "-T/foo=bar"], - '-T with new directory not absolute fails'); -command_fails(['pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', - "-Tfoo"], - '-T with invalid format fails'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', "-T=/foo" ], + '-T with empty old directory fails'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', "-T/foo=" ], + '-T with empty new directory fails'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', + "-T/foo=/bar=/baz" ], + '-T with multiple = fails'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', "-Tfoo=/bar" ], + '-T with old directory not absolute fails'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', "-T/foo=bar" ], + '-T with new directory not absolute fails'); +command_fails( + [ 'pg_basebackup', '-D', "$tempdir/backup_foo", '-Fp', "-Tfoo" ], + '-T with invalid format fails'); |