!1101 支持插件自定义会话级变量
Merge pull request !1101 from chenxiaobin/extension_session
This commit is contained in:
@ -179,6 +179,7 @@ void* internal_load_library(const char* libname)
|
||||
DynamicFileList* file_scanner = NULL;
|
||||
FileListInit* file_init_scanner = NULL;
|
||||
PGModuleMagicFunction magic_func;
|
||||
void (*set_extension_index)(uint32);
|
||||
struct stat stat_buf;
|
||||
PG_init_t PG_init = NULL;
|
||||
char* file = last_dir_separator(libname);
|
||||
@ -311,6 +312,13 @@ void* internal_load_library(const char* libname)
|
||||
errhint("Extension libraries are required to use the PG_MODULE_MAGIC macro.")));
|
||||
}
|
||||
|
||||
/* Look up the set_extension_index function within extension's so */
|
||||
set_extension_index = (void(*)(uint32))pg_dlsym(file_scanner->handle, "set_extension_index");
|
||||
if (set_extension_index) {
|
||||
uint32 idx = pg_atomic_fetch_add_u32(&g_instance.extensionNum, 1);
|
||||
(*set_extension_index)(idx);
|
||||
}
|
||||
|
||||
/* OK to link it into list */
|
||||
if (file_list == NULL)
|
||||
file_list = file_scanner;
|
||||
|
||||
@ -86,6 +86,11 @@
|
||||
#include "instruments/percentile.h"
|
||||
#include "instruments/instr_workload.h"
|
||||
#include "gs_policy/policy_common.h"
|
||||
#ifndef WIN32_ONLY_COMPILER
|
||||
#include "dynloader.h"
|
||||
#else
|
||||
#include "port/dynloader/win32.h"
|
||||
#endif
|
||||
|
||||
#ifdef PGXC
|
||||
#include "catalog/pgxc_node.h"
|
||||
@ -1737,6 +1742,8 @@ void PostgresInitializer::InitSession()
|
||||
|
||||
InitSettings();
|
||||
|
||||
InitExtensionVariable();
|
||||
|
||||
FinishInit();
|
||||
|
||||
AuditUserLogin();
|
||||
@ -2260,6 +2267,28 @@ void PostgresInitializer::InitSettings()
|
||||
InitializeClientEncoding();
|
||||
}
|
||||
|
||||
void PostgresInitializer::InitExtensionVariable()
|
||||
{
|
||||
int initExtArraySize = 10;
|
||||
void (*init_session_vars)(void);
|
||||
|
||||
/* initialize u_sess->attr.attr_common.extension_session_vars_array */
|
||||
u_sess->attr.attr_common.extension_session_vars_array_size = initExtArraySize;
|
||||
u_sess->attr.attr_common.extension_session_vars_array =
|
||||
(void**)MemoryContextAllocZero(u_sess->self_mem_cxt, (Size)(initExtArraySize * sizeof(void*)));
|
||||
|
||||
DynamicFileList* file_scanner = NULL;
|
||||
for (file_scanner = file_list; file_scanner != NULL; file_scanner = file_scanner->next) {
|
||||
/*
|
||||
* If the library has a init_session_vars() function, call it for
|
||||
* initializing extension session variables.
|
||||
*/
|
||||
init_session_vars = (void(*)(void))pg_dlsym(file_scanner->handle, "init_session_vars");
|
||||
if (init_session_vars != NULL)
|
||||
(*init_session_vars)();
|
||||
}
|
||||
}
|
||||
|
||||
void PostgresInitializer::FinishInit()
|
||||
{
|
||||
/* report this backend in the PgBackendStatus array */
|
||||
|
||||
@ -2889,3 +2889,25 @@ void AlterExtensionOwner_oid(Oid extensionOid, Oid newOwnerId)
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
* Expand the size of extension_session_vars_array_size by twice the number
|
||||
* of extensions each time if latter is bigger.
|
||||
*/
|
||||
void RepallocSessionVarsArrayIfNecessary()
|
||||
{
|
||||
uint32 currExtensionNum = pg_atomic_read_u32(&g_instance.extensionNum);
|
||||
uint32 currArraySize = u_sess->attr.attr_common.extension_session_vars_array_size;
|
||||
int rc;
|
||||
|
||||
if (currExtensionNum >= currArraySize) {
|
||||
u_sess->attr.attr_common.extension_session_vars_array = (void**)repalloc(
|
||||
u_sess->attr.attr_common.extension_session_vars_array, currExtensionNum * 2 * sizeof(void*));
|
||||
|
||||
rc = memset_s(&u_sess->attr.attr_common.extension_session_vars_array[currArraySize],
|
||||
(currExtensionNum * 2 - currArraySize) * sizeof(void*), 0,
|
||||
(currExtensionNum * 2 - currArraySize) * sizeof(void*));
|
||||
securec_check(rc, "", "");
|
||||
u_sess->attr.attr_common.extension_session_vars_array_size = currExtensionNum * 2;
|
||||
}
|
||||
}
|
||||
|
||||
@ -550,6 +550,7 @@ void knl_instance_init()
|
||||
g_instance.stat_cxt.track_memory_inited = false;
|
||||
g_instance.proc_base = NULL;
|
||||
g_instance.proc_array_idx = NULL;
|
||||
pg_atomic_init_u32(&g_instance.extensionNum, 0);
|
||||
|
||||
/*
|
||||
* Set up the process wise memory context. The memory allocated from this
|
||||
|
||||
@ -91,6 +91,8 @@ static void knl_u_attr_init(knl_session_attr* attr)
|
||||
attr->attr_sql.under_explain = false;
|
||||
attr->attr_resource.enable_auto_explain = false;
|
||||
attr->attr_sql.enable_upsert_to_merge = false;
|
||||
attr->attr_common.extension_session_vars_array_size = 0;
|
||||
attr->attr_common.extension_session_vars_array = NULL;
|
||||
}
|
||||
|
||||
void knl_u_executor_init(knl_u_executor_context* exec_cxt)
|
||||
|
||||
@ -42,4 +42,6 @@ extern void AlterExtensionNamespace(List* names, const char* newschema);
|
||||
|
||||
extern void AlterExtensionOwner_oid(Oid extensionOid, Oid newOwnerId);
|
||||
|
||||
extern void RepallocSessionVarsArrayIfNecessary();
|
||||
|
||||
#endif /* EXTENSION_H */
|
||||
|
||||
@ -208,6 +208,8 @@ typedef struct knl_session_attr_common {
|
||||
char* router_att;
|
||||
bool enable_router;
|
||||
int gpc_clean_timeout;
|
||||
uint32 extension_session_vars_array_size;
|
||||
void** extension_session_vars_array;
|
||||
} knl_session_attr_common;
|
||||
|
||||
#endif /* SRC_INCLUDE_KNL_KNL_SESSION_ATTR_COMMON_H_ */
|
||||
|
||||
@ -870,6 +870,7 @@ typedef struct knl_instance_context {
|
||||
void *raw_parser_hook[DB_CMPT_MAX];
|
||||
void *plsql_parser_hook[DB_CMPT_MAX];
|
||||
#endif
|
||||
pg_atomic_uint32 extensionNum;
|
||||
} knl_instance_context;
|
||||
|
||||
extern long random();
|
||||
|
||||
@ -160,6 +160,8 @@ private:
|
||||
|
||||
void InitSettings();
|
||||
|
||||
void InitExtensionVariable();
|
||||
|
||||
void FinishInit();
|
||||
|
||||
void AuditUserLogin();
|
||||
|
||||
Reference in New Issue
Block a user