diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
index 233441837f..d9ba2425df 100644
--- a/src/bin/pg_resetwal/pg_resetwal.c
+++ b/src/bin/pg_resetwal/pg_resetwal.c
@@ -59,6 +59,20 @@
 #include "pg_getopt.h"
 #include "storage/large_object.h"
 
+#define CLOG_XACTS_PER_BYTE 4
+#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
+
+#define TransactionIdToPage(xid)	((xid) / (TransactionId) CLOG_XACTS_PER_PAGE)
+#define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
+
+#define MULTIXACT_OFFSETS_PER_PAGE (BLCKSZ / sizeof(MultiXactOffset))
+
+#define MultiXactIdToOffsetPage(xid) \
+	((xid) / (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
+#define MultiXactIdToOffsetEntry(xid) \
+	((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
+
+
 static ControlFileData ControlFile; /* pg_control values */
 static XLogSegNo newXlogSegNo;	/* new XLOG segment # */
 static bool guessed = false;	/* T if we had to guess at any values */
@@ -430,6 +444,20 @@ main(int argc, char *argv[])
 
 	if (set_xid != 0)
 	{
+		/*
+		 * Only transactions on the same clog page or the last transaction ID of a page with
+		 * the nextFullXid transaction ID record in pg_control are allowed.
+		 */
+		if(TransactionIdToPgIndex(set_xid) != 0)
+		{
+			TransactionId	xid_in_control = XidFromFullTransactionId(ControlFile.checkPointCopy.nextFullXid);
+
+			if(TransactionIdToPage(set_xid) != TransactionIdToPage(xid_in_control))
+			{
+				pg_log_error("unsafe xid number %u pointed by -x", set_xid);
+				exit(1);
+			}
+		}
 		ControlFile.checkPointCopy.nextFullXid =
 			FullTransactionIdFromEpochAndXid(EpochFromFullTransactionId(ControlFile.checkPointCopy.nextFullXid),
 											 set_xid);
@@ -457,6 +485,20 @@ main(int argc, char *argv[])
 
 	if (set_mxid != 0)
 	{
+		/*
+		 * Only transactions on the same mutixact offset page or the last transaction ID
+		 * of a page with the nextMulti transaction ID record in pg_control are allowed.
+		 */
+		if(MultiXactIdToOffsetEntry(set_mxid) != 0)
+		{
+			MultiXactId	mxid_in_control = ControlFile.checkPointCopy.nextMulti;
+
+			if(MultiXactIdToOffsetPage(set_mxid) != MultiXactIdToOffsetPage(mxid_in_control))
+			{
+				pg_log_error("unsafe mxid number %u pointed by -m", set_mxid);
+				exit(1);
+			}
+		}
 		ControlFile.checkPointCopy.nextMulti = set_mxid;
 
 		ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
@@ -466,7 +508,23 @@ main(int argc, char *argv[])
 	}
 
 	if (set_mxoff != -1)
+	{
+		/*
+		 * Only offset point on the same mutixact member page with the nextMulti
+		 * transaction or the last offset point of a page are allowed.
+		 */
+		if(set_mxoff % BLCKSZ != 0)
+		{
+			MultiXactOffset mxoff_in_control = ControlFile.checkPointCopy.nextMultiOffset;
+
+			if(set_mxoff / BLCKSZ != mxoff_in_control / BLCKSZ)
+			{
+				pg_log_error("unsafe mxoff number %u pointed by -m", set_mxoff);
+				exit(1);
+			}
+		}
 		ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
+	}
 
 	if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
 	{
