Skip to content

Commit f2fb902

Browse files
committed
Heap-allocate insert_exec_ctx with NULL guards, resolve unqualified target
schema via the user's default schema, remove the unnecessary LockRelationOid try/catch, clean up the column-count probe error handling, and guard table-variable cleanup against an aborted transaction (fixes an IsTransactionState() crash on the INSERT EXEC error path in a function). Adds same-session DDL detection tests.
1 parent ccc8824 commit f2fb902

7 files changed

Lines changed: 522 additions & 151 deletions

File tree

contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,13 +2708,8 @@ StatementEnd_Internal(PLtsql_execstate *estate, PLtsql_stmt *stmt, bool error)
27082708
ListCell *l;
27092709
PLtsql_expr *expr = ((PLtsql_stmt_execsql *) stmt)->sqlstmt;
27102710

2711-
/*
2712-
* True if this statement runs inside an INSERT EXEC. Covers
2713-
* both paths: the new path uses the global context
2714-
* (pltsql_insert_exec_active), the legacy path uses the
2715-
* per-estate flag (estate->insert_exec).
2716-
*/
2717-
bool insert_exec_active = estate->insert_exec ||
2711+
/* True if running inside a new-path INSERT EXEC. */
2712+
bool insert_exec_active =
27182713
(pltsql_plugin_handler_ptr->pltsql_insert_exec_active &&
27192714
pltsql_plugin_handler_ptr->pltsql_insert_exec_active());
27202715

@@ -2742,19 +2737,20 @@ StatementEnd_Internal(PLtsql_execstate *estate, PLtsql_stmt *stmt, bool error)
27422737
* and it just returned error.
27432738
*/
27442739
row_count_valid =
2740+
!estate->insert_exec &&
27452741
!insert_exec_active &&
27462742
!(markErrorFlag &&
27472743
((PLtsql_stmt_execsql *) stmt)->insert_exec);
27482744
}
27492745
else if (plansource->commandTag == CMDTAG_UPDATE)
27502746
{
27512747
command_type = TDS_CMD_UPDATE;
2752-
row_count_valid = !insert_exec_active;
2748+
row_count_valid = !estate->insert_exec && !insert_exec_active;
27532749
}
27542750
else if (plansource->commandTag == CMDTAG_DELETE)
27552751
{
27562752
command_type = TDS_CMD_DELETE;
2757-
row_count_valid = !insert_exec_active;
2753+
row_count_valid = !estate->insert_exec && !insert_exec_active;
27582754
}
27592755

27602756
/*
@@ -2764,7 +2760,7 @@ StatementEnd_Internal(PLtsql_execstate *estate, PLtsql_stmt *stmt, bool error)
27642760
else if (plansource->commandTag == CMDTAG_SELECT)
27652761
{
27662762
command_type = TDS_CMD_SELECT;
2767-
row_count_valid = !insert_exec_active;
2763+
row_count_valid = !estate->insert_exec && !insert_exec_active;
27682764
}
27692765
}
27702766
}

contrib/babelfishpg_tsql/src/hooks.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,8 +3658,10 @@ bbf_object_access_hook(ObjectAccessType access, Oid classId, Oid objectId, int s
36583658
*/
36593659
if ((access == OAT_POST_ALTER || access == OAT_DROP) && classId == RelationRelationId)
36603660
{
3661-
if (OidIsValid(insert_exec_ctx.target_rel_oid) && objectId == insert_exec_ctx.target_rel_oid)
3662-
insert_exec_ctx.is_target_relation_modified = true;
3661+
if (insert_exec_ctx != NULL &&
3662+
OidIsValid(insert_exec_ctx->target_rel_oid) &&
3663+
objectId == insert_exec_ctx->target_rel_oid)
3664+
insert_exec_ctx->is_target_relation_modified = true;
36633665
}
36643666
}
36653667

contrib/babelfishpg_tsql/src/iterative_exec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,12 @@ exec_stmt_iterative(PLtsql_execstate *estate, ExecCodes *exec_codes, ExecConfig_
16211621

16221622
/* INSERT EXEC: re-throw errors that must abort the whole flush. */
16231623
if (ignore_catch_block_for_insert_exec(estate))
1624+
{
1625+
elog(DEBUG4,
1626+
"INSERT EXEC failed due to error (sqlerrcode=%d) inside procedure TRY-CATCH; re-throwing to abort flush",
1627+
estate->cur_error->error ? estate->cur_error->error->sqlerrcode : 0);
16241628
ReThrowError(estate->cur_error->error);
1629+
}
16251630

16261631
/* Goto error handling blocks */
16271632
*pc = err_handler_pc - 1; /* same as how goto handles PC */

0 commit comments

Comments
 (0)