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 Crash when Instead of View Trigger calls itself #1970

22 changes: 22 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,28 @@ static bool
pltsql_bbfViewHasInsteadofTrigger(Relation view, CmdType event)
{
TriggerDesc *trigDesc = view->trigdesc;
if (triggerOids)
{
int i;
for (i = 0; i < trigDesc->numtriggers; i++)
{
Trigger *trigger = &trigDesc->triggers[i];
Oid current_tgoid = trigger->tgoid;
Oid prev_tgoid = InvalidOid;
prev_tgoid = lfirst_oid(list_tail(triggerOids));
deepakshi-mittal marked this conversation as resolved.
Show resolved Hide resolved
if (prev_tgoid == current_tgoid)
{
return false; /** Direct recursive trigger case*/
}
else if (list_member_oid(triggerOids, current_tgoid))
{
/** Indirect recursive trigger case*/
ereport(ERROR,
lejaokri marked this conversation as resolved.
Show resolved Hide resolved
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)")));
}
}
}

switch (event)
{
Expand Down
60 changes: 59 additions & 1 deletion test/JDBC/expected/BABEL-2170-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Trigger db1_BABEL2170.dbo.babel_2170_vu_employees_view_iot_update Invoked
~~ROW COUNT: 1~~


SELECT EmployeeID,EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID = 2 ORDER BY EmployeeID;
SELECT EmployeeID,EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID = 2;
GO
~~START~~
int#!#varchar#!#varchar#!#numeric
Expand Down Expand Up @@ -483,6 +483,64 @@ int#!#varchar#!#varchar#!#numeric
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_txn_delete;
GO

DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_update;
GO

-- Transition table join test
CREATE TRIGGER babel_2170_vu_employees_view_transition ON babel_2170_vu_employees_view
INSTEAD OF UPDATE
AS
BEGIN
UPDATE babel_2170_vu_employees_view set MonthSalary = i.MonthSalary + 15 FROM inserted as i
JOIN babel_2170_vu_employees_view AS v ON v.EmployeeID = i.EmployeeID
END
GO

UPDATE babel_2170_vu_employees_view SET MonthSalary = 5 WHERE EmployeeID = 2;
GO
~~ROW COUNT: 2~~

~~ROW COUNT: 1~~


SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees_view WHERE EmployeeID = 2;
GO
~~START~~
int#!#varchar#!#varchar#!#numeric
2#!#angel#!#1st Street#!#2015.00
~~END~~


DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_transition;
GO

-- Recursive Trigger test Direct Recursion Trigger calling itself trigger 1 -> trigger 1
CREATE TRIGGER babel_2170_vu_employees_view_iot_update ON babel_2170_vu_employees_view
INSTEAD OF UPDATE
AS
BEGIN
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +100 WHERE EmployeeID = 2;
END
GO

UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID = 2;
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees_view WHERE EmployeeID = 2;
GO
~~START~~
int#!#varchar#!#varchar#!#numeric
2#!#angel#!#1st Street#!#2115.00
~~END~~


DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_update;
GO

-- test multi-db mode
SELECT set_config('role', 'jdbc_user', false);
GO
Expand Down
42 changes: 41 additions & 1 deletion test/JDBC/input/triggers/BABEL-2170-vu-verify.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ GO
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID = 2;
GO

SELECT EmployeeID,EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID = 2 ORDER BY EmployeeID;
SELECT EmployeeID,EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID = 2;
GO

BEGIN TRANSACTION
Expand Down Expand Up @@ -253,6 +253,46 @@ GO
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_txn_delete;
GO

DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_update;
GO

-- Transition table join test
CREATE TRIGGER babel_2170_vu_employees_view_transition ON babel_2170_vu_employees_view
INSTEAD OF UPDATE
AS
BEGIN
UPDATE babel_2170_vu_employees_view set MonthSalary = i.MonthSalary + 15 FROM inserted as i
JOIN babel_2170_vu_employees_view AS v ON v.EmployeeID = i.EmployeeID
END
GO

UPDATE babel_2170_vu_employees_view SET MonthSalary = 5 WHERE EmployeeID = 2;
GO

SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees_view WHERE EmployeeID = 2;
GO

DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_transition;
GO

-- Recursive Trigger test Direct Recursion Trigger calling itself trigger 1 -> trigger 1
CREATE TRIGGER babel_2170_vu_employees_view_iot_update ON babel_2170_vu_employees_view
INSTEAD OF UPDATE
AS
BEGIN
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +100 WHERE EmployeeID = 2;
END
GO

UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID = 2;
GO

SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees_view WHERE EmployeeID = 2;
GO

DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_update;
GO

-- test multi-db mode
SELECT set_config('role', 'jdbc_user', false);
GO
Expand Down
Loading