Skip to content

Commit bced316

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

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
@@ -204,7 +204,7 @@ open_file_in_directory(const char *directory, const char *fname)
204204
* wal segment size.
205205
*/
206206
static bool
207-
search_directory(const char *directory, const char *fname)
207+
search_directory(const char *directory, const char *fname, bool ignore_format_errors)
208208
{
209209
int fd = -1;
210210
DIR *xldir;
@@ -248,11 +248,35 @@ search_directory(const char *directory, const char *fname)
248248

249249
WalSegSz = longhdr->xlp_seg_size;
250250

251+
// if we skip errors, we don't need to check the segment size
251252
if (!IsValidWalSegSize(WalSegSz))
253+
{
254+
if (!ignore_format_errors)
255+
{
252256
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",
253257
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
254258
WalSegSz),
255259
fname, WalSegSz);
260+
}
261+
else
262+
{
263+
struct stat stat;
264+
if(fstat(fd, &stat) != 0)
265+
pg_fatal("could not stat file \"%s\"", fname);
266+
267+
WalSegSz = stat.st_size;
268+
269+
// if file size is invalid, the xlogreader will fail later with some obscure error
270+
// so better to fail here
271+
if (!IsValidWalSegSize(WalSegSz))
272+
{
273+
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",
274+
"WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" size is %d bytes",
275+
WalSegSz),
276+
fname, WalSegSz);
277+
}
278+
}
279+
}
256280
}
257281
else if (r < 0)
258282
pg_fatal("could not read file \"%s\": %m",
@@ -282,37 +306,37 @@ search_directory(const char *directory, const char *fname)
282306
* The valid target directory is returned.
283307
*/
284308
static char *
285-
identify_target_directory(char *directory, char *fname)
309+
identify_target_directory(char *directory, char *fname, bool ignore_format_errors)
286310
{
287311
char fpath[MAXPGPATH];
288312

289313
if (directory != NULL)
290314
{
291-
if (search_directory(directory, fname))
315+
if (search_directory(directory, fname, ignore_format_errors))
292316
return pg_strdup(directory);
293317

294318
/* directory / XLOGDIR */
295319
snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
296-
if (search_directory(fpath, fname))
320+
if (search_directory(fpath, fname, ignore_format_errors))
297321
return pg_strdup(fpath);
298322
}
299323
else
300324
{
301325
const char *datadir;
302326

303327
/* current directory */
304-
if (search_directory(".", fname))
328+
if (search_directory(".", fname, ignore_format_errors))
305329
return pg_strdup(".");
306330
/* XLOGDIR */
307-
if (search_directory(XLOGDIR, fname))
331+
if (search_directory(XLOGDIR, fname, ignore_format_errors))
308332
return pg_strdup(XLOGDIR);
309333

310334
datadir = getenv("PGDATA");
311335
/* $PGDATA / XLOGDIR */
312336
if (datadir != NULL)
313337
{
314338
snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
315-
if (search_directory(fpath, fname))
339+
if (search_directory(fpath, fname, ignore_format_errors))
316340
return pg_strdup(fpath);
317341
}
318342
}
@@ -1147,7 +1171,7 @@ main(int argc, char **argv)
11471171
pg_fatal("could not open directory \"%s\": %m", waldir);
11481172
}
11491173

1150-
waldir = identify_target_directory(waldir, fname);
1174+
waldir = identify_target_directory(waldir, fname, config.ignore_format_errors);
11511175
fd = open_file_in_directory(waldir, fname);
11521176
if (fd < 0)
11531177
pg_fatal("could not open file \"%s\"", fname);
@@ -1210,7 +1234,7 @@ main(int argc, char **argv)
12101234
}
12111235
else
12121236
if (!single_file)
1213-
waldir = identify_target_directory(waldir, NULL);
1237+
waldir = identify_target_directory(waldir, NULL, config.ignore_format_errors);
12141238

12151239
/* we don't know what to print */
12161240
if (XLogRecPtrIsInvalid(private.startptr) && !single_file)
@@ -1245,6 +1269,13 @@ main(int argc, char **argv)
12451269
}
12461270
else
12471271
{
1272+
if(config.ignore_format_errors)
1273+
{
1274+
xlogreader_state->skip_page_validation = true;
1275+
xlogreader_state->skip_invalid_records = true;
1276+
xlogreader_state->skip_lsn_checks = true;
1277+
}
1278+
12481279
/* first find a valid recptr to start from */
12491280
first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
12501281

0 commit comments

Comments
 (0)