From 545ac9d567ea6ca610ce08355451923cc787e13d Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Mon, 22 Apr 2019 21:35:06 +0200
Subject: [PATCH] Foreign table COPY FROM and tuple routing requires
 BeginForeignInsert

Commit 3d956d956a introduced support for foreign tables as partitions
and COPY FROM on foreign tables.

If a foreign data wrapper supports data modifications, but either has
not adapted to this change yet or doesn't want to support it
for other reasons, it probably got broken by the above commit,
because COPY will just call ExecForeignInsert anyway, which might not
work because neither PlanForeignModify nor BeginForeignModify have
been called.

To avoid breaking third-party foreign data wrappers in that way, allow
COPY FROM and tuple routing for foreign tables only if the foreign data
wrapper implements BeginForeignInsert.
---
 doc/src/sgml/fdwhandler.sgml         |  2 ++
 src/backend/commands/copy.c          |  5 +++++
 src/backend/executor/execPartition.c | 17 ++++++++++++-----
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/doc/src/sgml/fdwhandler.sgml b/doc/src/sgml/fdwhandler.sgml
index 2c07a86637..84f06d7f81 100644
--- a/doc/src/sgml/fdwhandler.sgml
+++ b/doc/src/sgml/fdwhandler.sgml
@@ -724,6 +724,8 @@ BeginForeignInsert(ModifyTableState *mtstate,
      perform any initialization needed prior to the actual insertion.
      Subsequently, <function>ExecForeignInsert</function> will be called for
      each tuple to be inserted into the foreign table.
+     All foreign data wrappers that support <command>COPY FROM</command> or
+     partitions that are foreign tables have to provide this callback function.
     </para>
 
     <para>
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index c39218f8db..2e09af0070 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -2857,6 +2857,11 @@ CopyFrom(CopyState cstate)
 		resultRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
 		resultRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate,
 														 resultRelInfo);
+	else
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("cannot copy to foreign table \"%s\"",
+						RelationGetRelationName(cstate->rel))));
 
 	/* Prepare to catch AFTER triggers. */
 	AfterTriggerBeginQuery();
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 50800aa5ca..0316d4cbd4 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -939,12 +939,19 @@ ExecInitRoutingInfo(ModifyTableState *mtstate,
 		partrouteinfo->pi_PartitionToRootMap = NULL;
 
 	/*
-	 * If the partition is a foreign table, let the FDW init itself for
-	 * routing tuples to the partition.
+	 * If the partition is a foreign table and the FDW supports it,
+	 * let it init itself for routing tuples to the partition.
 	 */
-	if (partRelInfo->ri_FdwRoutine != NULL &&
-		partRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
-		partRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate, partRelInfo);
+	if (partRelInfo->ri_FdwRoutine != NULL)
+	{
+		if (partRelInfo->ri_FdwRoutine->BeginForeignInsert != NULL)
+			partRelInfo->ri_FdwRoutine->BeginForeignInsert(mtstate, partRelInfo);
+		else
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("cannot route inserted tuples to foreign table \"%s\"",
+						RelationGetRelationName(partRelInfo->ri_RelationDesc))));
+	}
 
 	partRelInfo->ri_PartitionInfo = partrouteinfo;
 	partRelInfo->ri_CopyMultiInsertBuffer = NULL;
-- 
2.20.1

