diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c
index 359aafea681..08cf9dce2af 100644
--- a/src/backend/executor/functions.c
+++ b/src/backend/executor/functions.c
@@ -612,7 +612,14 @@ init_sql_fcache(FunctionCallInfo fcinfo, bool lazyEvalOK)
 	fcache->lazyEvalOK = lazyEvalOK;
 	fcache->lazyEval = false;
 
-	/* Also reset data about where we are in the function. */
+	/*
+	 * Also reset data about where we are in the function.  Notice we just
+	 * clear cplan without doing ReleaseCachedPlan.  The only way that cplan
+	 * could be non-NULL here is if we errored out of a previous execution of
+	 * this SQLFunctionCache, in which case error abort would have released
+	 * the plan reference, so we mustn't do so again.
+	 */
+	fcache->cplan = NULL;
 	fcache->eslist = NULL;
 	fcache->next_query_index = 0;
 	fcache->error_query_index = 0;
