Skip to content

Commit 38d2e5e

Browse files
amulsulCommitfest Bot
authored andcommitted
pg_verifybackup: enabled WAL parsing for tar-format backup
Now that pg_waldump supports decoding from tar archives, we should leverage this functionality to remove the previous restriction on WAL parsing for tar-backed formats.
1 parent 048af8c commit 38d2e5e

File tree

6 files changed

+50
-35
lines changed

6 files changed

+50
-35
lines changed

doc/src/sgml/ref/pg_verifybackup.sgml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ PostgreSQL documentation
3636
<literal>backup_manifest</literal> generated by the server at the time
3737
of the backup. The backup may be stored either in the "plain" or the "tar"
3838
format; this includes tar-format backups compressed with any algorithm
39-
supported by <application>pg_basebackup</application>. However, at present,
40-
<literal>WAL</literal> verification is supported only for plain-format
41-
backups. Therefore, if the backup is stored in tar-format, the
42-
<literal>-n, --no-parse-wal</literal> option should be used.
39+
supported by <application>pg_basebackup</application>.
4340
</para>
4441

4542
<para>

src/bin/pg_verifybackup/pg_verifybackup.c

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ pg_noreturn static void report_manifest_error(JsonManifestParseContext *context,
7474
const char *fmt,...)
7575
pg_attribute_printf(2, 3);
7676

77-
static void verify_tar_backup(verifier_context *context, DIR *dir);
77+
static void verify_tar_backup(verifier_context *context, DIR *dir,
78+
char **base_archive_path,
79+
char **wal_archive_path);
7880
static void verify_plain_backup_directory(verifier_context *context,
7981
char *relpath, char *fullpath,
8082
DIR *dir);
@@ -83,7 +85,9 @@ static void verify_plain_backup_file(verifier_context *context, char *relpath,
8385
static void verify_control_file(const char *controlpath,
8486
uint64 manifest_system_identifier);
8587
static void precheck_tar_backup_file(verifier_context *context, char *relpath,
86-
char *fullpath, SimplePtrList *tarfiles);
88+
char *fullpath, SimplePtrList *tarfiles,
89+
char **base_archive_path,
90+
char **wal_archive_path);
8791
static void verify_tar_file(verifier_context *context, char *relpath,
8892
char *fullpath, astreamer *streamer);
8993
static void report_extra_backup_files(verifier_context *context);
@@ -136,6 +140,8 @@ main(int argc, char **argv)
136140
bool no_parse_wal = false;
137141
bool quiet = false;
138142
char *wal_path = NULL;
143+
char *base_archive_path = NULL;
144+
char *wal_archive_path = NULL;
139145
char *pg_waldump_path = NULL;
140146
DIR *dir;
141147

@@ -327,17 +333,6 @@ main(int argc, char **argv)
327333
pfree(path);
328334
}
329335

330-
/*
331-
* XXX: In the future, we should consider enhancing pg_waldump to read WAL
332-
* files from an archive.
333-
*/
334-
if (!no_parse_wal && context.format == 't')
335-
{
336-
pg_log_error("pg_waldump cannot read tar files");
337-
pg_log_error_hint("You must use -n/--no-parse-wal when verifying a tar-format backup.");
338-
exit(1);
339-
}
340-
341336
/*
342337
* Perform the appropriate type of verification appropriate based on the
343338
* backup format. This will close 'dir'.
@@ -346,7 +341,7 @@ main(int argc, char **argv)
346341
verify_plain_backup_directory(&context, NULL, context.backup_directory,
347342
dir);
348343
else
349-
verify_tar_backup(&context, dir);
344+
verify_tar_backup(&context, dir, &base_archive_path, &wal_archive_path);
350345

351346
/*
352347
* The "matched" flag should now be set on every entry in the hash table.
@@ -364,9 +359,28 @@ main(int argc, char **argv)
364359
if (context.format == 'p' && !context.skip_checksums)
365360
verify_backup_checksums(&context);
366361

367-
/* By default, look for the WAL in the backup directory, too. */
362+
/*
363+
* By default, WAL files are expected to be found in the backup directory
364+
* for plain-format backups. In the case of tar-format backups, if a
365+
* separate WAL archive is not found, the WAL files are most likely
366+
* included within the main data directory archive.
367+
*/
368368
if (wal_path == NULL)
369-
wal_path = psprintf("%s/pg_wal", context.backup_directory);
369+
{
370+
if (context.format == 'p')
371+
wal_path = psprintf("%s/pg_wal", context.backup_directory);
372+
else if (wal_archive_path)
373+
wal_path = wal_archive_path;
374+
else if (base_archive_path)
375+
wal_path = base_archive_path;
376+
else
377+
{
378+
pg_log_error("wal archive not found");
379+
pg_log_error_hint("Specify the correct path using the option -w/--wal-path."
380+
"Or you must use -n/--no-parse-wal when verifying a tar-format backup.");
381+
exit(1);
382+
}
383+
}
370384

