When a DML with RETURNING clause is fired on a replicated table we get rows from each datanode in reply, whereas the application is expecting only one row in reply. To solve this problem we are calling ExecProcNode on RemoteQueryState repeatedly to accept tuples from one datanode. However we cannot ensure that we will pick the same datanode to accept a tuple always. The current code would pick the tuple that is received first from whatever datanode. This means that it is possible that for a multi-insert case the tuple returned from first iteration was from the first datanode and the one returned from the second iteration was from the second because that somehow arrived first.
One way to solve this problem could be to add xc_node_id in the returning list in any case and then compare it with one of the nodes in RemoteQuery::exec_nodes->nodeList to make sure we pick tuples from one datanode always.
Another way suggested by Ashutosh is by using quals in the RemoteQuery plan and making sure we consume all the rows returned from the datanodes.
The approach currently used is error prone if the DML statement constructed by the planner is such that it updates more than one row (even in case of non-replicated data)