Restore the memory context information if an InitPlan fails
Otherwise memories might be allocated wrongly in the query-lifespan
memory context `ecxt_per_query_memory`, which could be fatal.
@@ -1085,7 +1085,7 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext, QueryDesc *queryDesc
SubLinkType subLinkType = subplan->subLinkType;
EState *estate = planstate->state;
ScanDirection dir = estate->es_direction;
- MemoryContext oldcontext;
+ MemoryContext oldcontext = NULL;
TupleTableSlot *slot;
ListCell *pvar;
ListCell *l;
@@ -1415,6 +1415,12 @@ PG_CATCH();
/* Restore memory high-water mark for root slice of main query. */
MemoryContextSetPeakSpace(planstate->state->es_query_cxt, savepeakspace);
+ if (oldcontext)
+ MemoryContextSwitchTo(oldcontext);
+
+ /* restore scan direction */
+ estate->es_direction = dir;
+
/*
* Clean up the interconnect.
* CDB TODO: Is this needed following failure on QD?
