Skip to content

Commit

Permalink
Merge branch 'BABEL_4_X_DEV' into BABEL-5119
Browse files Browse the repository at this point in the history
  • Loading branch information
P Aswini Kumar committed Sep 11, 2024
2 parents 8e2c0be + 2160271 commit e9e2e29
Show file tree
Hide file tree
Showing 58 changed files with 4,125 additions and 113 deletions.
21 changes: 19 additions & 2 deletions contrib/babelfishpg_tds/src/backend/tds/tds.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,21 @@ tdsstat_fetch_stat_local_tdsentry(int beid)
return localentry;
}

/* ----------
* tdsstat_fetch_stat_numbackends() -
*
* Support function for the SQL-callable pgstat* functions. Returns
* the number of sessions known in the localTdsStatusTable, i.e.
* the maximum 1-based index to pass to tdsstat_fetch_stat_local_tdsentry().
* ----------
*/
int
tdsstat_fetch_stat_numbackends(void)
{
tdsstat_read_current_status();
return localNumBackends;
}

/* ----------
* tdsstat_read_current_status() -
*
Expand Down Expand Up @@ -735,14 +750,16 @@ tdsstat_read_current_status(void)

/* Only valid entries get included into the local array */
if (localentry->tdsStatus.st_procpid > 0)
{
BackendIdGetTransactionIds(i,
&localentry->backend_xid,
&localentry->backend_xmin,
&localentry->backend_subxact_count,
&localentry->backend_subxact_overflowed);

localentry++;
localNumBackends++;
localentry++;
localNumBackends++;
}
}

