@@ -352,10 +352,26 @@ aqo_copy_generic_path_info(PlannerInfo *root, Plan *dest, Path *src)
352352{
353353bool is_join_path ;
354354
355+ if (prev_copy_generic_path_info_hook )
356+ prev_copy_generic_path_info_hook (root ,dest ,src );
357+
355358is_join_path = (src -> type == T_NestPath || src -> type == T_MergePath ||
356359src -> type == T_HashPath );
357360
358- dest -> had_path = true;
361+ if (dest -> had_path )
362+ {
363+ /*
364+ * The convention is that any extension that sets had_path is also
365+ * responsible for setting path_clauses, path_jointype, path_relids,
366+ * path_parallel_workers, and was_parameterized.
367+ */
368+ Assert (dest -> path_clauses && dest -> path_jointype &&
369+ dest -> path_relids && dest -> path_parallel_workers );
370+ return ;
371+ }
372+ else
373+ dest -> had_path = true;
374+
359375if (is_join_path )
360376{
361377dest -> path_clauses = ((JoinPath * )src )-> joinrestrictinfo ;
@@ -372,9 +388,6 @@ aqo_copy_generic_path_info(PlannerInfo *root, Plan *dest, Path *src)
372388dest -> path_relids = get_list_of_relids (root ,src -> parent -> relids );
373389dest -> path_parallel_workers = src -> parallel_workers ;
374390dest -> was_parametrized = (src -> param_info != NULL );
375-
376- if (prev_copy_generic_path_info_hook )
377- prev_copy_generic_path_info_hook (root ,dest ,src );
378391}
379392
380393/*
@@ -460,17 +473,8 @@ learn_query_stat(QueryDesc *queryDesc)
460473}
461474selectivity_cache_clear ();
462475
463- if (prev_ExecutorEnd_hook )
464- prev_ExecutorEnd_hook (queryDesc );
465- else
466- standard_ExecutorEnd (queryDesc );
467-
468- ExtractFromQueryContext (queryDesc );
469-
470476/*
471477 * Store all learn data into the AQO service relations.
472- * We postpone this operation to avoid an invalidation recurse induced by
473- * CommandCounterIncrement.
474478 */
475479if ((query_context .collect_stat )&& (stat != NULL ))
476480{
@@ -481,6 +485,17 @@ learn_query_stat(QueryDesc *queryDesc)
481485pfree_query_stat (stat );
482486}
483487RemoveFromQueryContext (queryDesc );
488+
489+ if (prev_ExecutorEnd_hook )
490+ prev_ExecutorEnd_hook (queryDesc );
491+ else
492+ standard_ExecutorEnd (queryDesc );
493+
494+ /*
495+ * standard_ExecutorEnd clears the queryDesc->planstate. After this point no
496+ * one operation with the plan can be made.
497+ */
498+ //ExtractFromQueryContext(queryDesc);
484499}
485500
486501/*