1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

Support base backup targets.

pg_basebackup now has a --target=TARGET[:DETAIL] option. If specfied,
it is sent to the server as the value of the TARGET option to the
BASE_BACKUP command. If DETAIL is included, it is sent as the value of
the new TARGET_DETAIL option to the BASE_BACKUP command.  If the
target is anything other than 'client', pg_basebackup assumes that it
will now be the server's job to write the backup in a location somehow
defined by the target, and that it therefore needs to write nothing
locally. However, the server will still send messages to the client
for progress reporting purposes.

On the server side, we now support two additional types of backup
targets.  There is a 'blackhole' target, which just throws away the
backup data without doing anything at all with it. Naturally, this
should only be used for testing and debugging purposes, since you will
not actually have a backup when it finishes running. More usefully,
there is also a 'server' target, so you can now use something like
'pg_basebackup -Xnone -t server:/SOME/PATH' to write a backup to some
location on the server. We can extend this to more types of targets
in the future, and might even want to create an extensibility
mechanism for adding new target types.

Since WAL fetching is handled with separate client-side logic, it's
not part of this mechanism; thus, backups with non-default targets
must use -Xnone or -Xfetch.

Patch by me, with a bug fix by Jeevan Ladhe.  The patch set of which
this is a part has also had review and/or testing from Tushar Ahuja,
Suraj Kharage, Dipesh Pandit, and Mark Dilger.

Discussion: http://postgr.es/m/CA+TgmoaYZbz0=Yk797aOJwkGJC-LK3iXn+wzzMx7KdwNpZhS5g@mail.gmail.com
This commit is contained in:
Robert Haas
2021-11-16 15:20:50 -05:00
parent f80900be06
commit 3500ccc39b
11 changed files with 677 additions and 64 deletions

View File

@@ -19,6 +19,7 @@ OBJS = \
basebackup.o \
basebackup_copy.o \
basebackup_progress.o \
basebackup_server.o \
basebackup_sink.o \
basebackup_throttle.o \
repl_gram.o \

View File