localTdsStatusTable = localtable;
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tds/src/backend/tds/tds_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pe_tds_init(void)
pltsql_plugin_handler_ptr->get_tds_database_backend_count = &get_tds_database_backend_count;
pltsql_plugin_handler_ptr->get_stat_values = &tds_stat_get_activity;
pltsql_plugin_handler_ptr->invalidate_stat_view = &invalidate_stat_table;
pltsql_plugin_handler_ptr->get_tds_numbackends = &tdsstat_fetch_stat_numbackends;
pltsql_plugin_handler_ptr->get_host_name = &get_tds_host_name;
pltsql_plugin_handler_ptr->get_client_pid = &get_tds_client_pid;
pltsql_plugin_handler_ptr->get_context_info = &get_tds_context_info;
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tds/src/include/tds_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ extern void TdsSetAtAtStatVariable(TdsAtAtVarType at_at_var, int intVal, uint64
extern void TdsSetDatabaseStatVariable(int16 db_id);
extern bool get_tds_database_backend_count(int16 db_id, bool ignore_current_connection);
extern bool tds_stat_get_activity(Datum *values, bool *nulls, int len, int pid, int curr_backend);
extern int tdsstat_fetch_stat_numbackends(void);
extern void invalidate_stat_table(void);
extern char *get_tds_host_name(void);
extern uint32_t get_tds_client_pid(void);
Expand Down
5 changes: 4 additions & 1 deletion contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ Datum
tsql_stat_get_activity(PG_FUNCTION_ARGS)
{
Oid sysadmin_oid = get_role_oid("sysadmin", false);
int num_backends = pgstat_fetch_stat_numbackends();
int num_backends = 0;
int curr_backend;
char *view_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
int pid = -1;
Expand Down Expand Up @@ -1834,6 +1834,9 @@ tsql_stat_get_activity(PG_FUNCTION_ARGS)

MemoryContextSwitchTo(oldcontext);

if (*pltsql_protocol_plugin_ptr && (*pltsql_protocol_plugin_ptr)->get_tds_numbackends)
num_backends = (*pltsql_protocol_plugin_ptr)->get_tds_numbackends();

/* 1-based index */
for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
{
Expand Down
64 changes: 64 additions & 0 deletions contrib/babelfishpg_tsql/src/backend_parser/gram-tsql-rule.y
Original file line number Diff line number Diff line change
Expand Up @@ -4092,6 +4092,70 @@ tsql_AlterFunctionStmt:
n->actions = list_concat(list_make3(lang, body, location), $5); // piggy-back on actions to just put the new proc body instead
$$ = (Node *) n;
}
| TSQL_ALTER FUNCTION func_name tsql_createfunc_args
RETURNS func_return tsql_createfunc_options opt_as tokens_remaining
{
ObjectWithArgs *owa = makeNode(ObjectWithArgs);
AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
DefElem *lang = makeDefElem("language", (Node *) makeString("pltsql"), @1);
DefElem *body = makeDefElem("as", (Node *) list_make1(makeString($9)), @9);
DefElem *location = makeDefElem("location", (Node *) makeInteger(@3), @3);
/*
* Adding a option for volatility with value STABLE.
* Function created from tsql dialect will be created as STABLE
* by default
*/
DefElem *vol = makeDefElem("volatility", (Node *) makeString("stable"), @1);

/* Remove return defelem from list after extracting in pl_handler*/
DefElem *ret = makeDefElem("return", (Node *) $6, @6);

/* Fill in the ObjectWithArgs node */
owa->objname = $3;
owa->objargs = extractArgTypes($4);
owa->objfuncargs = $4;

n->objtype = OBJECT_PROCEDURE; /* Set as proc to avoid psql alter func impl */
n->func = owa;
n->actions = list_concat(list_make5(lang, body, location, vol, ret), $7); // piggy-back on actions to just put the new proc body instead
$$ = (Node *) n;
}
| TSQL_ALTER FUNCTION func_name tsql_createfunc_args
RETURNS TABLE opt_as tokens_remaining
{
ObjectWithArgs *owa = makeNode(ObjectWithArgs);
AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
DefElem *lang = makeDefElem("language", (Node *) makeString("pltsql"), @1);
DefElem *body = makeDefElem("as", (Node *) list_make1(makeString($8)), @8);
DefElem *location = makeDefElem("location", (Node *) makeInteger(@3), @3);
TypeName *returnType = SystemTypeName("record");
DefElem *ret;

/*
* Do not include table parameters here, will be added in
* pltsql_validator()
*/

owa->objname = $3;
owa->objargs = extractArgTypes($4);
owa->objfuncargs = $4;

/*
* Use RECORD type here. In case of single result column,
* will be changed to that column's type in
* pltsql_validator()
*/

returnType = SystemTypeName("record");
returnType->setof = true;
returnType->location = @6;
ret = makeDefElem("return", (Node *) returnType, @6);

n->objtype = OBJECT_PROCEDURE; /* Set as proc to avoid psql alter func impl */
n->func = owa;
n->actions = list_make4(lang, body, location, ret); // piggy-back on actions to just put the new proc body instead
$$ = (Node *)n;
}
;

/*
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/backend_parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pgtsql_base_yylex(YYSTYPE *lvalp, YYLTYPE * llocp, core_yyscan_t yyscanner)
{
case PROCEDURE:
case TSQL_PROC:
case FUNCTION:
cur_token = TSQL_ALTER;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ define_custom_variables(void)
gettext_noop("Temp oid buffer size"),
NULL,
&temp_oid_buffer_size,
0, 0, 131072,
65536, 0, 131072,
PGC_SUSET,
GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_DISALLOW_IN_AUTO_FILE,
NULL, NULL, NULL);
Expand Down
24 changes: 24 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
#include "parser/scansup.h"
#include "replication/logical.h"
#include "rewrite/rewriteHandler.h"
#include "storage/lock.h"
#include "storage/sinvaladt.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
Expand Down Expand Up @@ -163,6 +165,8 @@ static void pltsql_GetNewObjectId(VariableCache variableCache);
static Oid pltsql_GetNewTempObjectId(void);
static Oid pltsql_GetNewTempOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn);
static bool set_and_persist_temp_oid_buffer_start(Oid new_oid);
static bool pltsql_is_local_only_inval_msg(const SharedInvalidationMessage *msg);
static EphemeralNamedRelation pltsql_get_tsql_enr_from_oid(Oid oid);
static void pltsql_validate_var_datatype_scale(const TypeName *typeName, Type typ);
static bool pltsql_bbfCustomProcessUtility(ParseState *pstate,
PlannedStmt *pstmt,
Expand Down Expand Up @@ -240,6 +244,8 @@ static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
static GetNewObjectId_hook_type prev_GetNewObjectId_hook = NULL;
static GetNewTempObjectId_hook_type prev_GetNewTempObjectId_hook = NULL;
static GetNewTempOidWithIndex_hook_type prev_GetNewTempOidWithIndex_hook = NULL;
static pltsql_is_local_only_inval_msg_hook_type prev_pltsql_is_local_only_inval_msg_hook = NULL;
static pltsql_get_tsql_enr_from_oid_hook_type prev_pltsql_get_tsql_enr_from_oid_hook = NULL;
static inherit_view_constraints_from_table_hook_type prev_inherit_view_constraints_from_table = NULL;
static bbfViewHasInsteadofTrigger_hook_type prev_bbfViewHasInsteadofTrigger_hook = NULL;
static detect_numeric_overflow_hook_type prev_detect_numeric_overflow_hook = NULL;
Expand Down Expand Up @@ -367,6 +373,12 @@ InstallExtendedHooks(void)
prev_GetNewTempOidWithIndex_hook = GetNewTempOidWithIndex_hook;
GetNewTempOidWithIndex_hook = pltsql_GetNewTempOidWithIndex;

prev_pltsql_is_local_only_inval_msg_hook = pltsql_is_local_only_inval_msg_hook;
pltsql_is_local_only_inval_msg_hook = pltsql_is_local_only_inval_msg;

prev_pltsql_get_tsql_enr_from_oid_hook = pltsql_get_tsql_enr_from_oid_hook;
pltsql_get_tsql_enr_from_oid_hook = pltsql_get_tsql_enr_from_oid;

prev_inherit_view_constraints_from_table = inherit_view_constraints_from_table_hook;
inherit_view_constraints_from_table_hook = preserve_view_constraints_from_base_table;
TriggerRecuresiveCheck_hook = plsql_TriggerRecursiveCheck;
Expand Down Expand Up @@ -4685,6 +4697,18 @@ static bool set_and_persist_temp_oid_buffer_start(Oid new_oid)
return true;
}

static bool
pltsql_is_local_only_inval_msg(const SharedInvalidationMessage *msg)
{
return temp_oid_buffer_size > 0 && (msg->id == SHAREDINVALRELCACHE_ID && msg->rc.local_only);
}

static EphemeralNamedRelation
pltsql_get_tsql_enr_from_oid(const Oid oid)
{
return temp_oid_buffer_size > 0 ? get_ENR_withoid(currentQueryEnv, oid, ENR_TSQL_TEMP) : NULL;
}

/*
* Modify the Tuple Descriptor to match the expected
* result set. Currently used only for T-SQL OPENQUERY.
Expand Down
Loading

0 comments on commit e9e2e29

Please sign in to comment.