From 760d328696a460deb418c8fa34b80e53fa530129 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Wed, 1 Mar 2017 13:29:51 +0530
Subject: [PATCH 3/3] GUC zap_path to enable freeing memory consumed by paths.

For measuring the memory saved, add a GUC zap_path to enable/disable
freeing memory occupied by paths.
---
 src/backend/optimizer/path/costsize.c |    1 +
 src/backend/optimizer/plan/planner.c  |   15 +++++++++------
 src/backend/utils/misc/guc.c          |   10 ++++++++++
 src/include/optimizer/cost.h          |    1 +
 4 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index c138f57..f4dfa00 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -126,6 +126,7 @@ bool		enable_nestloop = true;
 bool		enable_material = true;
 bool		enable_mergejoin = true;
 bool		enable_hashjoin = true;
+bool		zap_paths = true;
 
 typedef struct
 {
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 410750d..c7b085f 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -421,12 +421,15 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
 	result->stmt_location = parse->stmt_location;
 	result->stmt_len = parse->stmt_len;
 
-	/*
-	 * We do not need paths any more, blow those away.
-	 * TODO: probably we should also set the pathlists to NIL.
-	 */
-	MemoryContextResetAndDeleteChildren(glob->path_cxt);
-	glob->path_cxt = NULL;
+	if (zap_paths)
+	{
+		/*
+		 * We do not need paths any more, blow those away.
+		 * TODO: probably we should also set the pathlists to NIL.
+		 */
+		MemoryContextResetAndDeleteChildren(glob->path_cxt);
+		glob->path_cxt = NULL;
+	}
 
 	MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, __FUNCTION__);
 
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 2477138..630d3f7 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -912,6 +912,16 @@ static struct config_bool ConfigureNamesBool[] =
 		true,
 		NULL, NULL, NULL
 	},
+
+	{
+		{"zap_paths", PGC_USERSET, QUERY_TUNING_GEQO,
+			gettext_noop("Free up the memory used by paths at the end of planning."),
+		},
+		&zap_paths,
+		true,
+		NULL, NULL, NULL
+	},
+
 	{
 		/* Not for general use --- used by SET SESSION AUTHORIZATION */
 		{"is_superuser", PGC_INTERNAL, UNGROUPED,
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index 72200fa..fc882ad 100644
--- a/src/include/optimizer/cost.h
+++ b/src/include/optimizer/cost.h
@@ -67,6 +67,7 @@ extern bool enable_material;
 extern bool enable_mergejoin;
 extern bool enable_hashjoin;
 extern int	constraint_exclusion;
+extern bool zap_paths;
 
 extern double clamp_row_est(double nrows);
 extern double index_pages_fetched(double tuples_fetched, BlockNumber pages,
-- 
1.7.9.5

