From d9d9e91b8d72217434474ef137133b14ffabf754 Mon Sep 17 00:00:00 2001
From: Marina Polyakova <m.polyakova@postgrespro.ru>
Date: Thu, 24 May 2018 11:33:38 +0300
Subject: [PATCH v9] Move the FreeExecutorState() call in the StoreAttrDefault

In the function StoreAttrDefault move the call of the function FreeExecutorState
until we get the full result so that the memory context of the latter is not
freed too early. This is used for cached expressions the results of which are
copied into the ecxt_per_query_memory context.
---
 src/backend/catalog/heap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 39813de..ffdf6f8 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -2110,8 +2110,6 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
 			missingval = ExecEvalExpr(exprState, econtext,
 									  &missingIsNull);
 
-			FreeExecutorState(estate);
-
 			defAttStruct = TupleDescAttr(rel->rd_att, attnum - 1);
 
 			if (missingIsNull)
@@ -2131,6 +2129,8 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
 															 defAttStruct->attalign));
 			}
 
+			FreeExecutorState(estate);
+
 			valuesAtt[Anum_pg_attribute_atthasmissing - 1] = !missingIsNull;
 			replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
 			valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval;
-- 
2.7.4

