*** a/src/backend/executor/execPartition.c
--- b/src/backend/executor/execPartition.c
***************
*** 1126,1131 **** ExecCleanupTupleRouting(ModifyTableState *mtstate,
--- 1126,1137 ----
  	{
  		ResultRelInfo *resultRelInfo = proute->partitions[i];
  
+ 		/* Allow any FDWs to shut down */
+ 		if (resultRelInfo->ri_FdwRoutine != NULL &&
+ 			resultRelInfo->ri_FdwRoutine->EndForeignInsert != NULL)
+ 			resultRelInfo->ri_FdwRoutine->EndForeignInsert(mtstate->ps.state,
+ 														   resultRelInfo);
+ 
  		/*
  		 * Check if this result rel is one belonging to the node's subplans,
  		 * if so, let ExecEndPlan() clean it up.
***************
*** 1142,1153 **** ExecCleanupTupleRouting(ModifyTableState *mtstate,
  				continue;
  		}
  
- 		/* Allow any FDWs to shut down if they've been exercised */
- 		if (resultRelInfo->ri_FdwRoutine != NULL &&
- 			resultRelInfo->ri_FdwRoutine->EndForeignInsert != NULL)
- 			resultRelInfo->ri_FdwRoutine->EndForeignInsert(mtstate->ps.state,
- 														   resultRelInfo);
- 
  		ExecCloseIndices(resultRelInfo);
  		table_close(resultRelInfo->ri_RelationDesc, NoLock);
  	}
--- 1148,1153 ----
