From 97ba1883e3df294294ea652f684b3503de63f247 Mon Sep 17 00:00:00 2001 From: Ashutosh Bapat Date: Mon, 17 Jul 2023 15:10:45 +0530 Subject: [PATCH 3/3] Local variables pointing to path node used repeatedly In match_unsorted_outer() we create a materialize path for inner relation and pass it to try_nestloop_path repeatedly. Link and unlink the path to and from the local variable to keep track of it. --- src/backend/optimizer/path/joinpath.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c index 6aca66f196..36dffb0af6 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -1801,12 +1801,14 @@ match_unsorted_outer(PlannerInfo *root, /* * Consider materializing the cheapest inner path, unless * enable_material is off or the path in question materializes its - * output anyway. + * output anyway. Link the path to a local reference so that it won't + * be freed by add_path if the surrounding nest path is freed by + * add_path. It will get freed if not used later. */ if (enable_material && inner_cheapest_total != NULL && !ExecMaterializesOutput(inner_cheapest_total->pathtype)) - matpath = (Path *) - create_material_path(innerrel, inner_cheapest_total); + link_path(&matpath, + (Path *) create_material_path(innerrel, inner_cheapest_total)); } foreach(lc1, outerrel->pathlist) @@ -1922,6 +1924,10 @@ match_unsorted_outer(PlannerInfo *root, false); } + /* Materialized inner path won't be used anymore. Unlink it */ + if (matpath) + unlink_path(&matpath); + /* * Consider partial nestloop and mergejoin plan if outerrel has any * partial path and the joinrel is parallel-safe. However, we can't -- 2.25.1