1818 *
1919 *******************************************************************************
2020 *
21- * Copyright (c) 2016-2022 , Postgres Professional
21+ * Copyright (c) 2016-2023 , Postgres Professional
2222 *
2323 * IDENTIFICATION
2424 * aqo/cardinality_hooks.c
@@ -82,6 +82,7 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
8282if (!query_context .use_aqo )
8383{
8484MemoryContextSwitchTo (old_ctx_m );
85+ MemoryContextReset (AQOPredictMemCtx );
8586gotodefault_estimator ;
8687}
8788
@@ -100,6 +101,7 @@ aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
100101
101102/* Return to the caller's memory context. */
102103MemoryContextSwitchTo (old_ctx_m );
104+ MemoryContextReset (AQOPredictMemCtx );
103105
104106if (predicted < 0 )
105107gotodefault_estimator ;
@@ -191,12 +193,15 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
191193cache_selectivity (current_hash ,rel -> relid ,rte -> relid ,
192194* ((double * )lfirst (l2 )));
193195}
196+
197+ pfree (args_hash );
198+ pfree (eclass_hash );
194199}
195200
196201if (!query_context .use_aqo )
197202{
198203MemoryContextSwitchTo (oldctx );
199-
204+ MemoryContextReset ( AQOPredictMemCtx );
200205gotodefault_estimator ;
201206}
202207
@@ -211,6 +216,7 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,
211216
212217/* Return to the caller's memory context */
213218MemoryContextSwitchTo (oldctx );
219+ MemoryContextReset (AQOPredictMemCtx );
214220
215221predicted_ppi_rows = predicted ;
216222fss_ppi_hash = fss ;
@@ -265,6 +271,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
265271if (!query_context .use_aqo )
266272{
267273MemoryContextSwitchTo (old_ctx_m );
274+ MemoryContextReset (AQOPredictMemCtx );
268275gotodefault_estimator ;
269276}
270277
@@ -284,6 +291,7 @@ aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
284291
285292/* Return to the caller's memory context */
286293MemoryContextSwitchTo (old_ctx_m );
294+ MemoryContextReset (AQOPredictMemCtx );
287295
288296rel -> fss_hash = fss ;
289297
@@ -343,6 +351,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
343351if (!query_context .use_aqo )
344352{
345353MemoryContextSwitchTo (old_ctx_m );
354+ MemoryContextReset (AQOPredictMemCtx );
346355gotodefault_estimator ;
347356}
348357
@@ -359,6 +368,7 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
359368& fss );
360369/* Return to the caller's memory context */
361370MemoryContextSwitchTo (old_ctx_m );
371+ MemoryContextReset (AQOPredictMemCtx );
362372
363373predicted_ppi_rows = predicted ;
364374fss_ppi_hash = fss ;
@@ -450,6 +460,7 @@ aqo_estimate_num_groups(PlannerInfo *root, List *groupExprs,
450460grouped_rel -> rows = predicted ;
451461grouped_rel -> fss_hash = fss ;
452462MemoryContextSwitchTo (old_ctx_m );
463+ MemoryContextReset (AQOPredictMemCtx );
453464return predicted ;
454465}
455466else
@@ -460,6 +471,7 @@ aqo_estimate_num_groups(PlannerInfo *root, List *groupExprs,
460471grouped_rel -> predicted_cardinality = -1 ;
461472
462473MemoryContextSwitchTo (old_ctx_m );
474+ MemoryContextReset (AQOPredictMemCtx );
463475
464476default_estimator :
465477if (aqo_estimate_num_groups_next )