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

create proc/func/trigger, create/drop database, transaction statements are not being tracked by the extensions which utilize processutility hook #1424

Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
70304e4
Update major-version-upgrade.yml
kritika-0601 Mar 31, 2023
130c99b
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into BABEL_3_X_DEV
kritika-0601 Mar 31, 2023
1c4572c
Transaction statements not visible in pg_stat_statements view
Mar 31, 2023
d99f4b2
retrigger tests
Mar 31, 2023
3c1b8bd
Transaction statements not visible in pg_stat_statements view
Apr 1, 2023
c94fc41
Update major-version-upgrade.yml
kritika-0601 Apr 1, 2023
3ae0464
Transaction statements not visible in pg_stat_statements view
Apr 1, 2023
f4cf79d
retrigger tests
Mar 31, 2023
1ed996d
Transaction statements not visible in pg_stat_statements view
Apr 1, 2023
628a124
retrigger tests
Mar 31, 2023
37603a6
retrigger tests
Mar 31, 2023
f2ca9e2
Create proc/func/database etc not visible in pg_stat_statements view
Apr 13, 2023
1cf1b44
Merge branch 'BABEL_3_X_DEV' into pg_stat_statements-hook
kritika-0601 Apr 13, 2023
839ce19
retrigger tests
Apr 13, 2023
3938a87
retrigger tests
Apr 14, 2023
588b202
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 Apr 14, 2023
4ab495f
retrigger tests
Apr 14, 2023
c22c461
retrigger tests
Apr 14, 2023
0eab309
Create proc/func/database etc not visible in pg_stat_statements view
Apr 14, 2023
224fb0e
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 Apr 17, 2023
51cc85c
retrigger tests
Apr 17, 2023
202fafe
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 Apr 18, 2023
9335b0a
retrigger tests
Apr 18, 2023
7bef777
retrigger tests
Apr 18, 2023
14cf51b
Create proc/func/database etc not visible in pg_stat_statements view
Apr 19, 2023
cc75b18
Create proc/func/database etc not visible in pg_stat_statements view
Apr 19, 2023
216088f
Create proc/func/database etc not visible in pg_stat_statements view
Apr 19, 2023
5466644
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 Apr 19, 2023
a55541d
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 Apr 20, 2023
bfa6979
Create proc/func/database etc not visible in pg_stat_statements view
Apr 20, 2023
c809b72
Create proc/func/database etc not visible in pg_stat_statements view
Apr 20, 2023
5951d53
Create proc/func/database etc not visible in pg_stat_statements view
Apr 20, 2023
9bec2e2
retrigger tests
Apr 27, 2023
84bf471
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 Apr 27, 2023
aed53a9
Create proc/func/database etc not visible in pg_stat_statements view
May 2, 2023
0cda0b2
Merge branch 'BABEL_3_X_DEV' into pg_stat_statements-hook
kritika-0601 May 2, 2023
c0018a3
retrigger tests
May 2, 2023
ed253a0
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 May 2, 2023
9b4ddfe
retrigger tests
May 2, 2023
54eacc4
Create proc/func/database etc not visible in pg_stat_statements view
May 2, 2023
dc43623
Create proc/func/database etc not visible in pg_stat_statements view
May 2, 2023
342c57e
Create proc/func/database etc not visible in pg_stat_statements view
May 2, 2023
d53c591
retrigger tests
May 2, 2023
bd9853b
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 May 2, 2023
86bb18f
Create proc/func/database etc not visible in pg_stat_statements view
May 2, 2023
f932087
Merge remote-tracking branch 'upstream/BABEL_3_X_DEV' into pg_stat_st…
May 2, 2023
16aee8b
Create proc/func/database etc not visible in pg_stat_statements view
May 2, 2023
a8762c6
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV' into pg_stat_st…
kritika-0601 May 2, 2023
a8419eb
Create proc/func/database etc not visible in pg_stat_statements view
May 3, 2023
48e8d0e
Create proc/func/database etc not visible in pg_stat_statements view
May 3, 2023
58f2149
Create proc/func/database etc not visible in pg_stat_statements view
May 3, 2023
e106245
retrigger tests
May 4, 2023
e6b5eb0
Create proc/func/database etc not visible in pg_stat_statements view
May 8, 2023
b6d00df
Merge branch 'BABEL_3_X_DEV' into pg_stat_statements-hook
kritika-0601 May 16, 2023
eb288a9
retrigger tests
May 16, 2023
b519ad7
Create proc/func/database etc not visible in pg_stat_statements view
May 29, 2023
a9b5fac
Fixed create proc/func/database drop database etc which were not trac…
Jun 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ static void insert_pltsql_function_defaults(HeapTuple func_tuple, List *defaults
static int print_pltsql_function_arguments(StringInfo buf, HeapTuple proctup, bool print_table_args, bool print_defaults);
static void pltsql_GetNewObjectId(VariableCache variableCache);
static void pltsql_validate_var_datatype_scale(const TypeName *typeName, Type typ);
static bool pltsql_bbfCustomProcessUtility(ParseState *pstate,
PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params, QueryCompletion *qc);

/*****************************************
* Executor Hooks
Expand Down Expand Up @@ -198,6 +203,7 @@ static validate_var_datatype_scale_hook_type prev_validate_var_datatype_scale_ho
static modify_RangeTblFunction_tupdesc_hook_type prev_modify_RangeTblFunction_tupdesc_hook = NULL;
static fill_missing_values_in_copyfrom_hook_type prev_fill_missing_values_in_copyfrom_hook = NULL;
static check_rowcount_hook_type prev_check_rowcount_hook = NULL;
static bbfCustomProcessUtility_hook_type prev_bbfCustomProcessUtility_hook = NULL;
static sortby_nulls_hook_type prev_sortby_nulls_hook = NULL;
static table_variable_satisfies_visibility_hook_type prev_table_variable_satisfies_visibility = NULL;
static table_variable_satisfies_update_hook_type prev_table_variable_satisfies_update = NULL;
Expand Down Expand Up @@ -325,6 +331,9 @@ InstallExtendedHooks(void)
prev_check_rowcount_hook = check_rowcount_hook;
check_rowcount_hook = bbf_check_rowcount_hook;

prev_bbfCustomProcessUtility_hook = bbfCustomProcessUtility_hook;
bbfCustomProcessUtility_hook = pltsql_bbfCustomProcessUtility;

prev_sortby_nulls_hook = sortby_nulls_hook;
sortby_nulls_hook = sort_nulls_first;

Expand Down Expand Up @@ -394,6 +403,7 @@ UninstallExtendedHooks(void)
modify_RangeTblFunction_tupdesc_hook = prev_modify_RangeTblFunction_tupdesc_hook;
fill_missing_values_in_copyfrom_hook = prev_fill_missing_values_in_copyfrom_hook;
check_rowcount_hook = prev_check_rowcount_hook;
bbfCustomProcessUtility_hook = prev_bbfCustomProcessUtility_hook;
sortby_nulls_hook = prev_sortby_nulls_hook;
table_variable_satisfies_visibility_hook = prev_table_variable_satisfies_visibility;
table_variable_satisfies_update_hook = prev_table_variable_satisfies_update;
Expand All @@ -406,6 +416,54 @@ UninstallExtendedHooks(void)
/*****************************************
* Hook Functions
*****************************************/
static bool
pltsql_bbfCustomProcessUtility(ParseState *pstate, PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context,
ParamListInfo params, QueryCompletion *qc)
{
Node *parsetree = pstmt->utilityStmt;

switch (nodeTag(parsetree))
{
case T_CreateFunctionStmt:
{
return pltsql_createFunction(pstate, pstmt, queryString, context, params);
break;
}
case T_CreatedbStmt:
{
if (sql_dialect == SQL_DIALECT_TSQL)
{
create_bbf_db(pstate, (CreatedbStmt *) parsetree);
return true;
}
break;
}
case T_DropdbStmt:
{
if (sql_dialect == SQL_DIALECT_TSQL)
{
DropdbStmt *stmt = (DropdbStmt *) parsetree;
drop_bbf_db(stmt->dbname, stmt->missing_ok, false);
return true;
}
break;
}
case T_TransactionStmt:
{
if (NestedTranCount > 0 || (sql_dialect == SQL_DIALECT_TSQL && !IsTransactionBlockActive()))
{
PLTsqlProcessTransaction(parsetree, params, qc);
return true;
}
break;
}
default:
return false;
break;
}
return false;
}


static void
pltsql_GetNewObjectId(VariableCache variableCache)
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ extern Oid get_tsql_trigger_oid(List *object,

extern char *update_delete_target_alias;
extern bool sp_describe_first_result_set_inprogress;

kritika-0601 marked this conversation as resolved.
Show resolved Hide resolved
#endif
279 changes: 0 additions & 279 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1882,95 +1882,6 @@ bbf_table_var_lookup(const char *relname, Oid relnamespace)
return relid;
}

/*
* Transaction processing using tsql semantics
*/
static void
PLTsqlProcessTransaction(Node *parsetree,
ParamListInfo params,
QueryCompletion *qc)
{
char *txnName = NULL;
TransactionStmt *stmt = (TransactionStmt *) parsetree;

if (params != NULL && params->numParams > 0 && !params->params[0].isnull)
{
Oid typOutput;
bool typIsVarlena;
FmgrInfo finfo;

Assert(params->numParams == 1);
getTypeOutputInfo(params->params[0].ptype, &typOutput, &typIsVarlena);
fmgr_info(typOutput, &finfo);
txnName = OutputFunctionCall(&finfo, params->params[0].value);
}
else
txnName = stmt->savepoint_name;

if (txnName != NULL && strlen(txnName) > TSQL_TXN_NAME_LIMIT / 2)
ereport(ERROR,
(errcode(ERRCODE_NAME_TOO_LONG),
errmsg("Transaction name length %zu above limit %u",
strlen(txnName), TSQL_TXN_NAME_LIMIT / 2)));

if (AbortCurTransaction)
{
if (stmt->kind == TRANS_STMT_BEGIN ||
stmt->kind == TRANS_STMT_COMMIT ||
stmt->kind == TRANS_STMT_SAVEPOINT)
ereport(ERROR,
(errcode(ERRCODE_TRANSACTION_ROLLBACK),
errmsg("The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.")));
}

switch (stmt->kind)
{
case TRANS_STMT_BEGIN:
{
PLTsqlStartTransaction(txnName);
}
break;

case TRANS_STMT_COMMIT:
{
if (exec_state_call_stack &&
exec_state_call_stack->estate &&
exec_state_call_stack->estate->insert_exec &&
NestedTranCount <= 1)
ereport(ERROR,
(errcode(ERRCODE_TRANSACTION_ROLLBACK),
errmsg("Cannot use the COMMIT statement within an INSERT-EXEC statement unless BEGIN TRANSACTION is used first.")));

PLTsqlCommitTransaction(qc, stmt->chain);
}
break;

case TRANS_STMT_ROLLBACK:
{
if (exec_state_call_stack &&
exec_state_call_stack->estate &&
exec_state_call_stack->estate->insert_exec)
ereport(ERROR,
(errcode(ERRCODE_TRANSACTION_ROLLBACK),
errmsg("Cannot use the ROLLBACK statement within an INSERT-EXEC statement.")));

PLTsqlRollbackTransaction(txnName, qc, stmt->chain);
}
break;

case TRANS_STMT_SAVEPOINT:
RequireTransactionBlock(true, "SAVEPOINT");
DefineSavepoint(txnName);
break;

default:
ereport(ERROR,
(errcode(ERRCODE_INVALID_TRANSACTION_INITIATION),
errmsg("Unsupported transaction command : %d", stmt->kind)));
break;
}
}

/*
* It returns TRUE when we should not execute the utility statement,
* e.g., CREATE FUNCTION, in EXPLAIN ONLY MODE.
Expand Down Expand Up @@ -2141,180 +2052,6 @@ bbf_ProcessUtility(PlannedStmt *pstmt,

switch (nodeTag(parsetree))
{
case T_CreateFunctionStmt:
{
CreateFunctionStmt *stmt = (CreateFunctionStmt *) parsetree;
bool isCompleteQuery = (context != PROCESS_UTILITY_SUBCOMMAND);
bool needCleanup;
ListCell *option,
*location_cell = NULL;
Node *tbltypStmt = NULL;
Node *trigStmt = NULL;
ObjectAddress tbltyp;
ObjectAddress address;
int origname_location = -1;

/*
* All event trigger calls are done only when isCompleteQuery
* is true
*/
needCleanup = isCompleteQuery && EventTriggerBeginCompleteQuery();

/*
* PG_TRY block is to ensure we call
* EventTriggerEndCompleteQuery
*/
PG_TRY();
{
if (isCompleteQuery)
EventTriggerDDLCommandStart(parsetree);

foreach(option, stmt->options)
{
DefElem *defel = (DefElem *) lfirst(option);

if (strcmp(defel->defname, "tbltypStmt") == 0)
{
/*
* tbltypStmt is an implicit option in tsql
* dialect, we use this mechanism to create tsql
* style multi-statement table-valued function and
* its return (table) type in one statement.
*/
tbltypStmt = defel->arg;
}
else if (strcmp(defel->defname, "trigStmt") == 0)
{
/*
* trigStmt is an implicit option in tsql dialect,
* we use this mechanism to create tsql style
* function and trigger in one statement.
*/
trigStmt = defel->arg;
}
else if (strcmp(defel->defname, "location") == 0)
{
/*
* location is an implicit option in tsql dialect,
* we use this mechanism to store location of
* function name so that we can extract original
* input function name from queryString.
*/
origname_location = intVal((Node *) defel->arg);
location_cell = option;
pfree(defel);
}
}

/*
* delete location cell if it exists as it is for internal
* use only
*/
if (location_cell)
stmt->options = list_delete_cell(stmt->options, location_cell);

/*
* For tbltypStmt, we need to first process the CreateStmt
* to create the type that will be used as the function's
* return type. Then, after the function is created, add a
* dependency between the type and the function.
*/
if (tbltypStmt)
{
/* Handle tbltypStmt, which is a CreateStmt */
PlannedStmt *wrapper;

wrapper = makeNode(PlannedStmt);
wrapper->commandType = CMD_UTILITY;
wrapper->canSetTag = false;
wrapper->utilityStmt = tbltypStmt;
wrapper->stmt_location = pstmt->stmt_location;
wrapper->stmt_len = pstmt->stmt_len;

ProcessUtility(wrapper,
queryString,
readOnlyTree,
PROCESS_UTILITY_SUBCOMMAND,
params,
NULL,
None_Receiver,
NULL);

/* Need CCI between commands */
CommandCounterIncrement();
}

address = CreateFunction(pstate, stmt);

/*
* Store function/procedure related metadata in babelfish
* catalog
*/
pltsql_store_func_default_positions(address, stmt->parameters, queryString, origname_location);

if (tbltypStmt || restore_tsql_tabletype)
{
/*
* Add internal dependency between the table type and
* the function.
*/
tbltyp.classId = TypeRelationId;
tbltyp.objectId = typenameTypeId(pstate,
stmt->returnType);
tbltyp.objectSubId = 0;
recordDependencyOn(&tbltyp, &address, DEPENDENCY_INTERNAL);
}

/*
* For trigStmt, we need to process the CreateTrigStmt
* after the function is created, and record bidirectional
* dependency so that Drop Trigger CASCADE will drop the
* implicit trigger function. Create trigger takes care of
* dependency addition.
*/
if (trigStmt)
{
(void) CreateTrigger((CreateTrigStmt *) trigStmt,
pstate->p_sourcetext, InvalidOid, InvalidOid,
InvalidOid, InvalidOid, address.objectId,
InvalidOid, NULL, false, false);
}

/*
* Remember the object so that ddl_command_end event
* triggers have access to it.
*/
EventTriggerCollectSimpleCommand(address, InvalidObjectAddress,
parsetree);

if (isCompleteQuery)
{
EventTriggerSQLDrop(parsetree);
EventTriggerDDLCommandEnd(parsetree);
}

}
PG_CATCH();
{
if (needCleanup)
EventTriggerEndCompleteQuery();
PG_RE_THROW();
}
PG_END_TRY();

if (needCleanup)
EventTriggerEndCompleteQuery();
return;
}
case T_TransactionStmt:
{
if (NestedTranCount > 0 || (sql_dialect == SQL_DIALECT_TSQL && !IsTransactionBlockActive()))
{
PLTsqlProcessTransaction(parsetree, params, qc);
return;
}
break;
}
case T_TruncateStmt:
{
if (sql_dialect == SQL_DIALECT_TSQL)
Expand Down Expand Up @@ -3298,22 +3035,6 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
return;
}
}
case T_CreatedbStmt:
if (sql_dialect == SQL_DIALECT_TSQL)
{
create_bbf_db(pstate, (CreatedbStmt *) parsetree);
return;
}
break;
case T_DropdbStmt:
if (sql_dialect == SQL_DIALECT_TSQL)
{
DropdbStmt *stmt = (DropdbStmt *) parsetree;

drop_bbf_db(stmt->dbname, stmt->missing_ok, false);
return;
}
break;
case T_GrantRoleStmt:
if (sql_dialect == SQL_DIALECT_TSQL)
{
Expand Down
Loading