@@ -207,7 +207,7 @@ static void process_query_params(ExprContext *econtext,
207207 MYSQL_BIND * * mysql_bind_buf ,
208208 Oid * param_types );
209209
210- static void create_cursor (ForeignScanState * node );
210+ static void bind_stmt_params_and_exec (ForeignScanState * node );
211211
212212void * mysql_dll_handle = NULL ;
213213static int wait_timeout = WAIT_TIMEOUT ;
@@ -475,7 +475,7 @@ mysqlBeginForeignScan(ForeignScanState *node, int eflags)
475475 festate -> query = strVal (list_nth (fsplan -> fdw_private , 0 ));
476476 festate -> retrieved_attrs = list_nth (fsplan -> fdw_private , 1 );
477477 festate -> conn = conn ;
478- festate -> cursor_exists = false;
478+ festate -> query_executed = false;
479479
480480#if PG_VERSION_NUM >= 110000
481481 festate -> temp_cxt = AllocSetContextCreate (estate -> es_query_cxt ,
@@ -530,13 +530,6 @@ mysqlBeginForeignScan(ForeignScanState *node, int eflags)
530530 & festate -> param_values ,
531531 & festate -> param_types );
532532
533- /*
534- * If this is the first call after Begin or ReScan, we need to create the
535- * cursor on the remote side.
536- */
537- if (!festate -> cursor_exists )
538- create_cursor (node );
539-
540533 /* int column_count = mysql_num_fields(festate->meta); */
541534
542535 /* Set the statement as cursor type */
@@ -579,13 +572,6 @@ mysqlBeginForeignScan(ForeignScanState *node, int eflags)
579572 /* Bind the results pointers for the prepare statements */
580573 if (mysql_stmt_bind_result (festate -> stmt , festate -> table -> mysql_bind ) != 0 )
581574 mysql_stmt_error_print (festate , "failed to bind the MySQL query" );
582-
583- /*
584- * Finally execute the query and result will be placed in the array we
585- * already bind
586- */
587- if (mysql_stmt_execute (festate -> stmt ) != 0 )
588- mysql_stmt_error_print (festate , "failed to execute the MySQL query" );
589575}
590576
591577/*
@@ -608,6 +594,13 @@ mysqlIterateForeignScan(ForeignScanState *node)
608594
609595 ExecClearTuple (tupleSlot );
610596
597+ /*
598+ * If this is the first call after Begin or ReScan, we need to bind the
599+ * params and execute the query.
600+ */
601+ if (!festate -> query_executed )
602+ bind_stmt_params_and_exec (node );
603+
611604 attid = 0 ;
612605 rc = mysql_stmt_fetch (festate -> stmt );
613606 if (rc == 0 )
@@ -723,12 +716,12 @@ mysqlReScanForeignScan(ForeignScanState *node)
723716{
724717 MySQLFdwExecState * festate = (MySQLFdwExecState * ) node -> fdw_state ;
725718
726- /* If we haven't created the cursor yet, nothing to do. */
727- if (!festate -> cursor_exists )
728- return ;
719+ /*
720+ * Set the query_executed flag to false so that the query will be executed
721+ * in mysqlIterateForeignScan().
722+ */
723+ festate -> query_executed = false;
729724
730- if (mysql_stmt_execute (festate -> stmt ) != 0 )
731- mysql_stmt_error_print (festate , "failed to execute the MySQL query" );
732725}
733726
734727/*
@@ -1908,10 +1901,11 @@ process_query_params(ExprContext *econtext,
19081901}
19091902
19101903/*
1911- * Create cursor for node's query with current parameter values.
1904+ * Process the query params and bind the same with the statement, if any.
1905+ * Also, execute the statement.
19121906 */
19131907static void
1914- create_cursor (ForeignScanState * node )
1908+ bind_stmt_params_and_exec (ForeignScanState * node )
19151909{
19161910 MySQLFdwExecState * festate = (MySQLFdwExecState * ) node -> fdw_state ;
19171911 ExprContext * econtext = node -> ss .ps .ps_ExprContext ;
@@ -1941,11 +1935,18 @@ create_cursor(ForeignScanState *node)
19411935
19421936 mysql_stmt_bind_param (festate -> stmt , mysql_bind_buffer );
19431937
1944- /* Mark the cursor as created, and show no tuples have been retrieved */
1945- festate -> cursor_exists = true;
1946-
19471938 MemoryContextSwitchTo (oldcontext );
19481939 }
1940+
1941+ /*
1942+ * Finally, execute the query. The result will be placed in the array we
1943+ * already bind.
1944+ */
1945+ if (mysql_stmt_execute (festate -> stmt ) != 0 )
1946+ mysql_stmt_error_print (festate , "failed to execute the MySQL query" );
1947+
1948+ /* Mark the query as executed */
1949+ festate -> query_executed = true;
19491950}
19501951
19511952Datum
0 commit comments