@@ -55,8 +55,10 @@
typedef enum
{
BACKUP_TARGET_BLACKHOLE,
BACKUP_TARGET_COMPAT,
BACKUP_TARGET_CLIENT
BACKUP_TARGET_CLIENT,
BACKUP_TARGET_SERVER
} backup_target_type;
typedef struct
@@ -69,6 +71,7 @@ typedef struct
uint32 maxrate;
bool sendtblspcmapfile;
backup_target_type target;
char *target_detail;
backup_manifest_option manifest;
pg_checksum_type manifest_checksum_type;
} basebackup_options;
@@ -702,6 +705,8 @@ parse_basebackup_options(List *options, basebackup_options *opt)
bool o_manifest = false;
bool o_manifest_checksums = false;
bool o_target = false;
bool o_target_detail = false;
char *target_str = "compat"; /* placate compiler */
MemSet(opt, 0, sizeof(*opt));
opt->target = BACKUP_TARGET_COMPAT;
@@ -847,25 +852,35 @@ parse_basebackup_options(List *options, basebackup_options *opt)
}
else if (strcmp(defel->defname, "target") == 0)
{
char *optval = defGetString(defel);
target_str = defGetString(defel);
if (o_target)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("duplicate option \"%s\"", defel->defname)));
if (strcmp(optval, "client") == 0)
if (strcmp(target_str, "blackhole") == 0)
opt->target = BACKUP_TARGET_BLACKHOLE;
else if (strcmp(target_str, "client") == 0)
opt->target = BACKUP_TARGET_CLIENT;
else if (strcmp(target_str, "server") == 0)
opt->target = BACKUP_TARGET_SERVER;
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unrecognized target: \"%s\"", optval)));
errmsg("unrecognized target: \"%s\"", target_str)));
o_target = true;
}
else
ereport(ERROR,
errcode(ERRCODE_SYNTAX_ERROR),
errmsg("option \"%s\" not recognized",
defel->defname));
else if (strcmp(defel->defname, "target_detail") == 0)
{
char *optval = defGetString(defel);
if (o_target_detail)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("duplicate option \"%s\"", defel->defname)));
opt->target_detail = optval;
o_target_detail = true;
}
}
if (opt->label == NULL)
opt->label = "base backup";
@@ -877,6 +892,22 @@ parse_basebackup_options(List *options, basebackup_options *opt)
errmsg("manifest checksums require a backup manifest")));
opt->manifest_checksum_type = CHECKSUM_TYPE_NONE;
}
if (opt->target == BACKUP_TARGET_SERVER)
{
if (opt->target_detail == NULL)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("target '%s' requires a target detail",
target_str)));
}
else
{
if (opt->target_detail != NULL)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("target '%s' does not accept a target detail",
target_str)));
}
}
@@ -908,14 +939,38 @@ SendBaseBackup(BaseBackupCmd *cmd)
/*
* If the TARGET option was specified, then we can use the new copy-stream
* protocol. If not, we must fall back to the old and less capable
* copy-tablespace protocol.
* protocol. If the target is specifically 'client' then set up to stream
* the backup to the client; otherwise, it's being sent someplace else and
* should not be sent to the client.
*
* If the TARGET option was not specified, we must fall back to the older
* and less capable copy-tablespace protocol.
*/
if (opt.target != BACKUP_TARGET_COMPAT)
sink = bbsink_copystream_new();
if (opt.target == BACKUP_TARGET_CLIENT)
sink = bbsink_copystream_new(true);
else if (opt.target != BACKUP_TARGET_COMPAT)
sink = bbsink_copystream_new(false);
else
sink = bbsink_copytblspc_new();
/*
* If a non-default backup target is in use, arrange to send the data
* wherever it needs to go.
*/
switch (opt.target)
{
case BACKUP_TARGET_BLACKHOLE:
/* Nothing to do, just discard data. */
break;
case BACKUP_TARGET_COMPAT:
case BACKUP_TARGET_CLIENT:
/* Nothing to do, handling above is sufficient. */
break;
case BACKUP_TARGET_SERVER:
sink = bbsink_server_new(sink, opt.target_detail);
break;
}
/* Set up network throttling, if client requested it */
if (opt.maxrate > 0)
sink = bbsink_throttle_new(sink, opt.maxrate);

View File

