From 199d160b42cb29ea5044a90008a05e1fc380934c Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Thu, 3 Oct 2024 17:46:06 +0900
Subject: [PATCH v35 03/21] Add new function TwoPhaseXidExists

The undo log system needs to know whether a transaction is in the
prepared state or not. Add a new function TwoPhaseXidExists to
accommodate this requirement.
---
 src/backend/access/transam/twophase.c | 31 +++++++++++++++++++++------
 src/include/access/twophase.h         |  1 +
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 23dd0c6ef6e..0a62c237b9d 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -794,10 +794,11 @@ pg_prepared_xact(PG_FUNCTION_ARGS)
  *		specified by XID
  *
  * If lock_held is set to true, TwoPhaseStateLock will not be taken, so the
- * caller had better hold it.
+ * caller had better hold it. If noerror is true, returns NULL if the global
+ * transaction does not exist.
  */
 static GlobalTransaction
-TwoPhaseGetGXact(TransactionId xid, bool lock_held)
+TwoPhaseGetGXact(TransactionId xid, bool lock_held, bool noerror)
 {
 	GlobalTransaction result = NULL;
 	int			i;
@@ -831,8 +832,13 @@ TwoPhaseGetGXact(TransactionId xid, bool lock_held)
 	if (!lock_held)
 		LWLockRelease(TwoPhaseStateLock);
 
-	if (result == NULL)			/* should not happen */
-		elog(ERROR, "failed to find GlobalTransaction for xid %u", xid);
+	if (result == NULL)
+	{
+		if (!noerror)
+			elog(ERROR, "failed to find GlobalTransaction for xid %u", xid);
+
+		return NULL;
+	}
 
 	cached_xid = xid;
 	cached_gxact = result;
@@ -902,7 +908,7 @@ TwoPhaseGetXidByVirtualXID(VirtualTransactionId vxid,
 ProcNumber
 TwoPhaseGetDummyProcNumber(TransactionId xid, bool lock_held)
 {
-	GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held);
+	GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held, false);
 
 	return gxact->pgprocno;
 }
@@ -917,11 +923,24 @@ TwoPhaseGetDummyProcNumber(TransactionId xid, bool lock_held)
 PGPROC *
 TwoPhaseGetDummyProc(TransactionId xid, bool lock_held)
 {
-	GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held);
+	GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held, false);
 
 	return GetPGProcByNumber(gxact->pgprocno);
 }
 
+/*
+ * TwoPhaseXidExists
+ *		Returns whether the prepared transaction specified by XID exists
+ *
+ * If lock_held is set to true, TwoPhaseStateLock will not be taken, so the
+ * caller had better hold it.
+ */
+bool
+TwoPhaseXidExists(TransactionId xid, bool lock_held)
+{
+	return TwoPhaseGetGXact(xid, lock_held, true) != NULL;
+}
+
 /************************************************************************/
 /* State file support													*/
 /************************************************************************/
diff --git a/src/include/access/twophase.h b/src/include/access/twophase.h
index b85b65c604e..c6298332d36 100644
--- a/src/include/access/twophase.h
+++ b/src/include/access/twophase.h
@@ -38,6 +38,7 @@ extern TransactionId TwoPhaseGetXidByVirtualXID(VirtualTransactionId vxid,
 												bool *have_more);
 extern PGPROC *TwoPhaseGetDummyProc(TransactionId xid, bool lock_held);
 extern int	TwoPhaseGetDummyProcNumber(TransactionId xid, bool lock_held);
+extern bool TwoPhaseXidExists(TransactionId xid, bool lock_held);
 
 extern GlobalTransaction MarkAsPreparing(TransactionId xid, const char *gid,
 										 TimestampTz prepared_at,
-- 
2.43.5

