Skip to content

Fix usage of pg_waldump --ignore option. #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions src/bin/pg_waldump/pg_waldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,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 @@ -248,11 +248,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)
{
pg_fatal(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);
}
else
{
struct stat stat;
if(fstat(fd, &stat) != 0)
pg_fatal("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))
{
pg_fatal(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)
pg_fatal("could not read file \"%s\": %m",
Expand Down Expand Up @@ -282,37 +306,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 @@ -1147,7 +1171,7 @@ main(int argc, char **argv)
pg_fatal("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)
pg_fatal("could not open file \"%s\"", fname);
Expand Down Expand Up @@ -1210,7 +1234,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 @@ -1245,6 +1269,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