Skip to content

Commit 21ec61d

Browse files
lubennikovaavtristan957
authored andcommitted
Fix usage of pg_waldump --ignore option. (#416)
Previously, the --ignore option was only used when reading from a single file.
1 parent 39448d5 commit 21ec61d

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/bin/pg_waldump/pg_waldump.c

+43-12
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ open_file_in_directory(const char *directory, const char *fname)
189189
* wal segment size.
190190
*/
191191
static bool
192-
search_directory(const char *directory, const char *fname)
192+
search_directory(const char *directory, const char *fname, bool ignore_format_errors)
193193
{
194194
int fd = -1;
195195
DIR *xldir;
@@ -233,11 +233,35 @@ search_directory(const char *directory, const char *fname)
233233

234234
WalSegSz = longhdr->xlp_seg_size;
235235

236+
// if we skip errors, we don't need to check the segment size
236237
if (!IsValidWalSegSize(WalSegSz))
238+
{
239+
if (!ignore_format_errors)
240+
{
237241
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",
238-
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
239-
WalSegSz),
240-
fname, WalSegSz);
242+
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
243+
WalSegSz),
244+
fname, WalSegSz);
245+
}
246+
else
247+
{
248+
struct stat stat;
249+
if(fstat(fd, &stat) != 0)
250+
fatal_error("could not stat file \"%s\"", fname);
251+
252+
WalSegSz = stat.st_size;
253+
254+
// if file size is invalid, the xlogreader will fail later with some obscure error
255+
// so better to fail here
256+
if (!IsValidWalSegSize(WalSegSz))
257+
{
258+
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",
259+
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" size is %d bytes",
260+
WalSegSz),
261+
fname, WalSegSz);
262+
}
263+
}
264+
}
241265
}
242266
else if (r < 0)
243267
fatal_error("could not read file \"%s\": %m",
@@ -267,37 +291,37 @@ search_directory(const char *directory, const char *fname)
267291
* The valid target directory is returned.
268292
*/
269293
static char *
270-
identify_target_directory(char *directory, char *fname)
294+
identify_target_directory(char *directory, char *fname, bool ignore_format_errors)
271295
{
272296
char fpath[MAXPGPATH];
273297

274298
if (directory != NULL)
275299
{
276-
if (search_directory(directory, fname))
300+
if (search_directory(directory, fname, ignore_format_errors))
277301
return pg_strdup(directory);
278302

279303
/* directory / XLOGDIR */
280304
snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
281-
if (search_directory(fpath, fname))
305+
if (search_directory(fpath, fname, ignore_format_errors))
282306
return pg_strdup(fpath);
283307
}
284308
else
285309
{
286310
const char *datadir;
287311

288312
/* current directory */
289-
if (search_directory(".", fname))
313+
if (search_directory(".", fname, ignore_format_errors))
290314
return pg_strdup(".");
291315
/* XLOGDIR */
292-
if (search_directory(XLOGDIR, fname))
316+
if (search_directory(XLOGDIR, fname, ignore_format_errors))
293317
return pg_strdup(XLOGDIR);
294318

295319
datadir = getenv("PGDATA");
296320
/* $PGDATA / XLOGDIR */
297321
if (datadir != NULL)
298322
{
299323
snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
300-
if (search_directory(fpath, fname))
324+
if (search_directory(fpath, fname, ignore_format_errors))
301325
return pg_strdup(fpath);
302326
}
303327
}
@@ -1119,7 +1143,7 @@ main(int argc, char **argv)
11191143
fatal_error("could not open directory \"%s\": %m", waldir);
11201144
}
11211145

1122-
waldir = identify_target_directory(waldir, fname);
1146+
waldir = identify_target_directory(waldir, fname, config.ignore_format_errors);
11231147
fd = open_file_in_directory(waldir, fname);
11241148
if (fd < 0)
11251149
fatal_error("could not open file \"%s\"", fname);
@@ -1182,7 +1206,7 @@ main(int argc, char **argv)
11821206
}
11831207
else
11841208
if (!single_file)
1185-
waldir = identify_target_directory(waldir, NULL);
1209+
waldir = identify_target_directory(waldir, NULL, config.ignore_format_errors);
11861210

11871211
/* we don't know what to print */
11881212
if (XLogRecPtrIsInvalid(private.startptr) && !single_file)
@@ -1218,6 +1242,13 @@ main(int argc, char **argv)
12181242
}
12191243
else
12201244
{
1245+
if(config.ignore_format_errors)
1246+
{
1247+
xlogreader_state->skip_page_validation = true;
1248+
xlogreader_state->skip_invalid_records = true;
1249+
xlogreader_state->skip_lsn_checks = true;
1250+
}
1251+
12211252
/* first find a valid recptr to start from */
12221253
first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
12231254

0 commit comments

Comments
 (0)