Skip to content

Commit 3da9a2a

Browse files
committed
Bugfix. AQOUtilityMemCtx is reset although some allocated data still in use.
Remove the AQOUtilityMemCtx memory context at all. It is used for too small operations. I don't buy that such operations can allocate so much memory that backend must free memory right after the end of operation to avoid OOM. I guess, prediction, planning and execution memory context set is good enough.
1 parent a065f9a commit 3da9a2a

File tree

5 files changed

+8
-40
lines changed

5 files changed

+8
-40
lines changed

aqo.c

+1-12
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ MemoryContext AQOTopMemCtx = NULL;
8989
/* Is released at the end of transaction */
9090
MemoryContext AQOCacheMemCtx = NULL;
9191

92-
/* Should be released in-place, just after a huge calculation */
93-
MemoryContext AQOUtilityMemCtx = NULL;
94-
9592
/* Is released at the end of planning */
9693
MemoryContext AQOPredictMemCtx = NULL;
9794

@@ -366,15 +363,7 @@ _PG_init(void)
366363
AQOCacheMemCtx = AllocSetContextCreate(AQOTopMemCtx,
367364
"AQOCacheMemCtx",
368365
ALLOCSET_DEFAULT_SIZES);
369-
/*
370-
* AQOUtilityMemoryContext containe short-lived information which
371-
* is appeared from having got clause, selectivity arrays and relid lists
372-
* while calculating hashes. It clean up inside calculated
373-
* function or immediately after her having completed.
374-
*/
375-
AQOUtilityMemCtx = AllocSetContextCreate(AQOTopMemCtx,
376-
"AQOUtilityMemoryContext",
377-
ALLOCSET_DEFAULT_SIZES);
366+
378367
/*
379368
* AQOPredictMemoryContext save necessary information for making predict of plan nodes
380369
* and clean up in the execution stage of query.

hash.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,20 @@ get_fss_for_object(List *relsigns, List *clauselist,
194194
int sh = 0,
195195
old_sh;
196196
int fss_hash;
197-
MemoryContext old_ctx_m;
198197

199198
n = list_length(clauselist);
200199

201200
/* Check parameters state invariant. */
202201
Assert(n == list_length(selectivities) ||
203202
(nfeatures == NULL && features == NULL));
204203

204+
/*
205+
* It should be allocated in a caller memory context, because it will be
206+
* returned.
207+
*/
205208
if (nfeatures != NULL)
206209
*features = palloc0(sizeof(**features) * n);
207210

208-
old_ctx_m = MemoryContextSwitchTo(AQOUtilityMemCtx);
209-
210211
get_eclasses(clauselist, &nargs, &args_hash, &eclass_hash);
211212
clause_hashes = palloc(sizeof(*clause_hashes) * n);
212213
clause_has_consts = palloc(sizeof(*clause_has_consts) * n);
@@ -281,9 +282,6 @@ get_fss_for_object(List *relsigns, List *clauselist,
281282
relations_hash = get_relations_hash(relsigns);
282283
fss_hash = get_fss_hash(clauses_hash, eclasses_hash, relations_hash);
283284

284-
MemoryContextSwitchTo(old_ctx_m);
285-
MemoryContextReset(AQOUtilityMemCtx);
286-
287285
if (nfeatures != NULL)
288286
{
289287
*nfeatures = n - sh;

postprocessing.c

-8
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,13 @@ restore_selectivities(List *clauselist, List *relidslist, JoinType join_type,
180180
double *cur_sel;
181181
int cur_hash;
182182
int cur_relid;
183-
MemoryContext old_ctx_m;
184183

185184
parametrized_sel = was_parametrized && (list_length(relidslist) == 1);
186185
if (parametrized_sel)
187186
{
188187
cur_relid = linitial_int(relidslist);
189188

190-
old_ctx_m = MemoryContextSwitchTo(AQOUtilityMemCtx);
191189
get_eclasses(clauselist, &nargs, &args_hash, &eclass_hash);
192-
MemoryContextSwitchTo(old_ctx_m);
193190
}
194191

195192
foreach(l, clauselist)
@@ -223,11 +220,6 @@ restore_selectivities(List *clauselist, List *relidslist, JoinType join_type,
223220
lst = lappend(lst, cur_sel);
224221
}
225222

226-
if (parametrized_sel)
227-
{
228-
MemoryContextReset(AQOUtilityMemCtx);
229-
}
230-
231223
return lst;
232224
}
233225

preprocessing.c

+3-10
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ aqo_planner(Query *parse,
166166
ParamListInfo boundParams)
167167
{
168168
bool query_is_stored = false;
169-
MemoryContext oldctx;
169+
MemoryContext oldctx;
170+
170171
oldctx = MemoryContextSwitchTo(AQOPredictMemCtx);
171172

172173
/*
@@ -195,21 +196,14 @@ aqo_planner(Query *parse,
195196
}
196197

197198
selectivity_cache_clear();
198-
MemoryContextSwitchTo(oldctx);
199199

200-
oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
201200
/* Check unlucky case (get a hash of zero) */
202201
if (parse->queryId == UINT64CONST(0))
203202
JumbleQuery(parse, query_string);
204203

205204
Assert(parse->utilityStmt == NULL);
206205
Assert(parse->queryId != UINT64CONST(0));
207206
query_context.query_hash = parse->queryId;
208-
MemoryContextSwitchTo(oldctx);
209-
210-
MemoryContextReset(AQOUtilityMemCtx);
211-
212-
oldctx = MemoryContextSwitchTo(AQOPredictMemCtx);
213207

214208
/* By default, they should be equal */
215209
query_context.fspace_hash = query_context.query_hash;
@@ -230,15 +224,14 @@ aqo_planner(Query *parse,
230224
cursorOptions,
231225
boundParams);
232226
}
233-
MemoryContextSwitchTo(oldctx);
234227

235228
elog(DEBUG1, "AQO will be used for query '%s', class "UINT64_FORMAT,
236229
query_string ? query_string : "null string", query_context.query_hash);
237230

231+
MemoryContextSwitchTo(oldctx);
238232
oldctx = MemoryContextSwitchTo(AQOCacheMemCtx);
239233
cur_classes = lappend_uint64(cur_classes, query_context.query_hash);
240234
MemoryContextSwitchTo(oldctx);
241-
242235
oldctx = MemoryContextSwitchTo(AQOPredictMemCtx);
243236

244237
if (aqo_mode == AQO_MODE_DISABLED)

storage.c

-4
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
20972097
for(i = 0; i < dentry->nrels; i++)
20982098
{
20992099
Oid reloid = ObjectIdGetDatum(*(Oid *)ptr);
2100-
MemoryContext oldctx = MemoryContextSwitchTo(AQOUtilityMemCtx);
21012100

21022101
if (!SearchSysCacheExists1(RELOID, reloid))
21032102
/* Remember this value */
@@ -2106,7 +2105,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
21062105
else
21072106
actual_fss = list_append_unique_int(actual_fss,
21082107
dentry->key.fss);
2109-
MemoryContextSwitchTo(oldctx);
21102108

21112109
ptr += sizeof(Oid);
21122110
}
@@ -2156,8 +2154,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
21562154
/* Query class preferences */
21572155
(*fs_num) += (int) _aqo_queries_remove(entry->queryid);
21582156
}
2159-
2160-
MemoryContextReset(AQOUtilityMemCtx);
21612157
}
21622158

21632159
/*

0 commit comments

Comments
 (0)