Skip to content

Commit

Permalink
Fix usage of pg_waldump --ignore option. (#416)
Browse files Browse the repository at this point in the history
Previously, the --ignore option was only used when reading from a single file.
  • Loading branch information
lubennikovaav authored and tristan957 committed May 10, 2024
1 parent 98f8945 commit b8e5379
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/bin/pg_waldump/pg_waldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ open_file_in_directory(const char *directory, const char *fname)
* wal segment size.
*/
static bool
search_directory(const char *directory, const char *fname)
search_directory(const char *directory, const char *fname, bool ignore_format_errors)
{
int fd = -1;
DIR *xldir;
Expand Down Expand Up @@ -233,11 +233,35 @@ search_directory(const char *directory, const char *fname)

WalSegSz = longhdr->xlp_seg_size;

// if we skip errors, we don't need to check the segment size
if (!IsValidWalSegSize(WalSegSz))
{
if (!ignore_format_errors)
{
fatal_error(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d byte",
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
WalSegSz),
fname, WalSegSz);
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
WalSegSz),
fname, WalSegSz);
}
else
{
struct stat stat;
if(fstat(fd, &stat) != 0)
fatal_error("could not stat file \"%s\"", fname);

WalSegSz = stat.st_size;

// if file size is invalid, the xlogreader will fail later with some obscure error
// so better to fail here
if (!IsValidWalSegSize(WalSegSz))
{
fatal_error(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" size is %d byte",
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" size is %d bytes",
WalSegSz),
fname, WalSegSz);
}
}
}
}
else if (r < 0)
fatal_error("could not read file \"%s\": %m",
Expand Down Expand Up @@ -267,37 +291,37 @@ search_directory(const char *directory, const char *fname)
* The valid target directory is returned.
*/
static char *
identify_target_directory(char *directory, char *fname)
identify_target_directory(char *directory, char *fname, bool ignore_format_errors)
{
char fpath[MAXPGPATH];

if (directory != NULL)
{
if (search_directory(directory, fname))
if (search_directory(directory, fname, ignore_format_errors))
return pg_strdup(directory);

/* directory / XLOGDIR */
snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
if (search_directory(fpath, fname))
if (search_directory(fpath, fname, ignore_format_errors))
return pg_strdup(fpath);
}
else
{
const char *datadir;

/* current directory */
if (search_directory(".", fname))
if (search_directory(".", fname, ignore_format_errors))
return pg_strdup(".");
/* XLOGDIR */
if (search_directory(XLOGDIR, fname))
if (search_directory(XLOGDIR, fname, ignore_format_errors))
return pg_strdup(XLOGDIR);

datadir = getenv("PGDATA");
/* $PGDATA / XLOGDIR */
if (datadir != NULL)
{
snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
if (search_directory(fpath, fname))
if (search_directory(fpath, fname, ignore_format_errors))
return pg_strdup(fpath);
}
}
Expand Down Expand Up @@ -1119,7 +1143,7 @@ main(int argc, char **argv)
fatal_error("could not open directory \"%s\": %m", waldir);
}

waldir = identify_target_directory(waldir, fname);
waldir = identify_target_directory(waldir, fname, config.ignore_format_errors);
fd = open_file_in_directory(waldir, fname);
if (fd < 0)
fatal_error("could not open file \"%s\"", fname);
Expand Down Expand Up @@ -1182,7 +1206,7 @@ main(int argc, char **argv)
}
else
if (!single_file)
waldir = identify_target_directory(waldir, NULL);
waldir = identify_target_directory(waldir, NULL, config.ignore_format_errors);

/* we don't know what to print */
if (XLogRecPtrIsInvalid(private.startptr) && !single_file)
Expand Down Expand Up @@ -1218,6 +1242,13 @@ main(int argc, char **argv)
}
else
{
if(config.ignore_format_errors)
{
xlogreader_state->skip_page_validation = true;
xlogreader_state->skip_invalid_records = true;
xlogreader_state->skip_lsn_checks = true;
}

/* first find a valid recptr to start from */
first_record = XLogFindNextRecord(xlogreader_state, private.startptr);

Expand Down

0 comments on commit b8e5379

Please sign in to comment.