@@ -144,20 +144,21 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
144
144
List * selectivities = NULL ;
145
145
List * clauses ;
146
146
int fss = 0 ;
147
+ MemoryContext old_ctx_m ;
147
148
148
149
if (IsQueryDisabled ())
149
150
/* Fast path. */
150
151
goto default_estimator ;
151
152
153
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
154
+
152
155
if (query_context .use_aqo || query_context .learn_aqo )
153
156
selectivities = get_selectivities (root , rel -> baserestrictinfo , 0 ,
154
157
JOIN_INNER , NULL );
155
158
156
159
if (!query_context .use_aqo )
157
160
{
158
- if (query_context .learn_aqo )
159
- list_free_deep (selectivities );
160
-
161
+ MemoryContextSwitchTo (old_ctx_m );
161
162
goto default_estimator ;
162
163
}
163
164
@@ -174,10 +175,8 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
174
175
& fss );
175
176
rel -> fss_hash = fss ;
176
177
177
- list_free (rels .hrels );
178
- list_free (rels .signatures );
179
- list_free_deep (selectivities );
180
- list_free (clauses );
178
+ /* Return to the caller's memory context. */
179
+ MemoryContextSwitchTo (old_ctx_m );
181
180
182
181
if (predicted >= 0 )
183
182
{
@@ -224,14 +223,16 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
224
223
int * eclass_hash ;
225
224
int current_hash ;
226
225
int fss = 0 ;
226
+ MemoryContext oldctx ;
227
227
228
228
if (IsQueryDisabled ())
229
229
/* Fast path */
230
230
goto default_estimator ;
231
231
232
+ oldctx = MemoryContextSwitchTo (AQOPredictMemCtx );
233
+
232
234
if (query_context .use_aqo || query_context .learn_aqo )
233
235
{
234
- MemoryContext old_ctx_m ;
235
236
236
237
selectivities = list_concat (
237
238
get_selectivities (root , param_clauses , rel -> relid ,
@@ -247,8 +248,6 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
247
248
rte = planner_rt_fetch (rel -> relid , root );
248
249
get_eclasses (allclauses , & nargs , & args_hash , & eclass_hash );
249
250
250
- old_ctx_m = MemoryContextSwitchTo (AQO_cache_mem_ctx );
251
-
252
251
forboth (l , allclauses , l2 , selectivities )
253
252
{
254
253
current_hash = get_clause_hash (
@@ -257,19 +256,11 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
257
256
cache_selectivity (current_hash , rel -> relid , rte -> relid ,
258
257
* ((double * ) lfirst (l2 )));
259
258
}
260
-
261
- MemoryContextSwitchTo (old_ctx_m );
262
- pfree (args_hash );
263
- pfree (eclass_hash );
264
259
}
265
260
266
261
if (!query_context .use_aqo )
267
262
{
268
- if (query_context .learn_aqo )
269
- {
270
- list_free_deep (selectivities );
271
- list_free (allclauses );
272
- }
263
+ MemoryContextSwitchTo (oldctx );
273
264
274
265
goto default_estimator ;
275
266
}
@@ -282,8 +273,9 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
282
273
}
283
274
284
275
predicted = predict_for_relation (allclauses , selectivities , rels .signatures , & fss );
285
- list_free (rels .hrels );
286
- list_free (rels .signatures );
276
+
277
+ /* Return to the caller's memory context */
278
+ MemoryContextSwitchTo (oldctx );
287
279
288
280
predicted_ppi_rows = predicted ;
289
281
fss_ppi_hash = fss ;
@@ -317,20 +309,20 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
317
309
List * outer_selectivities ;
318
310
List * current_selectivities = NULL ;
319
311
int fss = 0 ;
312
+ MemoryContext old_ctx_m ;
320
313
321
314
if (IsQueryDisabled ())
322
315
/* Fast path */
323
316
goto default_estimator ;
324
317
318
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
319
+
325
320
if (query_context .use_aqo || query_context .learn_aqo )
326
321
current_selectivities = get_selectivities (root , restrictlist , 0 ,
327
322
sjinfo -> jointype , sjinfo );
328
-
329
323
if (!query_context .use_aqo )
330
324
{
331
- if (query_context .learn_aqo )
332
- list_free_deep (current_selectivities );
333
-
325
+ MemoryContextSwitchTo (old_ctx_m );
334
326
goto default_estimator ;
335
327
}
336
328
@@ -347,8 +339,9 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
347
339
348
340
predicted = predict_for_relation (allclauses , selectivities , rels .signatures ,
349
341
& fss );
350
- list_free (rels .hrels );
351
- list_free (rels .signatures );
342
+
343
+ /* Return to the caller's memory context */
344
+ MemoryContextSwitchTo (old_ctx_m );
352
345
353
346
rel -> fss_hash = fss ;
354
347
@@ -389,20 +382,21 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
389
382
List * outer_selectivities ;
390
383
List * current_selectivities = NULL ;
391
384
int fss = 0 ;
385
+ MemoryContext old_ctx_m ;
392
386
393
387
if (IsQueryDisabled ())
394
388
/* Fast path */
395
389
goto default_estimator ;
396
390
391
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
392
+
397
393
if (query_context .use_aqo || query_context .learn_aqo )
398
394
current_selectivities = get_selectivities (root , clauses , 0 ,
399
395
sjinfo -> jointype , sjinfo );
400
396
401
397
if (!query_context .use_aqo )
402
398
{
403
- if (query_context .learn_aqo )
404
- list_free_deep (current_selectivities );
405
-
399
+ MemoryContextSwitchTo (old_ctx_m );
406
400
goto default_estimator ;
407
401
}
408
402
@@ -417,8 +411,8 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
417
411
418
412
predicted = predict_for_relation (allclauses , selectivities , rels .signatures ,
419
413
& fss );
420
- list_free ( rels . hrels );
421
- list_free ( rels . signatures );
414
+ /* Return to the caller's memory context */
415
+ MemoryContextSwitchTo ( old_ctx_m );
422
416
423
417
predicted_ppi_rows = predicted ;
424
418
fss_ppi_hash = fss ;
@@ -453,8 +447,6 @@ predict_num_groups(PlannerInfo *root, Path *subpath, List *group_exprs,
453
447
clauses = get_path_clauses (subpath , root , & selectivities );
454
448
(void ) predict_for_relation (clauses , selectivities , rels .signatures ,
455
449
& child_fss );
456
- list_free (rels .hrels );
457
- list_free (rels .signatures );
458
450
}
459
451
460
452
* fss = get_grouped_exprs_hash (child_fss , group_exprs );
@@ -475,6 +467,7 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
475
467
{
476
468
int fss ;
477
469
double predicted ;
470
+ MemoryContext old_ctx_m ;
478
471
479
472
if (!query_context .use_aqo )
480
473
goto default_estimator ;
@@ -489,12 +482,15 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
489
482
if (groupExprs == NIL )
490
483
return 1.0 ;
491
484
485
+ old_ctx_m = MemoryContextSwitchTo (AQOPredictMemCtx );
486
+
492
487
predicted = predict_num_groups (root , subpath , groupExprs , & fss );
493
488
if (predicted > 0. )
494
489
{
495
490
grouped_rel -> predicted_cardinality = predicted ;
496
491
grouped_rel -> rows = predicted ;
497
492
grouped_rel -> fss_hash = fss ;
493
+ MemoryContextSwitchTo (old_ctx_m );
498
494
return predicted ;
499
495
}
500
496
else
@@ -504,6 +500,8 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
504
500
*/
505
501
grouped_rel -> predicted_cardinality = -1 ;
506
502
503
+ MemoryContextSwitchTo (old_ctx_m );
504
+
507
505
default_estimator :
508
506
return default_estimate_num_groups (root , groupExprs , subpath , grouped_rel ,
509
507
pgset );
0 commit comments