Skip to content

Commit cf1a5be

Browse files
lubennikovaavtristan957
authored andcommitted
Fix usage of pg_waldump --ignore option. (#418)
Previously, the --ignore option was only used when reading from a single file.
1 parent 8cf5f46 commit cf1a5be

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

src/bin/pg_waldump/pg_waldump.c

+40-9
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ open_file_in_directory(const char *directory, const char *fname)
242242
* wal segment size.
243243
*/
244244
static bool
245-
search_directory(const char *directory, const char *fname)
245+
search_directory(const char *directory, const char *fname, bool ignore_format_errors)
246246
{
247247
int fd = -1;
248248
DIR *xldir;
@@ -286,11 +286,35 @@ search_directory(const char *directory, const char *fname)
286286

287287
WalSegSz = longhdr->xlp_seg_size;
288288

289+
// if we skip errors, we don't need to check the segment size
289290
if (!IsValidWalSegSize(WalSegSz))
291+
{
292+
if (!ignore_format_errors)
293+
{
290294
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",
291295
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
292296
WalSegSz),
293297
fname, WalSegSz);
298+
}
299+
else
300+
{
301+
struct stat stat;
302+
if(fstat(fd, &stat) != 0)
303+
pg_fatal("could not stat file \"%s\"", fname);
304+
305+
WalSegSz = stat.st_size;
306+
307+
// if file size is invalid, the xlogreader will fail later with some obscure error
308+
// so better to fail here
309+
if (!IsValidWalSegSize(WalSegSz))
310+
{
311+
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",
312+
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" size is %d bytes",
313+
WalSegSz),
314+
fname, WalSegSz);
315+
}
316+
}
317+
}
294318
}
295319
else if (r < 0)
296320
pg_fatal("could not read file \"%s\": %m",
@@ -320,37 +344,37 @@ search_directory(const char *directory, const char *fname)
320344
* The valid target directory is returned.
321345
*/
322346
static char *
323-
identify_target_directory(char *directory, char *fname)
347+
identify_target_directory(char *directory, char *fname, bool ignore_format_errors)
324348
{
325349
char fpath[MAXPGPATH];
326350

327351
if (directory != NULL)
328352
{
329-
if (search_directory(directory, fname))
353+
if (search_directory(directory, fname, ignore_format_errors))
330354
return pg_strdup(directory);
331355

332356
/* directory / XLOGDIR */
333357
snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
334-
if (search_directory(fpath, fname))
358+
if (search_directory(fpath, fname, ignore_format_errors))
335359
return pg_strdup(fpath);
336360
}
337361
else
338362
{
339363
const char *datadir;
340364

341365
/* current directory */
342-
if (search_directory(".", fname))
366+
if (search_directory(".", fname, ignore_format_errors))
343367
return pg_strdup(".");
344368
/* XLOGDIR */
345-
if (search_directory(XLOGDIR, fname))
369+
if (search_directory(XLOGDIR, fname, ignore_format_errors))
346370
return pg_strdup(XLOGDIR);
347371

348372
datadir = getenv("PGDATA");
349373
/* $PGDATA / XLOGDIR */
350374
if (datadir != NULL)
351375
{
352376
snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
353-
if (search_directory(fpath, fname))
377+
if (search_directory(fpath, fname, ignore_format_errors))
354378
return pg_strdup(fpath);
355379
}
356380
}
@@ -1279,7 +1303,7 @@ main(int argc, char **argv)
12791303
pg_fatal("could not open directory \"%s\": %m", waldir);
12801304
}
12811305

1282-
waldir = identify_target_directory(waldir, fname);
1306+
waldir = identify_target_directory(waldir, fname, config.ignore_format_errors);
12831307
fd = open_file_in_directory(waldir, fname);
12841308
if (fd < 0)
12851309
pg_fatal("could not open file \"%s\"", fname);
@@ -1342,7 +1366,7 @@ main(int argc, char **argv)
13421366
}
13431367
else
13441368
if (!single_file)
1345-
waldir = identify_target_directory(waldir, NULL);
1369+
waldir = identify_target_directory(waldir, NULL, config.ignore_format_errors);
13461370

13471371
/* we don't know what to print */
13481372
if (XLogRecPtrIsInvalid(private.startptr) && !single_file)
@@ -1377,6 +1401,13 @@ main(int argc, char **argv)
13771401
}
13781402
else
13791403
{
1404+
if(config.ignore_format_errors)
1405+
{
1406+
xlogreader_state->skip_page_validation = true;
1407+
xlogreader_state->skip_invalid_records = true;
1408+
xlogreader_state->skip_lsn_checks = true;
1409+
}
1410+
13801411
/* first find a valid recptr to start from */
13811412
first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
13821413

0 commit comments

Comments
 (0)