From fe7478d4ce624fd51497e0f4df3f6526ee2b58e4 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Mon, 18 Jan 2021 17:40:51 +0100
Subject: [PATCH 2/3] tweaks

---
 contrib/postgres_fdw/deparse.c         | 8 ++++++++
 contrib/postgres_fdw/postgres_fdw.c    | 6 +++++-
 src/backend/executor/execPartition.c   | 3 ++-
 src/backend/executor/nodeModifyTable.c | 1 +
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index 2d38ab25cb..644b03fed5 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -1705,6 +1705,9 @@ deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel,
  * The statement text is appended to buf, and we also create an integer List
  * of the columns being retrieved by WITH CHECK OPTION or RETURNING (if any),
  * which is returned to *retrieved_attrs.
+ *
+ * This also stores end position of the VALUES clause, so that we can rebuild
+ * an INSERT for a batch of rows later.
  */
 void
 deparseInsertSql(StringInfo buf, RangeTblEntry *rte,
@@ -1767,6 +1770,8 @@ deparseInsertSql(StringInfo buf, RangeTblEntry *rte,
 /*
  * rebuild remote INSERT statement
  *
+ * Provided a number of rows in a batch, builds INSERT statement with the
+ * right number of parameters.
  */
 void
 rebuildInsertSql(StringInfo buf, char *orig_query,
@@ -1777,6 +1782,9 @@ rebuildInsertSql(StringInfo buf, char *orig_query,
 	int			pindex;
 	bool		first;
 
+	/* Make sure the values_end_len is sensible */
+	Assert((values_end_len > 0) && (values_end_len <= strlen(orig_query)));
+
 	/* Copy up to the end of the first record from the original query */
 	appendBinaryStringInfo(buf, orig_query, values_end_len);
 
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index b317942596..0f6d0705ec 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1923,7 +1923,11 @@ postgresExecForeignBatchInsert(EState *estate,
 
 /*
  * postgresGetForeignModifyBatchSize
- *		Report the maximum number of tuples that can be inserted in bulk
+ *		Determine the maximum number of tuples that can be inserted in bulk
+ *
+ * Returns the batch size specified for server or table. When batching is not
+ * allowed (e.g. for tables with AFTER ROW triggers or with RETURNING clause),
+ * returns 1.
  */
 static int
 postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 9349e2c859..448a321630 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -996,13 +996,14 @@ ExecInitRoutingInfo(ModifyTableState *mtstate,
 	/*
 	 * Determine if the FDW supports batch insert and determine the batch
 	 * size (a FDW may support batching, but it may be disabled for the
-	 * server/table).
+	 * server/table or for this particular query).
 	 */
 	if (partRelInfo->ri_FdwRoutine != NULL &&
 		partRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize &&
 		partRelInfo->ri_FdwRoutine->ExecForeignBatchInsert)
 		partRelInfo->ri_BatchSize =
 			partRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize(partRelInfo);
+
 	Assert(partRelInfo->ri_BatchSize >= 0);
 
 	partRelInfo->ri_CopyMultiInsertBuffer = NULL;
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index 1412f97f20..48e4e2e34c 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -2805,6 +2805,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
 		resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert)
 		resultRelInfo->ri_BatchSize =
 			resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize(resultRelInfo);
+
 	Assert(resultRelInfo->ri_BatchSize >= 0);
 
 	/*
-- 
2.26.2

