diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 5e59c6b..2df1905 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -519,6 +519,7 @@ SnapBuildExportSnapshot(SnapBuild *builder)
 	TransactionId xid;
 	TransactionId *newxip;
 	int			newxcnt = 0;
+	int			newxcnt_max;
 
 	if (builder->state != SNAPBUILD_CONSISTENT)
 		elog(ERROR, "cannot export a snapshot before reaching a consistent state");
@@ -557,8 +558,9 @@ SnapBuildExportSnapshot(SnapBuild *builder)
 	MyPgXact->xmin = snap->xmin;
 
 	/* allocate in transaction context */
+	newxcnt_max = GetMaxSnapshotXidCount();
 	newxip = (TransactionId *)
-		palloc(sizeof(TransactionId) * GetMaxSnapshotXidCount());
+		palloc(sizeof(TransactionId) * newxcnt_max);
 
 	/*
 	 * snapbuild.c builds transactions in an "inverted" manner, which means it
@@ -579,8 +581,11 @@ SnapBuildExportSnapshot(SnapBuild *builder)
 
 		if (test == NULL)
 		{
-			if (newxcnt >= GetMaxSnapshotXidCount())
-				elog(ERROR, "snapshot too large");
+			if (newxcnt >= newxcnt_max)
+			{
+				newxcnt_max *= 2;
+				newxip = repalloc(newxip, sizeof(TransactionId) * newxcnt_max);
+			}
 
 			newxip[newxcnt++] = xid;
 		}
