From 7a0359813790c5c3872b5308ff562371e6467266 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Wed, 5 Oct 2022 08:59:45 +0000
Subject: [PATCH v2] Add functions for xlogbackup.c to call back into xlog.c

---
 src/backend/access/transam/xlog.c | 119 ++++++++++++++++++++++++++++--
 src/include/access/xlog.h         |  17 +++++
 2 files changed, 128 insertions(+), 8 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 27085b15a8..604220b474 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -697,10 +697,6 @@ static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos);
 static XLogRecPtr XLogBytePosToEndRecPtr(uint64 bytepos);
 static uint64 XLogRecPtrToBytePos(XLogRecPtr ptr);
 
-static void WALInsertLockAcquire(void);
-static void WALInsertLockAcquireExclusive(void);
-static void WALInsertLockRelease(void);
-static void WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt);
 
 /*
  * Insert an XLOG record represented by an already-constructed chain of data
@@ -1305,7 +1301,7 @@ CopyXLogRecordToWAL(int write_len, bool isLogSwitch, XLogRecData *rdata,
 /*
  * Acquire a WAL insertion lock, for inserting to WAL.
  */
-static void
+void
 WALInsertLockAcquire(void)
 {
 	bool		immed;
@@ -1350,7 +1346,7 @@ WALInsertLockAcquire(void)
  * Acquire all WAL insertion locks, to prevent other backends from inserting
  * to WAL.
  */
-static void
+void
 WALInsertLockAcquireExclusive(void)
 {
 	int			i;
@@ -1379,7 +1375,7 @@ WALInsertLockAcquireExclusive(void)
  * NB: Reset all variables to 0, so they cause LWLockWaitForVar to block the
  * next time the lock is acquired.
  */
-static void
+void
 WALInsertLockRelease(void)
 {
 	if (holdingAllLocks)
@@ -1405,7 +1401,7 @@ WALInsertLockRelease(void)
  * Update our insertingAt value, to let others know that we've finished
  * inserting up to that point.
  */
-static void
+void
 WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt)
 {
 	if (holdingAllLocks)
@@ -8983,3 +8979,110 @@ SetWalWriterSleeping(bool sleeping)
 	XLogCtl->WalWriterSleeping = sleeping;
 	SpinLockRelease(&XLogCtl->info_lck);
 }
+
+/*
+ * Get the ControlFile.
+ */
+ ControlFileData *
+ GetControlFile(void)
+ {
+	return ControlFile;
+ }
+
+/*
+ * Set the forcePageWrites flag.
+ */
+void
+SetforcePageWrites(bool need_lock, bool value)
+{
+	if (need_lock)
+		WALInsertLockAcquireExclusive();
+
+	XLogCtl->Insert.forcePageWrites = value;
+
+	if (need_lock)
+		WALInsertLockRelease();
+}
+
+/*
+ * Set the runningBackups value.
+ */
+void
+SetrunningBackups(bool need_lock, int value)
+{
+	if (need_lock)
+		WALInsertLockAcquireExclusive();
+
+	XLogCtl->Insert.runningBackups = value;
+
+	if (need_lock)
+		WALInsertLockRelease();
+}
+
+/*
+ * Get the runningBackups value.
+ */
+int
+GetrunningBackups(bool need_lock)
+{
+	int value;
+
+	if (need_lock)
+		WALInsertLockAcquireExclusive();
+
+	value = XLogCtl->Insert.runningBackups;
+
+	if (need_lock)
+		WALInsertLockRelease();
+
+	return value;
+}
+
+/*
+ * Get the lastFpwDisableRecPtr.
+ */
+XLogRecPtr
+GetlastFpwDisableRecPtr(void)
+{
+	XLogRecPtr recptr;
+
+	SpinLockAcquire(&XLogCtl->info_lck);
+	recptr = XLogCtl->lastFpwDisableRecPtr;
+	SpinLockRelease(&XLogCtl->info_lck);
+
+	return recptr;
+}
+
+/*
+ * Get the lastBackupStar.
+ */
+XLogRecPtr
+GetlastBackupStart(bool need_lock)
+{
+	XLogRecPtr recptr;
+
+	if (need_lock)
+		WALInsertLockAcquireExclusive();
+
+	recptr = XLogCtl->Insert.lastBackupStart;
+
+	if (need_lock)
+		WALInsertLockRelease();
+
+	return recptr;
+}
+
+/*
+ * Set the lastBackupStar.
+ */
+void
+SetlastBackupStart(bool need_lock, XLogRecPtr recptr)
+{
+	if (need_lock)
+		WALInsertLockAcquireExclusive();
+
+	XLogCtl->Insert.lastBackupStart = recptr;
+
+	if (need_lock)
+		WALInsertLockRelease();
+}
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index dce265098e..8109209eae 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -14,6 +14,7 @@
 #include "access/xlogbackup.h"
 #include "access/xlogdefs.h"
 #include "access/xlogreader.h"
+#include "catalog/pg_control.h"
 #include "datatype/timestamp.h"
 #include "lib/stringinfo.h"
 #include "nodes/pg_list.h"
@@ -258,6 +259,22 @@ extern void SetInstallXLogFileSegmentActive(void);
 extern bool IsInstallXLogFileSegmentActive(void);
 extern void XLogShutdownWalRcv(void);
 
+extern void WALInsertLockAcquire(void);
+extern void WALInsertLockAcquireExclusive(void);
+extern void WALInsertLockRelease(void);
+extern void WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt);
+
+/*
+ * Routines used by xlogbackup.c to call back into xlog.c during backup.
+ */
+extern ControlFileData *GetControlFile(void);
+extern void SetforcePageWrites(bool need_lock, bool value);
+extern void SetrunningBackups(bool need_lock, int value);
+extern int GetrunningBackups(bool need_lock);
+extern XLogRecPtr GetlastFpwDisableRecPtr(void);
+extern XLogRecPtr GetlastBackupStart(bool need_lock);
+extern void SetlastBackupStart(bool need_lock, XLogRecPtr recptr);
+
 /*
  * Routines to start, stop, and get status of a base backup.
  */
-- 
2.34.1

