@@ -74,9 +74,6 @@ static void insertexec_destroy(DestReceiver *self);
7474 */
7575InsertExecContext * insert_exec_ctx = NULL ;
7676PLtsql_execstate * insert_exec_flush_estate = NULL ;
77-
78- extern void exec_set_rowcount (uint64 rowno );
79- extern void exec_set_found (PLtsql_execstate * estate , bool state );
8077/*
8178 * The flush INSERT is routed through execute_batch (the top-level batch entry
8279 * point). It runs through the same econtext setup as a normal T-SQL batch.
@@ -268,19 +265,20 @@ pltsql_insert_exec_open_target_table(const char *target_table,
268265 char * schema_name = NULL ;
269266 char * table_name = NULL ;
270267 char * physical_schema = NULL ;
268+ char * db = NULL ;
271269
272270 if (target_table == NULL )
273271 return ;
274272
275273 table_name = pstrdup (target_table );
276- schema_name = resolve_insert_exec_schema_name ( schema_name_in , db_name_in );
277- /*
278- * Resolve against the target's database when a 3-part name
279- * (db..table) was used; otherwise the current database.
280- */
281- physical_schema = get_physical_schema_name (
282- ( db_name_in != NULL ) ? ( char * ) db_name_in : get_cur_db_name (),
283- schema_name );
274+ db = ( db_name_in != NULL ) ? pstrdup ( db_name_in ) : get_cur_db_name ( );
275+ if ( db != NULL && db [ 0 ] != '\0' )
276+ {
277+ schema_name = resolve_insert_exec_schema_name ( schema_name_in , db );
278+ physical_schema = get_physical_schema_name ( db , schema_name );
279+ }
280+ if ( db != NULL )
281+ pfree ( db );
284282
285283 /* Create RangeVar and get the relation OID */
286284 rv = makeRangeVar (physical_schema , table_name , -1 );
@@ -451,14 +449,25 @@ create_insert_exec_temp_table(const char *target_table, const char *column_list,
451449 */
452450 if (!(target_table [0 ] == '#' || target_table [0 ] == '@' ))
453451 {
454- char * sname = resolve_insert_exec_schema_name (schema_name_in , db_name_in );
455-
456- physical_schema = get_physical_schema_name (
457- (db_name_in != NULL ) ? (char * ) db_name_in : get_cur_db_name (), sname );
458- pfree (sname );
459- if (physical_schema == NULL )
460- elog (ERROR , "INSERT EXEC failed due to unresolvable schema for target table \"%s\"" ,
461- target_table );
452+ char * db = (db_name_in != NULL ) ? pstrdup (db_name_in ) : get_cur_db_name ();
453+ /*
454+ * On the PostgreSQL endpoint a T-SQL procedure runs without logical
455+ * database context (fn_dbid is InvalidDbid for non-TDS connections),
456+ * so the current database name can be empty. With no DB context, leave
457+ * physical_schema NULL and reference the target by its bare name, so
458+ * search_path resolves it - exactly as a plain INSERT does.
459+ */
460+ if (db != NULL && db [0 ] != '\0' )
461+ {
462+ char * sname = resolve_insert_exec_schema_name (schema_name_in , db );
463+ physical_schema = get_physical_schema_name (db , sname );
464+ pfree (sname );
465+ if (physical_schema == NULL )
466+ elog (ERROR , "INSERT EXEC failed due to unresolvable schema for target table \"%s\"" ,
467+ target_table );
468+ }
469+ if (db != NULL )
470+ pfree (db );
462471 }
463472
464473 /*
@@ -621,8 +630,6 @@ flush_insert_exec_temp_table(PLtsql_execstate *estate, const char *target_schema
621630 * Report rows-affected from the DestReceiver's captured-row count
622631 */
623632 estate -> eval_processed = insert_exec_ctx -> rows_processed ;
624- exec_set_rowcount (insert_exec_ctx -> rows_processed );
625- exec_set_found (estate , insert_exec_ctx -> rows_processed != 0 );
626633}
627634
628635/*
0 commit comments