From 6f8752f0940d48e8a4d3aacaaf04efbc514d17e9 Mon Sep 17 00:00:00 2001
From: "houzj.fnst" <houzj.fnst@cn.fujitsu.com>
Date: Fri, 21 May 2021 10:11:28 +0800
Subject: [PATCH] limit the fdw batch size

---
 contrib/postgres_fdw/postgres_fdw.c | 10 ++++++++++
 src/interfaces/libpq/fe-exec.c      |  6 +++---
 src/interfaces/libpq/libpq-fe.h     |  1 +
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index c48a421e88..68e9028ec1 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1994,6 +1994,16 @@ postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
 		 resultRelInfo->ri_TrigDesc->trig_insert_after_row))
 		return 1;
 
+	/*
+	 * The maximum number of parameters supported by the FE/BE protocol is
+	 * 65535, so set the batch_size to not exceed limit in a batch insert.
+	 */
+	Assert(fmstate->p_nums > 0);
+	if (batch_size * fmstate->p_nums > PQ_MAX_PARAM_NUMBER)
+	{
+		batch_size = PQ_MAX_PARAM_NUMBER / fmstate->p_nums;
+	}
+
 	/* Otherwise use the batch size specified for server/table. */
 	return batch_size;
 }
diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c
index 03592bdce9..90bccdeb6d 100644
--- a/src/interfaces/libpq/fe-exec.c
+++ b/src/interfaces/libpq/fe-exec.c
@@ -1403,7 +1403,7 @@ PQsendQueryParams(PGconn *conn,
 							 libpq_gettext("command string is a null pointer\n"));
 		return 0;
 	}
-	if (nParams < 0 || nParams > 65535)
+	if (nParams < 0 || nParams > PQ_MAX_PARAM_NUMBER)
 	{
 		appendPQExpBufferStr(&conn->errorMessage,
 							 libpq_gettext("number of parameters must be between 0 and 65535\n"));
@@ -1451,7 +1451,7 @@ PQsendPrepare(PGconn *conn,
 							 libpq_gettext("command string is a null pointer\n"));
 		return 0;
 	}
-	if (nParams < 0 || nParams > 65535)
+	if (nParams < 0 || nParams > PQ_MAX_PARAM_NUMBER)
 	{
 		appendPQExpBufferStr(&conn->errorMessage,
 							 libpq_gettext("number of parameters must be between 0 and 65535\n"));
@@ -1548,7 +1548,7 @@ PQsendQueryPrepared(PGconn *conn,
 							 libpq_gettext("statement name is a null pointer\n"));
 		return 0;
 	}
-	if (nParams < 0 || nParams > 65535)
+	if (nParams < 0 || nParams > PQ_MAX_PARAM_NUMBER)
 	{
 		appendPQExpBufferStr(&conn->errorMessage,
 							 libpq_gettext("number of parameters must be between 0 and 65535\n"));
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 227adde5a5..ed3b95e365 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -429,6 +429,7 @@ extern PGresult *PQexecPrepared(PGconn *conn,
 								int resultFormat);
 
 /* Interface for multiple-result or asynchronous queries */
+#define PQ_MAX_PARAM_NUMBER 65535
 extern int	PQsendQuery(PGconn *conn, const char *query);
 extern int	PQsendQueryParams(PGconn *conn,
 							  const char *command,
-- 
2.18.4