@@ -44,6 +44,9 @@ typedef struct bbsink_copystream
/* Common information for all types of sink. */
bbsink base;
/* Are we sending the archives to the client, or somewhere else? */
bool send_to_client;
/*
* Protocol message buffer. We assemble CopyData protocol messages by
* setting the first character of this buffer to 'd' (archive or manifest
@@ -131,11 +134,12 @@ const bbsink_ops bbsink_copytblspc_ops = {
* Create a new 'copystream' bbsink.
*/
bbsink *
bbsink_copystream_new(void)
bbsink_copystream_new(bool send_to_client)
{
bbsink_copystream *sink = palloc0(sizeof(bbsink_copystream));
*((const bbsink_ops **) &sink->base.bbs_ops) = &bbsink_copystream_ops;
sink->send_to_client = send_to_client;
/* Set up for periodic progress reporting. */
sink->last_progress_report_time = GetCurrentTimestamp();
@@ -212,8 +216,12 @@ bbsink_copystream_archive_contents(bbsink *sink, size_t len)
StringInfoData buf;
uint64 targetbytes;
/* Send the archive content to the client (with leading type byte). */
pq_putmessage('d', mysink->msgbuffer, len + 1);
/* Send the archive content to the client, if appropriate. */
if (mysink->send_to_client)
{
/* Add one because we're also sending a leading type byte. */
pq_putmessage('d', mysink->msgbuffer, len + 1);
}
/* Consider whether to send a progress report to the client. */
targetbytes = mysink->bytes_done_at_last_time_check
@@ -294,8 +302,11 @@ bbsink_copystream_manifest_contents(bbsink *sink, size_t len)
{
bbsink_copystream *mysink = (bbsink_copystream *) sink;
/* Send the manifest content to the client (with leading type byte). */
pq_putmessage('d', mysink->msgbuffer, len + 1);
if (mysink->send_to_client)
{
/* Add one because we're also sending a leading type byte. */
pq_putmessage('d', mysink->msgbuffer, len + 1);
}
}
/*

View File

@@ -0,0 +1,302 @@
/*-------------------------------------------------------------------------
*
* basebackup_server.c
* store basebackup archives on the server
*
* IDENTIFICATION
* src/backend/replication/basebackup_server.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "miscadmin.h"
#include "replication/basebackup.h"
#include "replication/basebackup_sink.h"
#include "storage/fd.h"
#include "utils/timestamp.h"
#include "utils/wait_event.h"
typedef struct bbsink_server
{
/* Common information for all types of sink. */
bbsink base;
/* Directory in which backup is to be stored. */
char *pathname;
/* Currently open file (or 0 if nothing open). */
File file;
/* Current file position. */
off_t filepos;
} bbsink_server;
static void bbsink_server_begin_archive(bbsink *sink,
const char *archive_name);
static void bbsink_server_archive_contents(bbsink *sink, size_t len);
static void bbsink_server_end_archive(bbsink *sink);
static void bbsink_server_begin_manifest(bbsink *sink);
static void bbsink_server_manifest_contents(bbsink *sink, size_t len);
static void bbsink_server_end_manifest(bbsink *sink);
const bbsink_ops bbsink_server_ops = {
.begin_backup = bbsink_forward_begin_backup,
.begin_archive = bbsink_server_begin_archive,
.archive_contents = bbsink_server_archive_contents,
.end_archive = bbsink_server_end_archive,
.begin_manifest = bbsink_server_begin_manifest,
.manifest_contents = bbsink_server_manifest_contents,
.end_manifest = bbsink_server_end_manifest,
.end_backup = bbsink_forward_end_backup,
.cleanup = bbsink_forward_cleanup
};
/*
* Create a new 'server' bbsink.
*/
bbsink *
bbsink_server_new(bbsink *next, char *pathname)
{
bbsink_server *sink = palloc0(sizeof(bbsink_server));
*((const bbsink_ops **) &sink->base.bbs_ops) = &bbsink_server_ops;
sink->pathname = pathname;
sink->base.bbs_next = next;
/* Replication permission is not sufficient in this case. */
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to create server backup")));
/*
* It's not a good idea to store your backups in the same directory that
* you're backing up. If we allowed a relative path here, that could easily
* happen accidentally, so we don't. The user could still accomplish the
* same thing by including the absolute path to $PGDATA in the pathname,
* but that's likely an intentional bad decision rather than an accident.
*/
if (!is_absolute_path(pathname))
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("relative path not allowed for server backup")));
switch (pg_check_dir(pathname))
{
case 0:
/*
* Does not exist, so create it using the same permissions we'd use
* for a new subdirectory of the data directory itself.
*/
if (MakePGDirectory(pathname) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create directory \"%s\": %m", pathname)));
break;
case 1:
/* Exists, empty. */
break;
case 2:
case 3:
case 4:
/* Exists, not empty. */
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_FILE),
errmsg("directory \"%s\" exists but is not empty",
pathname)));
break;
default:
/* Access problem. */
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not access directory \"%s\": %m",
pathname)));
}
return &sink->base;
}
/*
* Open the correct output file for this archive.
*/
static void
bbsink_server_begin_archive(bbsink *sink, const char *archive_name)
{
bbsink_server *mysink = (bbsink_server *) sink;
char *filename;
Assert(mysink->file == 0);
Assert(mysink->filepos == 0);
filename = psprintf("%s/%s", mysink->pathname, archive_name);
mysink->file = PathNameOpenFile(filename,
O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
if (mysink->file <= 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create file \"%s\": %m", filename)));
pfree(filename);
bbsink_forward_begin_archive(sink, archive_name);
}
/*
* Write the data to the output file.
*/
static void
bbsink_server_archive_contents(bbsink *sink, size_t len)
{
bbsink_server *mysink = (bbsink_server *) sink;
int nbytes;
nbytes = FileWrite(mysink->file, mysink->base.bbs_buffer, len,
mysink->filepos, WAIT_EVENT_BASEBACKUP_WRITE);
if (nbytes != len)
{
if (nbytes < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m",
FilePathName(mysink->file)),
errhint("Check free disk space.")));
/* short write: complain appropriately */
ereport(ERROR,
(errcode(ERRCODE_DISK_FULL),
errmsg("could not write file \"%s\": wrote only %d of %d bytes at offset %u",
FilePathName(mysink->file),
nbytes, (int) len, (unsigned) mysink->filepos),
errhint("Check free disk space.")));
}
mysink->filepos += nbytes;
bbsink_forward_archive_contents(sink, len);
}
/*
* fsync and close the current output file.
*/
static void
bbsink_server_end_archive(bbsink *sink)
{
bbsink_server *mysink = (bbsink_server *) sink;
/*
* We intentionally don't use data_sync_elevel here, because the server
* shouldn't PANIC just because we can't guarantee the the backup has been
* written down to disk. Running recovery won't fix anything in this case
* anyway.
*/
if (FileSync(mysink->file, WAIT_EVENT_BASEBACKUP_SYNC) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m",
FilePathName(mysink->file))));
/* We're done with this file now. */
FileClose(mysink->file);
mysink->file = 0;
mysink->filepos = 0;
bbsink_forward_end_archive(sink);
}
/*
* Open the output file to which we will write the manifest.
*
* Just like pg_basebackup, we write the manifest first under a temporary
* name and then rename it into place after fsync. That way, if the manifest
* is there and under the correct name, the user can be sure that the backup
* completed.
*/
static void
bbsink_server_begin_manifest(bbsink *sink)
{
bbsink_server *mysink = (bbsink_server *) sink;
char *tmp_filename;
Assert(mysink->file == 0);
tmp_filename = psprintf("%s/backup_manifest.tmp", mysink->pathname);
mysink->file = PathNameOpenFile(tmp_filename,
O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
if (mysink->file <= 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not create file \"%s\": %m", tmp_filename)));
pfree(tmp_filename);
bbsink_forward_begin_manifest(sink);
}
/*
* Each chunk of manifest data is sent using a CopyData message.
*/
static void
bbsink_server_manifest_contents(bbsink *sink, size_t len)
{
bbsink_server *mysink = (bbsink_server *) sink;
int nbytes;
nbytes = FileWrite(mysink->file, mysink->base.bbs_buffer, len,
mysink->filepos, WAIT_EVENT_BASEBACKUP_WRITE);
if (nbytes != len)
{
if (nbytes < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m",
FilePathName(mysink->file)),
errhint("Check free disk space.")));
/* short write: complain appropriately */
ereport(ERROR,
(errcode(ERRCODE_DISK_FULL),
errmsg("could not write file \"%s\": wrote only %d of %d bytes at offset %u",
FilePathName(mysink->file),
nbytes, (int) len, (unsigned) mysink->filepos),
errhint("Check free disk space.")));
}
mysink->filepos += nbytes;
bbsink_forward_manifest_contents(sink, len);
}
/*
* fsync the backup manifest, close the file, and then rename it into place.
*/
static void
bbsink_server_end_manifest(bbsink *sink)
{
bbsink_server *mysink = (bbsink_server *) sink;
char *tmp_filename;
char *filename;
/* We're done with this file now. */
FileClose(mysink->file);
mysink->file = 0;
/*
* Rename it into place. This also fsyncs the temporary file, so we don't
* need to do that here. We don't use data_sync_elevel here for the same
* reasons as in bbsink_server_end_archive.
*/
tmp_filename = psprintf("%s/backup_manifest.tmp", mysink->pathname);
filename = psprintf("%s/backup_manifest", mysink->pathname);
durable_rename(tmp_filename, filename, ERROR);
pfree(filename);
pfree(tmp_filename);
bbsink_forward_end_manifest(sink);
}