Skip to content

Commit

Permalink
Fix the bug allowing create trigger on tmp tables (babelfish-for-post…
Browse files Browse the repository at this point in the history
…gresql#2775) (babelfish-for-postgresql#2801)

previously babel allow create trigger on tmp tables
this commit fix the bug, and will print an error when
customer try to create trigger on tmp table


Task: BABEL-2177
Signed-off-by: Zhibai Song <[email protected]>
  • Loading branch information
forestkeeper authored Jul 29, 2024
1 parent 845747c commit 9c8a502
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,29 @@ pltsql_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)

switch (nodeTag(parsetree))
{
case T_CreateFunctionStmt:
{
ListCell *option;
CreateTrigStmt *trigStmt;
Relation rel;
foreach (option, ((CreateFunctionStmt *) parsetree)->options){
DefElem *defel = (DefElem *) lfirst(option);
if (strcmp(defel->defname, "trigStmt") == 0)
{
trigStmt = (CreateTrigStmt *) defel->arg;
rel = table_openrv(trigStmt->relation, ShareRowExclusiveLock);
if (rel->rd_islocaltemp){
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("Cannot create trigger on a temporary object."),
"Cannot create trigger on a temporary object."
));
}
table_close(rel, NoLock);
}
}
}
break;
case T_CreateStmt:
{
CreateStmt *stmt = (CreateStmt *) parsetree;
Expand Down
17 changes: 17 additions & 0 deletions test/JDBC/expected/babel_trigger.out
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,23 @@ babel_trigger_trig2#!#babel_trigger_sch1
~~END~~


CREATE TABLE #babel_2177(id int)
go

-- will fail and print error when trying to create trigger on temp table
CREATE TRIGGER trigger_babel_2177 ON #babel_2177
AFTER INSERT
AS
INSERT into #babel_2177 VALUES (7)
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Cannot create trigger on a temporary object.)~~


drop table #babel_2177;
GO

-- clean up
drop trigger babel_trigger_sch1.babel_trigger_trig1
GO
Expand Down
13 changes: 13 additions & 0 deletions test/JDBC/input/babel_trigger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,19 @@ GO
select name,schema_name(schema_id) from sys.objects where name in ('babel_trigger_trig1','babel_trigger_trig2','babel_trigger_trig3','babel_trigger_trig4') order by name;
GO

CREATE TABLE #babel_2177(id int)
go

-- will fail and print error when trying to create trigger on temp table
CREATE TRIGGER trigger_babel_2177 ON #babel_2177
AFTER INSERT
AS
INSERT into #babel_2177 VALUES (7)
go

drop table #babel_2177;
GO

-- clean up
drop trigger babel_trigger_sch1.babel_trigger_trig1
GO
Expand Down

0 comments on commit 9c8a502

Please sign in to comment.