371385
/*
372386
* Try to parse the required ranges of WAL records, unless we were told
@@ -787,7 +801,8 @@ verify_control_file(const char *controlpath, uint64 manifest_system_identifier)
787801
* close when we're done with it.
788802
*/
789803
static void
790-
verify_tar_backup(verifier_context *context, DIR *dir)
804+
verify_tar_backup(verifier_context *context, DIR *dir, char **base_archive_path,
805+
char **wal_archive_path)
791806
{
792807
struct dirent *dirent;
793808
SimplePtrList tarfiles = {NULL, NULL};
@@ -816,7 +831,8 @@ verify_tar_backup(verifier_context *context, DIR *dir)
816831
char *fullpath;
817832

818833
fullpath = psprintf("%s/%s", context->backup_directory, filename);
819-
precheck_tar_backup_file(context, filename, fullpath, &tarfiles);
834+
precheck_tar_backup_file(context, filename, fullpath, &tarfiles,
835+
base_archive_path, wal_archive_path);
820836
pfree(fullpath);
821837
}
822838
}
@@ -875,11 +891,13 @@ verify_tar_backup(verifier_context *context, DIR *dir)
875891
*
876892
* The arguments to this function are mostly the same as the
877893
* verify_plain_backup_file. The additional argument outputs a list of valid
878-
* tar files.
894+
* tar files, along with the full paths to the main archive and the WAL
895+
* directory archive.
879896
*/
880897
static void
881898
precheck_tar_backup_file(verifier_context *context, char *relpath,
882-
char *fullpath, SimplePtrList *tarfiles)
899+
char *fullpath, SimplePtrList *tarfiles,
900+
char **base_archive_path, char **wal_archive_path)
883901
{
884902
struct stat sb;
885903
Oid tblspc_oid = InvalidOid;
@@ -918,9 +936,17 @@ precheck_tar_backup_file(verifier_context *context, char *relpath,
918936
* extension such as .gz, .lz4, or .zst.
919937
*/
920938
if (strncmp("base", relpath, 4) == 0)
939+
{
921940
suffix = relpath + 4;
941+
942+
*base_archive_path = pstrdup(fullpath);
943+
}
922944
else if (strncmp("pg_wal", relpath, 6) == 0)
945+
{
923946
suffix = relpath + 6;
947+
948+
*wal_archive_path = pstrdup(fullpath);
949+
}
924950
else
925951
{
926952
/* Expected a <tablespaceoid>.tar file here. */

src/bin/pg_verifybackup/t/002_algorithm.pl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ sub test_checksums
3030
{
3131
# Add switch to get a tar-format backup
3232
push @backup, ('--format' => 'tar');
33-
34-
# Add switch to skip WAL verification, which is not yet supported for
35-
# tar-format backups
36-
push @verify, ('--no-parse-wal');
3733
}
3834

3935
# A backup with a bogus algorithm should fail.

src/bin/pg_verifybackup/t/003_corruption.pl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,8 @@
193193
command_ok([ $tar, '-cf' => "$tar_backup_path/base.tar", '.' ]);
194194
chdir($cwd) || die "chdir: $!";
195195

196-
# Now check that the backup no longer verifies. We must use -n
197-
# here, because pg_waldump can't yet read WAL from a tarfile.
198196
command_fails_like(
199-
[ 'pg_verifybackup', '--no-parse-wal', $tar_backup_path ],
197+
[ 'pg_verifybackup', $tar_backup_path ],
200198
$scenario->{'fails_like'},
201199
"corrupt backup fails verification: $name");
202200

src/bin/pg_verifybackup/t/008_untar.pl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@
123123
# Verify tar backup.
124124
$primary->command_ok(
125125
[
126-
'pg_verifybackup', '--no-parse-wal',
127-
'--exit-on-error', $backup_path,
126+
'pg_verifybackup', '--exit-on-error', $backup_path,
128127
],
129128
"verify backup, compression $method");
130129

src/bin/pg_verifybackup/t/010_client_untar.pl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@
137137
# Verify tar backup.
138138
$primary->command_ok(
139139
[
140-
'pg_verifybackup', '--no-parse-wal',
141-
'--exit-on-error', $backup_path,
140+
'pg_verifybackup', '--exit-on-error', $backup_path,
142141
],
143142
"verify backup, compression $method");
144143

0 commit comments

Comments
 (0)