From 057a5aed342ea7eafcde88543171dab57dd9e1b6 Mon Sep 17 00:00:00 2001 From: Ashutosh Bapat Date: Mon, 17 Jul 2023 15:10:45 +0530 Subject: [PATCH 4/4] 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 b2916ad690..696568a3f3 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -1782,12 +1782,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) @@ -1903,6 +1905,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