Fix handling of pending inserts in nodeModifyTable.c

Enterprise / PostgreSQL - Etsuro Fujita [] - 25 November 2022 08:45 UTC

Commit b663a4136, which allowed FDWs to INSERT rows in bulk, added to nodeModifyTable.c code to flush pending inserts to the foreign-table result relation(s) before completing processing of the ModifyTable node, but the code failed to take into account the case where the INSERT query has modifying CTEs, leading to incorrect results.

Also, that commit failed to flush pending inserts before firing BEFORE ROW triggers so that rows are visible to such triggers.

In that commit we scanned through EState's es_tuple_routing_result_relations or es_opened_result_relations list to find the foreign-table result relations to which pending inserts are flushed, but that would be inefficient in some cases. So to fix, 1) add a List member to EState to record the insert-pending result relations, and 2) modify nodeModifyTable.c so that it adds the foreign-table result relation to the list in ExecInsert() if appropriate, and flushes pending inserts properly using the list where needed.

While here, fix a copy-and-pasteo in a comment in ExecBatchInsert(), which was added by that commit.

Back-patch to v14 where that commit appeared.


ffbb7e65a8 Fix handling of pending inserts in nodeModifyTable.c.
contrib/postgres_fdw/expected/postgres_fdw.out | 123 +++++++++++++++++++++++++
contrib/postgres_fdw/sql/postgres_fdw.sql | 86 +++++++++++++++++
src/backend/executor/execMain.c | 1 +
src/backend/executor/execPartition.c | 7 ++
src/backend/executor/execUtils.c | 1 +
src/backend/executor/nodeModifyTable.c | 96 ++++++++++++++-----
src/include/nodes/execnodes.h | 9 ++
7 files changed, 302 insertions(+), 21 deletions(-)


  • Share