add interface

Offering: openGaussDev

More detail: add interface of get_client_info

Match-id-d860436ca1408ee2625b8a680a865a2f8f2784e0
This commit is contained in:
openGaussDev 2022-02-26 16:23:26 +08:00 committed by yanghao
parent 651da5cbe4
commit d17420c4f6
15 changed files with 160 additions and 5 deletions

View File

@ -3031,6 +3031,10 @@
"get_byte", 1,
AddBuiltinFunc(_0(721), _1("get_byte"), _2(2), _3(true), _4(false), _5(byteaGetByte), _6(23), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('i'), _19(0), _20(2, 17, 23), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("byteaGetByte"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33("get byte"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))
),
AddFuncGroup(
"get_client_info", 1,
AddBuiltinFunc(_0(7732), _1("get_client_info"), _2(0), _3(false), _4(true), _5(get_client_info), _6(2249), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(100), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(0), _21(2, 20, 25), _22(2, 'o', 'o'), _23(2, "sid", "client_info"), _24(NULL), _25("get_client_info"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(true), _32(false), _33("read current client"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))
),
AddFuncGroup(
"get_current_ts_config", 1,
AddBuiltinFunc(_0(3759), _1("get_current_ts_config"), _2(0), _3(true), _4(false), _5(get_current_ts_config), _6(3734), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(0), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("get_current_ts_config"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33("get current tsearch configuration"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))
@ -8173,14 +8177,14 @@
"pg_rotate_logfile", 1,
AddBuiltinFunc(_0(2622), _1("pg_rotate_logfile"), _2(0), _3(true), _4(false), _5(pg_rotate_logfile), _6(16), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('v'), _19(0), _20(0), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("pg_rotate_logfile"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33("rotate log file - old version for adminpack 1.0"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))
),
AddFuncGroup(
"pg_sequence_parameters", 1,
AddBuiltinFunc(_0(3078), _1("pg_sequence_parameters"), _2(1), _3(true), _4(false), _5(pg_sequence_parameters), _6(2249), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(1, 26), _21(6, 26, 34, 34, 34, 34, 16), _22(6, 'i', 'o', 'o', 'o', 'o', 'o'), _23(6, "sequence_oid", "start_value", "minimum_value", "maximum_value", "increment", "cycle_option"), _24(NULL), _25("pg_sequence_parameters"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33("sequence parameters, for use by information schema"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))
),
AddFuncGroup(
"pg_sequence_last_value", 1,
AddBuiltinFunc(_0(3080), _1("pg_sequence_last_value"), _2(1), _3(true), _4(false), _5(pg_sequence_last_value), _6(2249), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(1, 26), _21(3, 26, 34, 34), _22(3, 'i', 'o', 'o'), _23(3, "sequence_oid", "cache_value", "last_value"), _24(NULL), _25("pg_sequence_last_value"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33(NULL), _34('f'), _35(NULL), _36(0), _37(false))
),
AddFuncGroup(
"pg_sequence_parameters", 1,
AddBuiltinFunc(_0(3078), _1("pg_sequence_parameters"), _2(1), _3(true), _4(false), _5(pg_sequence_parameters), _6(2249), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(0), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(1, 26), _21(6, 26, 34, 34, 34, 34, 16), _22(6, 'i', 'o', 'o', 'o', 'o', 'o'), _23(6, "sequence_oid", "start_value", "minimum_value", "maximum_value", "increment", "cycle_option"), _24(NULL), _25("pg_sequence_parameters"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33("sequence parameters, for use by information schema"), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))
),
AddFuncGroup(
"pg_shared_memctx_detail", 1,
AddBuiltinFunc(_0(3987), _1("pg_shared_memctx_detail"), _2(1), _3(false), _4(true), _5(pg_shared_memctx_detail), _6(16), _7(PG_CATALOG_NAMESPACE), _8(BOOTSTRAP_SUPERUSERID), _9(INTERNALlanguageId), _10(1), _11(100), _12(0), _13(0), _14(false), _15(false), _16(false), _17(false), _18('s'), _19(0), _20(1, 2275), _21(NULL), _22(NULL), _23(NULL), _24(NULL), _25("pg_shared_memctx_detail"), _26(NULL), _27(NULL), _28(NULL), _29(0), _30(false), _31(NULL), _32(false), _33(NULL), _34('f'), _35(NULL), _36(0), _37(false), _38(NULL), _39(NULL), _40(0))

View File

@ -50,6 +50,7 @@
#include "tcop/utility.h"
#include "pgxc/pgxc.h"
#include "utils/fmgroids.h"
#include "funcapi.h"
const size_t ENCRYPTED_VALUE_MIN_LENGTH = 170;
const size_t ENCRYPTED_VALUE_MAX_LENGTH = 1024;
@ -1324,3 +1325,28 @@ ClientLogicColumnRef *get_column_enc_def(Oid rel_oid, const char *col_name)
return enc_def;
}
Datum get_client_info(PG_FUNCTION_ARGS)
{
ReturnSetInfo* rsinfo = (ReturnSetInfo*)fcinfo->resultinfo;
TupleDesc tupdesc;
Tuplestorestate* tupstore = NULL;
const int COLUMN_NUM = 2;
MemoryContext oldcontext = MemoryContextSwitchTo(rsinfo->econtext->ecxt_per_query_memory);
tupdesc = CreateTemplateTupleDesc(COLUMN_NUM, false, TAM_HEAP);
TupleDescInitEntry(tupdesc, (AttrNumber)1, "sid", INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber)2, "client_info", TEXTOID, -1, 0);
tupstore = tuplestore_begin_heap(true, false, u_sess->attr.attr_memory.work_mem);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = BlessTupleDesc(tupdesc);
(void)MemoryContextSwitchTo(oldcontext);
if (ENABLE_THREAD_POOL) {
g_threadPoolControler->GetSessionCtrl()->getSessionClientInfo(rsinfo->setResult, rsinfo->setDesc);
}
tuplestore_donestoring(rsinfo->setResult);
return (Datum)0;
}

View File

@ -812,6 +812,7 @@ static void knl_u_plpgsql_init(knl_u_plpgsql_context* plsql_cxt)
plsql_cxt->is_delete_function = false;
plsql_cxt->have_error = false;
plsql_cxt->client_info = NULL;
pthread_mutex_init(&plsql_cxt->client_info_lock, NULL);
plsql_cxt->sess_cxt_htab = NULL;
plsql_cxt->have_error = false;
plsql_cxt->stp_savepoint_cnt = 0;

View File

@ -568,6 +568,65 @@ void ThreadPoolSessControl::getSessionMemoryDetail(Tuplestorestate* tupStore,
PG_END_TRY();
}
void ThreadPoolSessControl::calculateClientInfo(
knl_session_context* sess, Tuplestorestate* tupStore, TupleDesc tupDesc)
{
/* build one tuple and save it in tuplestore. */
const int COLUMN_NUM = 2;
Datum values[COLUMN_NUM] = {0};
bool nulls[COLUMN_NUM] = {false};
values[0] = Int64GetDatum(sess->session_id);
if (sess->plsql_cxt.client_info != NULL) {
values[1] = CStringGetTextDatum(sess->plsql_cxt.client_info);
} else {
nulls[1] = true;
}
tuplestore_putvalues(tupStore, tupDesc, values, nulls);
}
void ThreadPoolSessControl::getSessionClientInfo(Tuplestorestate* tupStore, TupleDesc tupDesc)
{
AutoMutexLock alock(&m_sessCtrlock);
knl_sess_control* ctrl = NULL;
Dlelem* elem = NULL;
knl_sess_control* sess = NULL;
PG_TRY();
{
HOLD_INTERRUPTS();
alock.lock();
/* collect all the Memory Context status, put in data */
elem = DLGetHead(&m_activelist);
while (elem != NULL) {
ctrl = (knl_sess_control*)DLE_VAL(elem);
sess = ctrl;
if (ctrl->sess) {
(void)syscalllockAcquire(&ctrl->sess->plsql_cxt.client_info_lock);
calculateClientInfo(ctrl->sess, tupStore, tupDesc);
(void)syscalllockRelease(&ctrl->sess->plsql_cxt.client_info_lock);
}
elem = DLGetSucc(elem);
}
alock.unLock();
sess = NULL;
RESUME_INTERRUPTS();
}
PG_CATCH();
{
if (sess != NULL) {
ctrl = sess;
(void)syscalllockRelease(&ctrl->sess->plsql_cxt.client_info_lock);
}
alock.unLock();
PG_RE_THROW();
}
PG_END_TRY();
}
void ThreadPoolSessControl::getSessionMemoryContextInfo(const char* ctx_name,
StringInfoData* buf, knl_sess_control** sess)
{

View File

@ -0,0 +1 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;

View File

@ -0,0 +1,9 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_PROC, 7732;
CREATE OR REPLACE FUNCTION pg_catalog.get_client_info()
RETURNS text
LANGUAGE internal
STABLE STRICT NOT FENCED SHIPPABLE
AS $function$get_client_info$function$;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0;

View File

@ -0,0 +1 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;

View File

@ -0,0 +1,9 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_PROC, 7732;
CREATE OR REPLACE FUNCTION pg_catalog.get_client_info()
RETURNS text
LANGUAGE internal
STABLE STRICT NOT FENCED SHIPPABLE
AS $function$get_client_info$function$;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0;

View File

@ -0,0 +1,10 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_PROC, 7732;
CREATE OR REPLACE FUNCTION pg_catalog.get_client_info()
RETURNS text
LANGUAGE internal
STABLE STRICT NOT FENCED SHIPPABLE
AS $function$get_client_info$function$;
comment on function PG_CATALOG.get_client_info() is 'read current client';
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0;

View File

@ -0,0 +1,10 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_PROC, 7732;
CREATE OR REPLACE FUNCTION pg_catalog.get_client_info(OUT sid bigint, OUT client_info text)
RETURNS SETOF record
LANGUAGE internal
STABLE NOT FENCED SHIPPABLE ROWS 100
AS $function$get_client_info$function$;
comment on function PG_CATALOG.get_client_info() is 'read current client';
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0;

View File

@ -0,0 +1,10 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_PROC, 7732;
CREATE OR REPLACE FUNCTION pg_catalog.get_client_info()
RETURNS text
LANGUAGE internal
STABLE STRICT NOT FENCED SHIPPABLE
AS $function$get_client_info$function$;
comment on function PG_CATALOG.get_client_info() is 'read current client';
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0;

View File

@ -0,0 +1,10 @@
DROP FUNCTION IF EXISTS pg_catalog.get_client_info;
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_PROC, 7732;
CREATE OR REPLACE FUNCTION pg_catalog.get_client_info(OUT sid bigint, OUT client_info text)
RETURNS SETOF record
LANGUAGE internal
STABLE NOT FENCED SHIPPABLE ROWS 100
AS $function$get_client_info$function$;
comment on function PG_CATALOG.get_client_info() is 'read current client';
SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0;

View File

@ -60,4 +60,5 @@ void get_global_setting_description(StringInfo buffer, const ObjectAddress* obje
void get_column_setting_description(StringInfo buffer, const ObjectAddress* object);
void get_cached_column_description(StringInfo buffer, const ObjectAddress* object);
void get_global_setting_args_description(StringInfo buffer, const ObjectAddress* object);
void get_column_setting_args_description(StringInfo buffer, const ObjectAddress* object);
void get_column_setting_args_description(StringInfo buffer, const ObjectAddress* object);
extern Datum get_client_info(PG_FUNCTION_ARGS);

View File

@ -1537,6 +1537,7 @@ typedef struct knl_u_plpgsql_context {
bool is_delete_function;
bool is_package_instantiation;
char* client_info;
pthread_mutex_t client_info_lock;
char sess_cxt_name[128];
HTAB* sess_cxt_htab;
bool have_error;

View File

@ -62,6 +62,7 @@ public:
void CheckSessionTimeout();
void CheckPermissionForSendSignal(knl_session_context* sess, sig_atomic_t* lock);
void getSessionMemoryDetail(Tuplestorestate* tupStore, TupleDesc tupDesc, knl_sess_control** sess);
void getSessionClientInfo(Tuplestorestate* tupStore, TupleDesc tupDesc);
void getSessionMemoryContextInfo(const char* ctx_name, StringInfoData* buf, knl_sess_control** sess);
knl_session_context* GetSessionByIdx(int idx);
int FindCtrlIdxBySessId(uint64 id);
@ -78,6 +79,8 @@ private:
knl_session_context* sess, const MemoryContext context, Tuplestorestate* tupStore, TupleDesc tupDesc);
void calculateSessMemCxtStats(
knl_session_context* sess, const MemoryContext context, Tuplestorestate* tupStore, TupleDesc tupDesc);
void calculateClientInfo(
knl_session_context* sess, Tuplestorestate* tupStore, TupleDesc tupDesc);
inline bool IsValidCtrlIndex(int ctrl_index)
{