Skip to content

Commit

Permalink
Merge branch 'BABEL_3_X_DEV__PG_15_X' into BABEL_4442
Browse files Browse the repository at this point in the history
  • Loading branch information
tanscorpio7 committed Oct 12, 2023
2 parents 2772bf7 + d10cc3e commit 40938ca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/backend/commands/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,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)
if (rel->rd_rel->relkind == RELKIND_VIEW && sql_dialect != SQL_DIALECT_TSQL)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is a view",
Expand Down
7 changes: 4 additions & 3 deletions src/backend/executor/execMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "jit/jit.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/parser.h"
#include "parser/parsetree.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
Expand Down Expand Up @@ -1029,23 +1030,23 @@ CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
switch (operation)
{
case CMD_INSERT:
if (!trigDesc || !trigDesc->trig_insert_instead_row)
if (!trigDesc || (!trigDesc->trig_insert_instead_row && (sql_dialect == SQL_DIALECT_TSQL && !trigDesc->trig_insert_instead_statement)))
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)
if (!trigDesc || (!trigDesc->trig_update_instead_row && (sql_dialect == SQL_DIALECT_TSQL && !trigDesc->trig_update_instead_statement)))
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)
if (!trigDesc || (!trigDesc->trig_delete_instead_row && (sql_dialect == SQL_DIALECT_TSQL && !trigDesc->trig_delete_instead_statement)))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cannot delete from view \"%s\"",
Expand Down
8 changes: 6 additions & 2 deletions src/backend/rewrite/rewriteHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parser.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
Expand All @@ -45,6 +46,7 @@
#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 @@ -1472,7 +1474,8 @@ 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))
(!view_has_instead_trigger(target_relation, CMD_INSERT) &&
!(sql_dialect == SQL_DIALECT_TSQL && bbfViewHasInsteadofTrigger_hook && (bbfViewHasInsteadofTrigger_hook)(target_relation, CMD_INSERT))))
{
List *locks;
bool hasUpdate;
Expand Down Expand Up @@ -3953,7 +3956,8 @@ 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))
(!view_has_instead_trigger(rt_entry_relation, event)
&& !(sql_dialect == SQL_DIALECT_TSQL && bbfViewHasInsteadofTrigger_hook && (bbfViewHasInsteadofTrigger_hook)(rt_entry_relation, event))))
{
/*
* If there were any qualified INSTEAD rules, don't allow the view
Expand Down
14 changes: 12 additions & 2 deletions src/bin/pg_dump/dump_babel_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,24 @@ bbf_selectDumpableObject(DumpableObject *dobj, Archive *fout)
if (fout->dopt->binary_upgrade)
return;

/* Do not dump the definition of default babelfish schemas */
/*
* Do not dump the definition of default babelfish schemas but
* their contained objects will be dumped.
*/
if (strcmp(nsinfo->dobj.name, "master_dbo") == 0 ||
strcmp(nsinfo->dobj.name, "master_guest") == 0 ||
strcmp(nsinfo->dobj.name, "msdb_dbo") == 0 ||
strcmp(nsinfo->dobj.name, "msdb_guest") == 0 ||
strcmp(nsinfo->dobj.name, "tempdb_dbo") == 0 ||
strcmp(nsinfo->dobj.name, "tempdb_guest") == 0)
nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;

/*
* Do not dump any components of the schemas which get created as
* part of CREATE EXTENSION babelfish... command.
*/
if (strcmp(nsinfo->dobj.name, "babelfishpg_telemetry") == 0)
nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
}
break;
case DO_EXTENSION:
Expand All @@ -415,7 +425,7 @@ bbf_selectDumpableObject(DumpableObject *dobj, Archive *fout)
return;

if (strncmp(extinfo->dobj.name, "babelfishpg", 11) == 0)
extinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;
extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE;
}
break;
case DO_FUNC:
Expand Down
3 changes: 3 additions & 0 deletions src/include/executor/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook;
typedef bool (*TriggerRecuresiveCheck_hook_type) (ResultRelInfo *resultRelInfo);
extern PGDLLIMPORT TriggerRecuresiveCheck_hook_type TriggerRecuresiveCheck_hook;

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

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

Expand Down

0 comments on commit 40938ca

Please sign in to comment.