diff --git a/src/backend/utils/fmgr/fmgr.c b/src/backend/utils/fmgr/fmgr.c
index 3976496..d565d3e 100644
--- a/src/backend/utils/fmgr/fmgr.c
+++ b/src/backend/utils/fmgr/fmgr.c
@@ -1006,19 +1006,30 @@ fmgr_security_definer(PG_FUNCTION_ARGS)
  *-------------------------------------------------------------------------
  */
 
-/*
- * These are for invocation of a specifically named function with a
+
+/* These are for invocation of a specifically named function with a
  * directly-computed parameter list.  Note that neither arguments nor result
- * are allowed to be NULL.  Also, the function cannot be one that needs to
- * look at FmgrInfo, since there won't be any.
+ * are allowed to be NULL.
+ *
+ * If the flinfo parameter is not NULL then this used to supply context
+ * to the called function. The callee should only use the passed in fn_mcxt
+ * and fn_extra. Anything else is likely to be bogus.
+ *
+ * But the fn_extra can be used for example for caching results
+ * (see enum_cmp for an example).
+ *
+ * No context is supplied if the parameter is NULL. See DirectFunctionCall
+ * macros. In this case the function must not try to look at FmgrInfo, since
+ * there won't be one and it is likely to result in an access violation.
  */
 Datum
-DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
+CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 1, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 1, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.argnull[0] = false;
@@ -1033,12 +1044,13 @@ DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
 }
 
 Datum
-DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
+CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 2, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 2, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1055,13 +1067,14 @@ DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
 }
 
 Datum
-DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3)
+CallerFInfoFunctionCall3(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 3, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 3, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1080,13 +1093,14 @@ DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
 }
 
 Datum
-DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4)
+CallerFInfoFunctionCall4(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 4, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 4, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1107,13 +1121,14 @@ DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
 }
 
 Datum
-DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5)
+CallerFInfoFunctionCall5(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 5, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 5, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1136,14 +1151,15 @@ DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
 }
 
 Datum
-DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6)
+CallerFInfoFunctionCall6(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 6, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 6, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1168,14 +1184,15 @@ DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
 }
 
 Datum
-DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6, Datum arg7)
+CallerFInfoFunctionCall7(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6, Datum arg7)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 7, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 7, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1202,14 +1219,15 @@ DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
 }
 
 Datum
-DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6, Datum arg7, Datum arg8)
+CallerFInfoFunctionCall8(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6, Datum arg7, Datum arg8)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 8, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 8, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
@@ -1238,15 +1256,16 @@ DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
 }
 
 Datum
-DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6, Datum arg7, Datum arg8,
-						Datum arg9)
+CallerFInfoFunctionCall9(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6, Datum arg7, Datum arg8,
+						 Datum arg9)
 {
 	FunctionCallInfoData fcinfo;
 	Datum		result;
 
-	InitFunctionCallInfoData(fcinfo, NULL, 9, collation, NULL, NULL);
+	InitFunctionCallInfoData(fcinfo, flinfo, 9, collation, NULL, NULL);
 
 	fcinfo.arg[0] = arg1;
 	fcinfo.arg[1] = arg2;
diff --git a/src/include/fmgr.h b/src/include/fmgr.h
index a671480..c186d01 100644
--- a/src/include/fmgr.h
+++ b/src/include/fmgr.h
@@ -443,37 +443,48 @@ extern int no_such_variable
 /* These are for invocation of a specifically named function with a
  * directly-computed parameter list.  Note that neither arguments nor result
  * are allowed to be NULL.
+ *
+ * If the flinfo parameter is not NULL then this used to supply context
+ * to the called function. The callee should only use the passed in fn_mcxt
+ * and fn_extra. Anything else is likely to be bogus.
+ *
+ * But the fn_extra can be used for example for caching results
+ * (see enum_cmp for an example).
+ *
+ * No context is supplied if the parameter is NULL. See DirectFunctionCall
+ * macros below. In this case the function must not try to look at FmgrInfo,
+ * since there won't be one and it is likely to result in an access violation.
  */
-extern Datum DirectFunctionCall1Coll(PGFunction func, Oid collation,
-						Datum arg1);
-extern Datum DirectFunctionCall2Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2);
-extern Datum DirectFunctionCall3Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3);
-extern Datum DirectFunctionCall4Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4);
-extern Datum DirectFunctionCall5Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5);
-extern Datum DirectFunctionCall6Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6);
-extern Datum DirectFunctionCall7Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6, Datum arg7);
-extern Datum DirectFunctionCall8Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6, Datum arg7, Datum arg8);
-extern Datum DirectFunctionCall9Coll(PGFunction func, Oid collation,
-						Datum arg1, Datum arg2,
-						Datum arg3, Datum arg4, Datum arg5,
-						Datum arg6, Datum arg7, Datum arg8,
-						Datum arg9);
+extern Datum CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1);
+extern Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2);
+extern Datum CallerFInfoFunctionCall3(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3);
+extern Datum CallerFInfoFunctionCall4(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4);
+extern Datum CallerFInfoFunctionCall5(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5);
+extern Datum CallerFInfoFunctionCall6(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6);
+extern Datum CallerFInfoFunctionCall7(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6, Datum arg7);
+extern Datum CallerFInfoFunctionCall8(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6, Datum arg7, Datum arg8);
+extern Datum CallerFInfoFunctionCall9(PGFunction func, FmgrInfo *flinfo,
+						 Oid collation, Datum arg1, Datum arg2,
+						 Datum arg3, Datum arg4, Datum arg5,
+						 Datum arg6, Datum arg7, Datum arg8,
+						 Datum arg9);
 
 /* These are for invocation of a previously-looked-up function with a
  * directly-computed parameter list.  Note that neither arguments nor result
@@ -548,28 +559,55 @@ extern Datum OidFunctionCall9Coll(Oid functionId, Oid collation,
 					 Datum arg6, Datum arg7, Datum arg8,
 					 Datum arg9);
 
+/*
+ * These macros allow the flinfo argument to be omitted, so that no context is
+ * supplied to the called function.
+ */
+
+#define DirectFunctionCall1Coll(func, coll, arg1) \
+	CallerFInfoFunctionCall1(func, NULL, coll, arg1)
+#define DirectFunctionCall2Coll(func, coll, arg1, arg2) \
+	CallerFInfoFunctionCall2(func, NULL, coll, arg1, arg2)
+#define DirectFunctionCall3Coll(func, coll, arg1, arg2, arg3) \
+	CallerFInfoFunctionCall3(func, NULL, coll, arg1, arg2, arg3)
+#define DirectFunctionCall4Coll(func, coll, arg1, arg2, arg3, arg4) \
+	CallerFInfoFunctionCall4(func, NULL, coll, arg1, arg2, arg3, arg4)
+#define DirectFunctionCall5Coll(func, coll, arg1, arg2, arg3, arg4, arg5) \
+	CallerFInfoFunctionCall5(func, NULL, coll, arg1, arg2, arg3, arg4, arg5)
+#define DirectFunctionCall6Coll(func, coll, arg1, arg2, arg3, arg4, arg5, arg6) \
+	CallerFInfoFunctionCall6(func, NULL, coll, arg1, arg2, arg3, arg4, arg5, arg6)
+#define DirectFunctionCall7Coll(func, coll, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
+	CallerFInfoFunctionCall7(func, NULL, coll, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
+#define DirectFunctionCall8Coll(func, coll, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
+	CallerFInfoFunctionCall8(func, NULL, coll, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
+#define DirectFunctionCall9Coll(func, coll, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
+	CallerFInfoFunctionCall9(func, NULL, coll, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
+
+
 /* These macros allow the collation argument to be omitted (with a default of
  * InvalidOid, ie, no collation).  They exist mostly for backwards
- * compatibility of source code.
+ * compatibility of source code. The flinfo argument is also set as NULL
+ * in the DirectFunctionCall cases, so that no context is supplied to the
+ * called function.
  */
 #define DirectFunctionCall1(func, arg1) \
-	DirectFunctionCall1Coll(func, InvalidOid, arg1)
+	CallerFInfoFunctionCall1(func, NULL, InvalidOid, arg1)
 #define DirectFunctionCall2(func, arg1, arg2) \
-	DirectFunctionCall2Coll(func, InvalidOid, arg1, arg2)
+	CallerFInfoFunctionCall2(func, NULL, InvalidOid, arg1, arg2)
 #define DirectFunctionCall3(func, arg1, arg2, arg3) \
-	DirectFunctionCall3Coll(func, InvalidOid, arg1, arg2, arg3)
+	CallerFInfoFunctionCall3(func, NULL, InvalidOid, arg1, arg2, arg3)
 #define DirectFunctionCall4(func, arg1, arg2, arg3, arg4) \
-	DirectFunctionCall4Coll(func, InvalidOid, arg1, arg2, arg3, arg4)
+	CallerFInfoFunctionCall4(func, NULL, InvalidOid, arg1, arg2, arg3, arg4)
 #define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5) \
-	DirectFunctionCall5Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5)
+	CallerFInfoFunctionCall5(func, NULL, InvalidOid, arg1, arg2, arg3, arg4, arg5)
 #define DirectFunctionCall6(func, arg1, arg2, arg3, arg4, arg5, arg6) \
-	DirectFunctionCall6Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
+	CallerFInfoFunctionCall6(func, NULL, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
 #define DirectFunctionCall7(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
-	DirectFunctionCall7Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
+	CallerFInfoFunctionCall7(func, NULL, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
 #define DirectFunctionCall8(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
-	DirectFunctionCall8Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
+	CallerFInfoFunctionCall8(func, NULL, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
 #define DirectFunctionCall9(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
-	DirectFunctionCall9Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
+	CallerFInfoFunctionCall9(func, NULL, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
 #define FunctionCall1(flinfo, arg1) \
 	FunctionCall1Coll(flinfo, InvalidOid, arg1)
 #define FunctionCall2(flinfo, arg1, arg2) \
