Skip to content

Commit 1c80f61

Browse files
committed
Fix INSERT EXEC snapshot error when target table has AFTER triggers
1 parent 31c1136 commit 1c80f61

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

contrib/babelfishpg_tsql/src/hooks.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,9 +1682,6 @@ plsql_TriggerRecursiveCheck(ResultRelInfo *resultRelInfo)
16821682
return false;
16831683
if (pltsql_recursive_triggers)
16841684
return false;
1685-
/* Safety check for parallel workers where exec_state_call_stack may be NULL */
1686-
if (exec_state_call_stack == NULL)
1687-
return false;
16881685
cur = exec_state_call_stack;
16891686
while (cur != NULL)
16901687
{

contrib/babelfishpg_tsql/src/pl_exec-2.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,23 @@ exec_stmt_exec(PLtsql_execstate *estate, PLtsql_stmt_exec *stmt)
12761276
stmt->insert_exec_columns,
12771277
saved_nested_tran_count);
12781278
insert_exec_context_set = true;
1279+
1280+
/*
1281+
* Temporarily disable T-SQL trigger transaction handling during INSERT EXEC.
1282+
*
1283+
* When a T-SQL AFTER trigger fires during INSERT EXEC, pltsql_exec_trigger
1284+
* sets estate.atomic = false (because support_tsql_trans &&
1285+
* !pltsql_disable_txn_in_triggers is true). This makes the trigger use
1286+
* SPI_connect_ext(SPI_OPT_NONATOMIC), which pushes an independent snapshot
1287+
* that conflicts with the portal's snapshot tracking, causing:
1288+
* "portal snapshots (1) did not account for all active snapshots (2)"
1289+
*
1290+
* Setting pltsql_disable_txn_in_triggers = true forces triggers to use
1291+
* normal atomic SPI, which is compatible with the INSERT EXEC execution.
1292+
* The trigger body still fires and executes normally — only the
1293+
* Babelfish-specific non-atomic snapshot path is bypassed.
1294+
*/
1295+
pltsql_disable_txn_in_triggers = true;
12791296
}
12801297

12811298
rc = SPI_execute_plan_extended(expr->plan, &options);

contrib/babelfishpg_tsql/src/pl_exec.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5468,7 +5468,13 @@ exec_stmt_execsql(PLtsql_execstate *estate,
54685468
if ((!pltsql_disable_batch_auto_commit || (stmt->txn_data != NULL)) &&
54695469
support_tsql_trans &&
54705470
(enable_txn_in_triggers || estate->trigdata == NULL) &&
5471-
!ro_func && !estate->insert_exec)
5471+
!ro_func && !estate->insert_exec &&
5472+
!pltsql_insert_exec_rewrite_active()) /* Don't commit inside INSERT EXEC context:
5473+
* triggers firing during the flush INSERT would
5474+
* call CommitTransactionCommand() which destroys
5475+
* the outer SPI portal's snapshot state, causing
5476+
* "portal snapshots did not account for all
5477+
* active snapshots" at portalmem.c:1292 */
54725478
{
54735479
commit_stmt(estate, (estate->tsql_trigger_flags & TSQL_TRAN_STARTED));
54745480

0 commit comments

Comments
 (0)