From 34961669fdf8f952cd59076e7b4815fee889a408 Mon Sep 17 00:00:00 2001 From: YvShu <2833143491@qq.com> Date: Sun, 13 Oct 2024 23:33:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Egs=5Fstatement=5Fmemory=5Finf?= =?UTF-8?q?o=E8=AE=B0=E5=BD=95=E5=86=85=E5=AD=98=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/catalog/CMakeLists.txt | 2 +- src/common/backend/catalog/Makefile | 2 +- src/common/backend/catalog/dependency.cpp | 10 ++ .../backend/catalog/performance_views.sql | 5 + src/common/backend/utils/cache/relcache.cpp | 32 +++-- src/common/backend/utils/cache/syscache.cpp | 8 ++ src/common/backend/utils/misc/guc.cpp | 6 +- src/common/backend/utils/misc/guc/guc_sql.cpp | 13 ++ src/common/backend/utils/mmgr/mcxt.cpp | 67 ++++++++-- src/gausskernel/runtime/executor/execMain.cpp | 123 ++++++++++++++++++ src/include/catalog/catversion.h | 6 +- src/include/catalog/dependency.h | 3 + .../catalog/gs_statement_memory_info.h | 77 +++++++++++ src/include/catalog/indexing.h | 7 +- src/include/catalog/toasting.h | 4 + src/include/catalog/unused_oids | 0 .../upgrade_catalog_maindb_92_366.sql | 20 ++- .../upgrade_catalog_otherdb_92_366.sql | 18 +++ .../knl/knl_guc/knl_session_attr_sql.h | 3 + src/include/utils/palloc.h | 5 + src/include/utils/syscache.h | 5 +- 21 files changed, 390 insertions(+), 26 deletions(-) create mode 100644 src/include/catalog/gs_statement_memory_info.h mode change 100644 => 100755 src/include/catalog/unused_oids diff --git a/src/common/backend/catalog/CMakeLists.txt b/src/common/backend/catalog/CMakeLists.txt index 052d104071..42ae57f3b7 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 c38bf4a486..9dc4846db6 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 bd62afbd7b..943c76225d 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 4635a88afa..bed01ea0f1 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 fa87ca9efd..694607f318 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 67965551ea..9015fc25f4 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 add4504fdd..dfe0e6697d 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 edc4298c77..fc85c6aa10 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 79f1ec17de..5d69b2012e 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 63db5fc487..a117715e9b 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 f1b3ae5fc5..4ca01e32a7 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 f2081aa3b8..4c429cc948 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 0000000000..ac39dc56a9 --- /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 ec233c0a88..b0e00f5329 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 1494cbd52b..d18a95152b 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 eef1e2f152..61b16ef654 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 eef1e2f152..96c73d8a2b 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 537296e933..478392b1bb 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 271594e811..7c455abcfd 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 da3c91c7d7..0c5da4a493 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, -- Gitee