mirror of
https://github.com/postgres/postgres.git
synced 2026-07-08 01:01:01 -04:00
Previously, if a base backup failed after it had started streaming files, pg_stat_progress_basebackup could continue to show a stale progress entry even though the backup was no longer running. This could be observed when the client kept the replication connection open after the error. It is normally not observable when using pg_basebackup, because the client disconnects after the error. The problem was that progress reporting was cleared only after successful completion. This commit moves the progress reporting cleanup into the progress sink's cleanup callback so that it is cleared after both successful and failed backups. Backpatch to v15. v14 has the same issue, but the fix does not apply cleanly because it lacks the base backup sink infrastructure. Since the bug does not affect the backup itself and is normally not observable when using pg_basebackup, skip the v14 backpatch. Author: Chao Li <lic@highgo.com> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/EA1A6CD2-EFA6-462B-9A02-03003555AB4A@gmail.com Backpatch-through: 15
244 lines
7.5 KiB
C
244 lines
7.5 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* basebackup_progress.c
|
|
* Basebackup sink implementing progress tracking, including but not
|
|
* limited to command progress reporting.
|
|
*
|
|
* This should be used even if the PROGRESS option to the replication
|
|
* command BASE_BACKUP is not specified. Without that option, we won't
|
|
* have tallied up the size of the files that are going to need to be
|
|
* backed up, but we can still report to the command progress reporting
|
|
* facility how much data we've processed.
|
|
*
|
|
* Moreover, we also use this as a convenient place to update certain
|
|
* fields of the bbsink_state. That work is accurately described as
|
|
* keeping track of our progress, but it's not just for introspection.
|
|
* We need those fields to be updated properly in order for base backups
|
|
* to work.
|
|
*
|
|
* This particular basebackup sink requires extra callbacks that most base
|
|
* backup sinks don't. Rather than cramming those into the interface, we just
|
|
* have a few extra functions here that basebackup.c can call. (We could put
|
|
* the logic directly into that file as it's fairly simple, but it seems
|
|
* cleaner to have everything related to progress reporting in one place.)
|
|
*
|
|
* Portions Copyright (c) 2010-2025, PostgreSQL Global Development Group
|
|
*
|
|
* IDENTIFICATION
|
|
* src/backend/backup/basebackup_progress.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#include "postgres.h"
|
|
|
|
#include "backup/basebackup_sink.h"
|
|
#include "commands/progress.h"
|
|
#include "pgstat.h"
|
|
|
|
static void bbsink_progress_begin_backup(bbsink *sink);
|
|
static void bbsink_progress_archive_contents(bbsink *sink, size_t len);
|
|
static void bbsink_progress_end_archive(bbsink *sink);
|
|
static void bbsink_progress_cleanup(bbsink *sink);
|
|
|
|
static const bbsink_ops bbsink_progress_ops = {
|
|
.begin_backup = bbsink_progress_begin_backup,
|
|
.begin_archive = bbsink_forward_begin_archive,
|
|
.archive_contents = bbsink_progress_archive_contents,
|
|
.end_archive = bbsink_progress_end_archive,
|
|
.begin_manifest = bbsink_forward_begin_manifest,
|
|
.manifest_contents = bbsink_forward_manifest_contents,
|
|
.end_manifest = bbsink_forward_end_manifest,
|
|
.end_backup = bbsink_forward_end_backup,
|
|
.cleanup = bbsink_progress_cleanup
|
|
};
|
|
|
|
/*
|
|
* Create a new basebackup sink that performs progress tracking functions and
|
|
* forwards data to a successor sink.
|
|
*/
|
|
bbsink *
|
|
bbsink_progress_new(bbsink *next, bool estimate_backup_size)
|
|
{
|
|
bbsink *sink;
|
|
|
|
Assert(next != NULL);
|
|
|
|
sink = palloc0(sizeof(bbsink));
|
|
*((const bbsink_ops **) &sink->bbs_ops) = &bbsink_progress_ops;
|
|
sink->bbs_next = next;
|
|
|
|
/*
|
|
* Report that a base backup is in progress, and set the total size of the
|
|
* backup to -1, which will get translated to NULL. If we're estimating
|
|
* the backup size, we'll insert the real estimate when we have it.
|
|
*/
|
|
pgstat_progress_start_command(PROGRESS_COMMAND_BASEBACKUP, InvalidOid);
|
|
pgstat_progress_update_param(PROGRESS_BASEBACKUP_BACKUP_TOTAL, -1);
|
|
|
|
return sink;
|
|
}
|
|
|
|
/*
|
|
* Progress reporting at start of backup.
|
|
*/
|
|
static void
|
|
bbsink_progress_begin_backup(bbsink *sink)
|
|
{
|
|
const int index[] = {
|
|
PROGRESS_BASEBACKUP_PHASE,
|
|
PROGRESS_BASEBACKUP_BACKUP_TOTAL,
|
|
PROGRESS_BASEBACKUP_TBLSPC_TOTAL
|
|
};
|
|
int64 val[3];
|
|
|
|
/*
|
|
* Report that we are now streaming database files as a base backup. Also
|
|
* advertise the number of tablespaces, and, if known, the estimated total
|
|
* backup size.
|
|
*/
|
|
val[0] = PROGRESS_BASEBACKUP_PHASE_STREAM_BACKUP;
|
|
if (sink->bbs_state->bytes_total_is_valid)
|
|
val[1] = sink->bbs_state->bytes_total;
|
|
else
|
|
val[1] = -1;
|
|
val[2] = list_length(sink->bbs_state->tablespaces);
|
|
pgstat_progress_update_multi_param(3, index, val);
|
|
|
|
/* Delegate to next sink. */
|
|
bbsink_forward_begin_backup(sink);
|
|
}
|
|
|
|
/*
|
|
* End-of archive progress reporting.
|
|
*/
|
|
static void
|
|
bbsink_progress_end_archive(bbsink *sink)
|
|
{
|
|
/*
|
|
* We expect one archive per tablespace, so reaching the end of an archive
|
|
* also means reaching the end of a tablespace. (Some day we might have a
|
|
* reason to decouple these concepts.)
|
|
*
|
|
* If WAL is included in the backup, we'll mark the last tablespace
|
|
* complete before the last archive is complete, so we need a guard here
|
|
* to ensure that the number of tablespaces streamed doesn't exceed the
|
|
* total.
|
|
*/
|
|
if (sink->bbs_state->tablespace_num < list_length(sink->bbs_state->tablespaces))
|
|
pgstat_progress_update_param(PROGRESS_BASEBACKUP_TBLSPC_STREAMED,
|
|
sink->bbs_state->tablespace_num + 1);
|
|
|
|
/* Delegate to next sink. */
|
|
bbsink_forward_end_archive(sink);
|
|
|
|
/*
|
|
* This is a convenient place to update the bbsink_state's notion of which
|
|
* is the current tablespace. Note that the bbsink_state object is shared
|
|
* across all bbsink objects involved, but we're the outermost one and
|
|
* this is the very last thing we do.
|
|
*/
|
|
sink->bbs_state->tablespace_num++;
|
|
}
|
|
|
|
/*
|
|
* Handle progress tracking for new archive contents.
|
|
*
|
|
* Increment the counter for the amount of data already streamed
|
|
* by the given number of bytes, and update the progress report for
|
|
* pg_stat_progress_basebackup.
|
|
*/
|
|
static void
|
|
bbsink_progress_archive_contents(bbsink *sink, size_t len)
|
|
{
|
|
bbsink_state *state = sink->bbs_state;
|
|
const int index[] = {
|
|
PROGRESS_BASEBACKUP_BACKUP_STREAMED,
|
|
PROGRESS_BASEBACKUP_BACKUP_TOTAL
|
|
};
|
|
int64 val[2];
|
|
int nparam = 0;
|
|
|
|
/* First update bbsink_state with # of bytes done. */
|
|
state->bytes_done += len;
|
|
|
|
/* Now forward to next sink. */
|
|
bbsink_forward_archive_contents(sink, len);
|
|
|
|
/* Prepare to set # of bytes done for command progress reporting. */
|
|
val[nparam++] = state->bytes_done;
|
|
|
|
/*
|
|
* We may also want to update # of total bytes, to avoid overflowing past
|
|
* 100% or the full size. This may make the total size number change as we
|
|
* approach the end of the backup (the estimate will always be wrong if
|
|
* WAL is included), but that's better than having the done column be
|
|
* bigger than the total.
|
|
*/
|
|
if (state->bytes_total_is_valid && state->bytes_done > state->bytes_total)
|
|
val[nparam++] = state->bytes_done;
|
|
|
|
pgstat_progress_update_multi_param(nparam, index, val);
|
|
}
|
|
|
|
/*
|
|
* Clean up progress reporting.
|
|
*/
|
|
static void
|
|
bbsink_progress_cleanup(bbsink *sink)
|
|
{
|
|
pgstat_progress_end_command();
|
|
bbsink_forward_cleanup(sink);
|
|
}
|
|
|
|
/*
|
|
* Advertise that we are waiting for the start-of-backup checkpoint.
|
|
*/
|
|
void
|
|
basebackup_progress_wait_checkpoint(void)
|
|
{
|
|
pgstat_progress_update_param(PROGRESS_BASEBACKUP_PHASE,
|
|
PROGRESS_BASEBACKUP_PHASE_WAIT_CHECKPOINT);
|
|
}
|
|
|
|
/*
|
|
* Advertise that we are estimating the backup size.
|
|
*/
|
|
void
|
|
basebackup_progress_estimate_backup_size(void)
|
|
{
|
|
pgstat_progress_update_param(PROGRESS_BASEBACKUP_PHASE,
|
|
PROGRESS_BASEBACKUP_PHASE_ESTIMATE_BACKUP_SIZE);
|
|
}
|
|
|
|
/*
|
|
* Advertise that we are waiting for WAL archiving at end-of-backup.
|
|
*/
|
|
void
|
|
basebackup_progress_wait_wal_archive(bbsink_state *state)
|
|
{
|
|
const int index[] = {
|
|
PROGRESS_BASEBACKUP_PHASE,
|
|
PROGRESS_BASEBACKUP_TBLSPC_STREAMED
|
|
};
|
|
int64 val[2];
|
|
|
|
/*
|
|
* We report having finished all tablespaces at this point, even if the
|
|
* archive for the main tablespace is still open, because what's going to
|
|
* be added is WAL files, not files that are really from the main
|
|
* tablespace.
|
|
*/
|
|
val[0] = PROGRESS_BASEBACKUP_PHASE_WAIT_WAL_ARCHIVE;
|
|
val[1] = list_length(state->tablespaces);
|
|
pgstat_progress_update_multi_param(2, index, val);
|
|
}
|
|
|
|
/*
|
|
* Advertise that we are transferring WAL files into the final archive.
|
|
*/
|
|
void
|
|
basebackup_progress_transfer_wal(void)
|
|
{
|
|
pgstat_progress_update_param(PROGRESS_BASEBACKUP_PHASE,
|
|
PROGRESS_BASEBACKUP_PHASE_TRANSFER_WAL);
|
|
}
|