Skip to content

Commit 10745e3

Browse files
author
Alena Rybakina
committed
Add memory context to cover memory space when applying aqo_timeout handler and applying
isQueryUsingSystemRelation_walker and add UtilityMemCtx for allocation space for junk_fss and actual_fss list and reset it after cleaning aqo database process.
1 parent fc83bed commit 10745e3

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

‎aqo.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ _PG_init(void)
334334
shmem_request_hook = aqo_shmem_request;
335335

336336
init_deactivated_queries_storage();
337-
337+
338338
/*
339339
* Create own Top memory Context for reporting AQO memory in the future.
340340
*/

‎postprocessing.c

+2
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ static int exec_nested_level = 0;
617617
static void
618618
aqo_timeout_handler(void)
619619
{
620+
MemoryContext oldctx = MemoryContextSwitchTo(AQOLearnMemCtx);
620621
aqo_obj_stat ctx = {NIL, NIL, NIL, false, false};
621622

622623
if (!timeoutCtl.queryDesc || !ExtractFromQueryEnv(timeoutCtl.queryDesc))
@@ -629,6 +630,7 @@ aqo_timeout_handler(void)
629630

630631
elog(NOTICE, "[AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data.");
631632
learnOnPlanState(timeoutCtl.queryDesc->planstate, (void *) &ctx);
633+
MemoryContextSwitchTo(oldctx);
632634
}
633635

634636
static bool

‎preprocessing.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ aqo_planner(Query *parse,
196196

197197
selectivity_cache_clear();
198198
MemoryContextSwitchTo(oldctx);
199-
199+
200200
oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
201201
query_context.query_hash = get_query_hash(parse, query_string);
202202
MemoryContextSwitchTo(oldctx);
@@ -497,6 +497,7 @@ jointree_walker(Node *jtnode, void *context)
497497
static bool
498498
isQueryUsingSystemRelation_walker(Node *node, void *context)
499499
{
500+
MemoryContext oldctx = MemoryContextSwitchTo(AQOLearnMemCtx);
500501
AQOPreWalkerCtx *ctx = (AQOPreWalkerCtx *) context;
501502

502503
if (node == NULL)
@@ -538,6 +539,7 @@ isQueryUsingSystemRelation_walker(Node *node, void *context)
538539
}
539540

540541
jointree_walker((Node *) query->jointree, context);
542+
MemoryContextSwitchTo(oldctx);
541543

542544
/* Recursively plunge into subqueries and CTEs */
543545
return query_tree_walker(query,

‎selectivity_cache.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ cache_selectivity(int clause_hash,
5151
AQOCacheSelectivity = AllocSetContextCreate(AQOTopMemCtx,
5252
"AQOCacheSelectivity",
5353
ALLOCSET_DEFAULT_SIZES);
54-
54+
5555

5656
foreach(l, objects)
5757
{

‎storage.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
20422042
for(i = 0; i < dentry->nrels; i++)
20432043
{
20442044
Oid reloid = ObjectIdGetDatum(*(Oid *)ptr);
2045+
MemoryContext oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
20452046

20462047
if (!SearchSysCacheExists1(RELOID, reloid))
20472048
/* Remember this value */
@@ -2050,6 +2051,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
20502051
else
20512052
actual_fss = list_append_unique_int(actual_fss,
20522053
dentry->key.fss);
2054+
MemoryContextSwitchTo(oldctx);
20532055

20542056
ptr += sizeof(Oid);
20552057
}
@@ -2100,8 +2102,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
21002102
(*fs_num) += (int) _aqo_queries_remove(entry->queryid);
21012103
}
21022104

2103-
list_free(junk_fss);
2104-
list_free(actual_fss);
2105+
MemoryContextReset(AQOUtilityMemCtx);
21052106
}
21062107

21072108
/*

0 commit comments

Comments
 (0)