11#include "access/xact.h"
2+ #include "access/parallel.h"
23#include "commands/explain.h"
34#include "pltsql.h"
45#include "pltsql-2.h"
@@ -1329,8 +1330,14 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
13291330 * when the error is caught by TRY-CATCH
13301331 * 3. This causes "cannot DROP ... because it is being used" errors
13311332 * when the procedure later tries to DROP the temp table
1333+ *
1334+ * Note: exec_state_call_stack can be NULL in parallel workers, so we
1335+ * must check before calling is_part_of_pltsql_trycatch_block.
1336+ *
1337+ * Also skip internal savepoints in parallel workers because
1338+ * BeginInternalSubTransaction is not allowed in parallel mode.
13321339 */
1333- in_trycatch = is_part_of_pltsql_trycatch_block (estate );
1340+ in_trycatch = ( exec_state_call_stack != NULL ) ? is_part_of_pltsql_trycatch_block (estate ) : false ;
13341341 insert_exec_active = pltsql_insert_exec_rewrite_active ();
13351342 /*
13361343 * We need internal savepoints in two cases:
@@ -1346,7 +1353,7 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
13461353 txn_active_or_insert_exec_trycatch = IsTransactionBlockActive () || need_savepoint_for_insert_exec_trycatch ;
13471354
13481355 if (!ro_func && !pltsql_disable_internal_savepoint && !is_batch_command (stmt ) && txn_active_or_insert_exec_trycatch && !is_set_tran_isolation (stmt ) &&
1349- (!insert_exec_active || in_trycatch ))
1356+ (!insert_exec_active || in_trycatch ) && ! IsParallelWorker () )
13501357 {
13511358 elog (DEBUG5 , "TSQL TXN Start internal savepoint" );
13521359 BeginInternalSubTransaction (NULL );
@@ -1430,7 +1437,7 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
14301437 }
14311438 else if (!IsTransactionBlockActive ())
14321439 {
1433- if (is_part_of_pltsql_trycatch_block (estate ) && !pltsql_insert_exec_rewrite_active ())
1440+ if (exec_state_call_stack != NULL && is_part_of_pltsql_trycatch_block (estate ) && !pltsql_insert_exec_rewrite_active ())
14341441 {
14351442 HOLD_INTERRUPTS ();
14361443 elog (DEBUG1 , "TSQL TXN PG semantics : Rollback current transaction" );
0 commit comments