diff --git a/src/common/backend/catalog/CMakeLists.txt b/src/common/backend/catalog/CMakeLists.txt index 052d104071feaf476d6ac57b9b60c28e4aeda13e..42ae57f3b7e4d8222e1be50496202e1d66754e68 100755 --- a/src/common/backend/catalog/CMakeLists.txt +++ b/src/common/backend/catalog/CMakeLists.txt @@ -15,7 +15,7 @@ set(POSTGRES_BKI_SRCS_S @pg_object.h @pg_synonym.h @toasting.h @indexing.h @gs_obsscaninfo.h @pg_directory.h @pg_hashbucket.h @gs_global_chain.h @gs_global_config.h @pg_streaming_stream.h @pg_streaming_cont_query.h @pg_streaming_reaper_status.h @gs_matview.h @gs_matview_dependency.h @pgxc_slice.h @gs_opt_model.h @pg_recyclebin.h @pg_snapshot.h @gs_model.h @gs_package.h @gs_job_argument.h @gs_job_attribute.h @pg_uid.h @gs_db_privilege.h -@pg_replication_origin.h @pg_publication.h @pg_publication_rel.h @pg_subscription.h @gs_sql_patch.h @pg_subscription_rel.h" +@pg_replication_origin.h @pg_publication.h @pg_publication_rel.h @pg_subscription.h @gs_sql_patch.h @pg_subscription_rel.h @gs_statement_memory_info.h" ) diff --git a/src/common/backend/catalog/Makefile b/src/common/backend/catalog/Makefile index c38bf4a4867fb15142436ddc31184e721ca52b0e..9dc4846db6eda77f7b36a2b07f7b2dc0a68b6131 100644 --- a/src/common/backend/catalog/Makefile +++ b/src/common/backend/catalog/Makefile @@ -62,7 +62,7 @@ POSTGRES_BKI_SRCS = $(addprefix $(top_srcdir)/src/include/catalog/,\ gs_matview_dependency.h pgxc_slice.h gs_opt_model.h gs_model.h\ pg_recyclebin.h pg_snapshot.h gs_job_argument.h gs_job_attribute.h pg_uid.h gs_db_privilege.h\ pg_replication_origin.h pg_publication.h pg_publication_rel.h pg_subscription.h gs_sql_patch.h\ - pg_subscription_rel.h \ + pg_subscription_rel.h gs_statement_memory_info.h\ ) # location of Catalog.pm diff --git a/src/common/backend/catalog/dependency.cpp b/src/common/backend/catalog/dependency.cpp index bd62afbd7b13950c65fe4b9a423ed781e8a302cc..943c76225d51f7cc94e8dbcae8f79d4e2832e1b8 100644 --- a/src/common/backend/catalog/dependency.cpp +++ b/src/common/backend/catalog/dependency.cpp @@ -118,6 +118,7 @@ #include "utils/snapmgr.h" #include "datasource/datasource.h" #include "postmaster/rbcleaner.h" +#include "catalog/gs_statement_memory_info.h" /* * This constant table maps ObjectClasses to the corresponding catalog OIDs. @@ -1540,6 +1541,11 @@ static void doDeletion(const ObjectAddress* object, int flags) case OCLASS_PUBLICATION_REL: RemovePublicationRelById(object->objectId); break; + // change start + case OCLASS_MEMORY_INFO: + remove_info_by_oid(object->objectId); + break; + // chaneg end default: ereport(ERROR, (errcode(ERRCODE_UNRECOGNIZED_NODE_TYPE), errmsg("unrecognized object class: %u", object->classId))); @@ -2575,6 +2581,10 @@ ObjectClass getObjectClass(const ObjectAddress* object) case SubscriptionRelationId: return OCLASS_SUBSCRIPTION; + // change start + case StatementMemoryInfoRelationId: + return OCLASS_MEMORY_INFO; + // chanege end default: break; } diff --git a/src/common/backend/catalog/performance_views.sql b/src/common/backend/catalog/performance_views.sql index 4635a88afa07b52c87f87fb0b991628ec7dffa68..bed01ea0f1057791741530ef134b405764313e02 100644 --- a/src/common/backend/catalog/performance_views.sql +++ b/src/common/backend/catalog/performance_views.sql @@ -3938,6 +3938,11 @@ LANGUAGE 'plpgsql' NOT FENCED; CREATE VIEW DBE_PERF.statement_history AS select * from pg_catalog.statement_history; +/* change start */ +CREATE VIEW DBE_PERF.statement_memory_info AS + select * from catalog.gs_statement_memory_info; +/* change end */ + CREATE OR REPLACE FUNCTION DBE_PERF.get_global_full_sql_by_timestamp (in start_timestamp timestamp with time zone, in end_timestamp timestamp with time zone, diff --git a/src/common/backend/utils/cache/relcache.cpp b/src/common/backend/utils/cache/relcache.cpp index fa87ca9efd48e262660ea9a0bdbdeb0e0ba9c98c..694607f318d5153ce321d2641d72ff7f9e846f90 100755 --- a/src/common/backend/utils/cache/relcache.cpp +++ b/src/common/backend/utils/cache/relcache.cpp @@ -217,6 +217,7 @@ #include "utils/fmgrtab.h" #include "parser/parse_coerce.h" #include "access/amapi.h" +#include "catalog/gs_statement_memory_info.h" /* * name of relcache init file(s), used to speed up backend startup @@ -347,17 +348,30 @@ static const FormData_pg_attribute Desc_pg_replication_origin[Natts_pg_replicati }; static const FormData_pg_attribute Desc_pg_subscription_rel[Natts_pg_subscription_rel] = {Schema_pg_subscription_rel}; static const FormData_pg_attribute Desc_gs_sql_patch_origin[Natts_gs_sql_patch] = {Schema_gs_sql_patch}; +// change start +static const FormData_pg_attribute Desc_gs_statement_memory_info[Natts_gs_statement_memory_info] = {Schema_gs_statement_memory_info}; +// change end /* Please add to the array in ascending order of oid value */ -static struct CatalogRelationBuildParam catalogBuildParam[CATALOG_NUM] = {{DefaultAclRelationId, - "pg_default_acl", - DefaultAclRelation_Rowtype_Id, - false, - true, - Natts_pg_default_acl, - Desc_pg_default_acl, - false, - true}, +static struct CatalogRelationBuildParam catalogBuildParam[CATALOG_NUM] = { + {StatementMemoryInfoRelationId, + "gs_statement_memory_info", + StatementMemoryInfo_Rowtype_Id, + false, + true, + Natts_gs_statement_memory_info, + Desc_gs_statement_memory_info, + false, + true}, + {DefaultAclRelationId, + "pg_default_acl", + DefaultAclRelation_Rowtype_Id, + false, + true, + Natts_pg_default_acl, + Desc_pg_default_acl, + false, + true}, {PLTemplateRelationId, "pg_pltemplate", PLTemplateRelation_Rowtype_Id, diff --git a/src/common/backend/utils/cache/syscache.cpp b/src/common/backend/utils/cache/syscache.cpp index 67965551eaf6bf3fbcc8baeda93197ff666817ac..9015fc25f4e78b4b6735af17b81fb30224f237ea 100644 --- a/src/common/backend/utils/cache/syscache.cpp +++ b/src/common/backend/utils/cache/syscache.cpp @@ -111,6 +111,7 @@ #include "catalog/pg_publication_rel.h" #include "catalog/pg_replication_origin.h" #include "catalog/pg_subscription_rel.h" +#include "catalog/gs_statement_memory_info.h" /* --------------------------------------------------------------------------- @@ -149,6 +150,13 @@ */ const cachedesc cacheinfo[] = { + // change start + {StatementMemoryInfoRelationId, /*STATEMENTINFOOID*/ + GsStatementMemoryInfoOidIndexId, + 1, + {Anum_gs_statement_memory_info_queryid, Anum_gs_statement_memory_info_totalsize, 0, 0}, + 128}, + // change end {AggregateRelationId, /* AGGFNOID */ AggregateFnoidIndexId, 1, diff --git a/src/common/backend/utils/misc/guc.cpp b/src/common/backend/utils/misc/guc.cpp index add4504fddc18947b0440897f8bfb82c9b2d1e25..dfe0e6697de51a28ed898010c7ade20281a6081c 100755 --- a/src/common/backend/utils/misc/guc.cpp +++ b/src/common/backend/utils/misc/guc.cpp @@ -236,7 +236,11 @@ THR_LOCAL int comm_ackchk_time; THR_LOCAL GucContext currentGucContext; -const char* sync_guc_variable_namelist[] = {"work_mem", +const char* sync_guc_variable_namelist[] = { + // change start + "enable_memory_stat", + // change end + "work_mem", "query_mem", "ustore_attr", "ssl_renegotiation_limit", diff --git a/src/common/backend/utils/misc/guc/guc_sql.cpp b/src/common/backend/utils/misc/guc/guc_sql.cpp index edc4298c773ceac2f010b53c060549ff305f659f..fc85c6aa10a2ce75062fa5beef9e789d7ae9d049 100755 --- a/src/common/backend/utils/misc/guc/guc_sql.cpp +++ b/src/common/backend/utils/misc/guc/guc_sql.cpp @@ -463,6 +463,19 @@ void InitSqlConfigureNames() static void InitSqlConfigureNamesBool() { struct config_bool localConfigureNamesBool[] = { + // change start + {{"enable_memory_stat", + PGC_USERSET, + NODE_ALL, + QUERY_TUNING_METHOD, + gettext_noop("Enable Memory Info for Query"), + NULL}, + &u_sess->attr.attr_sql.enable_memory_stat, + false, + NULL, + NULL, + NULL}, + // change end {{"enable_fast_numeric", PGC_SUSET, NODE_ALL, diff --git a/src/common/backend/utils/mmgr/mcxt.cpp b/src/common/backend/utils/mmgr/mcxt.cpp index 79f1ec17dea5347b1d57d7c29a41fee272605322..5d69b2012e979c4c045352f89a57d8058e9e0682 100644 --- a/src/common/backend/utils/mmgr/mcxt.cpp +++ b/src/common/backend/utils/mmgr/mcxt.cpp @@ -1050,7 +1050,10 @@ void* MemoryAllocFromContext(MemoryContext context, Size size, const char* file, if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } - + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1113,7 +1116,10 @@ void* MemoryContextAllocZeroDebug(MemoryContext context, Size size, const char* MemoryContextCheckSessionMemory(context, size, file, line); } MemSetAligned(ret, 0, size); - + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1163,7 +1169,10 @@ void* MemoryContextAllocZeroAlignedDebug(MemoryContext context, Size size, const MemoryContextCheckSessionMemory(context, size, file, line); } MemSetLoop(ret, 0, size); - + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1224,6 +1233,10 @@ void* MemoryContextAllocExtendedDebug(MemoryContext context, Size size, int flag if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1265,6 +1278,10 @@ void* std_palloc_extended(Size size, int flags) if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(CurrentMemoryContext, size, __FILE__, __LINE__); } + // change start + CurrentMemoryContext->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, CurrentMemoryContext, __FILE__, __LINE__, size); return ret; @@ -1318,6 +1335,9 @@ void pfree(void* pointer) PreventActionOnSealedContext(context); } #endif + // change start + context->freed_memsize += GetMemoryChunkSpace(pointer); + // change end RemoveMemoryAllocInfo(pointer, context); (*context->methods->free_p)(context, pointer); @@ -1360,7 +1380,9 @@ void* repalloc_noexcept_Debug(void* pointer, Size size, const char* file, int li /* isReset must be false already */ Assert(!context->isReset); - + // change start + context->freed_memsize += GetMemoryChunkSpace(pointer); + // change end RemoveMemoryAllocInfo(pointer, context); ret = (*context->methods->realloc)(context, pointer, 0, size, file, line); @@ -1372,6 +1394,10 @@ void* repalloc_noexcept_Debug(void* pointer, Size size, const char* file, int li if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1418,7 +1444,9 @@ void* repallocDebug(void* pointer, Size size, const char* file, int line) PreventActionOnSealedContext(context); /* isReset must be false already */ Assert(!context->isReset); - + // change start + context->freed_memsize += GetMemoryChunkSpace(pointer); + // change end RemoveMemoryAllocInfo(pointer, context); ret = (*context->methods->realloc)(context, pointer, 0, size, file, line); @@ -1441,6 +1469,10 @@ void* repallocDebug(void* pointer, Size size, const char* file, int line) if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1484,6 +1516,10 @@ void* MemoryContextMemalignAllocDebug(MemoryContext context, Size align, Size si if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1530,6 +1566,10 @@ void* MemoryContextAllocHugeDebug(MemoryContext context, Size size, const char* if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1576,6 +1616,10 @@ void* MemoryContextAllocHugeZeroDebug(MemoryContext context, Size size, const ch if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1625,7 +1669,9 @@ void* repallocHugeDebug(void* pointer, Size size, const char* file, int line) /* isReset must be false already */ Assert(!context->isReset); - + // change start + context->freed_memsize += GetMemoryChunkSpace(pointer); + // change end RemoveMemoryAllocInfo(pointer, context); ret = (*context->methods->realloc)(context, pointer, 0, size, file, line); @@ -1648,7 +1694,10 @@ void* repallocHugeDebug(void* pointer, Size size, const char* file, int line) if (unlikely(STATEMENT_MAX_MEM)) { MemoryContextCheckSessionMemory(context, size, file, line); } - + // change start + context->allocated_memsize += GetMemoryChunkSpace(ret); + // context->allocated_memsize += size; + // change end InsertMemoryAllocInfo(ret, context, file, line, size); return ret; @@ -1668,7 +1717,9 @@ void MemoryContextMemalignFree(MemoryContext context, void* pointer) Assert(pointer != NULL); Assert(pointer == (void*)MAXALIGN(pointer)); AssertArg(MemoryContextIsValid(context)); - + // change start + context->freed_memsize += GetMemoryChunkSpace(pointer); + // change end RemoveMemoryAllocInfo(pointer, context); (*context->methods->free_p)(context, (char*)pointer); diff --git a/src/gausskernel/runtime/executor/execMain.cpp b/src/gausskernel/runtime/executor/execMain.cpp index 63db5fc487120239d7f8c3ef9e01878e2aa904c7..a117715e9bd3ff6fc16508de4b74f96cb85c823b 100755 --- a/src/gausskernel/runtime/executor/execMain.cpp +++ b/src/gausskernel/runtime/executor/execMain.cpp @@ -97,6 +97,8 @@ #include "gs_ledger/ledger_utils.h" #include "gs_policy/gs_policy_masking.h" #include "optimizer/gplanmgr.h" +#include "catalog/gs_statement_memory_info.h" + /* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */ THR_LOCAL ExecutorStart_hook_type ExecutorStart_hook = NULL; @@ -215,6 +217,9 @@ static void report_iud_time(QueryDesc *query) */ void ExecutorStart(QueryDesc* queryDesc, int eflags) { + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("SQL Start1"))); + gstrace_entry(GS_TRC_ID_ExecutorStart); /* it's unsafe to deal with plugins hooks as dynamic lib may be released */ @@ -226,6 +231,21 @@ void ExecutorStart(QueryDesc* queryDesc, int eflags) gstrace_exit(GS_TRC_ID_ExecutorStart); } +void ResetMemsize(MemoryContext context) +{ + context->allocated_memsize = 0; + context->freed_memsize = 0; + + MemoryContext child = context->firstchild; + while (child != NULL) + { + ResetMemsize(child); + child = child->nextchild; + } + + return; +} + void standard_ExecutorStart(QueryDesc *queryDesc, int eflags) { EState *estate = NULL; @@ -261,6 +281,11 @@ void standard_ExecutorStart(QueryDesc *queryDesc, int eflags) * Build EState, switch into per-query memory context for startup. */ estate = CreateExecutorState(); + + // change start + ResetMemsize(estate->es_query_cxt); + // change end + queryDesc->estate = estate; /* record the init memory track of the executor engine */ @@ -415,6 +440,37 @@ void standard_ExecutorStart(QueryDesc *queryDesc, int eflags) AfterTriggerBeginQuery(); } (void)MemoryContextSwitchTo(old_context); + + // if (u_sess->attr.attr_sql.enable_memory_stat) + // { + // HeapTuple tuple; + // Relation rel = NULL; + // Datum values[Natts_gs_statement_memory_info]; + // bool nulls[Natts_gs_statement_memory_info] = {false, false, false, false, false}; + + // rel = heap_open(StatementMemoryInfoRelationId, RowExclusiveLock); + + // values[Anum_gs_statement_memory_info_queryid - 1] = ObjectIdGetDatum(queryDesc->plannedstmt->queryId); + // values[Anum_gs_statement_memory_info_totalsize - 1] = Float4GetDatum(1.2); + // values[Anum_gs_statement_memory_info_freesize - 1] = Float4GetDatum(1.3); + // values[Anum_gs_statement_memory_info_usedsize - 1] = Float4GetDatum(1.4); + // values[Anum_gs_statement_memory_info_query - 1] = CStringGetTextDatum(queryDesc->sourceText); + + // // create tuple and insert into gs_statement_memory_info + // tuple = heap_form_tuple(RelationGetDescr(rel), values, nulls); + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("1"))); + // (void)simple_heap_insert(rel, tuple); + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("2"))); + // CatalogUpdateIndexes(rel, tuple); + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("3"))); + // heap_freetuple_ext(tuple); + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("4"))); + // heap_close(rel, RowExclusiveLock); + // } } /* ---------------------------------------------------------------- @@ -779,8 +835,75 @@ int ExecGetPlanNodeid(void) return key; } +// change start +Size GetTotalMemSize(MemoryContext context) +{ + Size total_memsize = context->allocated_memsize; + + MemoryContext child = context->firstchild; + while (child != NULL) + { + total_memsize += GetTotalMemSize(child); + child = child->nextchild; + } + + return total_memsize; +} +Size GetFreedMemSize(MemoryContext context) +{ + Size freed_memsize = context->freed_memsize; + + MemoryContext child = context->firstchild; + while (child != NULL) + { + freed_memsize += GetFreedMemSize(child); + child = child->nextchild; + } + + return freed_memsize; +} +// change end + void standard_ExecutorEnd(QueryDesc *queryDesc) { + // change start + if (u_sess->attr.attr_sql.enable_memory_stat) + { + u_sess->attr.attr_sql.enable_memory_stat = false; + Size total_memsize = GetTotalMemSize(queryDesc->estate->es_query_cxt); + Size freed_memsize = GetFreedMemSize(queryDesc->estate->es_query_cxt); + Size used_memsize = total_memsize-freed_memsize; + + char truncated_query[1001]; + strncpy(truncated_query, queryDesc->sourceText, 1000); + truncated_query[1000] = '\0'; /* 确保字符串终止 */ + + char sql[4096]; + + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("1"))); + + snprintf(sql, sizeof(sql), + "INSERT INTO gs_statement_memory_info (queryid, totalsize, freesize, usedsize, query) VALUES (%d, %f, %f, %f, '%s');", + (int)queryDesc->plannedstmt->uniqueSQLId, + (double)total_memsize / (1024.0 * 1024.0), + (double)freed_memsize / (1024.0 * 1024.0), + (double)used_memsize / (1024.0 * 1024.0), + truncated_query); + + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("2"))); + + SPI_connect(); + SPI_execute(sql, false, 0); + SPI_finish(); + + // ereport(WARNING, (errmodule(MOD_DB4AI), errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + // errmsg("3"))); + u_sess->attr.attr_sql.enable_memory_stat = true; + } + // change end + EState *estate = NULL; MemoryContext old_context; double totaltime = 0; diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index f1b3ae5fc542e9c1a69df706cb227fe5a400a8fe..4ca01e32a709b5c2309346d85542a94e0deb63cf 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -61,7 +61,7 @@ #endif #define NAILED_IN_CATALOG_NUM 8 - -#define CATALOG_NUM 108 - +// change start +#define CATALOG_NUM 109 +// change end #endif diff --git a/src/include/catalog/dependency.h b/src/include/catalog/dependency.h index f2081aa3b8a180def12bb77878b889386c20cfb9..4c429cc9480499d285bd1d51db7e04df7e3e676c 100644 --- a/src/include/catalog/dependency.h +++ b/src/include/catalog/dependency.h @@ -214,6 +214,9 @@ typedef enum ObjectClass { OCLASS_PUBLICATION_REL, /* pg_publication_rel */ OCLASS_SUBSCRIPTION, /* pg_subscription */ OCLASS_EVENT_TRIGGER, /* pg_event_trigger */ + // change start + OCLASS_MEMORY_INFO, /* gs_statement_memory_info */ + // change end MAX_OCLASS /* MUST BE LAST */ } ObjectClass; diff --git a/src/include/catalog/gs_statement_memory_info.h b/src/include/catalog/gs_statement_memory_info.h new file mode 100644 index 0000000000000000000000000000000000000000..ac39dc56a9661de4aec0b85c918918cb807510de --- /dev/null +++ b/src/include/catalog/gs_statement_memory_info.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2020 Huawei Technologies Co.,Ltd. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * gs_statement_memory_info.h + * + * IDENTIFICATION + * src/include/catalog/gs_statement_memory_info.h + * + * ------------------------------------------------------------------------- + */ + +#ifndef GS_STATEMENT_MEMORY_INFO_H +#define GS_STATEMENT_MEMORY_INFO_H +#include "catalog/genbki.h" + +#define StatementMemoryInfoRelationId 1038 +#define StatementMemoryInfo_Rowtype_Id 1061 + +CATALOG(gs_statement_memory_info,1038) BKI_SCHEMA_MACRO +{ + Oid queryid; + float4 totalsize; + float4 freesize; + float4 usedsize; + text query; +} FormData_gs_statement_memory_info; + +typedef FormData_gs_statement_memory_info *Form_gs_statement_memory_info; + +#define Natts_gs_statement_memory_info 5 +#define Anum_gs_statement_memory_info_queryid 1 +#define Anum_gs_statement_memory_info_query 2 +#define Anum_gs_statement_memory_info_totalsize 3 +#define Anum_gs_statement_memory_info_freesize 4 +#define Anum_gs_statement_memory_info_usedsize 5 + +inline void remove_info_by_oid(Oid queryid) +{ + Relation rel; + HeapTuple tup; + ScanKeyData skey[1]; + SysScanDesc scan; + + if (t_thrd.proc->workingVersionNum < 92366) + return; + + ScanKeyInit(&skey[0], ObjectIdAttributeNumber, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(queryid)); + + rel = heap_open(StatementMemoryInfoRelationId, StatementMemoryInfo_Rowtype_Id); + + scan = systable_beginscan(rel, GsStatementMemoryInfoOidIndexId, true, SnapshotNow, 1, skey); + + /* we expect exactly one match */ + tup = systable_getnext(scan); + if (!HeapTupleIsValid(tup)) + ereport( + ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("could not find tuple for model entry %u", queryid))); + + simple_heap_delete(rel, &tup->t_self); + + systable_endscan(scan); + heap_close(rel, RowExclusiveLock); +} + +#endif \ No newline at end of file diff --git a/src/include/catalog/indexing.h b/src/include/catalog/indexing.h index ec233c0a886073cacb322778bfd570e63ea87c60..b0e00f532991e561183933e99a741fda8c44db9e 100644 --- a/src/include/catalog/indexing.h +++ b/src/include/catalog/indexing.h @@ -64,7 +64,12 @@ extern void CatalogTupleDelete(Relation heapRel, ItemPointer tid); * the index in the C code should always use these #defines, not the actual * index name (much less the numeric OID). */ - +// change start +DECLARE_UNIQUE_INDEX(gs_statement_memory_info_oid_index, 1072, on gs_statement_memory_info using btree(queryid oid_ops, totalsize float4_ops)); +#define GsStatementMemoryInfoOidIndexId 1072 +// DECLARE_UNIQUE_INDEX(gs_statement_memory_info_mem_index, 1073, on gs_statement_memory_info using btree()); +// #define GsStatementMemoryInfoMemIndexId 1073 +// change end DECLARE_UNIQUE_INDEX(pg_aggregate_fnoid_index, 2650, on pg_aggregate using btree(aggfnoid oid_ops)); #define AggregateFnoidIndexId 2650 diff --git a/src/include/catalog/toasting.h b/src/include/catalog/toasting.h index 1494cbd52b1f9362afbb677f24cc719a0aacba48..d18a95152bd32cc815b1807f65fe1fe7346062d5 100644 --- a/src/include/catalog/toasting.h +++ b/src/include/catalog/toasting.h @@ -61,6 +61,10 @@ DECLARE_TOAST(gs_package, 8002, 8003); DECLARE_TOAST(gs_global_chain, 5816, 5817); DECLARE_TOAST(gs_model_warehouse, 3995, 3996); +// // change start +// DECLARE_TOAST(gs_statement_memory_info, 1073, 1074); +// // change end + /* shared catalogs */ DECLARE_TOAST(pg_shdescription, 2846, 2847); #define PgShdescriptionToastTable 2846 diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids old mode 100644 new mode 100755 diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade_catalog_maindb_92_366.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade_catalog_maindb_92_366.sql index eef1e2f152570a8f2ac4135c2836dbc2299e89d1..61b16ef6549b495a57ccf05bbecf452ccef09890 100644 --- a/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade_catalog_maindb_92_366.sql +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_maindb/upgrade_catalog_maindb_92_366.sql @@ -38,7 +38,25 @@ CREATE UNIQUE INDEX gs_model_name_index ON pg_catalog.gs_model_warehouse USING B SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0; GRANT SELECT ON TABLE pg_catalog.gs_model_warehouse TO PUBLIC; - +/* change start */ +SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 1038, 1061, 1073, 1074; + +CREATE TABLE IF NOT EXISTS pg_catalog.gs_statement_memory_info +( + queryid Oid NOCOMPRESS NOT NULL, + totalsize float4 NOCOMPRESS NOT NULL, + freesize float4 NOCOMPRESS NOT NULL, + usedsize float4 NOCOMPRESS NOT NULL, + query text +)WITH OIDS; + +SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 1072; +CREATE UNIQUE INDEX gs_statement_memory_info_oid_index ON pg_catalog.gs_statement_memory_info USING BTREE(queryid OID_OPS); + +SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0; +GRANT SELECT ON TABLE pg_catalog.gs_statement_memory_info TO PUBLIC; +/* change end */ + DROP SCHEMA IF EXISTS db4ai cascade; CREATE SCHEMA db4ai; COMMENT ON schema db4ai IS 'db4ai schema'; diff --git a/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade_catalog_otherdb_92_366.sql b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade_catalog_otherdb_92_366.sql index eef1e2f152570a8f2ac4135c2836dbc2299e89d1..96c73d8a2b8d7994cffbc592b1a3766ce22679ea 100644 --- a/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade_catalog_otherdb_92_366.sql +++ b/src/include/catalog/upgrade_sql/upgrade_catalog_otherdb/upgrade_catalog_otherdb_92_366.sql @@ -38,6 +38,24 @@ CREATE UNIQUE INDEX gs_model_name_index ON pg_catalog.gs_model_warehouse USING B SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0; GRANT SELECT ON TABLE pg_catalog.gs_model_warehouse TO PUBLIC; +/* change start */ +SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 1038, 1061, 1073, 1074; + +CREATE TABLE IF NOT EXISTS pg_catalog.gs_statement_memory_info +( + queryid Oid NOCOMPRESS NOT NULL, + totalsize float4 NOCOMPRESS NOT NULL, + freesize float4 NOCOMPRESS NOT NULL, + usedsize float4 NOCOMPRESS NOT NULL, + query text +)WITH OIDS; + +SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 1072; +CREATE UNIQUE INDEX gs_statement_memory_info_oid_index ON pg_catalog.gs_statement_memory_info USING BTREE(queryid OID_OPS); + +SET LOCAL inplace_upgrade_next_system_object_oids = IUO_CATALOG, false, true, 0, 0, 0, 0; +GRANT SELECT ON TABLE pg_catalog.gs_statement_memory_info TO PUBLIC; +/* change end */ DROP SCHEMA IF EXISTS db4ai cascade; CREATE SCHEMA db4ai; diff --git a/src/include/knl/knl_guc/knl_session_attr_sql.h b/src/include/knl/knl_guc/knl_session_attr_sql.h index 537296e933d8b002b1fbd17aece9f41b1d3f0935..478392b1bb1c141bfe49ce693362c8750b668cf7 100644 --- a/src/include/knl/knl_guc/knl_session_attr_sql.h +++ b/src/include/knl/knl_guc/knl_session_attr_sql.h @@ -41,6 +41,9 @@ #include "knl/knl_guc/knl_guc_common.h" typedef struct knl_session_attr_sql { + // change start + bool enable_memory_stat; + // change end bool enable_fast_numeric; bool enable_global_stats; bool enable_hdfs_predicate_pushdown; diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h index 271594e8116526f3ef8dabbc39cdce30992d0ff8..7c455abcfd7c14f0f7969ae07d73995860b41533 100644 --- a/src/include/utils/palloc.h +++ b/src/include/utils/palloc.h @@ -119,6 +119,11 @@ typedef struct MemoryContextData { uint64 session_id; /* session id of context owner */ ThreadId thread_id; /* thread id of context owner */ ListCell cell; /* cell to pointer to this context*/ + + // change start + Size allocated_memsize; + Size freed_memsize; + // change end } MemoryContextData; /* diff --git a/src/include/utils/syscache.h b/src/include/utils/syscache.h index da3c91c7d718206bd36d60b13f0c800de77d3c06..0c5da4a4932ec023fe5ca8706078fc71a5b3eef9 100644 --- a/src/include/utils/syscache.h +++ b/src/include/utils/syscache.h @@ -31,7 +31,10 @@ #define STATRELATTINH STATRELKINDATTINH enum SysCacheIdentifier { - AGGFNOID = 0, + // change start + STATEMENTINFOOID = 0, + // change end + AGGFNOID, AMNAME, AMOID, AMOPOPID,