Skip to content

Commit

Permalink
BABEL: Fix Crash when Instead of View Trigger calls itself (babelfish…
Browse files Browse the repository at this point in the history
…-for-postgresql#245)


This change will fix crashes when an
Instead of Trigger on View calls itself directly and indirectly

Task: BABEL-2170
  • Loading branch information
deepakshi-mittal authored and roshan0708 committed Oct 18, 2024
1 parent bb017ee commit 8a3e0f5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/backend/commands/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN;

/* How many levels deep into trigger execution are we? */
static int MyTriggerDepth = 0;
List *triggerOids = NIL; /* To store all trigger calls information */

/* Local function prototypes */
static void renametrig_internal(Relation tgrel, Relation targetrel,
Expand Down Expand Up @@ -2508,11 +2509,13 @@ ExecCallTriggerFunc(TriggerData *trigdata,
MyTriggerDepth++;
PG_TRY();
{
triggerOids = lappend_oid(triggerOids, trigdata->tg_trigger->tgoid);
result = FunctionCallInvoke(fcinfo);
}
PG_FINALLY();
{
MyTriggerDepth--;
triggerOids = list_delete_oid(triggerOids, trigdata->tg_trigger->tgoid);
}
PG_END_TRY();

Expand Down
3 changes: 2 additions & 1 deletion src/backend/rewrite/rewriteHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -3728,7 +3728,8 @@ RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)
result_relation = parsetree->resultRelation;
Assert(result_relation != 0);
rt_entry = rt_fetch(result_relation, parsetree->rtable);
Assert(rt_entry->rtekind == RTE_RELATION);
/** allow transition table in TSQL inside trigger body, rtekind can be RTE_NAMEDTUPLESTORE, eg inserted and deleted*/
Assert(rt_entry->rtekind == RTE_RELATION || (sql_dialect == SQL_DIALECT_TSQL && rt_entry->rtekind == RTE_NAMEDTUPLESTORE));

/*
* We can use NoLock here since either the parser or
Expand Down
1 change: 1 addition & 0 deletions src/include/commands/trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ extern void AfterTriggerEndSubXact(bool isCommit);
extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
extern bool AfterTriggerPendingOnRel(Oid relid);

extern List *triggerOids; /* To store all trigger calls information */

/*
* in utils/adt/ri_triggers.c
Expand Down

0 comments on commit 8a3e0f5

Please sign in to comment.