Skip to content

Commit

Permalink
Revert Instead of Trigger on View (#261)
Browse files Browse the repository at this point in the history
Revert this feature from release due to issues

Task: BABEL-2170

Signed-off-by: Deepakshi Mittal <[email protected]>
  • Loading branch information
deepakshi-mittal authored and priyansx committed Dec 29, 2023
1 parent 0aa268d commit 146b421
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 19 deletions.
5 changes: 1 addition & 4 deletions src/backend/commands/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ 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 @@ -485,7 +484,7 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
RelationGetRelationName(rel)),
errdetail("Triggers on foreign tables cannot have transition tables.")));

if (rel->rd_rel->relkind == RELKIND_VIEW && sql_dialect != SQL_DIALECT_TSQL)
if (rel->rd_rel->relkind == RELKIND_VIEW)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is a view",
Expand Down Expand Up @@ -2509,13 +2508,11 @@ 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
6 changes: 3 additions & 3 deletions src/backend/executor/execMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,23 +1063,23 @@ CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
switch (operation)
{
case CMD_INSERT:
if (!trigDesc || (!trigDesc->trig_insert_instead_row && (sql_dialect == SQL_DIALECT_TSQL && !trigDesc->trig_insert_instead_statement)))
if (!trigDesc || (!trigDesc->trig_insert_instead_row))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cannot insert into view \"%s\"",
RelationGetRelationName(resultRel)),
errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.")));
break;
case CMD_UPDATE:
if (!trigDesc || (!trigDesc->trig_update_instead_row && (sql_dialect == SQL_DIALECT_TSQL && !trigDesc->trig_update_instead_statement)))
if (!trigDesc || (!trigDesc->trig_update_instead_row))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cannot update view \"%s\"",
RelationGetRelationName(resultRel)),
errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.")));
break;
case CMD_DELETE:
if (!trigDesc || (!trigDesc->trig_delete_instead_row && (sql_dialect == SQL_DIALECT_TSQL && !trigDesc->trig_delete_instead_statement)))
if (!trigDesc || (!trigDesc->trig_delete_instead_row))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cannot delete from view \"%s\"",
Expand Down
11 changes: 3 additions & 8 deletions src/backend/rewrite/rewriteHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
#include "utils/lsyscache.h"
#include "utils/rel.h"

bbfViewHasInsteadofTrigger_hook_type bbfViewHasInsteadofTrigger_hook = NULL; /** BBF Hook to check Instead Of trigger on View */

/* We use a list of these to detect recursion in RewriteQuery */
typedef struct rewrite_event
{
Expand Down Expand Up @@ -1481,8 +1479,7 @@ rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
*/
isAutoUpdatableView = false;
if (target_relation->rd_rel->relkind == RELKIND_VIEW &&
(!view_has_instead_trigger(target_relation, CMD_INSERT) &&
!(sql_dialect == SQL_DIALECT_TSQL && bbfViewHasInsteadofTrigger_hook && (bbfViewHasInsteadofTrigger_hook)(target_relation, CMD_INSERT))))
!view_has_instead_trigger(target_relation, CMD_INSERT))
{
List *locks;
bool hasUpdate;
Expand Down Expand Up @@ -3728,8 +3725,7 @@ 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);
/** 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));
Assert(rt_entry->rtekind == RTE_RELATION);

/*
* We can use NoLock here since either the parser or
Expand Down Expand Up @@ -3949,8 +3945,7 @@ RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)
*/
if (!instead &&
rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
(!view_has_instead_trigger(rt_entry_relation, event)
&& !(sql_dialect == SQL_DIALECT_TSQL && bbfViewHasInsteadofTrigger_hook && (bbfViewHasInsteadofTrigger_hook)(rt_entry_relation, event))))
!view_has_instead_trigger(rt_entry_relation, event))
{
/*
* If there were any qualified INSTEAD rules, don't allow the view
Expand Down
1 change: 0 additions & 1 deletion src/include/commands/trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ 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
3 changes: 0 additions & 3 deletions src/include/executor/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
typedef bool (*TriggerRecuresiveCheck_hook_type) (ResultRelInfo *resultRelInfo);
extern PGDLLEXPORT TriggerRecuresiveCheck_hook_type TriggerRecuresiveCheck_hook;

typedef bool (*bbfViewHasInsteadofTrigger_hook_type) (Relation view, CmdType event);
extern PGDLLEXPORT bbfViewHasInsteadofTrigger_hook_type bbfViewHasInsteadofTrigger_hook;

typedef bool (*check_rowcount_hook_type) (int es_processed);
extern PGDLLEXPORT check_rowcount_hook_type check_rowcount_hook;

Expand Down

0 comments on commit 146b421

Please sign in to comment.