From e37e37273ccaeb4d135d1859fb9a068bd0b64340 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Thu, 18 Apr 2019 16:00:57 +0900
Subject: [PATCH 09/10] Make pg_rewind not use callback but call the function
 directly

This patch replaces the call to the callback in pg_rewind with direct
call to the original function. Then invalidate the parameters callback
and private for XLogReaderAllocate.
---
 src/bin/pg_rewind/parsexlog.c | 76 ++++++++++++-------------------------------
 1 file changed, 21 insertions(+), 55 deletions(-)

diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c
index 21b638ed34..f28f5d71f5 100644
--- a/src/bin/pg_rewind/parsexlog.c
+++ b/src/bin/pg_rewind/parsexlog.c
@@ -42,16 +42,8 @@ static int	xlogreadfd = -1;
 static XLogSegNo xlogreadsegno = -1;
 static char xlogfpath[MAXPGPATH];
 
-typedef struct XLogPageReadPrivate
-{
-	const char *datadir;
-	int			tliIndex;
-} XLogPageReadPrivate;
-
 static void SimpleXLogPageRead(XLogReaderState *xlogreader,
-				   XLogRecPtr targetPagePtr,
-				   int reqLen, XLogRecPtr targetRecPtr, char *readBuf,
-				   TimeLineID *pageTLI);
+							   const char *datadir, int *tliIndex);
 
 /*
  * Read WAL from the datadir/pg_wal, starting from 'startpoint' on timeline
@@ -65,12 +57,8 @@ extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex,
 	XLogRecord *record;
 	XLogReaderState *xlogreader;
 	char	   *errormsg;
-	XLogPageReadPrivate private;
 
-	private.datadir = datadir;
-	private.tliIndex = tliIndex;
-	xlogreader = XLogReaderAllocate(WalSegSz, &SimpleXLogPageRead,
-									&private);
+	xlogreader = XLogReaderAllocate(WalSegSz, NULL, NULL);
 	if (xlogreader == NULL)
 		pg_fatal("out of memory");
 
@@ -78,12 +66,7 @@ extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex,
 	{
 		while (XLogReadRecord(xlogreader, startpoint, &record, &errormsg) ==
 			   XLREAD_NEED_DATA)
-			xlogreader->read_page(xlogreader,
-								  xlogreader->loadPagePtr,
-								  xlogreader->loadLen,
-								  xlogreader->currRecPtr,
-								  xlogreader->readBuf,
-								  &xlogreader->readPageTLI);
+			SimpleXLogPageRead(xlogreader, datadir, &tliIndex);
 
 		if (record == NULL)
 		{
@@ -125,24 +108,15 @@ readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex)
 	XLogRecord *record;
 	XLogReaderState *xlogreader;
 	char	   *errormsg;
-	XLogPageReadPrivate private;
 	XLogRecPtr	endptr;
 
-	private.datadir = datadir;
-	private.tliIndex = tliIndex;
-	xlogreader = XLogReaderAllocate(WalSegSz, &SimpleXLogPageRead,
-									&private);
+	xlogreader = XLogReaderAllocate(WalSegSz, NULL, NULL);
 	if (xlogreader == NULL)
 		pg_fatal("out of memory");
 
 	while (XLogReadRecord(xlogreader, ptr, &record, &errormsg) ==
 		   XLREAD_NEED_DATA)
-				xlogreader->read_page(xlogreader,
-									  xlogreader->loadPagePtr,
-									  xlogreader->loadLen,
-									  xlogreader->currRecPtr,
-									  xlogreader->readBuf,
-									  &xlogreader->readPageTLI);
+		SimpleXLogPageRead(xlogreader, datadir, &tliIndex);
 
 	if (record == NULL)
 	{
@@ -178,7 +152,6 @@ findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
 	XLogRecPtr	searchptr;
 	XLogReaderState *xlogreader;
 	char	   *errormsg;
-	XLogPageReadPrivate private;
 
 	/*
 	 * The given fork pointer points to the end of the last common record,
@@ -194,10 +167,7 @@ findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
 			forkptr += SizeOfXLogShortPHD;
 	}
 
-	private.datadir = datadir;
-	private.tliIndex = tliIndex;
-	xlogreader = XLogReaderAllocate(WalSegSz, &SimpleXLogPageRead,
-									&private);
+	xlogreader = XLogReaderAllocate(WalSegSz, NULL, NULL);
 	if (xlogreader == NULL)
 		pg_fatal("out of memory");
 
@@ -208,12 +178,7 @@ findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
 
 		while (XLogReadRecord(xlogreader, searchptr, &record, &errormsg) ==
 			   XLREAD_NEED_DATA)
-			xlogreader->read_page(xlogreader,
-								  xlogreader->loadPagePtr,
-								  xlogreader->loadLen,
-								  xlogreader->currRecPtr,
-								  xlogreader->readBuf,
-								  &xlogreader->readPageTLI);
+			SimpleXLogPageRead(xlogreader, datadir, &tliIndex);
 
 		if (record == NULL)
 		{
@@ -260,11 +225,12 @@ findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
 
 /* XLogreader callback function, to read a WAL page */
 static void
-SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
-				   int reqLen, XLogRecPtr targetRecPtr, char *readBuf,
-				   TimeLineID *pageTLI)
+SimpleXLogPageRead(XLogReaderState *xlogreader,
+				   const char*datadir, int *tliIndex)
 {
-	XLogPageReadPrivate *private = (XLogPageReadPrivate *) xlogreader->private_data;
+	XLogRecPtr	targetPagePtr = xlogreader->loadPagePtr;
+	char	   *readBuf		  = xlogreader->readBuf;
+	TimeLineID *pageTLI		  = &xlogreader->readPageTLI;
 	uint32		targetPageOff;
 	XLogRecPtr	targetSegEnd;
 	XLogSegNo	targetSegNo;
@@ -297,17 +263,17 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
 		 * be done both forward and backward, consider also switching timeline
 		 * accordingly.
 		 */
-		while (private->tliIndex < targetNentries - 1 &&
-			   targetHistory[private->tliIndex].end < targetSegEnd)
-			private->tliIndex++;
-		while (private->tliIndex > 0 &&
-			   targetHistory[private->tliIndex].begin >= targetSegEnd)
-			private->tliIndex--;
+		while (*tliIndex < targetNentries - 1 &&
+			   targetHistory[*tliIndex].end < targetSegEnd)
+			(*tliIndex)++;
+		while (*tliIndex > 0 &&
+			   targetHistory[*tliIndex].begin >= targetSegEnd)
+			(*tliIndex)--;
 
-		XLogFileName(xlogfname, targetHistory[private->tliIndex].tli,
+		XLogFileName(xlogfname, targetHistory[*tliIndex].tli,
 					 xlogreadsegno, WalSegSz);
 
-		snprintf(xlogfpath, MAXPGPATH, "%s/" XLOGDIR "/%s", private->datadir, xlogfname);
+		snprintf(xlogfpath, MAXPGPATH, "%s/" XLOGDIR "/%s", datadir, xlogfname);
 
 		xlogreadfd = open(xlogfpath, O_RDONLY | PG_BINARY, 0);
 
@@ -348,7 +314,7 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
 
 	Assert(targetSegNo == xlogreadsegno);
 
-	*pageTLI = targetHistory[private->tliIndex].tli;
+	*pageTLI = targetHistory[*tliIndex].tli;
 
 	xlogreader->readLen = XLOG_BLCKSZ;
 	return;
-- 
2.16.3

