Skip to content

Commit

Permalink
Fix crash with COPY FROM and/or foreign partition routing operations.
Browse files Browse the repository at this point in the history
COPY FROM and/or foreign partition routing code path in core assumes
that FDW has BeginForeignInsert() APIs present and thus later
executes ExecForeignInsert().  However, mongo_fdw does not support
routable foreign-table partitions and/or executing COPY FROM on
foreign tables and thus do not have BeginForeignInsert() API
implemented.  But as it has ExecForeignInsert() API, it gets called
for these operations and results in the server crash.  To fix this,
add the BeginForeignInsert() API that throws an error.  Also, add
EndForeignInsert() similar to the Begin API.

The issue was originally reported on MySQL's GitHub through #208.

FDW-226, Suraj Kharage, reviewed by Jeevan Chalke.
  • Loading branch information
jeevanchalke committed Oct 12, 2020
1 parent de5db6e commit c4412a4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions mongo_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ static void MongoExplainForeignModify(ModifyTableState *mtstate,
static bool MongoAnalyzeForeignTable(Relation relation,
AcquireSampleRowsFunc *func,
BlockNumber *totalpages);
#if PG_VERSION_NUM >= 110000
static void MongoBeginForeignInsert(ModifyTableState *mtstate,
ResultRelInfo *resultRelInfo);
static void MongoEndForeignInsert(EState *estate,
ResultRelInfo *resultRelInfo);
#endif

/*
* Helper functions
Expand Down Expand Up @@ -202,6 +208,12 @@ mongo_fdw_handler(PG_FUNCTION_ARGS)
/* Support for ANALYZE */
fdwRoutine->AnalyzeForeignTable = MongoAnalyzeForeignTable;

#if PG_VERSION_NUM >= 110000
/* Partition routing and/or COPY from */
fdwRoutine->BeginForeignInsert = MongoBeginForeignInsert;
fdwRoutine->EndForeignInsert = MongoEndForeignInsert;
#endif

PG_RETURN_POINTER(fdwRoutine);
}

Expand Down Expand Up @@ -2186,3 +2198,36 @@ mongo_fdw_version(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(CODE_VERSION);
}

#if PG_VERSION_NUM >= 110000
/*
* MongoBeginForeignInsert
* Prepare for an insert operation triggered by partition routing
* or COPY FROM.
*
* This is not yet supported, so raise an error.
*/
static void
MongoBeginForeignInsert(ModifyTableState *mtstate,
ResultRelInfo *resultRelInfo)
{
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("COPY and foreign partition routing not supported in mongo_fdw")));
}

/*
* MongoEndForeignInsert
* BeginForeignInsert() is not yet implemented, hence we do not
* have anything to cleanup as of now. We throw an error here just
* to make sure when we do that we do not forget to cleanup
* resources.
*/
static void
MongoEndForeignInsert(EState *estate, ResultRelInfo *resultRelInfo)
{
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("COPY and foreign partition routing not supported in mongo_fdw")));
}
#endif

0 comments on commit c4412a4

Please sign in to comment.