diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
index 233441837f..0de931e387 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 */
@@ -96,6 +110,7 @@ main(int argc, char *argv[])
 		{"pgdata", required_argument, NULL, 'D'},
 		{"epoch", required_argument, NULL, 'e'},
 		{"force", no_argument, NULL, 'f'},
+		{"test", no_argument, NULL, 't'},
 		{"next-wal-file", required_argument, NULL, 'l'},
 		{"multixact-ids", required_argument, NULL, 'm'},
 		{"dry-run", no_argument, NULL, 'n'},
@@ -108,6 +123,7 @@ main(int argc, char *argv[])
 
 	int			c;
 	bool		force = false;
+	bool		testmode = false;
 	bool		noupdate = false;
 	MultiXactId set_oldestmxid = 0;
 	char	   *endptr;
@@ -135,7 +151,7 @@ main(int argc, char *argv[])
 	}
 
 
-	while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:x:", long_options, NULL)) != -1)
+	while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:tx:", long_options, NULL)) != -1)
 	{
 		switch (c)
 		{
@@ -146,6 +162,10 @@ main(int argc, char *argv[])
 			case 'f':
 				force = true;
 				break;
+
+			case 't':
+				testmode = true;
+				break;
 
 			case 'n':
 				noupdate = true;
@@ -430,6 +450,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) && !testmode)
+			{
+				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 +491,21 @@ 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)
+				&& !testmode)
+			{
+				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 +515,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 && !testmode)
+			{
+				pg_log_error("unsafe mxoff number %u pointed by -m", set_mxoff);
+				exit(1);
+			}
+		}
 		ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
+	}
 
 	if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
 	{
@@ -1214,6 +1279,7 @@ usage(void)
 	printf(_(" [-D, --pgdata=]DATADIR          data directory\n"));
 	printf(_("  -e, --epoch=XIDEPOCH           set next transaction ID epoch\n"));
 	printf(_("  -f, --force                    force update to be done\n"));
+	printf(_("  -t, --test                     test mode to force -O,-x,-m to be done\n"));
 	printf(_("  -l, --next-wal-file=WALFILE    set minimum starting location for new WAL\n"));
 	printf(_("  -m, --multixact-ids=MXID,MXID  set next and oldest multitransaction ID\n"));
 	printf(_("  -n, --dry-run                  no update, just show what would be done\n"));
