diff --git a/src/common/backend/catalog/builtin_funcs.ini b/src/common/backend/catalog/builtin_funcs.ini index 95ba20633..d489c8b2e 100755 --- a/src/common/backend/catalog/builtin_funcs.ini +++ b/src/common/backend/catalog/builtin_funcs.ini @@ -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)) diff --git a/src/common/backend/client_logic/client_logic.cpp b/src/common/backend/client_logic/client_logic.cpp index 7800f3cdc..479a84b4e 100644 --- a/src/common/backend/client_logic/client_logic.cpp +++ b/src/common/backend/client_logic/client_logic.cpp @@ -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; +} \ No newline at end of file diff --git a/src/gausskernel/process/threadpool/knl_session.cpp b/src/gausskernel/process/threadpool/knl_session.cpp index e21ba8497..85abb97b7 100755 --- a/src/gausskernel/process/threadpool/knl_session.cpp +++ b/src/gausskernel/process/threadpool/knl_session.cpp @@ -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; diff --git a/src/gausskernel/process/threadpool/threadpool_sessctl.cpp b/src/gausskernel/process/threadpool/threadpool_sessctl.cpp index e7e5090b8..1267f0ad1 100755 --- a/src/gausskernel/process/threadpool/threadpool_sessctl.cpp +++ b/src/gausskernel/process/threadpool/threadpool_sessctl.cpp @@ -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) { diff --git a/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_92_508.sql b/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_92_508.sql new file mode 100644 index 000000000..4c33ad65e --- /dev/null +++ b/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback-post_catalog_maindb_92_508.sql @@ -0,0 +1 @@ +DROP FUNCTION IF EXISTS pg_catalog.get_client_info; diff --git a/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback_catalog_maindb_92_512.sql b/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback_catalog_maindb_92_512.sql new file mode 100644 index 000000000..c7adf8dbe --- /dev/null +++ b/src/include/catalog/upgrade_sql/rollback_catalog_maindb/rollback_catalog_maindb_92_512.sql @@ -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; diff --git a/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_92_508.sql b/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_92_508.sql new file mode 100644 index 000000000..4c33ad65e --- /dev/null +++ b/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback-post_catalog_otherdb_92_508.sql @@ -0,0 +1 @@ +DROP FUNCTION IF EXISTS pg_catalog.get_client_info; diff --git a/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback_catalog_otherdb_92_512.sql b/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback_catalog_otherdb_92_512.sql new file mode 100644 index 000000000..c7adf8dbe --- /dev/null +++ b/src/include/catalog/upgrade_sql/rollback_catalog_otherdb/rollback_catalog_otherdb_92_512.sql @@ -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; diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_92_508.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_92_508.sql new file mode 100644 index 000000000..9a0dbe34d --- /dev/null +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_92_508.sql @@ -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; diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_92_512.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_92_512.sql new file mode 100644 index 000000000..b2378f20e --- /dev/null +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade-post_catalog_maindb_92_512.sql @@ -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; diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_92_508.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_92_508.sql new file mode 100644 index 000000000..9a0dbe34d --- /dev/null +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_92_508.sql @@ -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; diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_92_512.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_92_512.sql new file mode 100644 index 000000000..b2378f20e --- /dev/null +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade-post_catalog_otherdb_92_512.sql @@ -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; diff --git a/src/include/client_logic/client_logic.h b/src/include/client_logic/client_logic.h index e10768e27..04c6ab629 100644 --- a/src/include/client_logic/client_logic.h +++ b/src/include/client_logic/client_logic.h @@ -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); \ No newline at end of file +void get_column_setting_args_description(StringInfo buffer, const ObjectAddress* object); +extern Datum get_client_info(PG_FUNCTION_ARGS); \ No newline at end of file diff --git a/src/include/knl/knl_session.h b/src/include/knl/knl_session.h index 636706693..604ca95b4 100644 --- a/src/include/knl/knl_session.h +++ b/src/include/knl/knl_session.h @@ -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; diff --git a/src/include/threadpool/threadpool_sessctl.h b/src/include/threadpool/threadpool_sessctl.h index 943e246f0..15c1d3eff 100644 --- a/src/include/threadpool/threadpool_sessctl.h +++ b/src/include/threadpool/threadpool_sessctl.h @@ -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) {