Skip to content

Commit 46759fc

Browse files
committed
Fix INSERT EXEC stale OID errors by closing target table before clearing context
During major version upgrade tests, BABEL-1944 was failing with could not open relation with OID errors. The root cause was that error cleanup paths were calling pltsql_clear_insert_exec_context() WITHOUT first calling pltsql_insert_exec_close_target_table(), leaving insert_exec_target_rel_oid set to a stale OID. Fix: Add pltsql_insert_exec_close_target_table() calls before pltsql_clear_insert_exec_context() in all error cleanup paths: - iterative_exec.c: No TRY-CATCH cleanup path in !IsTransactionBlockActive - iterative_exec.c: TRY-CATCH cleanup path in exec_stmt_iterative - pl_exec-2.c: exec_stmt_try_catch cleanup This ensures the target table OID is properly cleared and the lock is released before the INSERT EXEC context is cleared.
1 parent 8f14a6e commit 46759fc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

contrib/babelfishpg_tsql/src/iterative_exec.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
14901490
edata = CopyErrorData();
14911491
error_mapped = get_tsql_error_code(edata, &last_error);
14921492
exec_set_error(estate, last_error, edata->sqlerrcode, !error_mapped);
1493+
14931494
if (internal_sp_started &&
14941495
before_lxid == MyProc->vxid.lxid &&
14951496
before_subtxn_id == GetCurrentSubTransactionId())
@@ -1544,6 +1545,7 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
15441545
* will be dropped by the transaction abort.
15451546
*/
15461547
elog(DEBUG1, "TSQL TXN TSQL semantics : INSERT EXEC active but no TRY-CATCH, clearing context for cleanup");
1548+
pltsql_insert_exec_close_target_table();
15471549
pltsql_clear_insert_exec_context();
15481550
}
15491551
}
@@ -1791,7 +1793,10 @@ exec_stmt_iterative(PLtsql_execstate *estate, ExecCodes *exec_codes, ExecConfig_
17911793

17921794
elog(DEBUG1, "INSERT-EXEC: Cleaning up after error caught by TRY-CATCH, temp_oid=%u", temp_oid);
17931795

1794-
/* Clear the INSERT EXEC context first */
1796+
/* Close target table and release lock first */
1797+
pltsql_insert_exec_close_target_table();
1798+
1799+
/* Clear the INSERT EXEC context */
17951800
pltsql_clear_insert_exec_context();
17961801

17971802
/* Drop the temp table if it exists */

contrib/babelfishpg_tsql/src/pl_exec-2.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,9 @@ exec_stmt_try_catch(PLtsql_execstate *estate, PLtsql_stmt_try_catch *stmt)
29222922
elog(LOG, "INSERT-EXEC TRY-CATCH cleanup: is_stmt_terminating=%d, insert_exec_was_active=%d, temp_oid=%u",
29232923
is_stmt_terminating, insert_exec_was_active, temp_oid);
29242924

2925+
/* Close target table and release lock first */
2926+
pltsql_insert_exec_close_target_table();
2927+
29252928
/* Clear the INSERT EXEC context */
29262929
pltsql_clear_insert_exec_context();
29272930

0 commit comments

Comments
 (0)