Skip to content

Commit cb2a53d

Browse files
amulsulCommitfest Bot
authored andcommitted
Refactor: pg_waldump: Separate logic used to calculate the required read size.
This refactoring prepares the codebase for an upcoming patch that will support reading WAL from tar files. The logic for calculating the required read size has been updated to handle both normal WAL files and WAL files located inside a tar archive.
1 parent c10d427 commit cb2a53d

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/bin/pg_waldump/pg_waldump.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,29 @@ identify_target_directory(char *directory, char *fname)
326326
return NULL; /* not reached */
327327
}
328328

329+
/* Returns the size in bytes of the data to be read. */
330+
static inline int
331+
required_read_len(XLogDumpPrivate *private, XLogRecPtr targetPagePtr,
332+
int reqLen)
333+
{
334+
int count = XLOG_BLCKSZ;
335+
336+
if (private->endptr != InvalidXLogRecPtr)
337+
{
338+
if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
339+
count = XLOG_BLCKSZ;
340+
else if (targetPagePtr + reqLen <= private->endptr)
341+
count = private->endptr - targetPagePtr;
342+
else
343+
{
344+
private->endptr_reached = true;
345+
return -1;
346+
}
347+
}
348+
349+
return count;
350+
}
351+
329352
/* pg_waldump's XLogReaderRoutine->segment_open callback */
330353
static void
331354
WALDumpOpenSegment(XLogReaderState *state, XLogSegNo nextSegNo,
@@ -383,21 +406,11 @@ WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen,
383406
XLogRecPtr targetPtr, char *readBuff)
384407
{
385408
XLogDumpPrivate *private = state->private_data;
386-
int count = XLOG_BLCKSZ;
409+
int count = required_read_len(private, targetPagePtr, reqLen);
387410
WALReadError errinfo;
388411

389-
if (private->endptr != InvalidXLogRecPtr)
390-
{
391-
if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
392-
count = XLOG_BLCKSZ;
393-
else if (targetPagePtr + reqLen <= private->endptr)
394-
count = private->endptr - targetPagePtr;
395-
else
396-
{
397-
private->endptr_reached = true;
398-
return -1;
399-
}
400-
}
412+
if (private->endptr_reached)
413+
return -1;
401414

402415
if (!WALRead(state, readBuff, targetPagePtr, count, private->timeline,
403416
&errinfo))

0 commit comments

Comments
 (0)