Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transaction leak when DMLs contains a non pltsql language function which calls another non tsql function inside try catch #3217

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions contrib/babelfishpg_tsql/src/pl_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,10 @@ pltsql_exec_trigger(PLtsql_function *func,
PLtsql_rec *rec_new,
*rec_old;
HeapTuple rettup;
bool support_tsql_trans = pltsql_support_tsql_transactions();

/* Check if this trigger is called as part of any of postgres' function, procedure or trigger. */
if (!pltsql_support_tsql_transactions())
if (!support_tsql_trans)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
Expand All @@ -1050,7 +1051,7 @@ pltsql_exec_trigger(PLtsql_function *func,
*/
pltsql_estate_setup(&estate, func, NULL, NULL);

if (pltsql_support_tsql_transactions() && !pltsql_disable_txn_in_triggers)
if (support_tsql_trans && !pltsql_disable_txn_in_triggers)
estate.atomic = false;

estate.trigdata = trigdata;
Expand Down Expand Up @@ -1173,15 +1174,15 @@ pltsql_exec_trigger(PLtsql_function *func,
* TSQL triggers terminate if there is no transaction active at the
* end
*/
if (pltsql_support_tsql_transactions() && !pltsql_disable_txn_in_triggers && NestedTranCount == 0)
if (support_tsql_trans && !pltsql_disable_txn_in_triggers && NestedTranCount == 0)
ereport(ERROR,
(errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
errmsg("The transaction ended in the trigger. The batch has been aborted.")));

/*
* If an error was encountered when executing trigger.
*/
if (pltsql_support_tsql_transactions() && !pltsql_disable_txn_in_triggers && exec_state_call_stack->error_data.trigger_error)
if (support_tsql_trans && !pltsql_disable_txn_in_triggers && exec_state_call_stack->error_data.trigger_error)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("An error was raised during trigger execution. The batch has been aborted and the user transaction, if any, has been rolled back.")));
Expand Down Expand Up @@ -4622,6 +4623,7 @@ exec_stmt_execsql(PLtsql_execstate *estate,
bool fmtonly_enabled = true;
CmdType cmd = CMD_UNKNOWN;
bool enable_txn_in_triggers = !pltsql_disable_txn_in_triggers;
bool support_tsql_trans = pltsql_support_tsql_transactions();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see if this fix is needed for other callers of pltsql_support_tsql_transactions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also made this change in pltsql_exec_trigger. Change was not needed there, but did it anyway for consistency in code.

StringInfoData query;
bool need_path_reset = false;
char *cur_dbname = get_cur_db_name();
Expand Down Expand Up @@ -4799,7 +4801,7 @@ exec_stmt_execsql(PLtsql_execstate *estate,
/* Open nesting level in engine */
BeginCompositeTriggers(CurrentMemoryContext);
/* TSQL commands must run inside an explicit transaction */
if (!pltsql_disable_batch_auto_commit && pltsql_support_tsql_transactions() &&
if (!pltsql_disable_batch_auto_commit && support_tsql_trans &&
stmt->txn_data == NULL && !IsTransactionBlockActive())
{
MemoryContext oldCxt = CurrentMemoryContext;
Expand All @@ -4825,7 +4827,7 @@ exec_stmt_execsql(PLtsql_execstate *estate,
portal, expr, cmd, paramLI);
else if (stmt->need_to_push_result)
rc = execute_plan_and_push_result(estate, expr, paramLI);
else if (stmt->txn_data != NULL && !pltsql_support_tsql_transactions())
else if (stmt->txn_data != NULL && !support_tsql_trans)
{
elog(DEBUG2, "TSQL TXN Execute transaction command with PG semantics");
rc = execute_txn_command(estate, stmt);
Expand Down Expand Up @@ -5058,7 +5060,7 @@ exec_stmt_execsql(PLtsql_execstate *estate,
*/
/* TODO To let procedure call from PSQL work with old semantics */
if ((!pltsql_disable_batch_auto_commit || (stmt->txn_data != NULL)) &&
pltsql_support_tsql_transactions() &&
support_tsql_trans &&
(enable_txn_in_triggers || estate->trigdata == NULL) &&
!ro_func && !estate->insert_exec)
{
Expand Down
28 changes: 28 additions & 0 deletions test/JDBC/expected/TestBasicInterop.out
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,31 @@ GO
-- psql
DROP PROCEDURE pg_interop_proc_sp_exec
GO

-- tsql
CREATE TABLE babel_5446 (Test INT NULL)
GO
INSERT INTO babel_5446 SELECT ISNUMERIC('test') AS my_isnumeric
GO
~~ROW COUNT: 1~~

SELECT @@trancount
GO
~~START~~
int
0
~~END~~


INSERT INTO babel_5446 SELECT ISNUMERIC('test') AS my_isnumeric
SELECT @@trancount
GO
~~ROW COUNT: 1~~

~~START~~
int
0
~~END~~

DROP TABLE babel_5446
GO
14 changes: 14 additions & 0 deletions test/JDBC/input/interoperability/TestBasicInterop.mix
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,17 @@ GO
-- psql
DROP PROCEDURE pg_interop_proc_sp_exec
GO

-- tsql
CREATE TABLE babel_5446 (Test INT NULL)
GO
INSERT INTO babel_5446 SELECT ISNUMERIC('test') AS my_isnumeric
GO
SELECT @@trancount
GO

INSERT INTO babel_5446 SELECT ISNUMERIC('test') AS my_isnumeric
SELECT @@trancount
GO
DROP TABLE babel_5446
GO
Loading